Skip to content

Instantly share code, notes, and snippets.

View tomicapretto's full-sized avatar
🇦🇷

Tomás Capretto tomicapretto

🇦🇷
View GitHub Profile
@tomicapretto
tomicapretto / sparse_khatri_rao.ipynb
Last active December 11, 2025 02:17
Khatri-Rao product with sparse matrices
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tomicapretto
tomicapretto / glmm_playground.py
Last active December 8, 2025 02:51
R2D2 for GLMMs in Python
import numpy as np
import matplotlib.pyplot as plt
import polars as pl
from scipy import stats
from scipy.optimize import minimize, minimize_scalar
from scipy import special
from abc import ABC, abstractmethod
@tomicapretto
tomicapretto / hmc.R
Last active March 14, 2024 21:53
HMC in R
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Utils #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
ltail <- function(l) {
l[[length(l)]]
}
lappend <- function(l, object) {
l[[length(l) + 1]] <- object
@tomicapretto
tomicapretto / plot_data.csv
Created January 5, 2023 03:30
Data for a plot
hp cyl gear wt mean low high
52 high A 3.2172500000000004 21.67458005068266 16.675258186122484 26.226023777045278
53 high A 3.2172500000000004 21.639924307207576 16.672473098966474 26.158665545109788
54 high A 3.2172500000000004 21.60526856373249 16.669688011810464 26.093763825389733
56 high A 3.2172500000000004 21.535957076782324 16.664117837498452 25.96509423662654
57 high A 3.2172500000000004 21.501301333307246 16.661332750342442 25.907608275838484
59 high A 3.2172500000000004 21.43198984635708 16.655762576030426 25.771625535629838
60 high A 3.2172500000000004 21.39733410288199 16.652977488874416 25.705187334912992
61 high A 3.2172500000000004 21.36267835940691 16.65019240171841 25.64056318668881
63 high A 3.2172500000000004 21.293366872456748 16.644622227406394 25.511314890240445
@tomicapretto
tomicapretto / split_top_level_terms.py
Created December 28, 2022 14:53
Split top level terms in a model formula
# Will be useful when we work with non-linear terms in Bambi.
# This is the first step.
# 1. Split top level terms DONE
# 2. Detect terms where non-linear parameters play a role TO DO
# 3. Manipulate the expression in terms where non-linear parameters play a role
# to pass only the 'predictor' to `formulae`, but keep the expression to be used later TO DO
import ast
from functools import singledispatch
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tomicapretto
tomicapretto / female_oscars.csv
Created June 23, 2022 16:28
Oscars female 2006-2016
year age name movie
2006 29 Reese Witherspoon Walk the Line
2007 61 Helen Mirren The Queen
2008 32 Marion Cotillard La Vie en rose
2009 33 Kate Winslet The Reader
2010 45 Sandra Bullock The Blind Side
2011 29 Natalie Portman Black Swan
2012 62 Meryl Streep The Iron Lady
2013 22 Jennifer Lawrence Silver Linings Playbook
2014 44 Cate Blanchett Blue Jasmine
@tomicapretto
tomicapretto / categorical_group_specific_prediction.ipynb
Created April 6, 2022 14:08
Obtaining and plotting predictions for model with categorical group specific effects
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tomicapretto
tomicapretto / issue_446.ipynb
Last active February 17, 2022 03:20
Logistic regression with varying intercept in Bambi (issue #446)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tomicapretto
tomicapretto / keywords_and_builtins.py
Created January 30, 2022 17:26
Print keywords and builtin functions as text in tabular format
import keyword
keywords = keyword.kwlist.copy()
kwrds_n = len(keywords)
rows_n = 7
cols_n = kwrds_n // rows_n if kwrds_n % rows_n == 0 else (kwrds_n // rows_n) + 1
keywords_list = [
[
keywords[rows_n * j + i]