Exemplo n.º 1
0
def test_construction():
    # Transition matrix
    transition_matrix = np.array([[0.5, 0.5, 0., 0., 0.],
                                  [0.25, 0.5, 0.25, 0., 0.],
                                  [0., 0.25, 0.5, 0.25, 0.],
                                  [0., 0., 0.25, 0.5, 0.25],
                                  [0., 0., 0., 0.5, 0.5]])

    states = np.arange(1, 6)

    def trans_func(i, j):
        if i - j == 0:
            return 0.5
        elif 2 <= i <= 4:
            if abs(i - j) == 1:
                return 0.25
            else:
                return 0
        elif i == 1:
            if j == 2:
                return 0.5
            else:
                return 0
        elif i == 5:
            if j == 4:
                return 0.5
            else:
                return 0

    table = Table().states(states).transition_function(trans_func)
    transition_prob = transition_matrix.reshape((25))
    table2 = Table().states(states).transition_probability(transition_prob)

    table_to_mc = table.to_markov_chain()
    table2_to_mc = table2.to_markov_chain()
    mc_from_table = MarkovChain.from_table(table)
    mc_from_function = MarkovChain.from_transition_function(states, trans_func)
    mc_from_matrix = MarkovChain.from_matrix(states, transition_matrix)

    assert_array_equal(transition_matrix, table_to_mc.get_transition_matrix())
    assert_array_equal(transition_matrix, table2_to_mc.get_transition_matrix())
    assert_array_equal(transition_matrix,
                       mc_from_table.get_transition_matrix())
    assert_array_equal(transition_matrix,
                       mc_from_function.get_transition_matrix())
    assert_array_equal(transition_matrix,
                       mc_from_matrix.get_transition_matrix())

    # Negative probability.
    with pytest.warns(UserWarning):
        MarkovChain.from_matrix([1, 2], [[-1, 2], [0.5, 0.5]])
    # Transition probability doesn't sum to 1.
    with pytest.warns(UserWarning):
        MarkovChain.from_matrix([1, 2], [[0.2, 0.3], [1, 2]])
Exemplo n.º 2
0
def test_construction():
    # String values
    table_str = Table().values('X', ['H', 'T'], 'Y', ['H', 'T'])
    table_str = table_str.probabilities(np.array([0.24, 0.36, 0.16, 0.24]))
    dist = table_str.to_joint()
    assert_joint_equal(dist, np.array([[0.36, 0.24], [0.24, 0.16]]))
    assert_equal(dist.get_possible_values(), [['H', 'T'], ['T', 'H']])
    dist = table_str.to_joint(reverse=False)
    assert_joint_equal(dist, np.array([[0.24, 0.16], [0.36, 0.24]]))
    assert_equal(dist.get_possible_values(), [['H', 'T'], ['H', 'T']])

    # Int values
    table_int = Table().values('X', [1], 'Y', [2]).probabilities([1])
    dist = JointDistribution.from_table(table_int)
    x_values = dist.get_possible_values('X')
    assert_approx_equal(x_values, [1])
    assert isinstance(x_values[0], int)
    y_values = dist.get_possible_values('Y')
    assert_approx_equal(y_values, [2])
    assert isinstance(y_values[0], int)

    # Float values
    table_float = Table().values('X', [1.1], 'Y', [2.2]).probabilities([1])
    dist = JointDistribution.from_table(table_float)
    x_values = dist.get_possible_values('X')
    assert isinstance(x_values[0], float)

    # Negative probability values
    with pytest.warns(UserWarning):
        Table().values('X', [1, 2], 'Y', [3])\
            .probabilities([-0.5, 1.5]).to_joint()

    # Doesn't sum to 1
    with pytest.warns(UserWarning):
        Table().values('X', [1, 2], 'Y', [3])\
            .probabilities([1., 1.]).to_joint()
Exemplo n.º 3
0
import pytest

from prob140 import (
    JointDistribution,
    Table,
)

from . import (
    assert_approx_equal,
    assert_dist_equal,
    assert_equal,
    assert_joint_equal,
)

DIST1 = Table().values([0, 1],
                       [2, 3, 4]).probabilities([0.1, 0.2, 0., 0.3, 0.4,
                                                 0.]).to_joint()
DIST2 = Table().values('Coin1', ['H', 'T'], 'Coin2',
                       ['H', 'T']).probabilities([0.24, 0.36, 0.16,
                                                  0.24]).to_joint()


def test_construction():
    # String values
    table_str = Table().values('X', ['H', 'T'], 'Y', ['H', 'T'])
    table_str = table_str.probabilities(np.array([0.24, 0.36, 0.16, 0.24]))
    dist = table_str.to_joint()
    assert_joint_equal(dist, np.array([[0.36, 0.24], [0.24, 0.16]]))
    assert_equal(dist.get_possible_values(), [['H', 'T'], ['T', 'H']])
    dist = table_str.to_joint(reverse=False)
    assert_joint_equal(dist, np.array([[0.24, 0.16], [0.36, 0.24]]))
Exemplo n.º 4
0
from numpy.testing import assert_array_equal
import pytest

from . import (
    assert_approx_equal,
    assert_dist_equal,
)
from prob140 import (
    MarkovChain,
    Table,
)

MC_SIMPLE = MarkovChain.from_matrix(states=np.array(['A', 'B']),
                                    transition_matrix=np.array([[0.1, 0.9],
                                                                [0.8, 0.2]]))
START_SIMPLE = Table().states(['A', 'B']).probabilities([0.8, 0.2])


def test_construction():
    # Transition matrix
    transition_matrix = np.array([[0.5, 0.5, 0., 0., 0.],
                                  [0.25, 0.5, 0.25, 0., 0.],
                                  [0., 0.25, 0.5, 0.25, 0.],
                                  [0., 0., 0.25, 0.5, 0.25],
                                  [0., 0., 0., 0.5, 0.5]])

    states = np.arange(1, 6)

    def trans_func(i, j):
        if i - j == 0:
            return 0.5
Exemplo n.º 5
0
from scipy import stats
import numpy as np
from prob140 import Table
from prob140 import Plot

# distribucion binomial ploteada

n = 4
p = 3 / 4
k = np.arange(n + 1)
binom_4_dist = stats.binom.pmf(k, n, p)
binom_4_dist_graph = Table().values(k).probabilities(binom_4_dist)
Plot(binom_4_dist_graph)
Exemplo n.º 6
0
def test_normalized():
    with pytest.warns(UserWarning):
        dist = Table().values([1, 2, 3]).probabilities([1] * 3)
    assert_dist_equal(dist.normalized(), [1 / 3] * 3)
Exemplo n.º 7
0
def test_construction():

    domain = Table().values(np.array([1, 2, 3]))
    assert domain.num_columns == 1

    dist1 = domain.probabilities(np.array([0.1, 0.2, 0.7]))
    assert dist1.num_columns == 2

    dist2 = domain.probability_function(lambda x: x / 6)
    assert dist2.num_columns == 2

    # Negative probability.
    with pytest.warns(UserWarning):
        domain.probabilities([0, 1.1, -0.1])
    with pytest.warns(UserWarning):
        domain.probability_function(lambda x: -x / 6)

    # Probability doesn't sum to 1.
    with pytest.warns(UserWarning):
        domain.probabilities([0.3, 0.1, 0.2])
    with pytest.warns(UserWarning):
        domain.probability_function(lambda x: x)
Exemplo n.º 8
0
def test_remove_zeros():
    dist = Table().values([2, 3, 4, 5]).probabilities([0.5, 0.0, 0.5, 0])
    assert dist.remove_zeros().num_rows == 2
Exemplo n.º 9
0
import numpy as np
import pytest

from prob140 import (
    emp_dist,
    Table,
)

from . import (
    assert_approx_equal,
    assert_dist_equal,
)

SIMPLE_DIST = Table().values([1, 2, 3]).probabilities([0.2, 0.3, 0.5])
UNIFORM_DIST = Table().values(np.arange(100)).probabilities([0.01] * 100)
NEGATIVE_DIST = Table().values([-2, -1, 0, 1]).probabilities([0.25] * 4)
NONINTEGER_DIST = Table().values([-1.5, -0.5, 0.5,
                                  1.5]).probabilities([0.25] * 4)


def test_construction():

    domain = Table().values(np.array([1, 2, 3]))
    assert domain.num_columns == 1

    dist1 = domain.probabilities(np.array([0.1, 0.2, 0.7]))
    assert dist1.num_columns == 2

    dist2 = domain.probability_function(lambda x: x / 6)
    assert dist2.num_columns == 2