-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Update examples #2254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Update examples #2254
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -1,59 +1,54 @@ | ||
import theano.tensor as tt | ||
import numpy as np | ||
from numpy.random import multivariate_normal | ||
|
||
import pymc3 as pm | ||
|
||
# Generate some multivariate normal data: | ||
n_obs = 1000 | ||
|
||
# Mean values: | ||
mu = np.linspace(0, 2, num=4) | ||
n_var = len(mu) | ||
mu_r = np.linspace(0, 2, num=4) | ||
n_var = len(mu_r) | ||
|
||
# Standard deviations: | ||
stds = np.ones(4) / 2.0 | ||
|
||
# Correlation matrix of 4 variables: | ||
corr = np.array([[1., 0.75, 0., 0.15], | ||
[0.75, 1., -0.06, 0.19], | ||
[0., -0.06, 1., -0.04], | ||
[0.15, 0.19, -0.04, 1.]]) | ||
cov_matrix = np.diag(stds).dot(corr.dot(np.diag(stds))) | ||
|
||
dataset = multivariate_normal(mu, cov_matrix, size=n_obs) | ||
|
||
corr_r = np.array([[1., 0.75, 0., 0.15], | ||
[0.75, 1., -0.06, 0.19], | ||
[0., -0.06, 1., -0.04], | ||
[0.15, 0.19, -0.04, 1.]]) | ||
cov_matrix = np.diag(stds).dot(corr_r.dot(np.diag(stds))) | ||
|
||
# In order to convert the upper triangular correlation values to a complete | ||
# correlation matrix, we need to construct an index matrix: | ||
n_elem = int(n_var * (n_var - 1) / 2) | ||
tri_index = np.zeros([n_var, n_var], dtype=int) | ||
tri_index[np.triu_indices(n_var, k=1)] = np.arange(n_elem) | ||
tri_index[np.triu_indices(n_var, k=1)[::-1]] = np.arange(n_elem) | ||
dataset = multivariate_normal(mu_r, cov_matrix, size=n_obs) | ||
|
||
with pm.Model() as model: | ||
|
||
mu = pm.Normal('mu', mu=0, sd=1, shape=n_var) | ||
|
||
# We can specify separate priors for sigma and the correlation matrix: | ||
sigma = pm.Uniform('sigma', shape=n_var) | ||
corr_triangle = pm.LKJCorr('corr', n=1, p=n_var) | ||
corr_matrix = corr_triangle[tri_index] | ||
corr_matrix = tt.fill_diagonal(corr_matrix, 1) | ||
# Note that we access the distribution for the standard | ||
# deviations, and do not create a new random variable. | ||
sd_dist = pm.HalfCauchy.dist(beta=2.5) | ||
packed_chol = pm.LKJCholeskyCov('chol_cov', n=n_var, eta=1, sd_dist=sd_dist) | ||
# compute the covariance matrix | ||
chol = pm.expand_packed_triangular(n_var, packed_chol, lower=True) | ||
cov = tt.dot(chol, chol.T) | ||
|
||
cov_matrix = tt.diag(sigma).dot(corr_matrix.dot(tt.diag(sigma))) | ||
# Extract the standard deviations etc | ||
sd = pm.Deterministic('sd', tt.sqrt(tt.diag(cov))) | ||
corr = tt.diag(sd**-1).dot(cov.dot(tt.diag(sd**-1))) | ||
r = pm.Deterministic('r', corr[np.triu_indices(n_var, k=1)]) | ||
|
||
like = pm.MvNormal('likelihood', mu=mu, cov=cov_matrix, observed=dataset) | ||
like = pm.MvNormal('likelihood', mu=mu, chol=chol, observed=dataset) | ||
|
||
|
||
def run(n=1000): | ||
if n == "short": | ||
n = 50 | ||
with model: | ||
start = pm.find_MAP() | ||
step = pm.NUTS(scaling=start) | ||
trace = pm.sample(n, step=step, start=start) | ||
return trace | ||
trace = pm.sample(n) | ||
pm.traceplot(trace, varnames=['mu', 'r'], | ||
lines={'mu': mu_r, 'r': corr_r[np.triu_indices(n_var, k=1)]}) | ||
|
||
if __name__ == '__main__': | ||
run() |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, does it require this to converge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's fine without, but there is a divergent warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem; just surprised.