-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cam Davidson-Pilon
committed
Mar 5, 2013
1 parent
c876056
commit c504f48
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import pymc as mc | ||
|
||
count_data = np.loadtxt("../../Chapter1_Introduction/chp1data/txtdata.csv") | ||
n_count_data = len(count_data) | ||
|
||
alpha = 1.0/count_data.mean() #recall count_data is | ||
#the variable that holds our txt counts | ||
|
||
lambda_1 = mc.Exponential( "lambda_1", alpha ) | ||
lambda_2 = mc.Exponential( "lambda_2", alpha ) | ||
|
||
tau = mc.DiscreteUniform( "tau", lower = 0, upper = n ) | ||
|
||
@mc.deterministic | ||
def lambda_( tau = tau, lambda_1 = lambda_1, lambda_2 = lambda_2 ): | ||
out = np.zeros( n_count_data ) | ||
out[:tau] = lambda_1 #lambda before tau is lambda1 | ||
out[tau:] = lambda_2 #lambda after tau is lambda1 | ||
return out | ||
|
||
observation = mc.Poisson( "obs", lambda_, value = count_data, observed = True) | ||
model = mc.Model( [observation, lambda_1, lambda_2, tau] ) | ||
|
||
|
||
mcmc = mc.MCMC(model) | ||
mcmc.sample( 100000, 50000, 1 ) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pymc as mc | ||
|
||
p = mc.Uniform( "freq_cheating", 0, 1) | ||
|
||
@mc.deterministic | ||
def p_skewed( p = p ): | ||
return 0.5*p + 0.25 | ||
|
||
yes_responses = mc.Binomial( "number_cheaters", 100, p_skewed, value = 35, observed = True ) | ||
|
||
model = mc.Model( [yes_responses, p_skewed, p ] ) | ||
|
||
### To Be Explained in Chapter 3! | ||
mcmc = mc.MCMC(model) | ||
mcmc.sample( 50000, 25000 ) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pymc as mc | ||
|
||
|
||
challenger_data = np.genfromtxt("../../Chapter2_MorePyMC/chp2data/challenger_data.csv", skip_header = 1, usecols=[1,2], missing_values="NA", delimiter=",") | ||
#drop the NA values | ||
challenger_data = challenger_data[ ~np.isnan(challenger_data[:,1]) ] | ||
|
||
|
||
temperature = challenger_data[:,0] | ||
D = challenger_data[:,1] #defect or not? | ||
|
||
beta = mc.Normal( "beta", 0, 0.001, value = 0 ) | ||
alpha = mc.Normal( "alpha", 0, 0.001, value = 0 ) | ||
|
||
@mc.deterministic | ||
def p( temp = temperature, alpha = alpha, beta = beta): | ||
return 1.0/( 1. + np.exp( beta*temperature + alpha) ) | ||
|
||
|
||
observed = mc.Bernoulli( "bernoulli_obs", p, value = D, observed=True) | ||
|
||
model = mc.Model( [observed, beta, alpha] ) | ||
|
||
#mysterious code to be explained in Chapter 3 | ||
map_ = mc.MAP(model) | ||
map_.fit() | ||
mcmc = mc.MCMC( model ) | ||
mcmc.sample( 260000, 220000, 2 ) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
import pymc as mc | ||
|
||
|
||
data = np.loadtxt( "../../Chapter3_MCMC/data/mixture_data.csv", delimiter="," ) | ||
|
||
|
||
p = mc.Uniform( "p", 0, 1) | ||
|
||
assignment = mc.Categorical("assignment", [p, 1-p], size = data.shape[0] ) | ||
|
||
taus = 1.0/mc.Uniform( "stds", 0, 100, size= 2)**2 #notice the size! | ||
centers = mc.Normal( "centers", [150, 150], [0.001, 0.001], size =2 ) | ||
|
||
""" | ||
The below determinsitic functions map a assingment, in this case 0 or 1, | ||
to a set of parameters, located in the (1,2) arrays `taus` and `centers.` | ||
""" | ||
|
||
@mc.deterministic | ||
def center_i( assignment = assignment, centers = centers ): | ||
return centers[ assignment] | ||
|
||
@mc.deterministic | ||
def tau_i( assignment = assignment, taus = taus ): | ||
return taus[ assignment] | ||
|
||
#and to combine it with the observations: | ||
observations = mc.Normal( "obs", center_i, tau_i, value = data, observed = True ) | ||
|
||
#below we create a model class | ||
model = mc.Model( [p, assignment, taus, centers ] ) | ||
|
||
|
||
map_ = mc.MAP( model ) | ||
map_.fit() | ||
mcmc = mc.MCMC( model ) | ||
mcmc.sample( 100000, 50000 ) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Read Me | ||
|
||
|
||
Included is all PyMC examples and models *out of context*, this is for users to easily view the entire Python program. | ||
|