コード例 #1
0
    def get_one_sample_score(self, q, free_parameters, seed_index):

        free_para1 = free_parameters['cov_para_1']
        free_para2 = free_parameters['cov_para_2']
        free_para3 = free_parameters['cov_para_3']
        free_para4 = free_parameters['cov_para_4']
        free_para5 = free_parameters['cov_para_5']
        free_para6 = free_parameters['cov_para_6']
        free_para7 = free_parameters['cov_para_7']
        free_para8 = free_parameters['cov_para_8']
        free_para9 = free_parameters['cov_para_9']
        free_para10 = free_parameters['cov_para_10']
        free_para11 = free_parameters['cov_para_11']
        free_para12 = free_parameters['cov_para_12']
        free_para13 = free_parameters['cov_para_13']
        free_para14 = free_parameters['cov_para_14']
        free_para15 = free_parameters['cov_para_15']

        diag_1 = 1
        diag_2 = np.sqrt(1 - free_para1**2)
        if (1 - free_para2**2 - free_para3**2) < 0:
            diag_3 = np.sqrt(np.abs(1 - free_para2**2 - free_para3**2))
        else:
            diag_3 = np.sqrt(1 - free_para2**2 - free_para3**2)

        if (1 - free_para4**2 - free_para5**2 - free_para6**2) < 0:
            diag_4 = np.sqrt(
                np.abs(1 - free_para4**2 - free_para5**2 - free_para6**2))
        else:
            diag_4 = np.sqrt(1 - free_para4**2 - free_para5**2 - free_para6**2)

        if (1 - free_para7**2 - free_para8**2 - free_para9**2 -
                free_para10**2) < 0:
            diag_5 = np.sqrt(
                np.abs(1 - free_para7**2 - free_para8**2 - free_para9**2 -
                       free_para10**2))
        else:
            diag_5 = np.sqrt(1 - free_para7**2 - free_para8**2 -
                             free_para9**2 - free_para10**2)

        if (1 - free_para11**2 - free_para12**2 - free_para13**2 -
                free_para14**2 - free_para15**2) < 0:
            diag_6 = np.sqrt(
                np.abs(1 - free_para11**2 - free_para12**2 - free_para13**2 -
                       free_para14**2 - free_para15**2))
        else:
            diag_6 = np.sqrt(1 - free_para11**2 - free_para12**2 -
                             free_para13**2 - free_para14**2 - free_para15**2)

        lower_triangular_matrix = np.asarray(
            [[diag_1, 0, 0, 0, 0, 0], [free_para1, diag_2, 0, 0, 0, 0],
             [free_para2, free_para3, diag_3, 0, 0, 0],
             [free_para4, free_para5, free_para6, diag_4, 0, 0],
             [free_para7, free_para8, free_para9, free_para10, diag_5, 0],
             [
                 free_para11, free_para12, free_para13, free_para14,
                 free_para15, diag_6
             ]])

        cov_matrix = lower_triangular_matrix @ lower_triangular_matrix.transpose(
        )

        #now, define the marginal distribution of the gaussian copula
        univerates = [{
            'loc': 0,
            'scale': 1,
            'a': free_parameters['beta_1a'],
            'b': free_parameters['beta_1b'],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }, {
            'loc': 0,
            'scale': 1,
            'a': free_parameters['beta_2a'],
            'b': free_parameters['beta_2b'],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }, {
            'loc': 0,
            'scale': 1,
            'a': free_parameters['beta_3a'],
            'b': free_parameters['beta_3b'],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }, {
            'loc': 0,
            'scale': 1,
            'a': free_parameters['beta_4a'],
            'b': free_parameters['beta_4b'],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }, {
            'loc': 0,
            'scale': 1,
            'a': free_parameters['beta_5a'],
            'b': free_parameters['beta_5b'],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }, {
            'loc': 0,
            'scale': 1,
            'a': free_parameters['beta_6a'],
            'b': free_parameters['beta_6b'],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }]

        #now, we construct the gaussian copula
        copula_parameters = {}
        copula_parameters['covariance'] = cov_matrix
        copula_parameters['univariates'] = univerates
        copula_parameters[
            'type'] = 'copulas.multivariate.gaussian.GaussianMultivariate'
        copula_parameters['columns'] = [0, 1, 2, 3, 4, 5]

        new_dist = Multivariate.from_dict(copula_parameters)

        #other parameters needed for transforming the features
        lambda_expon_1 = free_parameters['lambda_expon_1']
        lambda_expon_2 = free_parameters['lambda_expon_2']
        lambda_expon_3 = free_parameters['lambda_expon_3']
        lambda_expon_4 = free_parameters['lambda_expon_4']
        lambda_expon_5 = free_parameters['lambda_expon_5']
        lambda_expons = [
            lambda_expon_1, lambda_expon_2, lambda_expon_3, lambda_expon_4,
            lambda_expon_5
        ]

        #now, we begin to simulate trading
        #first, initialize the observation
        locol_env = trading_vix_non_random_seed.trading_vix(seed_index)
        this_trajectory_reward = []
        has_at_least_sell = False
        null_objective = True
        current_feature = locol_env.reset()

        for time_index in range(0, 200):

            #compute an action given current observation
            transformed_features = []
            for feature_index in range(len(lambda_expons)):
                transformation = expon.cdf(current_feature[feature_index, 0],
                                           scale=1.0 /
                                           lambda_expons[feature_index])
                min_transformation = 0.1
                transformation = min_transformation * np.exp(
                    np.log(1.0 / min_transformation) * transformation)
                transformed_features.append(transformation)
            transformed_features = np.asarray(transformed_features)
            transformed_features = np.reshape(transformed_features, (1, -1))
            holding_position = current_feature[-1, :][0]
            if holding_position < 0:
                print(
                    'holding is less than 0, there is some problem and the holding position is',
                    holding_position)
            if holding_position > 1:
                print(
                    'holding is greater than 1, there is some problem and the holding position is',
                    holding_position)
            min_transformed_holding = 0.1
            transformed_holding = min_transformed_holding * np.exp(
                np.log(1.0 / min_transformed_holding) * holding_position)
            transformed_holding = np.reshape(transformed_holding, (1, 1))
            data_point_for_df = np.concatenate(
                (transformed_features, transformed_holding), axis=1)

            assert data_point_for_df.shape[1] == 6
            data_point_for_copula = pd.DataFrame(data_point_for_df)
            action = new_dist.cdf(data_point_for_copula)

            #print('action in optimization trading vix is',action)

            #apply the action to the environment
            current_feature, reward, has_at_least_sell = locol_env.step(action)

            if has_at_least_sell and null_objective:
                #print('sold at least once')
                null_objective = False

            #record reward
            this_trajectory_reward.append(reward)

        #final time step,get the long term reward
        reward = locol_env.final()
        this_trajectory_reward.append(reward)

        if null_objective:
            objective = -1e9
        else:
            objective = np.sum(this_trajectory_reward)

        q.put([objective])
コード例 #2
0
def construct_a_copula(cov_input, marginal_input):

    assert len(cov_input) < 4

    if len(cov_input) == 3:
        diag_1 = 1
        diag_2 = np.sqrt(1 - cov_input[0]**2)
        if (1 - cov_input[1]**2 - cov_input[2]**2) < 0:
            diag_3 = np.sqrt(np.abs(1 - cov_input[1]**2 - cov_input[2]**2))
        else:
            diag_3 = np.sqrt(1 - cov_input[1]**2 - cov_input[2]**2)

        lower_triangular_matrix = np.asarray(
            [[diag_1, 0, 0], [cov_input[0], diag_2, 0],
             [cov_input[1], cov_input[2], diag_3]])

        cov_matrix = lower_triangular_matrix @ lower_triangular_matrix.transpose(
        )

    if len(cov_input) == 1:
        diag_1 = 1
        diag_2 = np.sqrt(1 - cov_input[0]**2)

        lower_triangular_matrix = np.asarray([[diag_1, 0],
                                              [cov_input[0], diag_2]])

        cov_matrix = lower_triangular_matrix @ lower_triangular_matrix.transpose(
        )

    if len(marginal_input) == 6:
        univerates = [{
            'loc': 0,
            'scale': 1,
            'a': marginal_input[0],
            'b': marginal_input[1],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }, {
            'loc': 0,
            'scale': 1,
            'a': marginal_input[2],
            'b': marginal_input[3],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }, {
            'loc': 0,
            'scale': 1,
            'a': marginal_input[4],
            'b': marginal_input[5],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }]

    if len(marginal_input) == 4:
        univerates = [{
            'loc': 0,
            'scale': 1,
            'a': marginal_input[0],
            'b': marginal_input[1],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }, {
            'loc': 0,
            'scale': 1,
            'a': marginal_input[2],
            'b': marginal_input[3],
            'type': 'copulas.univariate.beta.BetaUnivariate'
        }]

    copula_parameters = {}
    copula_parameters['covariance'] = cov_matrix
    copula_parameters['univariates'] = univerates
    copula_parameters[
        'type'] = 'copulas.multivariate.gaussian.GaussianMultivariate'
    if len(marginal_input) == 6:
        copula_parameters['columns'] = [0, 1, 2]
    if len(marginal_input) == 4:
        copula_parameters['columns'] = [0, 1]

    new_dist = Multivariate.from_dict(copula_parameters)

    return new_dist