Exemplo n.º 1
0
 def __init__(self, mean=0, sigma=1, name="", model=None):
     super().__init__(name, model)
     self.register_rv(Normal.dist(mu=mean, sigma=sigma), "v1")
     Normal("v2", mu=mean, sigma=sigma)
     Normal("v3", mu=mean, sigma=Normal("sd", mu=10, sigma=1, initval=1.0))
     Deterministic("v3_sq", self.v3**2)
     Potential("p1", at.constant(1))
Exemplo n.º 2
0
def test_find_MAP_issue_4488():
    # Test for https://github.com/pymc-devs/pymc/issues/4488
    with Model() as m:
        x = Gamma("x", alpha=3, beta=10, observed=np.array([1, np.nan]))
        y = Deterministic("y", x + 1)
        map_estimate = find_MAP()

    assert not set.difference({"x_missing", "x_missing_log__", "y"},
                              set(map_estimate.keys()))
    np.testing.assert_allclose(map_estimate["x_missing"],
                               0.2,
                               rtol=1e-4,
                               atol=1e-4)
    np.testing.assert_allclose(map_estimate["y"],
                               [2.0, map_estimate["x_missing"][0] + 1])
def CreateFullDetectorModel(detector, waveforms, startGuess, b_over_a0, c0, d0,
                            rc0):

    n_waveforms = len(waveforms)
    sample_length = len(waveforms[0].windowedWf)

    #detector-wide params
    tempEst = TruncatedNormal('temp',
                              mu=startGuess['temp'],
                              tau=sigToTau(2.),
                              value=startGuess['temp'],
                              a=40,
                              b=120)
    grad = Uniform('grad',
                   lower=detector.gradList[0],
                   upper=detector.gradList[-1],
                   value=startGuess['grad'])
    pcRad = Uniform('pcRad',
                    lower=detector.pcRadList[0],
                    upper=detector.pcRadList[-1],
                    value=startGuess['pcRad'])
    pcLen = Uniform('pcLen',
                    lower=detector.pcLenList[0],
                    upper=detector.pcLenList[-1],
                    value=startGuess['pcLen'])

    #  grad =  TruncatedNormal('grad', a=detector.gradList[0], b=detector.gradList[-1],    value=startGuess['grad'], mu=startGuess['grad'],tau=sigToTau(0.03) )
    #  pcRad =  TruncatedNormal('pcRad', a=detector.pcRadList[0], b=detector.pcRadList[-1],value=startGuess['pcRad'], mu=startGuess['pcRad'],tau=sigToTau(0.2) )
    #  pcLen = TruncatedNormal('pcLen', a=detector.pcLenList[0], b=detector.pcLenList[-1], value=startGuess['pcLen'], mu=startGuess['pcLen'],tau=sigToTau(0.2) )

    b_over_a = Normal('b_over_a',
                      mu=b_over_a0,
                      tau=sigToTau(.5),
                      value=b_over_a0)
    c = Normal('c', mu=c0, tau=sigToTau(0.2), value=c0)
    d = Normal('d', mu=d0, tau=sigToTau(0.2), value=d0)
    rc = Normal('rc', mu=rc0, tau=sigToTau(5), value=rc0)

    #Make an array of priors for each waveform-specific parameter
    radiusArray = np.empty(n_waveforms, dtype=object)
    zArray = np.empty(n_waveforms, dtype=object)
    phiArray = np.empty(n_waveforms, dtype=object)
    scaleArray = np.empty(n_waveforms, dtype=object)
    t0Array = np.empty(n_waveforms, dtype=object)
    sigArray = np.empty(n_waveforms, dtype=object)

    for idx in range(n_waveforms):
        radiusArray[idx] = (TruncatedNormal('radEst_%d' % idx,
                                            mu=3,
                                            a=0,
                                            b=detector.detector_radius,
                                            value=startGuess['radEst'][idx]))
        zArray[idx] = (TruncatedNormal('zEst_%d' % idx,
                                       mu=3,
                                       a=0,
                                       b=detector.detector_length,
                                       value=startGuess['zEst'][idx]))
        phiArray[idx] = (Uniform('phiEst_%d' % idx,
                                 lower=0,
                                 upper=np.pi / 4,
                                 value=startGuess['phiEst'][idx]))
        scaleArray[idx] = (Normal('wfScale_%d' % idx,
                                  mu=startGuess['wfScale'][idx],
                                  tau=sigToTau(0.01 *
                                               startGuess['wfScale'][idx]),
                                  value=startGuess['wfScale'][idx]))
        t0Array[idx] = (Normal('switchpoint_%d' % idx,
                               mu=startGuess['switchpoint'][idx],
                               tau=sigToTau(5.),
                               value=startGuess['switchpoint'][idx]))
        sigArray[idx] = (Normal('sigma_%d' % idx,
                                mu=startGuess['smooth'][idx],
                                tau=sigToTau(3),
                                value=startGuess['smooth'][idx]))

    #This is a deterministic (implicitly?  is this a problem?)
    def siggen_model(s, rad, phi, z, e, smooth, temp, b_over_a, c, d, rc, grad,
                     pc_rad, pc_len, fit_length):

        if s < 0 or s >= fit_length:
            return np.ones(fit_length) * -np.inf
#    if smooth<0:
#      return np.ones(fit_length)*-np.inf
        if not detector.IsInDetector(rad, phi, z):
            return -np.inf * np.ones(fit_length)

        if temp < 40 or temp > 120:
            return np.ones(fit_length) * -np.inf
        if (grad > detector.gradList[-1]) or (grad < detector.gradList[0]):
            return np.ones(fit_length) * -np.inf
        if (pc_rad > detector.pcRadList[-1]) or (pc_rad <
                                                 detector.pcRadList[0]):
            return np.ones(fit_length) * -np.inf
        if (pc_len > detector.pcLenList[-1]) or (pc_len <
                                                 detector.pcLenList[0]):
            return np.ones(fit_length) * -np.inf

        detector.SetTransferFunction(b_over_a, c, d, rc)
        detector.SetTemperature(temp)

        if detector.pcRad != pc_rad or detector.pcLen != pc_len or detector.impurityGrad != grad:
            detector.SetFields(pc_rad, pc_len, grad)

        siggen_wf = detector.MakeSimWaveform(rad,
                                             phi,
                                             z,
                                             e,
                                             s,
                                             fit_length,
                                             h_smoothing=None)
        if siggen_wf is None:
            return np.ones(fit_length) * -np.inf


#    plt.ion()
#    plt.figure(14)
#    plt.clf()
#    plt.plot(siggen_wf)
#    for (i, wf) in enumerate(waveforms):
#      plt.plot(wf.windowedWf, color="r")
#    print "Detector parameters: "
#    print "  temp = %0.3f" % temp
#    print "  zero_1 = %f" % zero_1
#    print "  pole_1 = %f" % pole_1
#    print "  pole_real = %f" % pole_real
#    print "  pole_imag = %f" % pole_imag
#    print "  grad = %0.3f" % grad
#    print "  pc_rad = %0.3f" % pc_rad
#    print "  pc_len = %0.3f" % pc_len
#
#    print "Waveform parameters: "
#    print "  (r,phi,z) = (%0.2f,%0.3f,%0.2f)" % (rad,phi,z)
#    print "  e = %0.3f" % e
#    print "  smooth = %0.3f" % smooth
#    print "  t0 = %0.3f" % s
#    value = raw_input('  --> Press q to quit, any other key to continue\n')
#    plt.ioff()

        return siggen_wf

    baseline_observed = np.empty(n_waveforms, dtype=object)
    baseline_sim = np.empty(n_waveforms, dtype=object)

    for (i, wf) in enumerate(waveforms):
        baseline_sim[i] = Deterministic(eval=siggen_model,
                                        doc='siggen wf %d' % i,
                                        name='siggen_model_%d' % i,
                                        parents={
                                            's': t0Array[i],
                                            'rad': radiusArray[i],
                                            'phi': phiArray[i],
                                            'z': zArray[i],
                                            'e': scaleArray[i],
                                            'smooth': sigArray[i],
                                            'temp': tempEst,
                                            'b_over_a': b_over_a,
                                            'c': c,
                                            'd': d,
                                            'rc': rc,
                                            'grad': grad,
                                            'pc_rad': pcRad,
                                            'pc_len': pcLen,
                                            'fit_length': wf.wfLength
                                        },
                                        trace=False,
                                        plot=False)
        baseline_observed[i] = Normal("baseline_observed_%d" % i,
                                      mu=baseline_sim[i],
                                      tau=sigToTau(wf.baselineRMS),
                                      observed=True,
                                      value=wf.windowedWf)

    return locals()
def createWaveformModel(detector, waveform, startGuess):

    furthest_point = np.sqrt(detector.detector_radius**2 +
                             detector.detector_length**2)

    radEst = TruncatedNormal('radEst',
                             mu=startGuess['radEst'],
                             a=0,
                             b=furthest_point,
                             tau=sigToTau(2),
                             value=startGuess['radEst'])
    thetaEst = TruncatedNormal('thetaEst',
                               mu=startGuess['thetaEst'],
                               a=0,
                               b=np.pi / 2,
                               tau=sigToTau(0.2),
                               value=startGuess['thetaEst'])

    phiEst = Uniform('phiEst',
                     lower=0,
                     upper=np.pi / 4,
                     value=startGuess['phiEst'])
    scaleEst = Normal('wfScale',
                      mu=startGuess['wfScale'],
                      tau=sigToTau(0.01 * startGuess['wfScale']),
                      value=startGuess['wfScale'])
    t0Est = Normal('switchpoint',
                   mu=startGuess['switchpoint'],
                   tau=sigToTau(5.),
                   value=startGuess['switchpoint'])
    sigEst = Normal('sigma',
                    mu=startGuess['smooth'],
                    tau=sigToTau(3),
                    value=startGuess['smooth'])

    fit_length = waveform.wfLength

    def siggen_model(s, r, theta, phi, e, smooth):

        if s < 0 or s >= fit_length:
            return np.ones(fit_length) * -np.inf
        if smooth < 0:
            return np.ones(fit_length) * -np.inf

        rad = np.cos(theta) * r
        z = np.sin(theta) * r

        if not detector.IsInDetector(rad, phi, z):
            return -np.inf * np.ones(fit_length)

        siggen_wf = detector.MakeSimWaveform(rad,
                                             phi,
                                             z,
                                             e,
                                             s,
                                             fit_length,
                                             h_smoothing=smooth)
        if siggen_wf is None:
            return np.ones(fit_length) * -np.inf


#    plt.ion()
#    plt.figure(14)
#    plt.clf()
#    plt.plot(siggen_wf)
#    plt.plot(waveform.windowedWf, color="r")
#
#    print "Waveform parameters: "
#    print "  (r,phi,z) = (%0.2f,%0.3f,%0.2f)" % (rad,phi,z)
#    print "  e = %0.3f" % e
#    print "  smooth = %0.3f" % smooth
#    print "  t0 = %0.3f" % s
#    value = raw_input('  --> Press q to quit, any other key to continue\n')
#    plt.ioff()

        return siggen_wf

    baseline_sim = Deterministic(eval=siggen_model,
                                 doc='siggen wf',
                                 name='siggen_model',
                                 parents={
                                     's': t0Est,
                                     'r': radEst,
                                     'phi': phiEst,
                                     'theta': thetaEst,
                                     'e': scaleEst,
                                     'smooth': sigEst
                                 },
                                 trace=False,
                                 plot=False)
    baseline_observed = Normal('baseline_observed',
                               mu=baseline_sim,
                               tau=sigToTau(waveform.baselineRMS * 0.5773),
                               observed=True,
                               value=waveform.windowedWf)

    return locals()
Exemplo n.º 5
0
Arquivo: fyba.py Projeto: 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')
Exemplo n.º 6
0
Arquivo: fyba.py Projeto: 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')