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 PyPlot
ϵ = 0.0001
n_grid = 10_000
ps = linspace(ϵ, 1 - ϵ, n_grid)
ss = [std(Bernoulli(p)) for p in ps]
sks = [skewness(Bernoulli(p)) for p in ps]
using Distributions
using HypothesisTests
n_sims = 1_000_000
n = 2
x = Array{Float64}(n)
y = Array{Float64}(n)
p_d = Array{Float64}(n_sims)
for s in 1:n_sims
for i in 1:n
using Distributions
using HypothesisTests
n_sims = 1_000_000
n = 20
x = Array{Float64}(n)
y = Array{Float64}(n)
p_d = Array{Float64}(n_sims)
for s in 1:n_sims
for i in 1:n
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)