コード例 #1
0
ファイル: test_distribution.py プロジェクト: kartinW/Ista311
def test_sim():
    passed_count = 0

    print("Test 1: Uniform distribution")
    d = Distribution(unif_dist)
    result = sp.zeros(10)
    for i in range(10000):
        out = d.sample()
        result[out - 1] += 1
    csr = sp.stats.chisquare(result, 1000 * sp.ones(10))
    print("Observed frequencies:", result)
    print("Expected frequencies:", 1000 * sp.ones(10))
    print("Chi-square statistic:", csr.statistic)
    print("p-value: ", csr.pvalue)
    if csr.pvalue < 0.01:
        print("Test FAILED")
    else:
        print("Test PASSED")
        passed_count += 1

    if plot_tests:
        plt.figure()
        plt.subplot(2, 1, 1)
        plt.bar(x=1 + sp.arange(10),
                height=result,
                tick_label=1 + sp.arange(10))
        plt.title("Uniform distribution: observed (top) vs. expected (bottom)")
        plt.subplot(2, 1, 2)
        plt.bar(x=1 + sp.arange(10),
                height=0.1 * sp.ones(10),
                tick_label=1 + sp.arange(10))
        plt.savefig("uniform.png")

    print("Test 2: Drawing from an urn")
    result = sp.zeros(3)
    picks = []
    d = Distribution(rgb)
    for i in range(10000):
        picks.append(d.sample())
    result[0] = sum([pick == "red" for pick in picks])
    result[1] = sum([pick == "green" for pick in picks])
    result[2] = sum([pick == "blue" for pick in picks])
    csr = sp.stats.chisquare(result, (10000 / 18) * sp.array([3, 7, 8]))
    print("Observed frequencies:", result)
    print("Expected frequencies:", (10000 / 18) * sp.array([3, 7, 8]))
    print("Chi-square statistic:", csr.statistic)
    print("p-value: ", csr.pvalue)
    if csr.pvalue < 0.01:
        print("Test FAILED")
    else:
        print("Test PASSED")
        passed_count += 1

    if plot_tests:
        plt.figure()
        plt.subplot(2, 1, 1)
        plt.bar(x=sp.arange(3),
                height=result,
                tick_label=["red", "green", "blue"])
        plt.title("Uniform distribution: observed (top) vs. expected (bottom)")
        plt.subplot(2, 1, 2)
        plt.bar(x=sp.arange(3),
                height=(1 / 18) * sp.array([3, 7, 8]),
                tick_label=["red", "green", "blue"])
        plt.savefig("urn.png")

    print("Test 3: Benford's law")
    result = sp.zeros(9)
    d = Distribution(benford)
    for i in range(10000):
        out = d.sample()
        result[out - 1] += 1
    csr = sp.stats.chisquare(
        result, 10000 * sp.array(
            [0.301, 0.176, 0.125, 0.097, 0.079, 0.067, 0.058, 0.051, 0.046]))
    print("Observed frequencies:", result)
    print(
        "Expected frequencies:", 10000 * sp.array(
            [0.301, 0.176, 0.125, 0.097, 0.079, 0.067, 0.058, 0.051, 0.046]))
    print("Chi-square statistic:", csr.statistic)
    print("p-value: ", csr.pvalue)
    if csr.pvalue < 0.01:
        print("Test FAILED")
    else:
        print("Test PASSED")
        passed_count += 1

    if plot_tests:
        plt.figure()
        plt.subplot(2, 1, 1)
        plt.bar(x=1 + sp.arange(9),
                height=result / 10000,
                tick_label=1 + sp.arange(9))
        plt.title("Uniform distribution: observed (top) vs. expected (bottom)")
        plt.subplot(2, 1, 2)
        plt.bar(x=1 + sp.arange(9),
                height=sp.array([
                    0.301, 0.176, 0.125, 0.097, 0.079, 0.067, 0.058, 0.051,
                    0.046
                ]),
                tick_label=1 + sp.arange(9))
        plt.savefig("benford.png")
    '''
    if simbonus:
        print("Test 4: Drawing from an urn, with a non-normalized distribution")
        result = sp.zeros(3)
        picks = []
        for i in range(10000):
            picks.append(simfunc(rgb_unscaled))
        result[0] = sum([pick == "red" for pick in picks])
        result[1] = sum([pick == "green" for pick in picks])
        result[2] = sum([pick == "blue" for pick in picks])
        csr = sp.stats.chisquare(result, (10000/18) * sp.array([3,7,8]))
        print("Observed frequencies:", result)
        print("Expected frequencies:", (10000/18) * sp.array([3,7,8]))
        print("Chi-square statistic:", csr.statistic)
        print("p-value: ", csr.pvalue)
        if csr.pvalue < 0.01:
            print("Test FAILED")
        else:
            print("Test PASSED")

        plt.figure()
        plt.subplot(2, 1, 1)
        plt.bar(x=sp.arange(3), height=result, tick_label = ["red", "green", "blue"])
        plt.title("Uniform distribution: observed (top) vs. expected (bottom)")
        plt.subplot(2, 1, 2)
        plt.bar(x=sp.arange(3), height=(1/18)*sp.array([3,7,8]), tick_label = ["red", "green", "blue"])
        plt.savefig("urnbonus.png")
    '''

    return passed_count
コード例 #2
0
ファイル: train.py プロジェクト: MurphyPone/PyTorch-basics
import torch

from distribution import Distribution
from visualize import *

n_epochs = 10000
lr = 0.05
batch_size = 64
vis_iter = 200

target = Distribution([.1, .3, .4, .1, .1], [-3, 3, 15, 10, -10],
                      [1, 2, 3, 1, 5])
Q = Distribution(K=10, requires_grad=True)

plot_dist(target, 'target', '#ff8200', (-20, 20))
plot_dist(Q, 'Q', '#7600fe', (-20, 20))

optimizer = torch.optim.Adam(Q.parameters(), lr=lr)

for epoch in range(int(n_epochs)):
    x = target.sample(batch_size)
    loss = torch.pow(Q(x) - target(x), 2).mean()  # MSE

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if epoch % vis_iter == vis_iter - 1:
        plot_loss(epoch, loss)
        plot_dist(Q, 'Q', '#7600fe', (-20, 20))