예제 #1
0
    def test_mixture_list_of_poissons(self):
        with Model() as model:
            w = Dirichlet("w",
                          floatX(np.ones_like(self.pois_w)),
                          shape=self.pois_w.shape)
            mu = Gamma("mu", 1.0, 1.0, shape=self.pois_w.size)
            Mixture(
                "x_obs",
                w,
                [Poisson.dist(mu[0]), Poisson.dist(mu[1])],
                observed=self.pois_x)
            step = Metropolis()
            trace = sample(5000,
                           step,
                           random_seed=self.random_seed,
                           progressbar=False,
                           chains=1)

        assert_allclose(np.sort(trace["w"].mean(axis=0)),
                        np.sort(self.pois_w),
                        rtol=0.1,
                        atol=0.1)
        assert_allclose(np.sort(trace["mu"].mean(axis=0)),
                        np.sort(self.pois_mu),
                        rtol=0.1,
                        atol=0.1)
예제 #2
0
def createHistogramFitModel(data_bins, simulation_bins, scaleGuess):
    scale = Normal('scale', mu=scaleGuess, tau=sigToTau(.10 * scaleGuess))
    scaled_sim = scale * simulation_bins

    baseline_observed = Poisson("baseline_observed",
                                mu=scaled_sim,
                                value=data_bins,
                                observed=True)
    return locals()
예제 #3
0
    def set_models(self):
        """Define models for each group.

        :return: None
        """
        for group in ['control', 'variant']:
            self.stochastics[group] = Poisson(
                group,
                self.stochastics[group + '_mu'],
                value=getattr(self, group),
                observed=True)
예제 #4
0
])

# Switchpoint
switch = DiscreteUniform('switch', lower=0, upper=110)
# Early mean
early_mean = Exponential('early_mean', beta=1)
# Late mean
late_mean = Exponential('late_mean', beta=1)


@deterministic(plot=False)
def rates(s=switch, e=early_mean, l=late_mean):
    """Allocate appropriate mean to time series"""
    out = np.empty(len(disasters_array))
    # Early mean prior to switchpoint
    out[:s] = e
    # Late mean following switchpoint
    out[s:] = l
    return out


# The inefficient way, using the Impute function:
# D = Impute('D', Poisson, disasters_array, mu=r)

# The efficient way, using masked arrays:
# Generate masked array. Where the mask is true,
# the value is taken as missing.
masked_values = masked_array(disasters_array, mask=disasters_array == -999)
# Pass masked array to data stochastic, and it does the right thing
disasters = Poisson('disasters', mu=rates, value=masked_values, observed=True)
예제 #5
0
from pymc import DiscreteUniform, Exponential, deterministic, Poisson, Uniform
import numpy as np

disasters_array = np.array([ 4, 5, 4, 0, 1, 4, 3, 4, 0, 6, 3, 3, 4, 0, 2, 6,
                   3, 3, 5, 4, 5, 3, 1, 4, 4, 1, 5, 5, 3, 4, 2, 5,
                   2, 2, 3, 4, 2, 1, 3, 2, 2, 1, 1, 1, 1, 3, 0, 0,
                   1, 0, 1, 1, 0, 0, 3, 1, 0, 3, 2, 2, 0, 1, 1, 1,
                   0, 1, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 1, 1, 0, 2,
                   3, 3, 1, 1, 2, 1, 1, 1, 1, 2, 4, 2, 0, 0, 1, 4,
                   0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1])

switchpoint = DiscreteUniform('switchpoint', lower=0, upper=110,
                              doc='Switchpoint[year]')

early_mean = Exponential('early_mean', beta=1.)
late_mean = Exponential('late_mean', beta=1.)

@deterministic(plot=False)
def rate(s=switchpoint, e=early_mean, l=late_mean):
    out = np.empty(len(disasters_array))
    out[:s] = e
    out[s:] = l
    return out

disasters = Poisson('disasters', mu=rate, value=disasters_array, observed=True)
예제 #6
0
import gen

pathways = gen.pathways()
features = gen.features_dict()
detected = gen.detected_features()
evidence = gen.evidence()
ap = {p.name: Gamma('p_' + p.name, p.rate, 1) for p in pathways}
#bmp = [Gamma('b_{' + str(i) + '}', 1, ap[i]) for i in range(3)]
bmp = {
    p.name: {
        feat: Gamma('b_{' + p.name + ',' + str(feat) + '}', ap[p.name], 1)
        for feat in p.mets
    }
    for p in pathways
}
print bmp
#g_bmp = {feat : Poisson('g_' + str(feat), sum([bmp[pname][feat] for pname in pathways])) for feat, pathways in features.iteritems()}
g_bmp = {}
for feat, pathways in features.iteritems():
    if detected(feat):
        print feat, "was detected"
        g_bmp[feat] = Poisson('g_' + str(feat),
                              sum([bmp[pname][feat] for pname in pathways]),
                              value=1,
                              observed=True)
    else:
        print feat, "was not detected"
        g_bmp[feat] = Poisson('g_' + str(feat),
                              sum([bmp[pname][feat] for pname in pathways]))
print g_bmp
예제 #7
0
# produces basically the same result as the exponential priors
mu = np.average(linear_array)
linear_mean     = TruncatedNormal('linear_mean', a=0, b=10, mu=mu, tau=1., value=mu)
mu = np.average(spacewatch_array)
spacewatch_mean = TruncatedNormal('spacewatch_mean', a=0, b=10, mu=mu, tau=1., value=mu)
mu = np.average(catalina_array)
catalina_mean   = TruncatedNormal('catalina_mean', a=0, b=10, mu=mu, tau=1., value=mu)
mu = 1.25
panstarrs_mean  = TruncatedNormal('panstarrs_mean', a=0, b=10, mu=mu, tau=1., value=mu)
mu = 2.25
neowise_mean    = TruncatedNormal('neowise_mean', a=0, b=10, mu=mu, tau=1., value=mu)
mu = np.average(other_array)
other_mean      = TruncatedNormal('other_mean', a=0, b=10, mu=mu, tau=1., value=mu)

linear_hits     = Poisson('linear_hits', mu=linear_mean,
                          value=linear_array, observed=True)
spacewatch_hits = Poisson('spacewatch_hits', mu=spacewatch_mean,
                          value=spacewatch_array, observed=True)
catalina_hits   = Poisson('catalina_hits', mu=catalina_mean,
                          value=catalina_array, observed=True)
panstarrs_hits  = Poisson('panstarrs_hits', mu=panstarrs_mean,
                          value=panstarrs_array, observed=True)
neowise_hits    = Poisson('neowise_hits', mu=neowise_mean,
                          value=neowise_array, observed=True)
other_hits      = Poisson('other_hits', mu=other_mean,
                          value=other_array, observed=True)

linear_next     = Poisson('linear_next', mu=linear_mean)
spacewatch_next = Poisson('spacewatch_next', mu=spacewatch_mean)
catalina_next   = Poisson('catalina_next', mu=catalina_mean)
panstarrs_next  = Poisson('panstarrs_next', mu=panstarrs_mean)
예제 #8
0
disasters: D[t] ~ Poisson(early if t <= s, l otherwise)
"""
__all__ = ['s', 'e', 'l', 'r', 'D']

from pymc import DiscreteUniform, Exponential, deterministic, Poisson, Uniform
import numpy as np

disasters_array = np.array([
    4, 5, 4, 0, 1, 4, 3, 4, 0, 6, 3, 3, 4, 0, 2, 6, 3, 3, 5, 4, 5, 3, 1, 4, 4,
    1, 5, 5, 3, 4, 2, 5, 2, 2, 3, 4, 2, 1, 3, 2, 2, 1, 1, 1, 1, 3, 0, 0, 1, 0,
    1, 1, 0, 0, 3, 1, 0, 3, 2, 2, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 2, 1, 0, 0,
    0, 1, 1, 0, 2, 3, 3, 1, 1, 2, 1, 1, 1, 1, 2, 4, 2, 0, 0, 1, 4, 0, 0, 0, 1,
    0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1
])

s = DiscreteUniform('s', lower=0, upper=110)
e = Exponential('e', beta=1)
l = Exponential('l', beta=1)


@deterministic(plot=False)
def r(s=s, e=e, l=l):
    """Concatenate Poisson means"""
    out = np.empty(len(disasters_array))
    out[:s] = e
    out[s:] = l
    return out


D = Poisson('D', mu=r, value=disasters_array, observed=True)
예제 #9
0
y = [(random.random() < pi) * random.poisson(theta) for i in range(n)]


def remcache(s):
    s._cache_depth = 0
    s.gen_lazy_function()


p = Beta('p', 1, 1)

z = Bernoulli('z', p, value=array(y) > 0, plot=False)

theta_hat = Uniform('theta_hat', 0, 100, value=3)

t = z * theta
counts = Poisson('counts', t, value=y, observed=True)
model = [p, z, theta_hat, counts]

#disable caching for all the nodes
v = model + [t]
for s in v:
    remcache(s)


def pymc_logp():
    return logp_of_set(model)


def pymc_dlogp():
    return logp_gradient_of_set(model)
예제 #10
0
파일: fyba.py 프로젝트: afcarl/fyba
    def __init__(self, fname, playedto=None):
        super(LeagueMultiHomeModel, self).__init__()
        league = League(fname, playedto)

        N = len(league.teams)

        def outcome_eval(home=None, away=None):
            if home > away:
                return 1
            if home < away:
                return -1
            if home == away:
                return 0

        def clip_rate(val):
            if val > 0.2: return val
            else: return 0.2

        self.goal_rate = np.empty(N, dtype=object)
        self.home_adv = np.empty(N, dtype=object)
        self.def_rate = np.empty(N, dtype=object)
        self.match_rate = np.empty(len(league.games) * 2, dtype=object)
        self.outcome_future = np.empty(len(league.games), dtype=object)
        self.match_goals_future = np.empty(len(league.future_games) * 2,
                                           dtype=object)
        self.league = league

        fmesh = np.arange(0., league.n_days + 2.)

        for t in league.teams.values():
            self.goal_rate[t.team_id] = Exponential('goal_rate_%i' % t.team_id,
                                                    beta=1.)
            self.def_rate[t.team_id] = Normal('def_rate_%i' % t.team_id,
                                              tau=1.,
                                              mu=0.)
            self.home_adv[t.team_id] = Normal('home_adv_%i' % t.team_id,
                                              tau=1.,
                                              mu=0.)

        for game in range(len(league.games)):
            self.match_rate[2 * game] = Poisson(
                'match_rate_%i' % (2 * game),
                mu=Deterministic(
                    eval=clip_rate,
                    parents={
                        'val':
                        self.goal_rate[league.games[game].hometeam.team_id] -
                        self.def_rate[league.games[game].awayteam.team_id] +
                        self.home_adv[league.games[game].hometeam.team_id]
                    },
                    doc='clipped goal rate',
                    name='clipped_h_%i' % game),
                value=league.games[game].homescore,
                observed=True)
            self.match_rate[2 * game + 1] = Poisson(
                'match_rate_%i' % (2 * game + 1),
                mu=Deterministic(
                    eval=clip_rate,
                    parents={
                        'val':
                        self.goal_rate[league.games[game].awayteam.team_id] -
                        self.def_rate[league.games[game].hometeam.team_id]
                    },
                    doc='clipped goal rate',
                    name='clipped_a_%i' % game),
                value=league.games[game].awayscore,
                observed=True)

        for game in range(len(league.future_games)):
            self.match_goals_future[2 * game] = Poisson(
                'match_goals_future_%i_home' % game,
                mu=Deterministic(
                    eval=clip_rate,
                    parents={
                        'val':
                        self.goal_rate[league.future_games[game][0].team_id] -
                        self.def_rate[league.future_games[game][1].team_id] +
                        self.home_adv[league.future_games[game][0].team_id]
                    },
                    doc='clipped goal rate',
                    name='clipped_fut_h_%i' % game))

            self.match_goals_future[2 * game + 1] = Poisson(
                'match_goals_future_%i_away' % game,
                mu=Deterministic(
                    eval=clip_rate,
                    parents={
                        'val':
                        self.goal_rate[league.future_games[game][1].team_id] -
                        self.def_rate[league.future_games[game][0].team_id]
                    },
                    doc='clipped goal rate',
                    name='clipped_fut_a_%i' % game))

            self.outcome_future[game] = Deterministic(
                eval=outcome_eval,
                parents={
                    'home': self.match_goals_future[2 * game],
                    'away': self.match_goals_future[2 * game + 1]
                },
                name='match_outcome_future_%i' % game,
                dtype=int,
                doc='The outcome of the match')
예제 #11
0
파일: fyba.py 프로젝트: afcarl/fyba
    def __init__(self, fname, playedto=None):
        super(LeagueFullModel, self).__init__()
        league = League(fname, playedto)

        N = len(league.teams)

        def outcome_eval(home=None, away=None):
            if home > away:
                return 1
            if home < away:
                return -1
            if home == away:
                return 0

        def clip_rate(val):
            if val > 0.2: return val
            else: return 0.2

        def linfun(x, c):
            return 0. * x + c


# The covariance dtrm C is valued as a Covariance object.
#@pm.deterministic
#def C(eval_fun = gp.matern.euclidean, diff_degree=diff_degree, amp=amp, scale=scale):
#    return gp.NearlyFullRankCovariance(eval_fun, diff_degree=diff_degree, amp=amp, scale=scale)

        self.goal_rate = np.empty(N, dtype=object)
        self.def_rate = np.empty(N, dtype=object)
        self.goal_var = np.empty(N, dtype=object)
        self.def_var = np.empty(N, dtype=object)
        self.match_rate = np.empty(len(league.games) * 2, dtype=object)
        self.outcome_future = np.empty(len(league.games), dtype=object)
        self.match_goals_future = np.empty(len(league.future_games) * 2,
                                           dtype=object)
        self.home_adv = Uniform(name='home_adv', lower=0., upper=2.0)
        self.league = league

        fmesh = np.arange(0., league.n_days + 2.)

        for t in league.teams.values():
            # Prior parameters of C
            diff_degree_g = pm.Uniform('diff_degree_g_%i' % t.team_id, 1., 3)
            amp_g = pm.Uniform('amp_g_%i' % t.team_id, .01, 2.)
            scale_g = pm.Uniform('scale_g_%i' % t.team_id, 1., 10.)
            diff_degree_d = pm.Uniform('diff_degree_d_%i' % t.team_id, 1., 3)
            amp_d = pm.Uniform('amp_d_%i' % t.team_id, .01, 2.)
            scale_d = pm.Uniform('scale_d_%i' % t.team_id, 1., 10.)

            @pm.deterministic(name='C_d%i' % t.team_id)
            def C_d(eval_fun=gp.matern.euclidean,
                    diff_degree=diff_degree_d,
                    amp=amp_d,
                    scale=scale_d):
                return gp.NearlyFullRankCovariance(eval_fun,
                                                   diff_degree=diff_degree,
                                                   amp=amp,
                                                   scale=scale)

            @pm.deterministic(name='C_g%i' % t.team_id)
            def C_g(eval_fun=gp.matern.euclidean,
                    diff_degree=diff_degree_g,
                    amp=amp_g,
                    scale=scale_g):
                return gp.NearlyFullRankCovariance(eval_fun,
                                                   diff_degree=diff_degree,
                                                   amp=amp,
                                                   scale=scale)

            self.goal_rate[t.team_id] = Exponential('goal_rate_%i' % t.team_id,
                                                    beta=1)
            self.def_rate[t.team_id] = Exponential('def_rate_%i' % t.team_id,
                                                   beta=1)

            @pm.deterministic(name='M_d%i' % t.team_id)
            def M_d(eval_fun=linfun, c=self.def_rate[t.team_id]):
                return gp.Mean(eval_fun, c=c)

            @pm.deterministic(name='M_g%i' % t.team_id)
            def M_g(eval_fun=linfun, c=self.goal_rate[t.team_id]):
                return gp.Mean(eval_fun, c=c)

            self.def_var[t.team_id] = gp.GPSubmodel('smd_%i' % t.team_id, M_d,
                                                    C_d, fmesh)
            self.goal_var[t.team_id] = gp.GPSubmodel('smg_%i' % t.team_id, M_g,
                                                     C_g, fmesh)

        for game in range(len(league.games)):
            gd = int(game / (league.n_teams / 2))
            assert (gd < league.n_days)
            self.match_rate[2 * game] = Poisson(
                'match_rate_%i' % (2 * game),
                mu=Deterministic(
                    eval=clip_rate,
                    parents={
                        'val':
                        self.goal_var[
                            league.games[game].hometeam.team_id].f_eval[gd] -
                        self.def_var[
                            league.games[game].awayteam.team_id].f_eval[gd] +
                        self.home_adv
                    },
                    doc='clipped goal rate',
                    name='clipped_h_%i' % game),
                value=league.games[game].homescore,
                observed=True)
            self.match_rate[2 * game + 1] = Poisson(
                'match_rate_%i' % (2 * game + 1),
                mu=Deterministic(
                    eval=clip_rate,
                    parents={
                        'val':
                        self.goal_var[
                            league.games[game].awayteam.team_id].f_eval[gd] -
                        self.def_var[
                            league.games[game].hometeam.team_id].f_eval[gd]
                    },
                    doc='clipped goal rate',
                    name='clipped_a_%i' % game),
                value=league.games[game].awayscore,
                observed=True)

        for game in range(len(league.future_games)):
            gd = league.n_days
            self.match_goals_future[2 * game] = Poisson(
                'match_goals_future_%i_home' % game,
                mu=Deterministic(
                    eval=clip_rate,
                    parents={
                        'val':
                        self.goal_var[league.future_games[game]
                                      [0].team_id].f_eval[gd] -
                        self.def_var[league.future_games[game]
                                     [1].team_id].f_eval[gd] + self.home_adv
                    },
                    doc='clipped goal rate',
                    name='clipped_fut_h_%i' % game))

            self.match_goals_future[2 * game + 1] = Poisson(
                'match_goals_future_%i_away' % game,
                mu=Deterministic(eval=clip_rate,
                                 parents={
                                     'val':
                                     self.goal_var[league.future_games[game]
                                                   [1].team_id].f_eval[gd] -
                                     self.def_var[league.future_games[game]
                                                  [0].team_id].f_eval[gd]
                                 },
                                 doc='clipped goal rate',
                                 name='clipped_fut_a_%i' % game))

            self.outcome_future[game] = Deterministic(
                eval=outcome_eval,
                parents={
                    'home': self.match_goals_future[2 * game],
                    'away': self.match_goals_future[2 * game + 1]
                },
                name='match_outcome_future_%i' % game,
                dtype=int,
                doc='The outcome of the match')
    (Conversions_t | start_t, before_rate, after_rate)  ~ Poisson(rate_t)

    start_t ~ DiscreteUniform(first_period, last_period)  //can be any period with equal probability

    rate_t = ( before_rate ... //start_t // .. after_rate)

'''

conversions_data = np.array(
    [1, 2, 1, 2, 3, 4, 1, 2, 3, 5, 7, 3, 8, 7, 4, 5, 7])
start_t = DiscreteUniform("start_t", lower=0, upper=len(conversions_data))

before_mean = Exponential('before_mean', beta=1.)
after_mean = Exponential('after_mean', beta=1.)


@deterministic(plot=False)
def rate(start_period=start_t,
         before_period_mean=before_mean,
         after_period_mean=after_mean):
    output = np.empty(len(conversions_data))
    output[:start_period] = before_period_mean
    output[start_period:] = after_period_mean
    return output


conversions = Poisson("conversions",
                      mu=rate,
                      value=conversions_data,
                      observed=True)