def process_model(input_csv_file,
                  output_csv_file,
                  supplementary_input_csv_file=None):

    df1, df2 = get_input_dataframes(input_csv_file,
                                    supplementary_input_csv_file)

    # 'periods' hold actual number of periods in the model
    periods = get_periods_as_list(df1)
    # check if we need lag handling
    has_lags = df_has_block(df1, 'value_lag')

    # Period 0 is reported data, we store it and not process
    values0, multipliers0, equations = get_input_parameters(df1, df2, 0)
    values_holder_list = [values0]
    # we put multipliers in holder list, because we want to write them down in ouput csv by period
    multiplier_holder_list = [multipliers0]

    # We start processing from period 1
    for p in periods[1:]:
        print("-------- Period", p)
        values, multipliers, equations = get_input_parameters(df1, df2, p)

        if has_lags:
            # get system definiton
            values_lagged = get_df_block_as_dict(df1, 'value_lag', p)
        else:
            # The linear system uses lagged variables as an input
            # here we construct them from last period
            previous_period_values = values_holder_list[-1]
            values_lagged = {
                k + '_lag': previous_period_values[k]
                for k in last_values
            }

        # solving the system:
        x = solve_lin_system(multipliers, equations, values_lagged)

        # 'x' must be saved to a new array/dataframe/list holding results for all periods
        # GL: We save into a list containing results + multipliers for the
        # current period
        values_holder_list.append(_get_result_as_dict(x, values))
        multiplier_holder_list.append(multipliers)

    # save results
    # must save array/dataframe/list holding results for all periods
    # dump_csv_output() should  be dump_csv_output(output_df, output_csv_file)
    # GL: we pass lists of values and multipliers

    dump_csv_output(values_holder_list,
                    equations,
                    multiplier_holder_list,
                    output_csv_file,
                    write_lagged=has_lags)
def process_model(input_csv_file, output_csv_file, supplementary_input_csv_file = None):

    df1, df2 = get_input_dataframes(input_csv_file, supplementary_input_csv_file)

    # 'periods' hold actual number of periods in the model
    periods = get_periods_as_list(df1)
    # check if we need lag handling
    has_lags = df_has_block(df1, 'value_lag')

    # Period 0 is reported data, we store it and not process
    values0, multipliers0, equations = get_input_parameters(df1, df2, 0)
    values_holder_list = [values0]
    # we put multipliers in holder list, because we want to write them down in ouput csv by period
    multiplier_holder_list = [multipliers0]

    # We start processing from period 1
    for p in periods[1:]:
        print("-------- Period", p)
        values, multipliers, equations = get_input_parameters(df1, df2, p)

        if has_lags:
            # get system definiton
            values_lagged = get_df_block_as_dict(df1, 'value_lag', p)
        else:
            # The linear system uses lagged variables as an input
            # here we construct them from last period
            previous_period_values = values_holder_list[-1]
            values_lagged = {k + '_lag' : previous_period_values[k]  for k in last_values}

        # solving the system:
        x = solve_lin_system(multipliers, equations, values_lagged)

        # 'x' must be saved to a new array/dataframe/list holding results for all periods
        # GL: We save into a list containing results + multipliers for the
        # current period
        values_holder_list.append(_get_result_as_dict(x, values))
        multiplier_holder_list.append(multipliers)

    # save results
    # must save array/dataframe/list holding results for all periods
    # dump_csv_output() should  be dump_csv_output(output_df, output_csv_file)
    # GL: we pass lists of values and multipliers
    
    dump_csv_output(values_holder_list,
                    equations,
                    multiplier_holder_list,
                    output_csv_file,
                    write_lagged=has_lags)
if __name__ == "__main__":

    df = get_input_df('input.tab')
    multipliers = get_multipliers_as_dict(df)
    values      = get_values_as_dict(df, 0)
    equations   = get_equations_as_list(df)

    print ('\nMultipliers:')
    pprint(multipliers)
    print ('\nValues:')
    pprint(values)
    print ('\nEquations:')
    pprint(equations)
    
    # solving the system:
    x = solve_lin_system(multipliers, equations, values)

    # CHECK 1:
    # change dict1 type to nparray
    # compare dict_1 to x

    data1 = get_ref_array()
    print("Reference data:")
    print(data1)

    print("Found solution:")
    print(x.ix[data1.index])

    # To actually assert their equality use np.allclose()
    print('Matching values?', np.allclose(data1, x.ix[data1.index]))
        
示例#4
0
if __name__ == "__main__":

    df = get_input_df('input.tab')
    multipliers = get_multipliers_as_dict(df)
    values = get_values_as_dict(df, 0)
    equations = get_equations_as_list(df)

    print('\nMultipliers:')
    pprint(multipliers)
    print('\nValues:')
    pprint(values)
    print('\nEquations:')
    pprint(equations)

    # solving the system:
    x = solve_lin_system(multipliers, equations, values)

    # CHECK 1:
    # change dict1 type to nparray
    # compare dict_1 to x

    data1 = get_ref_array()
    print("Reference data:")
    print(data1)

    print("Found solution:")
    print(x.ix[data1.index])

    # To actually assert their equality use np.allclose()
    print('Matching values?', np.allclose(data1, x.ix[data1.index]))