Skip to content

Instantly share code, notes, and snippets.

Avatar

Corey Chivers cjbayesian

View GitHub Profile
@cjbayesian
cjbayesian / plot_km.py
Created Jan 8, 2020
Plot kaplan-meier style survival curves with errorbars
View plot_km.py
import scipy as sp
def beta_errors(num, denom):
return sp.stats.beta.interval(0.95, num+1, denom-num+1)
def plot_km(df, threshold=0.5, max_days=365, y_text_shrink=1, ax=None):
days = range(max_days)
idb_above = df['Pred']>threshold
survival_series = df['survival_time_days']
@cjbayesian
cjbayesian / cdf_diff.py
Created Nov 15, 2018
Plot cummulative distributions of multiple groups for comparison
View cdf_diff.py
def cdf_diff(df, var, grp='label', col=None, rm_outlier=None, hard_lim=None, ax=None, xlim=None):
'''Plot cummulative distributions of multiple groups for comparison.
Arguments:
df: DataFrame
var: string, name of column to be plotted
grp: string, grouping variable
col: list, colors to use for each group
rm_outlier: None|float, remove datapoints beyond this many sigma.
ax: axis on which to plot. Default none will return a new figure
@cjbayesian
cjbayesian / calibration_plot.py
Last active Nov 12, 2018
Plot a calibration plot with error bars. Optionally overlay v in each bin.
View calibration_plot.py
from scipy import stats
import numpy as np
import matplotlib as plt
def beta_errors(num, denom):
return stats.beta.interval(.95, num+1, denom-num+1)
def calibration_curve_error_bars(a, p, n_bins=10):
pmin, pmax = p.min(), p.max()
@cjbayesian
cjbayesian / Weierstrass_monster.py
Created Oct 31, 2017
Plot Weierstrass' monster
View Weierstrass_monster.py
def Weierstrass(x, reps=10):
res = np.zeros(x.shape[0])
for i in range(reps):
num = x*(3**i)*np.pi
denom = 2.0**i
res = res + np.cos(num)/denom
return res
title = '$f(x) = {cos(3x\pi)}/{2} + {cos(3^2x\pi)}/{2^2} + {cos(3^3x\pi)}/{2^3} ...$'
delta = 0.5
@cjbayesian
cjbayesian / gp_resources.md
Created Oct 11, 2017
Gaussian Processes Learning Resources
View gp_resources.md

Gaussian Processes Learning Resources

Author: Corey Chivers

View MatplotlibHighResolutionViaEPS.md

Create arbitrarily high resolution figures using matplotlib

In python, create your figure and save it as .eps. This will generate a vector image of your figure:

fig, ax = plt.subplots()
ax.plot(range(10))
fig.savefig('straightLine.eps', format='eps')
View HW_sim.R
#################################################################################################
#### This is a simulation to demonstrate how real populations reach Hardy Weinburg equilibrium
#### under random mating.
#### Author: Corey Chivers, 2011
#################################################################################################
cross<-function(parents)
{
offspring<-c('d','d') #initiate a child object
offspring[1]<-sample(parents[1,],1)
View plot_correlogram.py
def plot_correlogram(df,figsize=(20,20)):
''' Creat an n x n matrix of scatter plots for every
combination of numeric columns in a dataframe'''
cols = list(df.columns[df.dtypes=='float64'])
n = len(cols)
fig, ax = plt.subplots(n,n,figsize=figsize)
for i,y in enumerate(cols):
for j,x in enumerate(cols):
if i != n-1:
@cjbayesian
cjbayesian / buffet_basketball.R
Last active Jan 4, 2016
What’s Warren Buffett’s $1 Billion Basketball Bet Worth? I look into it in this post http://wp.me/p1C5UP-gF
View buffet_basketball.R
## Warren Buffet's 1B Basketball Challenge ##
expected_value <- function(p,ngames=63,prize=1000000000){
p^ngames * prize
}
## What is the expected value of an entry
## given a particular level of prediction accuracy
expected_value(p=0.80)
expected_value(p=0.85)
@cjbayesian
cjbayesian / parseBMC_XML.py
Created Dec 20, 2013
Parse BMC OA articles from XML to utf-8 text of the title, abstract, and body.
View parseBMC_XML.py
#!/usr/bin/python
"""
Parse BMC OA articles from XML to utf-8 text of the title, abstract, and body.
"""
import libxml2
from os import listdir
from time import gmtime, strftime
input_dir = './BMC_FTP/content/articles/'
files = listdir(input_dir)
You can’t perform that action at this time.