Exemple #1
0
def prerun_scenario(debug=False):
    '''
    Prerun function for step 2, quantfit.
    
    This function cannot take in any arguments due to limitations
    of the RunPython VBA code in xlwings.

    Check that the necessary steps beforehand have been done.
    Read in/check the input parameters and return a
    dict for input parameters and a df for data.
    '''

    # Check that the necessary steps beforehand have been done.

    # --------------------------
    # Read in parameters
    # --------------------------
    dict_input_scenario = read_parameters_scenario()

    # --------------------------
    # Check parameter values
    # --------------------------
    check_parameters_scenario(dict_input_scenario)

    # Read in global parameters
    # (this also checks if values have changed since being initially set)
    dict_global_params = read_parameters_global()
    #print(dict_input_scenario)
    #print(dict_global_params)

    # Add each key, val from dict_global_params to dict_input_quantfit
    for key, val in dict_global_params.items():
        # Check that the keys do not clash
        if key in dict_input_scenario:
            message = 'dict_input_quantfit should not have key ' + key + ' that is common with dict_global_params'
            show_message(message, halt=True)
        dict_input_scenario[key] = val

    # Create df for data
    input_sheetnames = [
        dict_input_scenario['sheet_partition'],
        dict_input_scenario['sheet_quantreg'],
        dict_input_scenario['sheet_cond_quant']
    ]
    sheet_partition = dict_input_scenario['sheet_partition']
    df_scenario_collections = read_data_scenario(input_sheetnames)
    df_scenario_collections['Data'] = df_scenario_collections['Data'][
        (df_scenario_collections['Data'].index >=
         df_scenario_collections[sheet_partition]['date'].values[0])
        & (df_scenario_collections['Data'].index <=
           df_scenario_collections[sheet_partition]['date'].values[-1])]

    # return a dict for input parameters and a df
    return dict_input_scenario, df_scenario_collections
Exemple #2
0
def prerun_quantfit(debug=False):
    '''
    Prerun function for step 2, quantfit.
    
    This function cannot take in any arguments due to limitations
    of the RunPython VBA code in xlwings.

    Check that the necessary steps beforehand have been done.
    Read in/check the input parameters and return a
    dict for input parameters and a df for data.
    '''

    if debug:
        print('=' * 30)
        print('start of prerun_quantfit')
        print('=' * 30)

    # Keys for input parameter dict
    keys = [
        'quantlist', 'regressors', 'sheet_input', 'sheet_quantreg',
        'sheet_cond_quant'
    ]
    # Check that the necessary steps beforehand have been done.

    # --------------------------
    # Read in parameters
    # --------------------------
    dict_input_quantfit = read_parameters_quantfit()

    # --------------------------
    # Check parameter values
    # --------------------------
    check_parameters_quantfit(dict_input_quantfit, keys)

    # Read in global parameters
    # (this also checks if values have changed since being initially set)
    dict_global_params = read_parameters_global()

    # Add each key, val from dict_global_params to dict_input_quantfit
    for key, val in dict_global_params.items():
        # Check that the keys do not clash
        if key in dict_input_quantfit:
            message = 'dict_input_quantfit should not have key ' + key + ' that is common with dict_global_params'
            show_message(message, halt=True)
        dict_input_quantfit[key] = val

    # Create df for data
    input_sheetname = dict_input_quantfit['sheet_input']
    df_quantfit = read_data_quantfit(input_sheetname)

    # return a dict for input parameters and a df
    return dict_input_quantfit, df_quantfit
def prerun_tsfit(debug=False):
    '''
    Prerun function for step 3, tsfit.
    
    Check that the necessary steps beforehand have been done.
    Read in/check the input parameters and return a
    dict for input parameters and a df for data.
    '''

    if debug:
        print('=' * 30)
        print('start of prerun_tsfit')
        print('=' * 30)

    # Keys for input parameter dict
    keys = ['latest_date', 'fit_params', 'sheet_tsfit']

    # Read in parameters
    dict_input_tsfit = read_parameters_tsfit()
    # Check parameter values
    check_parameters_tsfit(dict_input_tsfit, keys)

    # Read in global parameters
    # (this also checks if values have changed since being initially set)
    dict_global_params = read_parameters_global()

    # Add each key, val from dict_global_params to dict_input_tsfit
    for key, val in dict_global_params.items():
        # Check that the keys do not clash
        if key in dict_input_tsfit:
            message = 'dict_input_tsfit should not have key ' + key + ' that is common with dict_global_params'
            show_message(message, halt=True)
        dict_input_tsfit[key] = val

    # Create df for data
    input_sheetnames = [
        dict_input_tsfit['sheet_partition'],
        dict_input_tsfit['sheet_quantreg'],
        dict_input_tsfit['sheet_cond_quant']
    ]
    df_tsfit_collections = read_data_tsfit(input_sheetnames)

    # return a dict for input parameters and a df
    return dict_input_tsfit, df_tsfit_collections
Exemple #4
0
def prerun_segment(debug=False):
    '''
    Prerun function for step 2, quantfit.
    
    This function cannot take in any arguments due to limitations
    of the RunPython VBA code in xlwings.

    Check that the necessary steps beforehand have been done.
    Read in/check the input parameters and return a
    dict for input parameters and a df for data.
    '''

    # Check that the necessary steps beforehand have been done.

    # --------------------------
    # Read in parameters
    # --------------------------
    dict_input_segment = read_parameters_segment()

    # --------------------------
    # Check parameter values
    # --------------------------
    check_parameters_segment(dict_input_segment)

    # Read in global parameters
    # (this also checks if values have changed since being initially set)
    dict_global_params = read_parameters_global()
    #print(dict_input_segment)
    #print(dict_global_params)

    # Add each key, val from dict_global_params to dict_input_quantfit
    for key, val in dict_global_params.items():
        # Check that the keys do not clash
        if key in dict_input_segment:
            message = 'dict_input_quantfit should not have key ' + key + ' that is common with dict_global_params'
            show_message(message, halt=True)
        dict_input_segment[key] = val

    # Create df for data
    df_segment_collections = read_data_segment()

    # return a dict for input parameters and a df
    return dict_input_segment, df_segment_collections
Exemple #5
0
def prerun_partition(debug=False):
    '''
    Prerun function for step 1, partition.

    This function cannot take in any arguments due to limitations
    of the RunPython VBA code in xlwings.

    Read in/check the input parameters and return a
    dict for input parameters and a df for data.
    '''

    if debug:
        print('=' * 30)
        print('start of prerun_partition')
        print('=' * 30)

    # Keys for input parameter dict
    keys = [
        'freq', 'sdate', 'edate', 'method', 'pcutoff', 'method_growth',
        'retropolate', 'sheet_partitions', 'sheet_loadings'
    ]

    # --------------------------
    # Read in parameters
    # --------------------------
    dict_input_partition = read_parameters_partition()

    # --------------------------
    # Check parameter values
    # --------------------------
    check_parameters_partition(dict_input_partition, keys)

    # --------------------------
    # Read in global parameters
    # --------------------------
    # (this also checks if values have changed since being initially set)
    dict_global_params = read_parameters_global()

    # Add each key, val from dict_global_params to dict_input_partition
    for key, val in dict_global_params.items():
        # Check that the keys do not clash
        if key in dict_input_partition:
            message = 'dict_input_partition should not have key ' + key + ' that is common with dict_global_params'
            show_message(message, halt=True)
        dict_input_partition[key] = val

    # --------------------------
    # Create df for data
    # --------------------------
    df_partition = read_data_partition()

    # --------------------------
    # Create a dict for groups
    # --------------------------

    if dict_input_partition['method'] == "PLS":
        dict_groups, dict_PLS = read_partition_groupsPLS()
        dict_input_partition['PLS_target'] = dict_PLS
    else:
        dict_groups = read_partition_groups()
        dict_input_partition['PLS_target'] = None

    # --------------------------
    # Check df for partition
    # --------------------------
    check_data_partition(df_partition, dict_input_partition)

    # --------------------------
    # Set start and end dates and
    # fill missing values for data df
    # --------------------------
    df_partition = format_data_partition(df_partition,
                                         dict_input_partition['sdate'],
                                         dict_input_partition['edate'])

    #---------------------------
    # Check PLS target coverage
    #---------------------------

    if dict_input_partition['method'] == "PLS":
        PLSvar = set()
        for g in dict_PLS.values():
            for e in g:
                PLSvar.add(e)
        PLSvar = list(PLSvar)
        if df_partition.loc[:, PLSvar].isnull().sum().sum() > 0:
            print(df_partition.loc[:, PLSvar].head())
            message = 'PLS target should cover all dates'
            show_message(message, halt=True)

    # --------------------------
    # Check partition groups
    # --------------------------
    check_partition_groups(dict_groups, df_partition)

    # --------------------------
    # Return a dict for input parameters,
    # a dict for groups,
    # and a df for the data
    # --------------------------
    return dict_input_partition, dict_groups, df_partition