Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bwengals
bwengals / hpdplot2d.py
Created February 14, 2017 02:42
2d credible intervals in matplotlib from mcmc samples
def hpdplot2d(x_samples, y_samples, bins, perc=0.95, extent=None):
if extent is None:
xmin, xmax = np.min(x_samples), np.max(x_samples)
ymin, ymax = np.min(y_samples), np.max(y_samples)
else:
xmin, xmax, ymin, ymax = extent
x_flat = np.linspace(xmin, xmax, bins)
y_flat = np.linspace(ymin, ymax, bins)
x,y = np.meshgrid(x_flat, y_flat)
@bwengals
bwengals / retrodesign.R
Last active January 19, 2017 23:27
R code for Gelman & Carlin --- Beyond Power Calculations
retrodesign <- function(A, s, alpha=.05, df=Inf, n.sims=10000){
z <- qt(1-alpha/2, df)
p.hi <- 1 - pt(z-A/s, df)
p.lo <- pt(-z-A/s, df)
power <- p.hi + p.lo
typeS <- p.lo/power
estimate <- A + s*rt(n.sims,df)
significant <- abs(estimate) > s*z
exaggeration <- mean(abs(estimate)[significant])/A
return(list(power=power, typeS=typeS, exaggeration=exaggeration))
@bwengals
bwengals / splines.py
Created September 2, 2016 20:40
Patsy b-splines with an option to return the derivative
# This file is part of Patsy
# Copyright (C) 2012-2013 Nathaniel Smith <[email protected]>
# See file LICENSE.txt for license information.
# R-compatible spline basis functions
# These are made available in the patsy.* namespace
__all__ = ["bs"]
import numpy as np