コード例 #1
0
def estimate_pvalue_SI(underlying: Network,
                       theta: float,
                       outcome: dict,
                       initials: set,
                       immunes: set = set(),
                       tmax=1000.0,
                       dt=1,
                       n_iterations=10000):
    est_p = Simulator.estimate_SI(underlying=underlying,
                                  theta=theta,
                                  outcome=outcome,
                                  initials=initials,
                                  tmax=tmax,
                                  dt=dt)
    n_less = 0
    for i in range(n_iterations):
        result = Simulator.simulate_SI(underlying=underlying,
                                       theta=theta,
                                       infected=initials,
                                       immunes=immunes,
                                       tmax=tmax,
                                       dt=dt)
        if result['p'] <= est_p:
            n_less += 1
    return n_less / n_iterations
コード例 #2
0
def test_SI(ntw: Network, true_theta: float):
    result = Simulator.simulate_SI(underlying=ntw,
                                   theta=true_theta,
                                   infected={1},
                                   tmax=300,
                                   dt=1)
    print(result)
    th = 0.0
    est_th = th
    best_pest = 0
    ml_pval = 0
    thetas = []
    ps = []
    total_pest = 0
    while th <= 1.0:
        pest = Simulator.estimate_SI(underlying=ntw,
                                     outcome=result['outcome'],
                                     theta=th,
                                     initials={1},
                                     tmax=300,
                                     dt=1)
        # print(str(th) + ' ' + str(pest))
        if pest > best_pest:
            best_pest = pest
            est_th = th
        thetas.append(th)
        ps.append(pest)
        total_pest += pest
        th += 0.01
    pval = de.estimate_pvalue_SI(underlying=ntw,
                                 outcome=result['outcome'],
                                 theta=est_th,
                                 initials={1},
                                 tmax=300,
                                 dt=1,
                                 n_iterations=1000)
    print('True theta: ' + str(true_theta))
    print('Max liklehood estimation: ' + str(est_th) + ' p-val ' + str(pval))
    psnorm = stat.normalize_series(ps)
    confide = stat.confidential_from_p_series(psnorm, 0.95)
    print('0.95 confidential interval: (' + str(thetas[confide[0]]) + ', ' +
          str(thetas[confide[1]]) + ')')

    pyplot.plot(thetas, psnorm)
    pyplot.show()