예제 #1
0
def fix(d, T, p, q, iters, epsilon, steps, nu):
    start = time.time()

    info = 'd=%d_T=%d_p=%0.3f_q=%0.3f_steps=%0.1E' % (d, T, p, q, steps)
    name = 'plots/data_' + info
    parent = "./"
    directory = os.path.join(parent, name)

    opd = rt_util.pkl_get(os.path.join(directory,'local_one_particle.pkl'))
    full = rt_util.pkl_get(os.path.join(directory,'full_one_particle.pkl'))

    plt.plot([full[p] for p in rt_util.bin_tuples(T)], label='full')
    plt.plot([opd[p] for p in rt_util.bin_tuples(T)], label='local')
    plt.legend(loc=2)
    l1 = sum([abs(full[p] - opd[p]) for p in rt_util.bin_tuples(T)])
    info = ('L1 distance = %0.4f ' % l1) + info.replace("_", " ")
    plt.title(info)
    plt.xlabel('One Particle Path')
    plt.ylabel('Probability')
    plt.savefig(os.path.join(directory,"fig.png"))
    F = open(os.path.join(directory,"L1.txt"), "w")
    F.write('%f' % l1)
    F.close()

    F = open(os.path.join(directory,"time.txt"), "w")
    F.write('%f' % l1)
    F.close()

    end = time.time()
    F = open(os.path.join(directory,"time.txt"), "w")
    F.write('%f' % (end-start))
    F.close()
예제 #2
0
def init_observations(T):
    observations = [{} for t in range(T)]
    for t in range(1, T):
        for p1 in rt_util.bin_tuples(t):
            for p2 in rt_util.bin_tuples(t):
                observations[t][(p1, p2)] = 0.
    return observations
예제 #3
0
def init_cond(T):
    cond = [{} for t in range(T)]
    for t in range(1, T):
        for p1 in rt_util.bin_tuples(t):
            for p2 in rt_util.bin_tuples(t):
                cond[t][(p1, p2)] = {}
    return cond
예제 #4
0
def rt_local_approx_iteration(d, T, p, q, steps, nu, cond):
    f_new = {}
    cond_new = init_cond(T)
    one_particle_distr_new = init_one_particle_distr(T)

    observed = init_observations(T)

    bad = 0.

    increment = 1.0 / steps
    for step in range(steps):
        # X[0] is root. X[1:d+1] is the d children.
        X, b = rt_realization(d, T, p, q, nu, cond)
        bad += b
        # record the realization in f_new
        if X.tostring() not in f_new:
            f_new[X.tostring()] = increment
        else:
            f_new[X.tostring()] += increment
        # record the realization in cond_new
        for t in range(1, T):
            for k in range(1, d + 1):
                # TODO: See email clarification and change to t instead of t-1
                # maybe.
                other_children = tuple(X[t - 1, 1:k]) + \
                    tuple(X[t - 1, (k + 1):])
                root_and_child = (tuple(X[:t, 0]), tuple(X[:t, k]))
                observed[t][root_and_child] += 1
                if other_children not in cond_new[t][root_and_child]:
                    cond_new[t][root_and_child][other_children] = 1
                else:
                    cond_new[t][root_and_child][other_children] += 1
        # Record realization in one_particle_distr
        one_particle_distr_new[tuple(X[:, 0])] += increment

    # normalize cond_new
    for t in range(1, T):
        for p1 in rt_util.bin_tuples(t):
            for p2 in rt_util.bin_tuples(t):
                p = (p1, p2)
                for c in cond_new[t][p]:
                    if observed[t][p] != 0:
                        cond_new[t][p][c] = cond_new[t][p][c] / observed[t][p]

    return f_new, cond_new, one_particle_distr_new, bad / steps
예제 #5
0
def compare(d, T, p, q, iters, epsilon, steps, nu):
    start = time.time()

    info = 'd=%d_T=%d_p=%0.3f_q=%0.3f_steps=%0.1E' % (d, T, p, q, steps)
    name = 'data/data_' + info
    parent = "./"
    rt_util.make_directory(parent, name)
    directory = os.path.join(parent, name)
    info_file = os.path.join(directory, "info")
    f, c, opd, res, op_res, bad = rt_local_approx.rt_local_approx(
        d, T, p, q, iters, epsilon, steps, nu, info_file)

    rt_util.pkl_save(f, os.path.join(directory, 'local_joint.pkl'))
    rt_util.pkl_save(c, os.path.join(directory, 'local_cond.pkl'))
    rt_util.pkl_save(opd, os.path.join(directory, 'local_one_particle.pkl'))
    rt_util.pkl_save(res, os.path.join(directory, 'local_joint_residual.pkl'))
    rt_util.pkl_save(op_res, os.path.join(
        directory, 'local_one_particle_residual.pkl'))

    full = rt_full_simulation.generate_distr(d, T, p, q, nu, steps)
    rt_util.pkl_save(full, os.path.join(directory, 'full_one_particle.pkl'))

    plt.plot([full[p] for p in rt_util.bin_tuples(T)], label='full')
    plt.plot([opd[p] for p in rt_util.bin_tuples(T)], label='local')
    plt.legend(loc=2)
    l1 = sum([abs(full[p] - opd[p]) for p in rt_util.bin_tuples(T)])
    info = ('L1 distance = %0.4f ' % l1) + info.replace("_", " ")
    plt.title(info)
    plt.xlabel('One Particle Path')
    plt.ylabel('Probability')
    plt.savefig(os.path.join(directory,"fig.png"))
    F = open(os.path.join(directory,"L1.txt"), "w")
    F.write('%f' % l1)
    F.close()

    F = open(os.path.join(directory,"time.txt"), "w")
    F.write('%f' % l1)
    F.close()

    end = time.time()
    F = open(os.path.join(directory,"time.txt"), "w")
    F.write('%f' % (end-start))
    F.close()
예제 #6
0
def generate_distr(d, T, p, q, nu, steps):
    f = {}
    for st in rt_util.bin_tuples(T):
        f[st] = 0

    inc = 1. / steps
    for step in range(steps):
        X = rt_full_simulation(d, T, p, q, steps, nu)
        f[X] += inc

    return f
예제 #7
0
def rt_local_approx(d, T, p, q, iters, epsilon, steps, nu, file=None):
    # f is the joint distribution over the single neighborhood of the root
    f = {}
    # cond is the conditional law of d-1 children given the root and the dth
    cond = init_cond(T)

    one_particle_distr = init_one_particle_distr(T)

    distances = []

    one_particle_distances = []

    if file:
        F = open(file, "w")

    for iteration in range(iters):
        f_new, cond_new, one_particle_distr_new, bad = rt_local_approx_iteration(
            d, T, p, q, steps, nu, cond)

        # distance
        l = 1
        dist = 0
        dist += sum([f_new[k]**l for k in f_new if k not in f])
        dist += sum([f[k]**l for k in f if k not in f_new])
        dist += sum([abs(f[k] - f_new[k])**l for k in f if k in f_new])
        distances.append(dist)

        op_dist = 0
        op_dist = sum([
            abs(one_particle_distr[k] - one_particle_distr_new[k])**l
            for k in rt_util.bin_tuples(T)
        ])
        one_particle_distances.append(op_dist)
        info_str = 'iteration %d, Full distance %f, OP Distance %f, baddness %f' % (
            iteration, dist, op_dist, bad)

        if file:
            F.write(info_str)
            F.write('\n')
        else:
            print info_str

        f = f_new
        cond = cond_new
        one_particle_distr = one_particle_distr_new

        if dist < epsilon:
            break

    return f, cond, one_particle_distr, distances, one_particle_distances, bad
예제 #8
0
def init_one_particle_distr(T):
    one_particle_distr = {}
    for p in rt_util.bin_tuples(T):
        one_particle_distr[p] = 0.
    return one_particle_distr