Exemple #1
0
def build_softmax_linear(X, y, force_softmax=False):
    """
    Sample from Bayesian Softmax Linear Regression
    """
    num_features = X.shape[1]
    num_classes = len(np.unique(y))
    logistic_regression = num_classes == 2
    Xt = theano.shared(X)
    
    if logistic_regression and not force_softmax:
        print('running logistic regression')
        with pm.Model() as model:
            W = pm.Normal('W', 0, sd=1e6, shape=num_features)
            b = pm.Flat('b')
            logit = Xt.dot(W) + b
            p = tt.nnet.sigmoid(logit)
            observed = pm.Bernoulli('obs', p=p, observed=y)
    else:
        with pm.Model() as model:
            W = pm.Normal('W', 0, sd=1e6, shape=(num_features, num_classes))
            b = pm.Flat('b', shape=num_classes)
            logit = Xt.dot(W) + b
            p = tt.nnet.softmax(logit)
            observed = pm.Categorical('obs', p=p, observed=y)
    return model
def fitFlat(x, y):

    model = pm.Model()

    with model:
        # Priors for unknown model parameters
        a = pm.Flat('a')
        b = pm.Flat('b')
        c = pm.Flat('c')

        # Expected value of outcome
        mu = Model(x, a, b, c)

        # Likelihood (sampling distribution) of observations
        Like = pm.Normal('Like', mu=mu, sd=0.01 * np.ones_like(y), observed=y)

        # do sampling
        trace = pm.sample(1000,
                          progressbar=False,
                          init='ADVI',
                          step=pm.NUTS(),
                          njobs=1)

        # give summary
        summary = pm.df_summary(trace)

        return summary
Exemple #3
0
def set_model(num_teams, home_team, away_team, observed_home_goals,
              observed_away_goals):

    with pm.Model() as model:
        # global model parameters
        home = pm.Flat('home')
        sd_att = pm.HalfStudentT('sd_att', nu=3, sd=2.5)
        sd_def = pm.HalfStudentT('sd_def', nu=3, sd=2.5)
        intercept = pm.Flat('intercept')

        # team-specific model parameters
        atts_star = pm.Normal("atts_star", mu=0, sd=sd_att, shape=num_teams)
        defs_star = pm.Normal("defs_star", mu=0, sd=sd_def, shape=num_teams)

        atts = pm.Deterministic('atts', atts_star - tt.mean(atts_star))
        defs = pm.Deterministic('defs', defs_star - tt.mean(defs_star))
        home_theta = tt.exp(intercept + home + atts[home_team] +
                            defs[away_team])
        away_theta = tt.exp(intercept + atts[away_team] + defs[home_team])

        # likelihood of observed data
        home_points = pm.Poisson('home_points',
                                 mu=home_theta,
                                 observed=observed_home_goals)
        away_points = pm.Poisson('away_points',
                                 mu=away_theta,
                                 observed=observed_away_goals)

    return model
Exemple #4
0
    def construct_prior(self):
        gradient, intercept = self.initialise()
        angle, displacement = gradient_to_angle([gradient, intercept])
        residuals = self.data[:, 1] - (intercept +
                                       (gradient * self.data[:, 0]))

        pm.Flat('angle', testval=angle)
        pm.Flat('displacement', testval=displacement)
        variance = pm.HalfCauchy('variance',
                                 beta=10,
                                 testval=np.mean(residuals**2))
        pm.Deterministic('ln_variance', tt.log(variance))
Exemple #5
0
    def fit(self, X, y, y_error=1, x_error=None, *,
            sample_kwargs={'draws': 1000, 'target_accept': 0.9,
                           'return_inferencedata': False}):

        kwds = {}
        if self.kwds is not None:
            kwds.update(self.kwds)
        kwds['fit_intercept'] = False
        model = self._choose_regressor()
        self.clf_ = model(**kwds)

        self.fit_intercept = False

        if x_error is not None:
            x_error = np.atleast_2d(x_error)
        with pm.Model():
            # slope and intercept of eta-ksi relation
            slope = pm.Flat('slope', shape=(X.shape[0], ))
            inter = pm.Flat('inter')

            # intrinsic scatter of eta-ksi relation
            int_std = pm.HalfFlat('int_std')
            # standard deviation of Gaussian that ksi are drawn from (assumed mean zero)
            tau = pm.HalfFlat('tau', shape=(X.shape[0],))
            # intrinsic ksi
            mu = pm.Normal('mu', mu=0, sigma=tau, shape=(X.shape[0],))

            # Some wizzarding with the dimensions all around.
            ksi = pm.Normal('ksi', mu=mu, tau=tau, shape=X.T.shape)

            # intrinsic eta-ksi linear relation + intrinsic scatter
            eta = pm.Normal('eta', mu=(tt.dot(slope.T, ksi.T) + inter),
                            sigma=int_std, shape=y.shape)

            # observed xi, yi
            x = pm.Normal('xi', mu=ksi.T, sigma=x_error, observed=X, shape=X.shape)  # noqa: F841
            y = pm.Normal('yi', mu=eta, sigma=y_error, observed=y, shape=y.shape)

            self.trace = pm.sample(**sample_kwargs)

            # TODO: make it optional to choose a way to define best

            HND, edges = np.histogramdd(np.hstack((self.trace['slope'],
                                                   self.trace['inter'][:, None])), bins=50)

            w = np.where(HND == HND.max())

            # choose the maximum posterior slope and intercept
            slope_best = [edges[i][w[i][0]] for i in range(len(edges) - 1)]
            intercept_best = edges[-1][w[-1][0]]
            self.clf_.coef_ = np.array([intercept_best, *slope_best])

        return self
Exemple #6
0
 def FlatExp(name, shape=(), testval=ones):
     with modelcontext():
         return pm.Flat(name,
                        transform=non_transform_log,
                        shape=shape,
                        testval=testval(shape),
                        dtype=th.config.floatX)
Exemple #7
0
 def ExpFlat(name, shape=(), testval=ones):
     with modelcontext():
         return pm.Flat(name,
                        transform=pm.distributions.transforms.log,
                        shape=shape,
                        testval=testval(shape),
                        dtype=th.config.floatX)
Exemple #8
0
    def init_model(self, target):
        days, counties = target.index, target.columns

        # extract features
        features = self.evaluate_features(days, counties)
        Y_obs = target.stack().values.astype(np.float32)
        T_S = features["temporal_seasonal"].values.astype(np.float32)
        T_T = features["temporal_trend"].values.astype(np.float32)
        TS = features["spatiotemporal"].values.astype(np.float32)

        log_exposure = np.log(
            features["exposure"].values.astype(np.float32).ravel())

        # extract dimensions
        num_obs = np.prod(target.shape)
        num_t_s = T_S.shape[1]
        num_t_t = T_T.shape[1]
        num_ts = TS.shape[1]

        with pm.Model() as self.model:
            # interaction effects are generated externally -> flat prior
            IA = pm.Flat("IA", testval=np.ones(
                (num_obs, self.num_ia)), shape=(num_obs, self.num_ia))

            # priors
            # δ = 1/√α
            δ = pm.HalfCauchy("δ", 10, testval=1.0)
            α = pm.Deterministic("α", np.float32(1.0) / δ)
            W_ia = pm.Normal("W_ia", mu=0, sd=10, testval=np.zeros(
                self.num_ia), shape=self.num_ia)
            W_t_s = pm.Normal("W_t_s", mu=0, sd=10,
                              testval=np.zeros(num_t_s), shape=num_t_s)
            W_t_t = pm.Normal("W_t_t", mu=0, sd=10,
                              testval=np.zeros(num_t_t), shape=num_t_t)
            W_ts = pm.Normal("W_ts", mu=0, sd=10,
                             testval=np.zeros(num_ts), shape=num_ts)
            self.param_names = ["δ", "W_ia", "W_t_s", "W_t_t", "W_ts"]
            self.params = [δ, W_ia, W_t_s, W_t_t, W_ts]

            # calculate interaction effect
            IA_ef = tt.dot(tt.dot(IA, self.Q), W_ia)

            # calculate mean rates
            μ = pm.Deterministic(
                "μ",
                tt.exp(
                    IA_ef +
                    tt.dot(
                        T_S,
                        W_t_s) + 
                    tt.dot(
                        T_T,
                        W_t_t) +
                    tt.dot(
                        TS,
                        W_ts) +
                    log_exposure))

            # constrain to observations
            pm.NegativeBinomial("Y", mu=μ, alpha=α, observed=Y_obs)
Exemple #9
0
 def FlatExpId(name, shape=(), testval=ones):
     with modelcontext():
         return pm.Flat(name,
                        transform=LogIdTransform(),
                        shape=shape,
                        testval=testval(shape),
                        dtype=th.config.floatX)
Exemple #10
0
def mv_prior_simple():
    n = 3
    noise = 0.1
    X = np.linspace(0, 1, n)[:, None]

    K = pm.gp.cov.ExpQuad(1, 1)(X).eval()
    L = np.linalg.cholesky(K)
    K_noise = K + noise * np.eye(n)
    obs = floatX_array([-0.1, 0.5, 1.1])

    # Posterior mean
    L_noise = np.linalg.cholesky(K_noise)
    alpha = np.linalg.solve(L_noise.T, np.linalg.solve(L_noise, obs))
    mu_post = np.dot(K.T, alpha)

    # Posterior standard deviation
    v = np.linalg.solve(L_noise, K)
    std_post = (K - np.dot(v.T, v)).diagonal()**0.5

    with pm.Model() as model:
        x = pm.Flat("x", shape=n)
        x_obs = pm.MvNormal("x_obs",
                            observed=obs,
                            mu=x,
                            cov=noise * np.eye(n),
                            shape=n)

    return model.test_point, model, (K, L, mu_post, std_post, noise)
Exemple #11
0
def run():
    teams = df.home_team.unique()
    teams = pd.DataFrame(teams, columns=['team'])
    teams['i'] = teams.index
    
    df = pd.merge(df, teams, left_on='home_team', right_on='team', how='left')
    df = df.rename(columns = {'i': 'i_home'}).drop('team', 1)
    df = pd.merge(df, teams, left_on='away_team', right_on='team', how='left')
    df = df.rename(columns = {'i': 'i_away'}).drop('team', 1)
    
    observed_home_goals = df.home_score.values
    observed_away_goals = df.away_score.values
    
    home_team = df.i_home.values
    away_team = df.i_away.values
    
    num_teams = len(df.i_home.drop_duplicates())
    num_games = len(home_team)
    
    g = df.groupby('i_away')
    att_starting_points = np.log(g.away_score.mean())
    g = df.groupby('i_home')
    def_starting_points = -np.log(g.away_score.mean())
    with pm.Model() as model:
        # global model parameters
        home = pm.Flat('home')
        sd_att = pm.HalfStudentT('sd_att', nu=3, sigma=2.5)
        sd_def = pm.HalfStudentT('sd_def', nu=3, sigma=2.5)
        intercept = pm.Flat('intercept')
    
        # team-specific model parameters
        atts_star = pm.Normal("atts_star", mu=0, sigma=sd_att, shape=num_teams)
        defs_star = pm.Normal("defs_star", mu=0, sigma=sd_def, shape=num_teams)
    
        atts = pm.Deterministic('atts', atts_star - tt.mean(atts_star))
        defs = pm.Deterministic('defs', defs_star - tt.mean(defs_star))
        home_theta = tt.exp(intercept + home + atts[home_team] + defs[away_team])
        away_theta = tt.exp(intercept + atts[away_team] + defs[home_team])
    
        # likelihood of observed data
        home_points = pm.Poisson('home_points', mu=home_theta, observed=observed_home_goals)
        away_points = pm.Poisson('away_points', mu=away_theta, observed=observed_away_goals)
    trace = pm.sample(1000, tune=1000, cores=3)
    pm.traceplot(trace, var_names=['intercept', 'home', 'sd_att', 'sd_def']);
    bfmi = pm.bfmi(trace)
    max_gr = max(np.max(gr_stats) for gr_stats in pm.gelman_rubin(trace).values())
    (pm.energyplot(trace, legend=False, figsize=(6, 4))
Exemple #12
0
def build_shallow_nn(X, y, output='regression', num_hidden=NUM_HIDDEN):
    """
    Build basic shallow Bayesian neural network
    """
    if output not in SUPPORTED_OUTPUTS:
        raise ValueError(
            'Unsupported neural network output: {}\nSupported outputs: {}'.
            format(output, SUPPORTED_OUTPUTS))
    if 'regression' == output:
        num_output_units = 1
    elif 'classification' == output:
        num_output_units = len(np.unique(y))

    num_features = X.shape[1]
    floatX = theano.config.floatX
    Xt = theano.shared(X)
    W1_init = np.random.randn(num_features, num_hidden).astype(floatX)
    W2_init = np.random.randn(num_hidden, num_output_units).astype(floatX)
    with pm.Model() as model_nn:
        # priors
        W1 = pm.Normal('W1',
                       0,
                       sd=WEIGHT_SD,
                       shape=W1_init.shape,
                       testval=W1_init)
        b1 = pm.Flat('b1', shape=W1_init.shape[1])
        W2 = pm.Normal('W2',
                       0,
                       sd=WEIGHT_SD,
                       shape=W2_init.shape,
                       testval=W2_init)
        b2 = pm.Flat('b2', shape=W2_init.shape[1])

        # deterministic transformations
        z1 = Xt.dot(W1) + b1
        a1 = pm.math.tanh(z1)
        z2 = a1.dot(W2) + b2

        # format output and plug in data
        if 'regression' == output:
            observed = pm.Normal('obs', mu=z2, sd=LIKELIHOOD_SD, observed=y)
        elif 'classification' == output:
            p = tt.nnet.softmax(z2)
            observed = pm.Categorical('obs', p=p, observed=y)

    return model_nn
Exemple #13
0
def build_fc_nn(X, y, output='regression', hidden_dims=[NUM_HIDDEN]):
    """
    Build basic fully connected Bayesian neural network
    
    Args:
        X: data matrix
        y: targets
        output: one of SUPPORTED_OUTPUTS to specify the kind of outputs
        hidden_dims: integer list indicating the size of the hidden layers
    
    Returns:
        PyMC3 Bayesian neural network model
    """
    if output not in SUPPORTED_OUTPUTS:
        raise ValueError(
            'Unsupported neural network output: {}\nSupported outputs: {}'.
            format(output, SUPPORTED_OUTPUTS))
    if 'regression' == output:
        num_output_units = 1
    elif 'classification' == output:
        num_output_units = len(np.unique(y))

    floatX = theano.config.floatX
    Xt = theano.shared(X)
    num_features = X.shape[1]
    layer_dims = [num_features] + hidden_dims

    # initialize weights (switch to Xavier initiallization?)
    Ws = []
    for i in range(len(layer_dims) - 1):
        in_dim = layer_dims[i]
        out_dim = layer_dims[i + 1]
        Ws.append(np.random.randn(in_dim, out_dim).astype(floatX))

    with pm.Model() as model_nn:
        for i in range(len(Ws)):
            # priors
            W_i = pm.Normal('W' + str(i),
                            0,
                            sd=WEIGHT_SD,
                            shape=Ws[i].shape,
                            testval=Ws[i])
            b_i = pm.Flat('b' + str(i), shape=Ws[i].shape[1])

            # deterministic transformations
            in_layer = a_i if i > 0 else Xt
            z_i = in_layer.dot(W_i) + b_i
            a_i = pm.math.tanh(z_i)

        # format output and plug in data
        # uses pre-activation of last layer
        if 'regression' == output:
            observed = pm.Normal('obs', mu=z_i, sd=LIKELIHOOD_SD, observed=y)
        elif 'classification' == output:
            p = tt.nnet.softmax(z_i)
            observed = pm.Categorical('obs', p=p, observed=y)

    return model_nn
    def fit(self,
            X,
            y,
            y_error,
            x_error,
            sample_kwargs={
                'draws': 1000,
                'target_accept': 0.9
            }):

        X = np.atleast_2d(X)
        x_error = np.atleast_2d(x_error)

        with pm.Model():
            # slope and intercept of eta-ksi relation
            slope = pm.Flat('slope', shape=(X.shape[0], ))
            inter = pm.Flat('inter')

            # intrinsic scatter of eta-ksi relation
            int_std = pm.HalfFlat('int_std')
            # standard deviation of Gaussian that ksi are drawn from (assumed mean zero)
            tau = pm.HalfFlat('tau', shape=(X.shape[0], ))
            # intrinsic ksi
            mu = pm.Normal('mu', mu=0, sd=tau, shape=(X.shape[0], ))

            # Some wizzarding with the dimensions all around.
            ksi = pm.Normal('ksi', mu=mu, tau=tau, shape=X.T.shape)

            # intrinsic eta-ksi linear relation + intrinsic scatter
            eta = pm.Normal('eta',
                            mu=(tt.dot(slope.T, ksi.T) + inter),
                            sd=int_std,
                            shape=y.shape)

            # observed xi, yi
            x = pm.Normal('xi',
                          mu=ksi.T,
                          sd=x_error,
                          observed=X,
                          shape=X.shape)
            y = pm.Normal('yi', mu=eta, sd=y_error, observed=y, shape=y.shape)

            self.trace = pm.sample(**sample_kwargs)

        return self
Exemple #15
0
def run(steppers, p):
    steppers = set(steppers)
    traces = {}
    effn = {}
    runtimes = {}

    with pm.Model() as model:
        if USE_XY:
            x = pm.Flat("x")
            y = pm.Flat("y")
            mu = np.array([0.0, 0.0])
            cov = np.array([[1.0, p], [p, 1.0]])
            z = pm.MvNormal.dist(mu=mu, cov=cov,
                                 shape=(2, )).logp(tt.stack([x, y]))
            pot = pm.Potential("logp_xy", z)
            start = {"x": 0, "y": 0}
        else:
            mu = np.array([0.0, 0.0])
            cov = np.array([[1.0, p], [p, 1.0]])
            z = pm.MvNormal("z", mu=mu, cov=cov, shape=(2, ))
            start = {"z": [0, 0]}

        for step_cls in steppers:
            name = step_cls.__name__
            t_start = time.time()
            mt = pm.sample(draws=10000,
                           chains=16,
                           parallelize=False,
                           step=step_cls(),
                           start=start)
            runtimes[name] = time.time() - t_start
            print("{} samples across {} chains".format(
                len(mt) * mt.nchains, mt.nchains))
            traces[name] = mt
            en = pm.ess(mt)
            print(f"effective: {en}\r\n")
            if USE_XY:
                effn[name] = np.mean(en["x"]) / len(mt) / mt.nchains
            else:
                effn[name] = np.mean(en["z"]) / len(mt) / mt.nchains
    return traces, effn, runtimes
Exemple #16
0
def run(steppers, p):
    steppers = set(steppers)
    traces = {}
    effn = {}
    runtimes = {}

    with pm.Model() as model:
        if USE_XY:
            x = pm.Flat('x')
            y = pm.Flat('y')
            mu = np.array([0., 0.])
            cov = np.array([[1., p], [p, 1.]])
            z = pm.MvNormal.dist(mu=mu, cov=cov,
                                 shape=(2, )).logp(tt.stack([x, y]))
            pot = pm.Potential('logp_xy', z)
            start = {'x': 0, 'y': 0}
        else:
            mu = np.array([0., 0.])
            cov = np.array([[1., p], [p, 1.]])
            z = pm.MvNormal('z', mu=mu, cov=cov, shape=(2, ))
            start = {'z': [0, 0]}

        for step_cls in steppers:
            name = step_cls.__name__
            t_start = time.time()
            mt = pm.sample(draws=10000,
                           chains=16,
                           parallelize=False,
                           step=step_cls(),
                           start=start)
            runtimes[name] = time.time() - t_start
            print('{} samples across {} chains'.format(
                len(mt) * mt.nchains, mt.nchains))
            traces[name] = mt
            en = pm.diagnostics.effective_n(mt)
            print('effective: {}\r\n'.format(en))
            if USE_XY:
                effn[name] = np.mean(en['x']) / len(mt) / mt.nchains
            else:
                effn[name] = np.mean(en['z']) / len(mt) / mt.nchains
    return traces, effn, runtimes
def bayesian_fit(x, y, y_error, num_samples=500):

    with pm.Model() as model:

        # Setup the model
        A1 = pm.Flat("A1")
        A2 = pm.Flat("A2")
        A3 = pm.Flat("A3")
        A4 = pm.Flat("A4")
        B1 = pm.Flat("B1")
        B2 = pm.Flat("B2")
        B3 = pm.Flat("B3")
        B4 = pm.Flat("B4")
        # y_stdev = pm.HalfNormal("Y_stdev", sd=1)

        y_mean = pm.Deterministic("y_mean",
                                  pade_func(x, A1, A2, A3, A4, B1, B2, B3, B4))
        # y_pred = pm.Normal("y_pred", mu=y_mean, observed=y)
        # y_pred = pm.Normal("y_pred", mu=y_mean, sd=y_error, observed=y)
        y_pred = pm.Normal("y_pred",
                           mu=y_mean,
                           tau=1.0 / y_error**2,
                           observed=y)

        # Do the fitting
        start = pm.find_MAP()
        trace = pm.sample(num_samples,
                          start=start,
                          tune=int(num_samples / 2.0),
                          chains=mp.cpu_count())

    varnames = ["A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4"]
    pm.traceplot(trace, var_names=varnames)
    summary = az.summary(trace, var_names=varnames, credible_interval=0.95)
    print(summary)

    plt.show()

    # az.plot_hpd(x, trace["y_mean"], credible_interval=0.95)

    ppc = pm.sample_posterior_predictive(trace, samples=500, model=model)
    sample = ppc["y_pred"]

    y_fit_min = np.percentile(sample, 2.5, axis=0)
    y_fit = np.percentile(trace.y_mean, 50, axis=0)
    y_fit_max = np.percentile(sample, 97.5, axis=0)

    return y_fit, y_fit_min, y_fit_max
Exemple #18
0
def simple_normal(bounded_prior=False):
    """Simple normal for testing MLE / MAP; probes issue #2482."""
    x0 = 10.0
    sd = 1.0
    a, b = (9, 12)  # bounds for uniform RV, need non-symmetric to reproduce issue

    with pm.Model() as model:
        if bounded_prior:
            mu_i = pm.Uniform("mu_i", a, b)
        else:
            mu_i = pm.Flat("mu_i")
        pm.Normal("X_obs", mu=mu_i, sigma=sd, observed=x0)

    return model.test_point, model, None
Exemple #19
0
def test_adam(kwargs, seed=20200520):
    np.random.seed(seed)
    x0 = np.random.randn(2)

    x_torch = torch.tensor(x0, dtype=torch.float64, requires_grad=True)
    optimizer = torch.optim.Adam([x_torch], **kwargs)

    with pm.Model():
        x = pm.Flat("x", shape=2, testval=x0)
        pm.Potential("rosenbrock", -rosenbrock(x))
        for obj, point in op.optimize_iterator(
            op.Adam(**kwargs), 100, vars=[x]
        ):
            optimizer.zero_grad()
            loss = rosenbrock(x_torch)
            loss.backward()
            optimizer.step()
            assert np.allclose(x_torch.detach().numpy(), point["x"])
    def fit(self, n_steps=50000):
        """
        Creates a Bayesian Estimation model for replicate measurements of
        treatment(s) vs. control.

        Parameters
        ----------
        n_steps : int
            The number of steps to run ADVI.
        """

        sample_names = set(self.data[self.sample_col].values)

        with pm.Model() as model:
            # Hyperpriors
            # upper = pm.Exponential('upper', lam=0.05)
            nu = pm.Exponential('nu_minus_one', 1/29.) + 1

            # "fold", which is the estimated fold change.
            fold = pm.Flat('fold', shape=len(sample_names))

            # Assume that data have heteroskedastic (i.e. variable) error but
            # are drawn from the same HalfCauchy distribution.
            sigma = pm.HalfCauchy('sigma', beta=1, shape=len(sample_names))

            # Model prediction
            mu = fold[self.data['indices'].values]
            sig = sigma[self.data['indices'].values]

            # Data likelihood
            like = pm.StudentT('like', nu=nu, mu=mu, sd=sig**-2,
                               observed=self.data[self.output_col])

            # Sample from posterior
            v_params = pm.variational.advi(n=n_steps)
            start = pm.variational.sample_vp(v_params, 1)[0]
            cov = np.power(model.dict_to_array(v_params.stds), 2)
            step = pm.NUTS(scaling=cov, is_cov=True)
            logging.info('Starting MCMC sampling')
            trace = pm.sample(step=step, start=start, draws=2000)

        self.trace = trace
        self.model = model
Exemple #21
0
def mcmc_sf_clf(n, K, K_noiseless, K_s, f, f_latent):
    with pm.Model(theano_config={"compute_test_value": "ignore"}) as model:
        f_sample = pm.Flat('f_sample', shape=n)
        f_transform = pm.invlogit(f_sample)
        y = pm.Binomial('y', observed=f, n=np.ones(n), p=f_transform, shape=n)
        L_h = tt.slinalg.cholesky(K)
        f_pred = pm.Deterministic(
            'f_pred',
            pm.invlogit(
                tt.dot(
                    K_s.T,
                    tt.slinalg.solve(L_h.T, tt.slinalg.solve(L_h, f_sample)))))
        ess_step = pm.EllipticalSlice(vars=[f_sample], prior_cov=K)
        trace = pm.sample(1000,
                          tune=1000,
                          start=model.test_point,
                          step=[ess_step],
                          progressbar=True,
                          njobs=1)
    return model, trace
Exemple #22
0
        def _gen_d_vars_pm(tmñ=(), fmt_nmbrs='{}'):
            egr = {}
            for p, líms in líms_paráms.items():
                nmbr = fmt_nmbrs.format(p)
                if aprioris is None:
                    if líms[0] is None:
                        if líms[1] is None:
                            dist_pm = pm.Flat(nmbr, shape=tmñ)
                        else:
                            if líms[1] == 0:
                                dist_pm = -pm.HalfFlat(nmbr, shape=tmñ)
                            else:
                                dist_pm = líms[1] - pm.HalfFlat(nmbr,
                                                                shape=tmñ)
                    else:
                        if líms[1] is None:
                            if líms[0] == 0:
                                dist_pm = pm.HalfFlat(nmbr, shape=tmñ)
                            else:
                                dist_pm = líms[0] + pm.HalfFlat(nmbr,
                                                                shape=tmñ)
                        else:
                            dist_pm = pm.Uniform(nmbr,
                                                 lower=líms[0],
                                                 upper=líms[1],
                                                 shape=tmñ)
                else:
                    dist, prms = aprioris[p]
                    if (líms[0] is not None
                            or líms[1] is not None) and dist != pm.Uniform:
                        acotada = pm.Bound(dist, lower=líms[0], upper=líms[1])
                        dist_pm = acotada(nmbr, shape=tmñ, **prms)
                    else:
                        if dist == pm.Uniform:
                            prms['lower'] = max(prms['lower'], líms[0])
                            prms['upper'] = min(prms['upper'], líms[1])
                        dist_pm = dist(nmbr, shape=tmñ, **prms)

                egr[p] = dist_pm
            return egr
Exemple #23
0
def sample_ensemble(x, y, curves, map_options, sample_options):
    if map_options is None:
        map_options = default_map_options
    if sample_options is None:
        sample_options = default_sample_options

    curves = skeletons.get_curve_set(curves)
    map_estimates = {
        curve[0]: map_single(x, y, curve, map_options)
        for curve in curves
    }
    print(curves)
    print(map_estimates)
    start = {
        name: map_estimate[name]
        for name, map_estimate in map_estimates.items()
    }
    start['weights_unnormalized_interval_'] = np.zeros(len(curves))
    start['sd_interval_'] = 0

    model_ensemble = pymc3.Model()
    with model_ensemble:
        mu_single = []
        for name, n_params, func in curves:
            params = pymc3.Flat(name, shape=n_params)
            mu_single.append(func(x, params))
        weights_unnormalized = pymc3.Uniform(
            'weights_unnnormalized', lower=0, upper=1, shape=len(curves))
        weights_normalized = pymc3.Deterministic(
            'weights_normalized', weights_unnormalized / weights_unnormalized.sum())
        mu_ensemble = weights_normalized.dot(mu_single)
        sd = pymc3.Uniform('sd', lower=1e-9, upper=1e-1)
        pymc3.Deterministic('sd', sd)
        pymc3.Normal('y_obs', mu=mu_ensemble, observed=y, sd=sd)

    with model_ensemble:
        trace = pymc3.sample(
            start=start, step=pymc3.Metropolis(), **sample_options)

    return trace
Exemple #24
0
def test_set_initval():
    # Make sure the dependencies between variables are maintained when
    # generating initial values
    rng = np.random.RandomState(392)

    with pm.Model(rng_seeder=rng) as model:
        eta = pm.Uniform("eta", 1.0, 2.0, size=(1, 1))
        mu = pm.Normal("mu", sd=eta, initval=[[100]])
        alpha = pm.HalfNormal("alpha", initval=100)
        value = pm.NegativeBinomial("value", mu=mu, alpha=alpha)

    assert np.array_equal(model.initial_values[model.rvs_to_values[mu]], np.array([[100.0]]))
    np.testing.assert_almost_equal(model.initial_values[model.rvs_to_values[alpha]], np.log(100))
    assert 50 < model.initial_values[model.rvs_to_values[value]] < 150

    # `Flat` cannot be sampled, so let's make sure that doesn't break initial
    # value computations
    with pm.Model() as model:
        x = pm.Flat("x")
        y = pm.Normal("y", x, 1)

    assert model.rvs_to_values[y] in model.initial_values
Exemple #25
0
def _single(x, y, curve, map_only, map_options, sample_options):
    if map_options is None:
        map_options = default_map_options
    if sample_options is None:
        sample_options = default_sample_options

    curve = skeletons.get_curve(curve)
    name, n_params, func = curve

    model_single = pymc3.Model()
    with model_single:
        params = pymc3.Flat(name, shape=n_params)
        mu = func(x, params)
        sd = pymc3.Uniform('sd', lower=1e-9, upper=1e-1)
        pymc3.Normal('y_obs', mu=mu, sd=sd, observed=y)

        map_estimate = pymc3.find_MAP(**map_options)
        if map_only:
            return map_estimate

        trace = pymc3.sample(start=map_estimate,
                             step=pymc3.Metropolis(), **sample_options)  #
        return trace
Exemple #26
0
        xpu = pm.Uniform('xpu', lower=xu - eu, upper=xu)
        xnl = tt.min(xn)
        xnu = tt.max(xn)

        # linear transform (naive to x_p^prime calculation)
        a1 = pm.Deterministic(
            'a1', (xpu - xpl) / (xnu - xnl)
        )  #TODO: think how we can work around the fact that here we might divide by zero
        a0 = pm.Deterministic('a0', (xpu - a1 * xnu))

        x = pm.Deterministic('x', a1 * xn + a0)
        q = pm.Deterministic('q', x + model.q0)

        # poly coeffs
        bn = [
            pm.Flat('bn' + str(k), shape=M, testval=testval['b' + str(k)])
            for k in range(K)
        ]
        b = [pm.Deterministic('b0',(bn[0]-scb)/sca)] + \
            [pm.Deterministic('b' + str(k),bn[k]/sca) for k in range(1,K)]

        xxx = pm.math.concatenate(
            [x for _ in range(M)],
            axis=1)  #that list comprehension is just a "repmat"
        # mu = b0 + \
        #      xxx*b1 + \
        #      xxx*xxx*b2
        mu = bn[0]
        for k in range(1, K):
            mu = mu + bn[k] * xxx**k
Exemple #27
0
def _to_pymc3_distribution(name, par):
    """
    Create a pymc3 continuous distribution from a Bounds object.

    Parameters
    ----------
    name : str
        Name of parameter
    par : refnx.analysis.Parameter
        The parameter to wrap

    Returns
    -------
    d : pymc3.Distribution
        The pymc3 distribution

    """
    import pymc3 as pm
    import theano.tensor as T
    from theano.compile.ops import as_op

    dist = par.bounds
    # interval and both lb, ub are finite
    if (isinstance(dist, Interval) and
            np.isfinite([dist.lb, dist.ub]).all()):
        return pm.Uniform(name, dist.lb, dist.ub)
    # no bounds
    elif (isinstance(dist, Interval) and
          np.isneginf(dist.lb) and
          np.isinf(dist.lb)):
        return pm.Flat(name)
    # half open uniform
    elif isinstance(dist, Interval) and not np.isfinite(dist.lb):
        return dist.ub - pm.HalfFlat(name)
    # half open uniform
    elif isinstance(dist, Interval) and not np.isfinite(dist.ub):
        return dist.lb + pm.HalfFlat(name)

    # it's a PDF
    if isinstance(dist, PDF):
        dist_gen = getattr(dist.rv, 'dist', None)

        if isinstance(dist.rv, stats.rv_continuous):
            dist_gen = dist.rv

        if isinstance(dist_gen, type(stats.uniform)):
            if hasattr(dist.rv, 'args'):
                p = pm.Uniform(name, dist.rv.args[0],
                               dist.rv.args[1] + dist.rv.args[0])
            else:
                p = pm.Uniform(name, 0, 1)
            return p

        # norm from scipy.stats
        if isinstance(dist_gen, type(stats.norm)):
            if hasattr(dist.rv, 'args'):
                p = pm.Normal(name, mu=dist.rv.args[0], sd=dist.rv.args[1])
            else:
                p = pm.Normal(name, mu=0, sd=1)
            return p

    # not open, uniform, or normal, so fall back to DensityDist.
    d = as_op(itypes=[T.dscalar], otypes=[T.dscalar])(dist.logp)
    r = as_op(itypes=[T.dscalar], otypes=[T.dscalar])(dist.rvs)
    p = pm.DensityDist(name, d, random=r)

    return p
 def test_flat(self):
     with pm.Model():
         f = pm.Flat('f')
         with pytest.raises(ValueError):
             f.random(1)
Exemple #29
0
beta0 = 2.0  # coefficients for linear predictor
beta1 = 0.75
beta2 = -1.25

xb = beta0 + beta1 * x1 + beta2 * x2  # linear predictor
exb = 1 - norm.sf(xb)  # inverse probit link
py = bernoulli.rvs(exb)
df = pandas.DataFrame({'x1': x1, 'x2': x2, 'by': py})  # re-write data

# Fit
niter = 10000  # parameters for MCMC

with pm.Model() as model_glm:
    # define priors
    beta0 = pm.Flat('beta0')
    beta1 = pm.Flat('beta1')
    beta2 = pm.Flat('beta2')

    # define likelihood
    theta_p = beta0 + beta1 * x1 + beta2 * x2
    theta = probit_phi(theta_p)
    y_obs = pm.Bernoulli('y_obs', p=theta, observed=py)

    # inference
    start = pm.find_MAP()  # find starting value by optimization
    step = pm.NUTS()
    trace = pm.sample(niter, step, start, random_seed=135, progressbar=True)

# Print summary to screen
pm.summary(trace)
Exemple #30
0
# log demand ratio
d['log_d_ratio'] = np.log(1 + d.demand_ratio)
d = d[d['log_d_ratio'] < d['log_d_ratio'].quantile(0.997)]

del data

with pm.Model() as hm:
    # priors for reg coefficients
    a = pm.Normal('a_int', mu=0, sd=100)
    b = pm.Normal('b_slope', mu=0, sd=100)
    # estimate of log demand ratio
    log_d_est = a + b * d['Semana']

    # student-t parameters
    sigma = pm.Flat('sigma')

    # define prior for Student T degrees of freedom
    #nu = pm.DiscreteUniform('nu', lower=1, upper=100)

    lh = pm.StudentT('likelihood',
                     mu=log_d_est,
                     sd=sigma,
                     nu=30,
                     observed=d['log_d_ratio'])

from scipy import optimize

with hm:
    # MAP
    start_MAP = pm.find_MAP(fmin=optimize.fmin_powell, disp=True)