Skip to content

Instantly share code, notes, and snippets.

View johnmyleswhite's full-sized avatar

John Myles White johnmyleswhite

View GitHub Profile
using Distributions
using HypothesisTests
n_sims = 100_000
n = 25
x = Array{Float64}(n)
y = Array{Float64}(n)
p_x = Array{Float64}(n_sims)
p_y = Array{Float64}(n_sims)
p_d = Array{Float64}(n_sims)
function construct_x(x9)
x = fill(0, 9)
r = fill(0, 9)
x[9], r[9] = x9, 0
x[8], r[8] = fld(7 * x[9], 6), rem(7 * x[9], 6)
x[7], r[7] = fld(7 * x[8] + 1, 6), rem(7 * x[8] + 1, 6)
for i in 7:-1:2
x[i], r[i] = fld(7 * x[i + 1] + 1, 6), rem(7 * x[i + 1] + 1, 6)
end
> x <- 1:8
> y <- c(1, 1, 2, 3, 5, 8, 13, 21)
> summary(lm(y ~ x - 1))
Call:
lm(formula = y ~ x - 1)
Residuals:
Min 1Q Median 3Q Max
-3.922 -3.306 -2.422 -0.326 7.157
@johnmyleswhite
johnmyleswhite / deans_example.R
Created October 7, 2017 12:04
Ratios in causal inference
library("ggplot2")
library("dplyr")
# Population size
n <- 2500
# Sessions per user if assigned to test
sessions_test <- as.integer(exp(rnorm(n, 0.5, 1)))
# Sessions per user if assigned to control
@johnmyleswhite
johnmyleswhite / wealth_transfers.jl
Created June 21, 2017 20:47
Dan Goldstein's fairness illusion
using Distributions
function simulate_coupled(v0, n, t_max, allow_debt)
cur = fill(v0, n)
nxt = copy(cur)
for t in 1:t_max
for i in 1:n
j = rand(1:n)
if allow_debt
@johnmyleswhite
johnmyleswhite / cor_-1_to_1.R
Last active April 26, 2017 23:14
non_robust_correlations.R
n <- 100000L
x <- rnorm(n, 0, 1)
y <- -x
cor(x, y)
for (z in c(10, 100, 1000, 10000, 100000)) {
rho <- cor(c(-z, x, z), c(-z, y, z))
print(paste0("z = ", z, "; rho = ", rho))
@johnmyleswhite
johnmyleswhite / no_term_yes_effects.R
Created February 26, 2017 16:23
Interaction Term vs Interaction Effects
library("ggplot2")
n <- 10000
s_e <- 0.01
x1 <- runif(n, -10, 10)
x2 <- runif(n, -10, 10)
y <- sin(x1 * x2) + rnorm(n, 0, s_e)
df <- data.frame(y = y, x1 = x1, x2 = x2, z = x1 * x2)
@johnmyleswhite
johnmyleswhite / median.R
Created February 23, 2017 00:40
R's Medians as a Rabbit Hole of Type Promotions and Function Indirection
> median(FALSE)
[1] FALSE
> median(c(TRUE, FALSE))
[1] 0.5
> median(c(TRUE, FALSE, TRUE))
[1] TRUE
> f <- factor(c('a', 'b', 'c'), levels = c('a', 'b', 'c'), ordered = TRUE)
@johnmyleswhite
johnmyleswhite / lifting_in_julia.md
Created February 14, 2017 17:24
Overview of Lifting in Julia

Handling Nulls in Julia

There are several core cases that need to be handled to ensure that all operations on non-nullable data can be lifted to work on nullable data.

  • The scalar case: lift f(x::T) to f(x::?T).
  • Tuple case: lift f(xs::Tuple{T1, ..., TN}) to f(x::Tuple{?T1, ..., ?Tn}).
  • Single array case: lift f(xs::Array{T}) to f(xs::Array{?T}).
  • Multiple array case: lift f(xs::Tuple{Array{T1}, ..., Array{Tn}}) to f(xs::Tuple{Array{?T1}, ..., Array{?Tn}})

Others may exist, but these are the obvious cases that we absolutely must resolve.

@johnmyleswhite
johnmyleswhite / cor_means.jl
Created January 17, 2017 16:47
Sampling distribution of means of uncorrelated vectors
using Distributions
p = 100
d_x = MultivariateNormal(zeros(p), eye(p, p))
d_y = MultivariateNormal(100 * ones(p), 2 * eye(p, p))
n_sims = 10_000
mean_x = Array(Float64, n_sims)