Skip to content

Instantly share code, notes, and snippets.

View ckrapu's full-sized avatar

Christopher Krapu ckrapu

View GitHub Profile
@ckrapu
ckrapu / pymc3-gpu-test.py
Last active November 21, 2021 20:59
PyMC3 GPU test
import os
os.environ['THEANO_FLAGS'] = 'device=cuda,floatX=float64'
import pymc3 as pm
with pm.Model():
x = pm.Normal('x', shape=1000)
trace = pm.sample(chains=1, cores=1)
@ckrapu
ckrapu / theano-gpu-test.py
Last active November 21, 2021 20:48
Theano GPU test
import theano
from theano import function, config, shared, tensor as tt
import numpy
import time
vlen = 10 * 30 * 768 # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
@ckrapu
ckrapu / fixed-sampling-step.py
Created April 24, 2021 16:35
fixed-sampling-step
import pymc3 as pm
import numpy as np
import matplotlib.pyplot as plt
import arviz as az
from pymc3.step_methods.arraystep import BlockedStep
b = [2,1.5]
sigma = 2
@ckrapu
ckrapu / conjugate-step-lr.py
Created April 24, 2021 16:30
Conjugate step for linear regression in PyMC3
import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pymc3 as pm
from pymc3.step_methods.arraystep import BlockedStep
# ### Create simulated data under linear regression model
@ckrapu
ckrapu / fa-xr-ufunc.py
Created April 23, 2021 16:28
fa-xr-ufunc
Y = xr.DataArray(Y_raw, dims=('observed_columns','rows'))
Y = Y.assign_coords({'observed_columns':np.arange(d),'rows':np.arange(n)})
def post_hoc_fun(W, psi, Y, rng=None):
'''
Recovers latent variables given observation Y and single draw of W, psi
from the posterior distribution.
'''
k, d = W.shape[-2:]
_, n = Y.shape
@ckrapu
ckrapu / download-ca-2020-ozone.py
Created April 8, 2021 04:27
Preprocess California ozone measurements from EPA Air Quality Portal
import pandas as pd
# Run this as a notebook so that the bash cell magic ! works
import pandas as pd
! wget https://aqs.epa.gov/aqsweb/airdata/daily_44201_2020.zip
filepath = './daily_44201_2020.csv'
df = pd.read_csv(filepath)
import enum
import numpy as np
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid
from mesa.datacollection import DataCollector
from tqdm import tqdm
class InfectionModel(Model):
@ckrapu
ckrapu / mpl-color-cycle.py
Created April 6, 2021 15:52
mpl-color-cycle
colors = [
'tab:blue',
'tab:orange',
'tab:green',
'tab:red',
'tab:purple',
'tab:brown',
'tab:pink',
'tab:gray',
'tab:olive',
@ckrapu
ckrapu / test-error-msg-v4.txt
Created April 1, 2021 16:30
test-error-msg-v4.txt
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-6e14f40a5c7d> in <module>
5 p = pm.Uniform("p", 0, 1)
6 pm.Binomial("w", p=p, n=2, observed=1)
----> 7 inference_data = pm.sample(500, chains=2, return_inferencedata=True)
8
9 assert inference_data
~/anaconda3/envs/pymc3-dev-py39/lib/python3.9/site-packages/pymc3-3.11.1-py3.9.egg/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, progressbar, model, random_seed, discard_tuned_samples, compute_convergence_checks, callback, jitter_max_retries, return_inferencedata, idata_kwargs, mp_ctx, pickle_backend, **kwargs)
@ckrapu
ckrapu / dirichlet-concat-divergence
Created April 1, 2021 04:24
dirichlet-concat-divergence.py
## Combined model
c_comb = np.asarray([[16,29,4],
[16,29,6],
[14,30,4],
[16,29,3],
[16,31,5],
[13,29,5],
[15,32,5],
[15,29,6],
[17,31,6],