Пример #1
0
def test_steady_state(set_up_2x2):

    weather, correct_answer = set_up_2x2

    # Test case using problem 1 to generate a steady state matrix.
    markov_matrix = mc.random_chain(4)
    steady_state_x = mc.steady_state(markov_matrix)
    assert(np.allclose(markov_matrix @ steady_state_x, steady_state_x))

    # Test weather 2×2 example from book
    assert np.allclose(mc.steady_state(weather), correct_answer)
Пример #2
0
def test_forecast():
    """Tests the forecast() function with respect to the steady_state() function."""
    #Set tolerance for error
    tol = 1e-2
    transition = np.array([[0.7, 0.6], [0.3, 0.4]])
    x = MC.steady_state(transition)
    fcast = MC.forecast(int(1e5))
    assert fcast.count(0) / len(
        fcast) - x[0] < tol, "failed on ratio of hot days"
    assert fcast.count(1) / len(
        fcast) - x[1] < tol, "failed on ratio of cold days"
Пример #3
0
def test_four_state_forecast():
    """Tests the four_state_forecast() function with respect to the steady_state() 
    function."""
    #Set tolerance for error
    tol = 1e-2
    transition = np.array([[0.5, 0.3, 0.1, 0], [0.3, 0.3, 0.3, 0.3],
                           [0.2, 0.3, 0.4, 0.5], [0, 0.1, 0.2, 0.2]])
    x = MC.steady_state(transition)
    fcast = MC.four_state_forecast(int(1e5))
    assert fcast.count(0) / len(
        fcast) - x[0] < tol, "failed on ratio of hot days"
    assert fcast.count(1) / len(
        fcast) - x[1] < tol, "failed on ratio of mild days"
    assert fcast.count(2) / len(
        fcast) - x[2] < tol, "failed on ratio of cold days"
    assert fcast.count(3) / len(
        fcast) - x[3] < tol, "failed on ratio of freezing days"
Пример #4
0
def test_steady_state():
    """A function for testing the steady_state function. Also validates the results of 
    forecast() and four_state_forecast()."""
    #Set tolerance for error
    tol = 1e-6
    for n in range(1, 6):
        #Make a random transition matrix and it's steady state distribution
        A = MC.random_chain(n)
        x = MC.steady_state(A)
        #Compare x and Ax
        x_new = A @ x
        for i in range(n):
            assert x_new[i] - x[i] < tol, "failed on Ax = x"
        #Verify that the columns of A^k approach x as k approaches infinity
        Ak = np.linalg.matrix_power(A, 20)
        for i in range(n):
            for j in range(n):
                assert Ak[i, j] - x[
                    i] < tol, "failed on A^k approaches x column-wise"
Пример #5
0
def test_forcast(set_up_2x2):
    transition, correct_answer = set_up_2x2

    # Test if all the values are zeroes and ones... how else could you test this???
    n = 10
    weather = mc.forecast(n)

    print(weather)
    assert all([weather[i] in [0, 1] for i in range(n)])

    steady_state_x = mc.steady_state(transition)
    # Make sure steady_state calculated from problem 4 is correct
    assert(np.allclose(transition @ steady_state_x, steady_state_x))

    trials = int(1e4)
    results = mc.forecast(trials)
    average_hot_days = np.array(results).sum() / trials

    assert np.allclose(average_hot_days, correct_answer[1], atol=.01)
Пример #6
0
def test_four_state_forcast(set_up_4x4):
    # get 4x4 matrix
    weather_matrix = set_up_4x4

    # number of samples
    n = int(1e5)
    weather = mc.four_state_forecast(n)

    # Test: is each entry valid?
    assert all( [weather[i] in [0, 1, 2, 3] for i in range(n)] )


    # Test: does the steady state match
    steady_state = mc.steady_state(weather_matrix)

    # count number of 0's, 1's, 2's, and 3's and divide by length of weather
    answer = np.array([ # list comprehension
        sum( [weather[j] == i for j in range(len(weather))] ) / len(weather)
        for i in [0, 1, 2, 3]
    ])

    print(answer)
    assert np.allclose(steady_state, answer, atol=.005)