Skip to content

Instantly share code, notes, and snippets.

View adamlauretig's full-sized avatar
🎶

Adam Lauretig adamlauretig

🎶
View GitHub Profile
@mblondel
mblondel / check_convex.py
Last active March 21, 2022 22:25
A small script to get numerical evidence that a function is convex
# Authors: Mathieu Blondel, Vlad Niculae
# License: BSD 3 clause
import numpy as np
def _gen_pairs(gen, max_iter, max_inner, random_state, verbose):
rng = np.random.RandomState(random_state)
# if tuple, interpret as randn
@wmay
wmay / latex_engine.Rmd
Last active December 2, 2019 02:30
Simple Latex knitr engine
---
title: "Latex engine test"
author: "William May"
date: "December 1, 2019"
output: pdf_document
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
functions {
real matrix_f_lpdf(matrix cov, real nu, real delta){
int k = cols(cov);
return(lmgamma(k, (nu + delta + k - 1)/2) - (lmgamma(k, nu/2) + lmgamma(k, (delta + k - 1)/2) + log(1)) + log_determinant(cov)*((nu -k - 1)/2) - (nu + delta + k - 1)/2 * log_determinant(cov + diag_matrix(rep_vector(1, k))));
}
real matrix_f_fast_lpdf(matrix cov, real nu, real delta){
int k = cols(cov);
real log_det_cov = 2*sum(log(diagonal(cholesky_decompose(cov))));
real I_Sig_log_det = 2*sum(log(diagonal(cholesky_decompose(diag_matrix(rep_vector(1,k)) + cov))));
@khakieconomics
khakieconomics / matrix_completion.stan
Created November 27, 2018 01:02
A simple matrix completion model for fixed K in Stan
data {
int N; // number of individuals
int T; // number of time periods
matrix[N, T] Y; // outcome matrix; missing entries set to -9.0
int K; // rank of matrix
}
parameters {
matrix[N, K] M; // individual loadings
matrix[T, K] U; // time factors
real<lower = 0> sigma; // error scale
library(rstanarm); library(tidyverse)
options(mc.cores = parallel::detectCores())
set.seed(42)
data("radon")
head(treatment_sample)
# Some levels have no variance in the outcomes, making likelihood estimates impossible
# Adding a tiny bit of noise fixes the problem
radon$log_uranium <- rnorm(nrow(radon), radon$log_uranium, 0.05)
@ahalterman
ahalterman / spacy_events.py
Created March 13, 2018 20:21
Event Data in 30 Lines of Python
import spacy
nlp = spacy.load("en_core_web_lg")
with open("scraped.json", "r") as f:
news = json.load(f)
news = [i['body'] for i in news]
processed_docs = list(nlp.pipe(news))
verb_list = ["launch", "begin", "initiate", "start"]
dobj_list = ["attack", "offensive", "operation", "assault"]
@bfbraum
bfbraum / Simple ABM template.R
Created May 29, 2017 15:44
A template for running and plotting a very simple agent-based model in R
# Template for running and plotting a very simple agent-based model in R
# Professor Bear Braumoeller, Department of Political Science, Ohio State
# This code creates a 20x20 grid of 0s and 1s, which represent values of some
# variable held by agents in those cells. It then chooses two adjacent cells,
# the first at random and the second at random from among the first cell's
# neighbors, and applies a simple rule -- the first cell takes on the value
# of the second. It iterates this cell selection and rule application 1,000
# times, displays the result, and tracks the fraction of 1s in the matrix
# over time.
@rmcelreath
rmcelreath / garden plots.R
Created November 4, 2016 09:34
Code for drawing the forking data gardens in Chapter 2 of "Statistical Rethinking" textbook
# functions for plotting garden of forking data plots
library(rethinking)
polar2screen <- function( dist, origin, theta ) {
## takes dist, angle and origin and returns x and y of destination point
vx <- cos(theta) * dist;
vy <- sin(theta) * dist;
c( origin[1]+vx , origin[2]+vy );
}
@mbjoseph
mbjoseph / leung_drton_factor_analysis.R
Last active January 16, 2020 08:56
Factor analysis sandbox
# Simulation script for factor analysis ala Leung & Drton (2016) ----------
library(rstan)
library(bayesplot)
m <- 5 # dimension of observed data (e.g., # traits)
k <- 2 # number of latent factors
n <- 100 # number of sample units (e.g., # species)
# residual variance matrix (is diagonal)
Omega <- diag(.3 + abs(rnorm(m, sd = .3)))