Created
July 14, 2020 14:11
-
-
Save ckrapu/52ff3eb6b9d745ed6e9d3146c0085cbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymc3 as pm | |
import numpy as np | |
def sigmoid(x): | |
return np.exp(x) / (1. + np.exp(x)) | |
# Create toy dataset | |
n = 100 | |
n_components = 2 | |
p = 2 | |
beta_true = np.random.randn(p,n_components) | |
x = np.random.randn(n,p) | |
prob = sigmoid(x @ beta_true) | |
prob = prob / prob.sum(axis=1,keepdims=True) | |
labels = (prob > 0.5).astype(int) | |
mu = [-2, 2] | |
sd = np.ones([1,1]) | |
y_raw = labels * mu + np.random.randn(n,n_components)*labels*sd | |
y = y_raw.sum(axis=1) | |
with pm.Model() as model: | |
mu = pm.Normal('mu', sd=10, shape=n_components) | |
sd = pm.Exponential('sd', lam=1, shape=n_components) | |
beta = pm.Normal('beta', sd=10, shape=(p,n_components)) | |
p = pm.Deterministic('p',pm.math.sigmoid(x@beta)) | |
components = [pm.Normal.dist(mu=mu[i], sd=sd[i]) for i in range(n_components)] | |
like = pm.Mixture('like', w=p, comp_dists=components, observed=y) | |
trace = pm.sample(init='adapt_diag') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment