コード例 #1
0
ファイル: test_arviz_json.py プロジェクト: lgarzia/arviz_json
def test_extract_dag():
    import pymc3 as pm

    model = pm.Model()
    # test empty model
    dag = get_dag(model)
    assert len(dag) == 0

    # 3 basic variables
    with model:
        left_slope = pm.Normal("left_slope", mu=0, sd=0.5)
        right_slope = pm.Normal("right_slope", mu=0, sd=0.5)
        switchpoint = pm.Normal("switchpoint", mu=0, sd=100)
    dag = get_dag(model)
    assert len(dag) == 3

    with model:
        y_obs = pm.Normal(
            "y_obs", mu=switchpoint, sd=left_slope, observed=np.ones((10,))
        )
        rescaled = pm.Deterministic("rescaled", np.log(np.abs(left_slope)))
        penalty = pm.Potential("potential", right_slope)

    # check all six nodes are accounted for
    dag = get_dag(model)
    assert len(dag) == 6

    # check parents
    assert "left_slope" in dag["y_obs"]["parents"]
    assert "switchpoint" in dag["y_obs"]["parents"]
    assert "left_slope" in dag["rescaled"]["parents"]
    assert "right_slope" not in dag["y_obs"]["parents"]
    # check variable types
    assert dag["left_slope"]["type"] == "free"
    assert dag["y_obs"]["type"] == "observed"
    assert dag["potential"]["type"] == "potential"
    assert dag["rescaled"]["type"] == "deterministic"

    assert dag["y_obs"]["distribution"]["type"] == "Normal"
    assert dag["left_slope"]["distribution"]["type"] == "Normal"
コード例 #2
0
ファイル: demo_model.py プロジェクト: lgarzia/arviz_json
def capture_inference(model, samples=1000, chains=4, predictive=500):
    with model:
        # Get posterior trace, prior trace, posterior predictive samples, and the DAG
        trace = pm.sample(samples=samples, chains=chains)
        prior = pm.sample_prior_predictive(samples=samples)
        posterior_predictive = pm.sample_posterior_predictive(trace)

        dag = get_dag(model)

    # will also capture all the sampler statistics
    data = az.from_pymc3(trace=trace,
                         prior=prior,
                         posterior_predictive=posterior_predictive)

    # insert dag into sampler stat attributes
    data.sample_stats.attrs["graph"] = dag
    return data
コード例 #3
0
ファイル: model_geometry.py プロジェクト: mcguenther/ipme
fileName='golf_geometry_PyMC3'
samples=2000
chains=2 
tune=1000
geometry_model=pm.Model(coords=coords)
with geometry_model:
    #to store the n-parameter of Binomial dist 
    #in the constant group of ArviZ InferenceData
    #You should always call it n for imd to retrieve it
    n = pm.Data('n', data.tries)
    sigma_angle = pm.HalfNormal('sigma_angle')
    p_goes_in = pm.Deterministic('p_goes_in', 2 * Phi(tt.arcsin((CUP_RADIUS - BALL_RADIUS) / data.distance) / sigma_angle) - 1, dims='distance')
    successes = pm.Binomial('successes', n=n, p=p_goes_in, observed=data.successes, dims='distance')
    #inference
    trace_g = pm.sample(draws=samples, chains=chains, tune=tune)
    prior_g= pm.sample_prior_predictive(samples=samples)
    posterior_predictive_g = pm.sample_posterior_predictive(trace_g,samples=samples)    
    
## STEP 1
# will also capture all the sampler statistics
data_g = az.from_pymc3(trace=trace_g, prior=prior_g, posterior_predictive=posterior_predictive_g)

## STEP 2
#dag    
dag_g = get_dag(geometry_model)    
# insert dag into sampler stat attributes
data_g.sample_stats.attrs["graph"] = str(dag_g)

## STEP 3    
# save data
arviz_to_json(data_g, fileName+'.npz')
コード例 #4
0
ファイル: model_centered.py プロジェクト: mcguenther/ipme
sigma = np.array([15., 10., 16., 11.,  9., 11., 10., 18.])

#model-inference
coords_c = {"school": ["A","B","C","D","E","F","G","H"]}
fileName_c="eight_schools_centered"
samples=4000
chains=2
tune=1000
with pm.Model(coords=coords_c) as centered_eight:
    mu = pm.Normal('mu', mu=0, sigma=5)
    tau = pm.HalfCauchy('tau', beta=5)
    theta = pm.Normal('theta', mu=mu, sigma=tau, dims='school')
    y = pm.Normal('y', mu=theta, sigma=sigma, observed=obs, dims='school')
	#inference
	trace_c = pm.sample(samples, chains=chains, tune=tune, random_seed=SEED)
    prior_c= pm.sample_prior_predictive(samples=samples)
    posterior_predictive_c = pm.sample_posterior_predictive(trace_c, samples=samples) 

## STEP 1	
# will also capture all the sampler statistics
data_c = az.from_pymc3(trace = trace_c, prior = prior_c, posterior_predictive = posterior_predictive_c)

## STEP 2
#dag
dag_c = get_dag(centered_eight)   
# insert dag into sampler stat attributes
data_c.sample_stats.attrs["graph"] = str(dag_c)

## STEP 3    
# save data   
arviz_to_json(data_c, fileName_c+'.npz')
コード例 #5
0
    a = pm.Normal("a", mu=mu_a, sd=sigma_a, dims="driver")
    b = pm.Normal("b", mu=mu_b, sd=sigma_b, dims="driver")
    sigma = pm.HalfNormal("sigma", sd=sigma_sigma, dims="driver")
    y_pred = pm.Normal('y_pred',
                       mu=a[driver_idx] + b[driver_idx] * day_idx,
                       sd=sigma[driver_idx],
                       observed=reactions.Reaction,
                       dims="driver_idx_day")
    ## inference
    trace_hi = pm.sample(draws=samples, chains=chains, tune=tune)
    prior_hi = pm.sample_prior_predictive(samples=samples)
    posterior_predictive_hi = pm.sample_posterior_predictive(trace_hi,
                                                             samples=samples)

## STEP 1
## export inference results in ArviZ InferenceData obj
## will also capture all the sampler statistics
data_hi = az.from_pymc3(trace=trace_hi,
                        prior=prior_hi,
                        posterior_predictive=posterior_predictive_hi)

## STEP 2
## extract dag
dag_hi = get_dag(hierarchical_model)
## insert dag into sampler stat attributes
data_hi.sample_stats.attrs["graph"] = str(dag_hi)

## STEP 3
## save data
fileName_hi = "reaction_times_hierarchical"
arviz_to_json(data_hi, fileName_hi + '.npz')
コード例 #6
0
#model-inference
coords = {"school": ["A","B","C","D","E","F","G","H"]}
samples=5000
chains=2
tune=1000
fileName="eight_schools_non_centered"
with pm.Model(coords=coords) as NonCentered_eight:
    mu = pm.Normal('mu', mu=0, sigma=5)
    tau = pm.HalfCauchy('tau', beta=5)
    theta_tilde = pm.Normal('theta_t', mu=0, sigma=1, dims='school')
    theta = pm.Deterministic('theta', mu + tau * theta_tilde, dims='school')
    y = pm.Normal('y', mu=theta, sigma=sigma, observed=obs, dims='school')
	#inference
    trace_nc = pm.sample(samples, chains=chains, tune=tune, random_seed=SEED, target_accept=.90)
    prior_nc= pm.sample_prior_predictive(samples=samples)
    posterior_predictive_nc = pm.sample_posterior_predictive(trace_nc,samples=samples)
	
## STEP 1	    
# will also capture all the sampler statistics
data_nc = az.from_pymc3(trace = trace_nc, prior = prior_nc, posterior_predictive = posterior_predictive_nc)

## STEP 2
#dag	
dag_nc = get_dag(NonCentered_eight)    
# insert dag into sampler stat attributes
data_nc.sample_stats.attrs["graph"] = str(dag_nc)

## STEP 3   
# save data   
arviz_to_json(data_nc, fileName+'.npz')
コード例 #7
0
ファイル: demo_model.py プロジェクト: lgarzia/arviz_json
        trace = pm.sample(samples=samples, chains=chains)
        prior = pm.sample_prior_predictive(samples=samples)
        posterior_predictive = pm.sample_posterior_predictive(trace)

        dag = get_dag(model)

    # will also capture all the sampler statistics
    data = az.from_pymc3(trace=trace,
                         prior=prior,
                         posterior_predictive=posterior_predictive)

    # insert dag into sampler stat attributes
    data.sample_stats.attrs["graph"] = dag
    return data


if __name__ == "__main__":
    # generate a single switchpoint model
    poverty = load_data()
    model = define_model(poverty)
    dag = get_dag(model)
    data = capture_inference(model)
    arviz_to_json(data, "switchpoint.npz")

    # generate multiple models
    models = {
        "centered": az.load_arviz_data("centered_eight"),
        "noncentered": az.load_arviz_data("non_centered_eight"),
    }
    multi_arviz_to_json(models, "multimodel.zip")
コード例 #8
0
ファイル: model.py プロジェクト: mcguenther/ipme
dates=returns.index.strftime("%Y/%m/%d").tolist()

#model-inference
fileName='stochastic_volatility_PyMC3'
samples=2000
tune=2000
chains=2
coords = {"date": dates}
with pm.Model(coords=coords) as model:
	step_size = pm.Exponential('step_size', 10)
    volatility = pm.GaussianRandomWalk('volatility', sigma=step_size, dims='date')
    nu = pm.Exponential('nu', 0.1)
    returns = pm.StudentT('returns',nu=nu,lam=np.exp(-2*volatility) ,observed=data["change"], dims='date')
	#inference
	trace = pm.sample(draws=samples, chains=chains, tune=tune)
    prior = pm.sample_prior_predictive(samples=samples)
    posterior_predictive = pm.sample_posterior_predictive(trace,samples=samples)    

## STEP 1
# will also capture all the sampler statistics
data = az.from_pymc3(trace=trace, prior=prior, posterior_predictive=posterior_predictive)

## STEP 2
#dag
dag = get_dag(stochastic_vol_model)
# insert dag into sampler stat attributes
data.sample_stats.attrs["graph"] = str(dag)

## STEP 3   
# save data      
arviz_to_json(data, fileName+'.npz')
コード例 #9
0
#model-inference
fileName='coal_mining_disasters_PyMC3'
samples=10000
tune=10000
chains=2
coords = {"year": years}
with pm.Model(coords=coords) as disaster_model:
    switchpoint = pm.DiscreteUniform('switchpoint', lower=years.min(), upper=years.max(), testval=1900)
    early_rate = pm.Exponential('early_rate', 1)
    late_rate = pm.Exponential('late_rate', 1)
    rate = pm.math.switch(switchpoint >= years, early_rate, late_rate)
    disasters = pm.Poisson('disasters', rate, observed=disaster_data, dims='year')
	#inference
	trace = pm.sample(samples, chains=chains, tune=tune)
    prior = pm.sample_prior_predictive(samples=samples)
    posterior_predictive = pm.sample_posterior_predictive(trace,samples=samples) 
	
## STEP 1	
# will also capture all the sampler statistics
data = az.from_pymc3(trace=trace, prior=prior, posterior_predictive=posterior_predictive)

## STEP 2	
# extract dag
dag = get_dag(disaster_model)
# insert dag into sampler stat attributes
data.sample_stats.attrs["graph"] = str(dag)

## STEP 3  
# save data      
arviz_to_json(data, fileName+'.npz')
コード例 #10
0
ファイル: model_pooled.py プロジェクト: mcguenther/ipme
    a = pm.Normal("a", mu=100, sd=250)
    b = pm.Normal("b", mu=10, sd=250)
    sigma = pm.HalfNormal("sigma", sd=200)
    y_pred = pm.Normal('y_pred',
                       mu=a + b * day_idx,
                       sd=sigma,
                       observed=reactions.Reaction,
                       dims="driver_idx_day")
    ## inference
    trace_p = pm.sample(samples, chains=chains, tune=tune)
    prior_p = pm.sample_prior_predictive(samples=samples)
    posterior_predictive_p = pm.sample_posterior_predictive(trace_p,
                                                            samples=samples)

## STEP 1
## export inference results in ArviZ InferenceData obj
## will also capture all the sampler statistics
data_p = az.from_pymc3(trace=trace_p,
                       prior=prior_p,
                       posterior_predictive=posterior_predictive_p)

## STEP 2
## extract dag
dag_p = get_dag(fullyPooled_model)
## insert dag into sampler stat attributes
data_p.sample_stats.attrs["graph"] = str(dag_p)

## STEP 3
## save data
fileName_p = "reaction_times_pooled"
arviz_to_json(data_p, fileName_p + '.npz')
コード例 #11
0
ファイル: model_simple.py プロジェクト: mcguenther/ipme
chains=2
tune=1000
simple_model=pm.Model(coords=coords)
with simple_model:
    #to store the n-parameter of Binomial dist 
    #in the constant group of ArviZ InferenceData
    #You should always call it n for imd to retrieve it
    n = pm.Data('n', data.tries)
    a = pm.Normal('a')
    b = pm.Normal('b')
    p_goes_in = pm.Deterministic('p_goes_in', pm.math.invlogit(a * data.distance + b), dims='distance')
    successes = pm.Binomial('successes', n=n, p=p_goes_in, observed=data.successes, dims='distance')
    #inference
    # Get posterior trace, prior trace, posterior predictive samples, and the DAG
    trace = pm.sample(draws=samples, chains=chains, tune=tune)
    prior= pm.sample_prior_predictive(samples=samples)
    posterior_predictive = pm.sample_posterior_predictive(trace,samples=samples)       
    
## STEP 1
# will also capture all the sampler statistics
data_s = az.from_pymc3(trace=trace, prior=prior, posterior_predictive=posterior_predictive)

## STEP 2
#dag 
dag = get_dag(simple_model)    
# insert dag into sampler stat attributes
data_s.sample_stats.attrs["graph"] = str(dag)

## STEP 3    
# save data
arviz_to_json(data_s, fileName+'.npz')