Example #1
0
def test_parse_rvs2():
    outcomes = ['00', '11']
    pmf = [1 / 2] * 2
    d = Distribution(outcomes, pmf)
    d.set_rv_names('XY')
    with pytest.raises(ditException):
        parse_rvs(d, ['X', 'Y', 'Z'])
Example #2
0
def test_parse_rvs2():
    outcomes = ['00', '11']
    pmf = [1/2]*2
    d = Distribution(outcomes, pmf)
    d.set_rv_names('XY')
    with pytest.raises(ditException):
        parse_rvs(d, ['X', 'Y', 'Z'])
Example #3
0
def brute_marginal_array(d, rvs, rv_mode=None):
    """A brute force computation of the marginal array.

    The parameter array tells which elements of the joint pmf must be summed
    to yield each element of the marginal distribution specified by `indexes`.

    This is more general than AbstractDenseDistribution since it allows the
    alphabet to vary with each random variable. It still requires a Cartestian
    product sample space, however.

    TODO: Expand this to construct arrays for coalescings as well.

    """
    from dit.helpers import parse_rvs, RV_MODES

    # We need to filter the indexes for duplicates, etc. So that we can be
    # sure that when we query the joint outcome, we have the right indexes.
    rvs, indexes = parse_rvs(d, rvs, rv_mode, unique=True, sort=True)
    marginal = d.marginal(indexes, rv_mode=RV_MODES.INDICES)

    shape = (len(marginal._sample_space), len(d._sample_space))
    arr = np.zeros(shape, dtype=int)

    mindex = marginal._sample_space.index
    for i, joint_outcome in enumerate(d._sample_space):
        outcome = [joint_outcome[j] for j in indexes]
        outcome = marginal._outcome_ctor(outcome)
        idx = mindex(outcome)
        arr[idx, i] = 1

    # Now we need to turn this into a sparse matrix where there are only as
    # many columns as there are nonzero elements in each row.

    # Apply nonzero, use [1] to get only the columns
    nz = np.nonzero(arr)[1]
    n_rows = len(marginal._sample_space)
    arr = nz.reshape((n_rows, len(nz) / n_rows))

    return arr
Example #4
0
def brute_marginal_array(d, rvs, rv_mode=None):
    """A brute force computation of the marginal array.

    The parameter array tells which elements of the joint pmf must be summed
    to yield each element of the marginal distribution specified by `indexes`.

    This is more general than AbstractDenseDistribution since it allows the
    alphabet to vary with each random variable. It still requires a Cartestian
    product sample space, however.

    TODO: Expand this to construct arrays for coalescings as well.

    """
    from dit.helpers import parse_rvs, RV_MODES

    # We need to filter the indexes for duplicates, etc. So that we can be
    # sure that when we query the joint outcome, we have the right indexes.
    rvs, indexes = parse_rvs(d, rvs, rv_mode, unique=True, sort=True)
    marginal = d.marginal(indexes, rv_mode=RV_MODES.INDICES)

    shape = (len(marginal._sample_space), len(d._sample_space))
    arr = np.zeros(shape, dtype=int)

    mindex = marginal._sample_space.index
    for i, joint_outcome in enumerate(d._sample_space):
        outcome = [joint_outcome[j] for j in indexes]
        outcome = marginal._outcome_ctor(outcome)
        idx = mindex(outcome)
        arr[idx, i] = 1

    # Now we need to turn this into a sparse matrix where there are only as
    # many columns as there are nonzero elements in each row.

    # Apply nonzero, use [1] to get only the columns
    nz = np.nonzero(arr)[1]
    n_rows = len(marginal._sample_space)
    arr = nz.reshape((n_rows, len(nz) / n_rows))

    return arr
Example #5
0
def test_parse_rvs3():
    outcomes = ['00', '11']
    pmf = [1 / 2] * 2
    d = Distribution(outcomes, pmf)
    with pytest.raises(ditException):
        parse_rvs(d, [0, 1, 2])
Example #6
0
def test_parse_rvs3():
    outcomes = ['00', '11']
    pmf = [1/2]*2
    d = Distribution(outcomes, pmf)
    with pytest.raises(ditException):
        parse_rvs(d, [0, 1, 2])