crabbymetrics
  • Home
  • API
    • API Overview
    • Regression And GLMs
    • Survival / Event-Time
    • Causal Inference And Panels
    • Hypothesis Testing And Utilities
    • Transforms
    • Estimation Interfaces
  • Binding Crash Course
  • Regression And GLMs
    • OLS
    • ABC OLS
    • Anytime-Valid Confidence Sequences
    • Ridge
    • Bagged Polynomial Regression
    • Fixed Effects OLS
    • ElasticNet
    • Logit
    • Multinomial Logit
    • Poisson
    • MLE Prediction Interface
    • Survival / Recurrent Events
    • GMM
    • FTRL
    • MEstimator Poisson
  • Causal Inference
    • Balancing Weights
    • EPLM
    • Average Derivative
    • Double ML And AIPW
    • Richer Regression
    • TwoSLS
    • Synthetic Control
    • Synthetic DID
    • Horizontal Panel Ridge
    • Matrix Completion
    • Interactive Fixed Effects
    • Staggered Panel Event Study
    • Joint Hypothesis Tests
  • Transforms
    • PCA And Kernel Basis
  • Ablations
    • Variance Estimators
    • Semiparametric Estimator Comparisons
    • Two-Period Semiparametric DID
    • Bridging Finite And Superpopulation
    • Panel Estimator DGP Comparisons
    • Same Root Panel Case Studies
    • Randomized Sketching And Least Squares
  • Optimization
    • Optimizers
    • GMM With Optimizers
  • Ding: First Course
    • Overview And TOC
    • Ch 1 Correlation And Simpson
    • Ch 2 Potential Outcomes
    • Ch 3 CRE And Fisher RT
    • Ch 4 CRE And Neyman
    • Ch 9 Bridging Finite And Superpopulation
    • Ch 11 Propensity Score
    • Ch 12 Double Robust ATE
    • Ch 13 Double Robust ATT
    • Ch 21 Experimental IV
    • Ch 23 Econometric IV
    • Ch 27 Mediation

FTRL Example

This page mirrors examples/ftrl_example.py.

Warning

The current Python wrapper performs one aggregate batch update from a fixed random FTRL state. It has no intercept, epochs, convergence criterion, or persistent online update API. This example displays that update; it is not evidence of a converged classifier. See the FTRL API page for the exact equations and limitations.

1 Run The Experimental FTRL Update

import numpy as np
from pprint import pprint

from crabbymetrics import FTRL

np.set_printoptions(precision=4, suppress=True)
rng = np.random.default_rng(6)
n = 1000
k = 5
beta = np.array([0.8, -1.2, 0.4, 0.0, 0.6])

x = rng.normal(size=(n, k))
logits = x @ beta
probs = 1.0 / (1.0 + np.exp(-logits))
y = rng.binomial(1, probs).astype(np.int32)

model = FTRL(alpha=0.1, beta=1.0, l1_ratio=1.0, l2_ratio=1.0)
model.fit(x, y)

print("true coef:", beta)
pprint(model.summary())
true coef: [ 0.8 -1.2  0.4  0.   0.6]
{'alpha': 0.1,
 'beta': 1.0,
 'coef': array([ 0.0986, -0.0992,  0.0974,  0.0829,  0.0978]),
 'coef_se': None,
 'inference_available': False,
 'l1_ratio': 1.0,
 'l2_ratio': 1.0}