Example #1
0
def run_cras(row_totals: numpy.matrix,
             column_totals: numpy.matrix,
             conditions: dict,
             maximum_iterations: int=10000,
             tol: float=0.00001) -> numpy.matrix:
    """
    :param row_totals:
    :param column_totals:
    :param conditions: a condition is a group of cells and a total, dict(tuple(tuples): float),
    a cell is (row_index, col_index)
    :param maximum_iterations: big number to stop this running forever
    :param tol: how close is close enough?
    :return:
    """
    # TODO remove this hack [max(thing.shape)]
    a = numpy.matrix(numpy.ones(shape=(max(row_totals.shape), max(column_totals.shape))))
    e = numpy.matrix(numpy.ones(shape=(len(row_totals), 1)))
    for _ in range(maximum_iterations):

        r_hat = row_scaling(a, row_totals, e)
        a = matrix_multiply(r_hat, a)

        s_hat = column_scaling(a, column_totals, e)
        a = matrix_multiply(a, s_hat)

        a = apply_conditions(a, conditions)

        if is_matrix_close_to_i(r_hat, tol) and is_matrix_close_to_i(s_hat, tol):
            return a

    return a
Example #2
0
def run_ras(row_totals: numpy.matrix,
            column_totals: numpy.matrix,
            maximum_iterations: int=10000,
            a: numpy.matrix=None,
            tol: float=0.00001) -> numpy.matrix:
    """
    creates a matrix who's respective rows sum to the row_totals and who's respective columns sum to the column_totals
    within the tolerance.
    :param row_totals:
    :param column_totals:
    :param maximum_iterations: big number to stop this running forever
    :param tol: how close is close enough?
    :return:
    """
    if not a:
        a = numpy.matrix(numpy.ones(shape=(row_totals.shape[0], column_totals.shape[1])))
    e = numpy.matrix(numpy.ones(shape=row_totals.shape))

    for _ in range(maximum_iterations):

        r_hat = row_scaling(a, row_totals, e)
        a = matrix_multiply(r_hat, a)

        s_hat = column_scaling(a, column_totals, e)
        a = matrix_multiply(a, s_hat)

        if is_matrix_close_to_i(r_hat, tol) and is_matrix_close_to_i(s_hat, tol):
            return a

    return a
def run_model(consumption: numpy.matrix, production: numpy.matrix,
              emissions: numpy.matrix, final_demand: numpy.matrix=None) -> numpy.matrix:
    """
    :param consumption: U
    :param production: V
    :param emissions: e
    :return:
    """
    i = numpy.identity(len(consumption))

    q = get_row_sum(production.source_data.elements)
    g = get_col_sum(production.source_data.elements)

    if final_demand is None:
        y = get_row_sum(consumption.source_data.elements)
    else:
        y = final_demand

    b = matrix_divide(consumption.source_data.elements, diagonal(g))
    d = matrix_divide(production.source_data.elements, diagonal(q))

    a = matrix_multiply(b, d)

    money_part = matrix_multiply(inv(i - a), y)

    return element_wise_divide(emissions.source_data.elements, money_part)
Example #4
0
def row_scaling(a: numpy.matrix,
                row_totals: numpy.matrix,
                e: numpy.matrix) -> numpy.matrix:
    r_hat = matrix_divide(diagonal(row_totals),
                          diagonal(matrix_multiply(a, e)))

    return r_hat
Example #5
0
def column_scaling(a: numpy.matrix,
                   column_totals: numpy.matrix,
                   e: numpy.matrix) -> numpy.matrix:

    s_hat = matrix_divide(diagonal(column_totals),
                          diagonal(matrix_multiply(e.T, a)))

    return s_hat
 def test_multi_dimension_float(self):
     a = numpy.matrix([[3.0], [4.0]])
     b = numpy.matrix([1.0, 2.0])
     d = matrix_multiply(a, b)
     numpy.testing.assert_allclose(d, [[3.0, 6.0], [4.0, 8.0]])
 def test_multi_dimension(self):
     a = numpy.matrix([[3], [4]])
     b = numpy.matrix([1, 2])
     d = matrix_multiply(a, b)
     numpy.testing.assert_allclose(d, [[3, 6], [4, 8]])
 def test_simple_float(self):
     a = numpy.matrix([1.0, 2.0])
     b = numpy.matrix([[3.0], [4.0]])
     d = matrix_multiply(a, b)
     numpy.testing.assert_allclose(d, [[11.0]])
 def test_simple_case(self):
     a = numpy.matrix([1, 2])
     b = numpy.matrix([[3], [4]])
     d = matrix_multiply(a, b)
     numpy.testing.assert_allclose(d, [[11]])
 def test_type(self):
     a = numpy.matrix([[3], [4]])
     b = numpy.matrix([1, 2])
     d = matrix_multiply(a, b)
     self.assertIs(type(d), numpy.matrix)