本文整理汇总了Python中pymc3.Deterministic方法的典型用法代码示例。如果您正苦于以下问题:Python pymc3.Deterministic方法的具体用法?Python pymc3.Deterministic怎么用?Python pymc3.Deterministic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymc3
的用法示例。
在下文中一共展示了pymc3.Deterministic方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def fit(self, X, Y, n_samples=10000, tune_steps=1000, n_jobs=4):
with pm.Model() as self.model:
# Priors
std = pm.Uniform("std", 0, self.sps, testval=X.std())
beta = pm.StudentT("beta", mu=0, lam=self.sps, nu=self.nu)
alpha = pm.StudentT("alpha", mu=0, lam=self.sps, nu=self.nu, testval=Y.mean())
# Deterministic model
mean = pm.Deterministic("mean", alpha + beta * X)
# Posterior distribution
obs = pm.Normal("obs", mu=mean, sd=std, observed=Y)
## Run MCMC
# Find search start value with maximum a posterior estimation
start = pm.find_MAP()
# sample posterior distribution for latent variables
trace = pm.sample(n_samples, njobs=n_jobs, tune=tune_steps, start=start)
# Recover posterior samples
self.burned_trace = trace[int(n_samples / 2):]
示例2: built_model
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def built_model(self):
"""
Initialise :class:`pymc3.Model` depending on problem composites,
geodetic and/or seismic data are included. Composites also determine
the problem to be solved.
"""
logger.info('... Building model ...\n')
pc = self.config.problem_config
with Model() as self.model:
self.rvs, self.fixed_params = self.get_random_variables()
self.init_hyperparams()
total_llk = tt.zeros((1), tconfig.floatX)
for datatype, composite in self.composites.items():
if datatype in bconfig.modes_catalog[pc.mode].keys():
input_rvs = weed_input_rvs(
self.rvs, pc.mode, datatype=datatype)
fixed_rvs = weed_input_rvs(
self.fixed_params, pc.mode, datatype=datatype)
else:
input_rvs = self.rvs
fixed_rvs = self.fixed_params
total_llk += composite.get_formula(
input_rvs, fixed_rvs, self.hyperparams, pc)
# deterministic RV to write out llks to file
like = Deterministic('tmp', total_llk)
# will overwrite deterministic name ...
llk = Potential(self._like_name, like)
logger.info('Model building was successful! \n')
示例3: built_hyper_model
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def built_hyper_model(self):
"""
Initialise :class:`pymc3.Model` depending on configuration file,
geodetic and/or seismic data are included. Estimates initial parameter
bounds for hyperparameters.
"""
logger.info('... Building Hyper model ...\n')
pc = self.config.problem_config
if len(self.hierarchicals) == 0:
self.init_hierarchicals()
point = self.get_random_point(include=['hierarchicals', 'priors'])
if self.config.problem_config.mode == bconfig.geometry_mode_str:
for param in pc.priors.values():
point[param.name] = param.testvalue
with Model() as self.model:
self.init_hyperparams()
total_llk = tt.zeros((1), tconfig.floatX)
for composite in self.composites.values():
if hasattr(composite, 'analyse_noise'):
composite.analyse_noise(point)
composite.init_weights()
composite.update_llks(point)
total_llk += composite.get_hyper_formula(self.hyperparams)
like = Deterministic('tmp', total_llk)
llk = Potential(self._like_name, like)
logger.info('Hyper model building was successful!')
示例4: get_hyper_formula
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def get_hyper_formula(self, hyperparams):
"""
Get likelihood formula for the hyper model built. Has to be called
within a with model context.
problem_config : :class:`config.ProblemConfig`
"""
hp_specific = self.config.dataset_specific_residual_noise_estimation
logpts = hyper_normal(
self.datasets, hyperparams, self._llks,
hp_specific=hp_specific)
llk = Deterministic(self._like_name, logpts)
return llk.sum()
示例5: get_hyper_formula
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def get_hyper_formula(self, hyperparams):
"""
Get likelihood formula for the hyper model built. Has to be called
within a with model context.
"""
logpts = tt.zeros((self.n_t), tconfig.floatX)
for k in range(self.n_t):
logpt = self._eval_prior(
hyperparams[bconfig.hyper_name_laplacian], self._llks[k])
logpts = tt.set_subtensor(logpts[k:k + 1], logpt)
llk = Deterministic(self._like_name, logpts)
return llk.sum()
示例6: fit
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def fit(self, X, y):
"""
Fits a Gaussian Process regressor using MCMC.
Parameters
----------
X: np.ndarray, shape=(nsamples, nfeatures)
Training instances to fit the GP.
y: np.ndarray, shape=(nsamples,)
Corresponding continuous target values to `X`.
"""
self.X = X
self.n = self.X.shape[0]
self.y = y
self.model = pm.Model()
with self.model as model:
l = pm.Uniform('l', 0, 10)
log_s2_f = pm.Uniform('log_s2_f', lower=-7, upper=5)
s2_f = pm.Deterministic('sigmaf', tt.exp(log_s2_f))
log_s2_n = pm.Uniform('log_s2_n', lower=-7, upper=5)
s2_n = pm.Deterministic('sigman', tt.exp(log_s2_n))
f_cov = s2_f * covariance_equivalence[type(self.covfunc).__name__](1, l)
Sigma = f_cov(self.X) + tt.eye(self.n) * s2_n ** 2
y_obs = pm.MvNormal('y_obs', mu=np.zeros(self.n), cov=Sigma, observed=self.y)
with self.model as model:
if self.step is not None:
self.trace = pm.sample(self.niter, step=self.step())[self.burnin:]
else:
self.trace = pm.sample(self.niter, init=self.init)[self.burnin:]
示例7: fit
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def fit(self, X, y):
"""
Fits a Student-t regressor using MCMC.
Parameters
----------
X: np.ndarray, shape=(nsamples, nfeatures)
Training instances to fit the GP.
y: np.ndarray, shape=(nsamples,)
Corresponding continuous target values to `X`.
"""
self.X = X
self.n = self.X.shape[0]
self.y = y
self.model = pm.Model()
with self.model as model:
l = pm.Uniform('l', 0, 10)
log_s2_f = pm.Uniform('log_s2_f', lower=-7, upper=5)
s2_f = pm.Deterministic('sigmaf', tt.exp(log_s2_f))
log_s2_n = pm.Uniform('log_s2_n', lower=-7, upper=5)
s2_n = pm.Deterministic('sigman', tt.exp(log_s2_n))
f_cov = s2_f * covariance_equivalence[type(self.covfunc).__name__](1, l)
Sigma = f_cov(self.X) + tt.eye(self.n) * s2_n ** 2
y_obs = pm.MvStudentT('y_obs', nu=self.nu, mu=np.zeros(self.n), Sigma=Sigma, observed=self.y)
with self.model as model:
if self.step is not None:
self.trace = pm.sample(self.niter, step=self.step())[self.burnin:]
else:
self.trace = pm.sample(self.niter, init=self.init)[self.burnin:]
示例8: pymc3_noncentered_schools
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def pymc3_noncentered_schools(data, draws, chains):
"""Non-centered eight schools implementation for pymc3."""
import pymc3 as pm
with pm.Model() as model:
mu = pm.Normal("mu", mu=0, sd=5)
tau = pm.HalfCauchy("tau", beta=5)
eta = pm.Normal("eta", mu=0, sd=1, shape=data["J"])
theta = pm.Deterministic("theta", mu + tau * eta)
pm.Normal("obs", mu=theta, sd=data["sigma"], observed=data["y"])
trace = pm.sample(draws, chains=chains)
return model, trace
示例9: _build_dist
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def _build_dist(self, spec, label, dist, **kwargs):
"""Build and return a PyMC3 Distribution."""
if isinstance(dist, str):
if hasattr(pm, dist):
dist = getattr(pm, dist)
elif dist in self.dists:
dist = self.dists[dist]
else:
raise ValueError(
f"The Distribution {dist} was not found in PyMC3 or the PyMC3BackEnd."
)
# Inspect all args in case we have hyperparameters
def _expand_args(key, value, label):
if isinstance(value, Prior):
label = f"{label}_{key}"
return self._build_dist(spec, label, value.name, **value.args)
return value
kwargs = {k: _expand_args(k, v, label) for (k, v) in kwargs.items()}
# Non-centered parameterization for hyperpriors
if (
spec.noncentered
and "sigma" in kwargs
and "observed" not in kwargs
and isinstance(kwargs["sigma"], pm.model.TransformedRV)
):
old_sigma = kwargs["sigma"]
_offset = pm.Normal(label + "_offset", mu=0, sigma=1, shape=kwargs["shape"])
return pm.Deterministic(label, _offset * old_sigma)
return dist(label, **kwargs)
示例10: _create_model
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def _create_model(self):
with pm.Model() as self.model:
# getting the location primers
for layer_index in range(self.num_layers):
setattr(self, 'w%d' % layer_index, self.__get_weights(layer_index, self.weight_shapes[layer_index]))
setattr(self, 'b%d' % layer_index, self.__get_biases(layer_index, self.bias_shapes[layer_index]))
if layer_index == 0:
fc = pm.Deterministic('fc%d' % layer_index, pm.math.tanh(pm.math.dot(self.network_input, self.weight(layer_index)) + self.bias(layer_index)))
setattr(self, 'fc%d' % layer_index, fc)
elif 0 < layer_index < self.num_layers - 1:
fc = pm.Deterministic('fc%d' % layer_index, pm.math.tanh(pm.math.dot(getattr(self, 'fc%d' % (layer_index - 1)), self.weight(layer_index)) + self.bias(layer_index)))
setattr(self, 'fc%d' % layer_index, fc)
else:
self._loc = pm.Deterministic('bnn_out', pm.math.sigmoid(pm.math.dot(getattr(self, 'fc%d' % (layer_index - 1)), self.weight(layer_index)) + self.bias(layer_index)) )
# getting the precision / standard deviation / variance
self.tau_rescaling = np.zeros((self.num_obs, self.network_input.shape[1]))
for obs_index in range(self.num_obs):
self.tau_rescaling[obs_index] += self.var_e_ranges
self.tau_rescaling = self.tau_rescaling**2
tau = pm.Gamma('tau', self.num_obs**2, 1., shape = (self.num_obs, self.network_input.shape[1]))
self.tau = tau / self.tau_rescaling
self.scale = pm.Deterministic('scale', 1. / pm.math.sqrt(self.tau))
# learn the floats
self.loc = pm.Deterministic('loc', (self.upper_rescalings - self.lower_rescalings) * self._loc + self.lower_rescalings)
self.out_floats = pm.Normal('out_floats', self.loc[:, self.floats], tau = self.tau[:, self.floats], observed = self.network_output[:, self._floats])
# learn the integers
self.int_scale = pm.Deterministic('int_scale', 1. * self.scale)
self.out_ints = DiscreteLaplace('out_ints', loc = self.loc[:, self.ints], scale = self.int_scale[:, self.ints], observed = self.network_output[:, self._ints])
# learn the categories
dist_counter, cat_var_index = 0, 0
self.alpha = pm.Deterministic('alpha', (self.loc + 1.) * self.scale)
self.num_cats = 0
for var_e_index, var_e_type in enumerate(self.var_e_types):
if var_e_type == 'categorical' and self.var_e_begin[var_e_index] == var_e_index:
begin, end = self.var_e_begin[var_e_index], self.var_e_end[var_e_index]
var_e_name = self.var_e_names[var_e_index]
param_index = np.argwhere(self.var_p_names == var_e_name)[0, 0]
self.param_index = param_index
out_dirichlet = pm.Dirichlet('dirich_%d' % dist_counter, a = self.alpha[:, begin : end], shape = (self.num_obs, int(end - begin)) )
out_cats = pm.Categorical('out_cats_%d' % dist_counter, p = out_dirichlet, observed = self.network_output[:, param_index])
self.num_cats += 1
dist_counter += 1
示例11: _create_model_old
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def _create_model_old(self):
self._get_rescalings()
with pm.Model() as self.model:
# getting the location
for layer_index in range(self.num_layers):
setattr(self, 'w%d' % layer_index, self.__get_weights(layer_index, self.weight_shapes[layer_index]))
setattr(self, 'b%d' % layer_index, self.__get_biases(layer_index, self.bias_shapes[layer_index]))
if layer_index == 0:
fc = pm.Deterministic('fc%d' % layer_index, pm.math.tanh(pm.math.dot(self.network_input, self.weight(layer_index)) + self.bias(layer_index)))
setattr(self, 'fc%d' % layer_index, fc)
elif 0 < layer_index < self.num_layers - 1:
fc = pm.Deterministic('fc%d' % layer_index, pm.math.tanh(pm.math.dot(getattr(self, 'fc%d' % (layer_index - 1)), self.weight(layer_index)) + self.bias(layer_index)))
setattr(self, 'fc%d' % layer_index, fc)
else:
self.loc = pm.Deterministic('loc', (self.upper_rescalings - self.lower_rescalings) * pm.math.sigmoid(pm.math.dot(getattr(self, 'fc%d' % (layer_index - 1)), self.weight(layer_index)) + self.bias(layer_index)) + self.lower_rescalings)
# getting the standard deviation (or rather precision)
self.tau_rescaling = np.zeros((self.num_obs, self.observed_params.shape[1]))
for obs_index in range(self.num_obs):
self.tau_rescaling[obs_index] += self.domain_ranges
self.tau_rescaling = self.tau_rescaling**2
self.tau = pm.Gamma('tau', self.num_obs**2, 1., shape = (self.num_obs, self.observed_params.shape[1]))
self.tau = self.tau / self.tau_rescaling
# self.sd = pm.Deterministic('sd', 0.05 + 1. / pm.math.sqrt(self.tau))
self.scale = pm.Deterministic('scale', 1. / pm.math.sqrt(self.tau))
print(self.observed_params.shape)
print(self._floats)
print(self._integers)
quit()
# now that we got all locations and scales we can start getting the distributions
# floats are easy, as we can take loc and scale as they are
self.out = pm.Normal('out', self.loc, tau = self.tau, observed = self.observed_params)
# integers are a bit more tricky and require the following transformation for the beta binomial
alpha = ((n - mu) / sigma**2 - 1) / (n / mu - (n - mu) / sigma**2)
beta = (n / mu - 1) * alpha
self.alpha = pm.Deterministic('alpha', alpha)
self.beta = pm.Deterministic('beta', beta)
示例12: get_formula
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def get_formula(
self, input_rvs, fixed_rvs, hyperparams, problem_config):
"""
Get geodetic likelihood formula for the model built. Has to be called
within a with model context.
Part of the pymc3 model.
Parameters
----------
input_rvs : dict
of :class:`pymc3.distribution.Distribution`
fixed_rvs : dict
of :class:`numpy.array`
hyperparams : dict
of :class:`pymc3.distribution.Distribution`
problem_config : :class:`config.ProblemConfig`
Returns
-------
posterior_llk : :class:`theano.tensor.Tensor`
"""
hp_specific = self.config.dataset_specific_residual_noise_estimation
self.input_rvs = input_rvs
self.fixed_rvs = fixed_rvs
logger.info(
'Geodetic optimization on: \n '
'%s' % ', '.join(self.input_rvs.keys()))
self.input_rvs.update(fixed_rvs)
t0 = time()
disp = self.get_synths(self.input_rvs)
t1 = time()
logger.debug(
'Geodetic forward model on test model takes: %f' %
(t1 - t0))
los_disp = (disp * self.slos_vectors).sum(axis=1)
residuals = self.Bij.srmap(
tt.cast((self.sdata - los_disp) * self.sodws, tconfig.floatX))
self.init_hierarchicals(problem_config)
if len(self.hierarchicals) > 0:
residuals = self.remove_ramps(residuals)
logpts = multivariate_normal_chol(
self.datasets, self.weights, hyperparams, residuals,
hp_specific=hp_specific)
llk = Deterministic(self._like_name, logpts)
return llk.sum()
示例13: impute
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def impute(self, X):
"""Generate imputations using predictions from the fit bayesian model.
The transform method returns the values for imputation. Missing values
in a given dataset are replaced with the samples from the posterior
predictive distribution of each missing data point.
Args:
X (pd.DataFrame): predictors to determine imputed values.
Returns:
np.array: imputed dataset.
"""
# check if fitted then predict with least squares
check_is_fitted(self, "statistics_")
model = self.statistics_["param"]
# add a Deterministic node for each missing value
# sampling then pulls from the posterior predictive distribution
# each missing data point. I.e. distribution for EACH missing
with model:
mu_pred = pm.Deterministic(
"mu_pred", model["alpha"]+model["beta"].dot(X.T)
)
tr = pm.sample(
self.sample,
tune=self.tune,
init=self.init,
**self.sample_kwargs
)
self.trace_ = tr
# decide how to impute. Use mean of posterior predictive or random draw
# not supported yet, but eventually consider using the MAP
if not self.fill_value or self.fill_value == "mean":
imp = tr["mu_pred"].mean(0)
elif self.fill_value == "random":
imp = np.apply_along_axis(np.random.choice, 0, tr["mu_pred"])
else:
err = f"{self.fill_value} must be 'mean' or 'random'."
raise ValueError(err)
return imp
示例14: constant_data_to_xarray
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def constant_data_to_xarray(self):
"""Convert constant data to xarray."""
# For constant data, we are concerned only with deterministics and data.
# The constant data vars must be either pm.Data (TensorSharedVariable) or pm.Deterministic
constant_data_vars = {} # type: Dict[str, Var]
for var in self.model.deterministics:
ancestors = self.theano.tensor.gof.graph.ancestors(var.owner.inputs)
# no dependency on a random variable
if not any((isinstance(a, self.pymc3.model.PyMC3Variable) for a in ancestors)):
constant_data_vars[var.name] = var
def is_data(name, var) -> bool:
assert self.model is not None
return (
var not in self.model.deterministics
and var not in self.model.observed_RVs
and var not in self.model.free_RVs
and var not in self.model.potentials
and (self.observations is None or name not in self.observations)
)
# I don't know how to find pm.Data, except that they are named variables that aren't
# observed or free RVs, nor are they deterministics, and then we eliminate observations.
for name, var in self.model.named_vars.items():
if is_data(name, var):
constant_data_vars[name] = var
if not constant_data_vars:
return None
if self.dims is None:
dims = {}
else:
dims = self.dims
constant_data = {}
for name, vals in constant_data_vars.items():
if hasattr(vals, "get_value"):
vals = vals.get_value()
# this might be a Deterministic, and must be evaluated
elif hasattr(self.model[name], "eval"):
vals = self.model[name].eval()
vals = np.atleast_1d(vals)
val_dims = dims.get(name)
val_dims, coords = generate_dims_coords(
vals.shape, name, dims=val_dims, coords=self.coords
)
# filter coords based on the dims
coords = {key: xr.IndexVariable((key,), data=coords[key]) for key in val_dims}
try:
constant_data[name] = xr.DataArray(vals, dims=val_dims, coords=coords)
except ValueError as e: # pylint: disable=invalid-name
raise ValueError("Error translating constant_data variable %s: %s" % (name, e))
return xr.Dataset(data_vars=constant_data, attrs=make_attrs(library=self.pymc3))
示例15: fit_cross_cov
# 需要导入模块: import pymc3 [as 别名]
# 或者: from pymc3 import Deterministic [as 别名]
def fit_cross_cov(self, n_exp=2, n_gauss=2, range_mu=None):
"""
Fit an analytical covariance to the experimental data.
Args:
n_exp (int): number of exponential basic functions
n_gauss (int): number of gaussian basic functions
range_mu: prior mean of the range. Default mean of the lags
Returns:
pymc.Model: PyMC3 model to be sampled using MCMC
"""
self.n_exp = n_exp
self.n_gauss = n_gauss
n_var = self.n_properties
df = self.exp_var
lags = self.lags
# Prior standard deviation for the error of the regression
prior_std_reg = df.std(0).max() * 10
# Prior value for the mean of the ranges
if not range_mu:
range_mu = lags.mean()
# pymc3 Model
with pm.Model() as model: # model specifications in PyMC3 are wrapped in a with-statement
# Define priors
sigma = pm.HalfCauchy('sigma', beta=prior_std_reg, testval=1., shape=n_var)
psill = pm.Normal('sill', prior_std_reg, sd=.5 * prior_std_reg, shape=(n_exp + n_gauss))
range_ = pm.Normal('range', range_mu, sd=range_mu * .3, shape=(n_exp + n_gauss))
lambda_ = pm.Uniform('weights', 0, 1, shape=(n_var * (n_exp + n_gauss)))
# Exponential covariance
exp = pm.Deterministic('exp',
# (lambda_[:n_exp*n_var]*
psill[:n_exp] *
(1. - T.exp(T.dot(-lags.values.reshape((len(lags), 1)),
(range_[:n_exp].reshape((1, n_exp)) / 3.) ** -1))))
gauss = pm.Deterministic('gaus',
psill[n_exp:] *
(1. - T.exp(T.dot(-lags.values.reshape((len(lags), 1)) ** 2,
(range_[n_exp:].reshape((1, n_gauss)) * 4 / 7.) ** -2))))
# We stack the basic functions in the same matrix and tile it to match the number of properties we have
func = pm.Deterministic('func', T.tile(T.horizontal_stack(exp, gauss), (n_var, 1, 1)))
# We weight each basic function and sum them
func_w = pm.Deterministic("func_w", T.sum(func * lambda_.reshape((n_var, 1, (n_exp + n_gauss))), axis=2))
for e, cross in enumerate(df.columns):
# Likelihoods
pm.Normal(cross + "_like", mu=func_w[e], sd=sigma[e], observed=df[cross].values)
return model