Skip to content

Instantly share code, notes, and snippets.

View tonyduan's full-sized avatar

Tony Duan tonyduan

View GitHub Profile
@tonyduan
tonyduan / ece-estimators-sim.py
Last active July 28, 2020 07:46
Simulated comparison of plugin and debased estimators for L1 calibration error.
"""
Simulated comparison of estimators for L1 calibration error:
1. Plugin estimator, i.e. ECE [Guo et al. 2017]
2. De-biased estimator [Kumar et al. 2019]
In each simulation, we draw y_per_bin, p_per_bin, and fix n_per_bin to a constant.
We draw samples y_hat_per_bin and compare MSE of the two estimators as a function of bin size.
"""
import numpy as np
@tonyduan
tonyduan / sgld-example.py
Created May 2, 2020 00:03
An example toy implementation of Stochastic Gradient Langevin Dynamics.
"""
We combine Robbins-Monro stochastic gradients of the MAP estimate with
an amount of Gaussian noise balanced with the step size.
Then it turns out the algorithm produces samples from the posterior as t → ∞.
Here we have a prior and data-generating process specified by the following.
θ₁ ~ N(0, σ₁²)
θ₂ ~ N(0, σ₂²)
@tonyduan
tonyduan / logreg-label-shift.py
Last active May 2, 2020 00:23
Exploring different scoring rules in a logistic regression model with label shift.
"""
Simulation experiment with a logistic regression model with 2D class-conditional Gaussians.
μ₁ = [1, 1]
μ₀ = [-1, -0.5]
x⁽¹⁾ ~ N(μ₁, I) for y = 1
x⁽⁰⁾ ~ N(μ₀, I) for y = 0
At training time we sample y ~ Bernoulli(0.1) but at test time y ~ Bernoulli(0.5) i.e. label balanced.
@tonyduan
tonyduan / heteroskedastic-linear-model.py
Created March 17, 2020 06:57
Heteroskedastic linear model fit with CVXPY.
"""
This file contains code to fit a heteroskedastic linear model; i.e.
y ~ N(η₁, η₂) η₁ = θ₁ᵀx, η₂ = θ₂ᵀx.
Here η represent the natural parameters,
η₁ = μ/σ², η₂ = 0.5/σ² > 0.
Since this model is in the Exponential Family, it is convex.
@tonyduan
tonyduan / dfply-dash-docset.sql
Created December 3, 2019 02:36
Dash documentation for the dfply Python library.
CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT);
CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);
INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('select', 'Function', 'docs.html#select-and-drop-functions');
INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('drop', 'Function', 'docs.html#select-and-drop-functions');
INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('starts_with', 'Function', 'docs.html#selection-filter-functions');
INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('ends_with', 'Function', 'docs.html#selection-filter-functions');
INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('contains', 'Function', 'docs.html#selection-filter-functions');
INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('everything', 'Function', 'docs.html#selection-filter-functions');
INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES ('columns_between', 'Function', 'docs.html#selection-filter-functions');
@tonyduan
tonyduan / l1-ball-projection-pytorch.py
Last active March 12, 2023 11:05
Compute Euclidean projections onto the L1-ball in PyTorch.
def project_onto_l1_ball(x, eps):
"""
Compute Euclidean projection onto the L1 ball for a batch.
min ||x - u||_2 s.t. ||u||_1 <= eps
Inspired by the corresponding numpy version by Adrien Gaidon.
Parameters
----------