コード例 #1
0
ファイル: mcmc.py プロジェクト: arvidfm/statmet-project
def generate_graph_and_paths(n, t, d):
    print('Generating graph and data...');
    G, sig = generate_g.generate_graph_and_settings(n)
    D = [];  
    for i in range(0,d):
        a, _ = generate_g.simulate_train(G, sig, t);
        D.append(a);
    print('Done!');
    return G, sig, D;
コード例 #2
0
ファイル: main.py プロジェクト: arvidfm/statmet-project
def run_experiment(nodes=20, observations=10):
    G, sigma = generate_g.generate_graph_and_settings(nodes)
    O, actual_path = generate_g.simulate_train(G, sigma, observations + 1)

    last_node = actual_path[-2]
    last_label = numpy.where(G[last_node] == actual_path[-1])[0][0]

    s = calculate_final_distribution(G, O)

    print("Guessed the correct stop position with probability {}.".format(
        s[last_node, last_label]))
コード例 #3
0
def run_experiment(nodes=20, observations=10):
    G, sigma = generate_g.generate_graph_and_settings(nodes)
    O, actual_path = generate_g.simulate_train(G, sigma, observations + 1)

    last_node = actual_path[-2]
    last_label = numpy.where(G[last_node] == actual_path[-1])[0][0]

    s = calculate_final_distribution(G, O)

    print("Guessed the correct stop position with probability {}.".format(
        s[last_node, last_label]))
コード例 #4
0
def setup(nodes, chain_specification, burn_in=1000, calculate_actual_distribution=False):
    G, sigma = generate_g.generate_graph_and_settings(nodes)
    if G.min() < 0:
        print("Erroneous graph!")
        return

    O, path = generate_g.simulate_train(G, sigma, 10)

    chains = [[itertools.islice(chain, burn_in, None)
               for chain in chains]
              for chains in build_chains(chain_specification, G, O)]

    if calculate_actual_distribution:
        sigma_dist = calculate_sigma(G, O)
        return chains, sigma_dist
    else:
        return chains
コード例 #5
0
def setup(nodes,
          chain_specification,
          burn_in=1000,
          calculate_actual_distribution=False):
    G, sigma = generate_g.generate_graph_and_settings(nodes)
    if G.min() < 0:
        print("Erroneous graph!")
        return

    O, path = generate_g.simulate_train(G, sigma, 10)

    chains = [[itertools.islice(chain, burn_in, None) for chain in chains]
              for chains in build_chains(chain_specification, G, O)]

    if calculate_actual_distribution:
        sigma_dist = calculate_sigma(G, O)
        return chains, sigma_dist
    else:
        return chains
コード例 #6
0
def observations_needed(max_nodes=100, runs=50):
    """Calculates the number of observations needed
    to get a good estimate of the stop position."""

    failure_probabilities = []
    with open("observations_needed.dat", 'w') as obs_file:
        for nodes in range(6, max_nodes + 1, 2):
            total_observations_needed = 0
            failed_runs = 0

            i = 0
            while True:
                G, sigma = generate_g.generate_graph_and_settings(nodes)
                O, actual_path = generate_g.simulate_train(G, sigma, 200)

                for observations in range(1, len(O)):
                    s, _ = state_probabilities.state_probabilities(
                        G, sigma, O[:observations])
                    if s.max() > 0.90:
                        break
                else:
                    print(s.max())
                    failed_runs += 1
                    continue

                total_observations_needed += observations
                i += 1
                if i >= runs:
                    break

            obs_file.write("{}\t{}\n".format(nodes,
                                             total_observations_needed / runs))
            print(nodes, total_observations_needed / runs)
            failure_probability = failed_runs / (runs + failed_runs)
            print("Failure probability: {}".format(failure_probability))
            failure_probabilities.append(failure_probability)

    print("Average failure probability: {}".format(
        sum(failure_probabilities) / len(failure_probabilities)))
コード例 #7
0
ファイル: main.py プロジェクト: arvidfm/statmet-project
def observations_needed(max_nodes=100, runs=50):
    """Calculates the number of observations needed
    to get a good estimate of the stop position."""

    failure_probabilities = []
    with open("observations_needed.dat", 'w') as obs_file:
        for nodes in range(6, max_nodes + 1, 2):
            total_observations_needed = 0
            failed_runs = 0

            i = 0
            while True:
                G, sigma = generate_g.generate_graph_and_settings(nodes)
                O, actual_path = generate_g.simulate_train(G, sigma, 200)

                for observations in range(1, len(O)):
                    s, _ = state_probabilities.state_probabilities(
                        G, sigma, O[:observations])
                    if s.max() > 0.90:
                        break
                else:
                    print(s.max())
                    failed_runs += 1
                    continue

                total_observations_needed += observations
                i += 1
                if i >= runs:
                    break

            obs_file.write("{}\t{}\n".format(nodes, total_observations_needed / runs))
            print(nodes, total_observations_needed / runs)
            failure_probability = failed_runs / (runs + failed_runs)
            print("Failure probability: {}".format(failure_probability))
            failure_probabilities.append(failure_probability)

    print("Average failure probability: {}".format(sum(failure_probabilities) / len(failure_probabilities)))