コード例 #1
0
def selector(algo, func_details, popSize, Iter):
    function_name = func_details[0]
    lb = func_details[1]
    ub = func_details[2]
    dim = func_details[3]

    if (algo == 0):
        x = pso.PSO(getattr(benchmarks, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 1):
        x = mvo.MVO(getattr(benchmarks, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 2):
        x = gwo.GWO(getattr(benchmarks, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 3):
        x = mfo.MFO(getattr(benchmarks, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 4):
        x = cs.CS(getattr(benchmarks, function_name), lb, ub, dim, popSize,
                  Iter)
    if (algo == 5):
        x = bat.BAT(getattr(benchmarks, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 6):
        x = woa.WOA(getattr(benchmarks, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 7):
        x = ffa.FFA(getattr(benchmarks, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 8):
        x = ssa.SSA(getattr(benchmarks, function_name), lb, ub, dim, popSize,
                    Iter)
    return x
コード例 #2
0
	def test_get_weight_from_MVO(self):
		mu = [0.02, 0.1]
		Q = [[0.18,0.0021],[0.0022,0.09]]
		target_return = 0.07
		weight = MVO.get_weight_from_MVO(mu, Q, target_return)
		expected_weight = [0.3306, 0.6694]
		self.assertEqual(len(weight), len(expected_weight))
		for i in range(len(weight)):
			self.assertEqual(round(weight[i], 4), expected_weight[i])
コード例 #3
0
def selector(algo, func_details, popSize, Iter):
    function_name = func_details[0]
    lb = func_details[1]
    ub = func_details[2]
    dim = 30

    if (algo == 'PSO'):
        x = pso.PSO(getattr(cec2005, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 'SSA'):
        x = ssa.SSA(getattr(cec2005, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 'GOA'):
        x = goa.GOA(getattr(cec2005, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 'IGOA'):
        x = igoa.IGOA(getattr(cec2005, function_name), lb, ub, dim, popSize,
                      Iter)
    if (algo == 'MVO'):
        x = mvo.MVO(getattr(cec2005, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 'GWO'):
        x = gwo.GWO(getattr(cec2005, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 'MFO'):
        x = mfo.MFO(getattr(cec2005, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 'CS'):
        x = cs.CS(getattr(cec2005, function_name), lb, ub, dim, popSize, Iter)
    if (algo == 'BAT'):
        x = bat.BAT(getattr(cec2005, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 'WOA'):
        x = woa.WOA(getattr(cec2005, function_name), lb, ub, dim, popSize,
                    Iter)
    if (algo == 'FFA'):
        x = ffa.FFA(getattr(cec2005, function_name), lb, ub, dim, popSize,
                    Iter)

    return x
コード例 #4
0
def selector(algo, func_details, popSize, Iter, trainDataset, testDataset,
             actv):
    function_name = func_details[0]
    lb = func_details[1]
    ub = func_details[2]

    Dataset_train = trainDataset
    Dataset_test = testDataset

    numRowsTrain = numpy.shape(Dataset_train)[
        0]  # number of instances in the train dataset
    numInputsTrain = numpy.shape(
        Dataset_train)[1] - 1  #number of features in the train dataset

    numRowsTest = numpy.shape(Dataset_test)[
        0]  # number of instances in the test dataset

    numInputsTest = numpy.shape(
        Dataset_test)[1] - 1  #number of features in the test dataset

    trainInput = Dataset_train[0:numRowsTrain, 0:-1]
    trainOutput = Dataset_train[0:numRowsTrain, -1]

    testInput = Dataset_test[0:numRowsTest, 0:-1]
    testOutput = Dataset_test[0:numRowsTest, -1]

    #number of hidden neurons
    HiddenNeurons = numInputsTrain * 2 + 1
    net = nl.net.newff([[0, 1]] * numInputsTrain, [HiddenNeurons, 1])
    if (actv == 1):
        net = nl.net.newff(
            [[0, 1]] * numInputsTrain, [HiddenNeurons, 1],
            [nl.trans.LogSig(), nl.trans.LogSig()])
    if (actv == 2):
        net = nl.net.newff(
            [[0, 1]] * numInputsTrain, [HiddenNeurons, 1],
            [nl.trans.SatLinPrm(1, 0, 1),
             nl.trans.SatLinPrm(1, 0, 1)])

    dim = (numInputsTrain * HiddenNeurons) + (2 * HiddenNeurons) + 1
    if (algo == 12):
        x = adam.adamse(getattr(costNN, function_name), lb, ub, dim, popSize,
                        Iter, trainInput, trainOutput, net)
    if (algo == 0):
        x = pso.PSO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)
    if (algo == 1):
        x = mvo.MVO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)
    if (algo == 2):
        x = gwo.GWO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)
    if (algo == 3):
        x = cs.CS(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                  trainInput, trainOutput, net)
    if (algo == 4):
        x = bat.BAT(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)
    if (algo == 5):
        x = de.DE(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                  trainInput, trainOutput, net)
    if (algo == 6):
        x = ga.GA(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                  trainInput, trainOutput, net)
    if (algo == 7):
        x = fa.FFA(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                   trainInput, trainOutput, net)
    if (algo == 8):
        x = bbo.BBO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)
    if (algo == 9):

        printAcc = []
        printAcc2 = []

        x = solution()
        timerStart = time.time()
        x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S")
        if (function_name == "costNN"):
            net.trainf.defaults['trainf'] = nl.error.MSE()
        elif (function_name == "costNN4"):
            net.trainf.defaults['trainf'] = nl.error.CEE()
        else:
            return x
        net.trainf = nl.train.train_gd
        newOutput = [[x] for x in trainOutput]
        newOutput = numpy.asarray(newOutput)
        e = net.train(trainInput, newOutput, epochs=Iter * popSize)
        timerEnd = time.time()
        x.optimizer = "BP"
        x.objfname = function_name
        x.popnum = 0
        x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S")
        x.executionTime = timerEnd - timerStart
        x.convergence = e
        pred = net.sim(trainInput).reshape(len(trainOutput))
        pred = numpy.round(pred).astype(int)
        trainOutput = trainOutput.astype(int)
        pred = numpy.clip(pred, 0, 1)
        ConfMatrix = confusion_matrix(trainOutput, pred)
        ConfMatrix1D = ConfMatrix.flatten()
        printAcc.append(accuracy_score(trainOutput, pred, normalize=True))
        classification_results = numpy.concatenate((printAcc, ConfMatrix1D))
        x.trainAcc = classification_results[0]
        x.trainTP = classification_results[1]
        x.trainFN = classification_results[2]
        x.trainFP = classification_results[3]
        x.trainTN = classification_results[4]

        pred = net.sim(testInput).reshape(len(testOutput))
        pred = numpy.round(pred).astype(int)
        testOutput = testOutput.astype(int)
        pred = numpy.clip(pred, 0, 1)
        ConfMatrix = confusion_matrix(testOutput, pred)
        ConfMatrix1D = ConfMatrix.flatten()
        printAcc2.append(accuracy_score(testOutput, pred, normalize=True))
        classification_results2 = numpy.concatenate((printAcc2, ConfMatrix1D))
        x.testAcc = classification_results2[0]
        x.testTP = classification_results2[1]
        x.testFN = classification_results2[2]
        x.testFP = classification_results2[3]
        x.testTN = classification_results2[4]

        return x
    if (algo == 10):

        printAcc = []
        printAcc2 = []
        x = solution()
        timerStart = time.time()
        x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S")
        if (function_name == "costNN"):
            net.trainf.defaults['trainf'] = nl.error.MSE()
        elif (function_name == "costNN4"):
            net.trainf.defaults['trainf'] = nl.error.CEE()
        else:
            return x
        net.trainf = nl.train.train_gdx
        newOutput = [[x] for x in trainOutput]
        newOutput = numpy.asarray(newOutput)
        e = net.train(trainInput, newOutput, epochs=Iter * popSize)
        timerEnd = time.time()
        x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S")
        x.optimizer = "BPMA"
        x.objfname = function_name
        x.popnum = 0
        x.executionTime = timerEnd - timerStart
        x.convergence = e
        pred = net.sim(trainInput).reshape(len(trainOutput))
        pred = numpy.round(pred).astype(int)
        trainOutput = trainOutput.astype(int)
        pred = numpy.clip(pred, 0, 1)
        ConfMatrix = confusion_matrix(trainOutput, pred)
        ConfMatrix1D = ConfMatrix.flatten()
        printAcc.append(accuracy_score(trainOutput, pred, normalize=True))
        classification_results = numpy.concatenate((printAcc, ConfMatrix1D))
        x.trainAcc = classification_results[0]
        x.trainTP = classification_results[1]
        x.trainFN = classification_results[2]
        x.trainFP = classification_results[3]
        x.trainTN = classification_results[4]

        pred = net.sim(testInput).reshape(len(testOutput))
        pred = numpy.round(pred).astype(int)
        testOutput = testOutput.astype(int)
        pred = numpy.clip(pred, 0, 1)
        ConfMatrix = confusion_matrix(testOutput, pred)
        ConfMatrix1D = ConfMatrix.flatten()
        printAcc2.append(accuracy_score(testOutput, pred, normalize=True))
        classification_results2 = numpy.concatenate((printAcc2, ConfMatrix1D))
        x.testAcc = classification_results2[0]
        x.testTP = classification_results2[1]
        x.testFN = classification_results2[2]
        x.testFP = classification_results2[3]
        x.testTN = classification_results2[4]

        return x
    if (algo == 11):
        x = solution()
        printAcc = []
        printAcc2 = []
        if (function_name == "costNN4"):
            if (actv == 0):
                timerStart = time.time()
                x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S")
                clf = MLPClassifier(hidden_layer_sizes=HiddenNeurons,
                                    activation='tanh',
                                    max_iter=Iter * popSize,
                                    learning_rate_init=0.01,
                                    n_iter_no_change=7500).fit(
                                        trainInput, trainOutput)

                timerEnd = time.time()
                x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S")
                x.optimizer = "Adam"
                x.objfname = function_name
                x.popnum = 0
                x.executionTime = timerEnd - timerStart
                x.convergence = clf.loss_curve_

                pred = clf.predict(trainInput)
                ConfMatrix = confusion_matrix(trainOutput, pred)
                ConfMatrix1D = ConfMatrix.flatten()
                printAcc.append(
                    accuracy_score(trainOutput, pred, normalize=True))
                classification_results = numpy.concatenate(
                    (printAcc, ConfMatrix1D))
                x.trainAcc = classification_results[0]
                x.trainTP = classification_results[1]
                x.trainFN = classification_results[2]
                x.trainFP = classification_results[3]
                x.trainTN = classification_results[4]

                pred = clf.predict(testInput)
                ConfMatrix = confusion_matrix(testOutput, pred)
                ConfMatrix1D = ConfMatrix.flatten()
                printAcc2.append(
                    accuracy_score(testOutput, pred, normalize=True))
                classification_results2 = numpy.concatenate(
                    (printAcc2, ConfMatrix1D))
                x.testAcc = classification_results2[0]
                x.testTP = classification_results2[1]
                x.testFN = classification_results2[2]
                x.testFP = classification_results2[3]
                x.testTN = classification_results2[4]

                return x
            elif (actv == 1):
                timerStart = time.time()
                x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S")
                clf = MLPClassifier(hidden_layer_sizes=HiddenNeurons,
                                    activation='logistic',
                                    max_iter=Iter * popSize,
                                    learning_rate_init=0.01,
                                    n_iter_no_change=7500).fit(
                                        trainInput, trainOutput)
                timerEnd = time.time()
                x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S")
                x.optimizer = "Adam"
                x.objfname = function_name
                x.popnum = 0
                x.executionTime = timerEnd - timerStart
                x.convergence = clf.loss_curve_

                pred = clf.predict(trainInput)
                ConfMatrix = confusion_matrix(trainOutput, pred)
                ConfMatrix1D = ConfMatrix.flatten()
                printAcc.append(
                    accuracy_score(trainOutput, pred, normalize=True))
                classification_results = numpy.concatenate(
                    (printAcc, ConfMatrix1D))
                x.trainAcc = classification_results[0]
                x.trainTP = classification_results[1]
                x.trainFN = classification_results[2]
                x.trainFP = classification_results[3]
                x.trainTN = classification_results[4]

                pred = clf.predict(testInput)
                ConfMatrix = confusion_matrix(testOutput, pred)
                ConfMatrix1D = ConfMatrix.flatten()
                printAcc2.append(
                    accuracy_score(testOutput, pred, normalize=True))
                classification_results2 = numpy.concatenate(
                    (printAcc2, ConfMatrix1D))
                x.testAcc = classification_results2[0]
                x.testTP = classification_results2[1]
                x.testFN = classification_results2[2]
                x.testFP = classification_results2[3]
                x.testTN = classification_results2[4]

                return x
            elif (actv == 2):
                timerStart = time.time()
                x.startTime = time.strftime("%Y-%m-%d-%H-%M-%S")
                clf = MLPClassifier(hidden_layer_sizes=HiddenNeurons,
                                    activation='relu',
                                    max_iter=Iter * popSize,
                                    learning_rate_init=0.01,
                                    n_iter_no_change=7500).fit(
                                        trainInput, trainOutput)
                timerEnd = time.time()
                x.endTime = time.strftime("%Y-%m-%d-%H-%M-%S")
                x.optimizer = "Adam"
                x.objfname = function_name
                x.popnum = 0
                x.executionTime = timerEnd - timerStart
                x.convergence = clf.loss_curve_

                pred = clf.predict(trainInput)
                ConfMatrix = confusion_matrix(trainOutput, pred)
                ConfMatrix1D = ConfMatrix.flatten()
                printAcc.append(
                    accuracy_score(trainOutput, pred, normalize=True))
                classification_results = numpy.concatenate(
                    (printAcc, ConfMatrix1D))
                x.trainAcc = classification_results[0]
                x.trainTP = classification_results[1]
                x.trainFN = classification_results[2]
                x.trainFP = classification_results[3]
                x.trainTN = classification_results[4]

                pred = clf.predict(testInput)
                ConfMatrix = confusion_matrix(testOutput, pred)
                ConfMatrix1D = ConfMatrix.flatten()
                printAcc2.append(
                    accuracy_score(testOutput, pred, normalize=True))
                classification_results2 = numpy.concatenate(
                    (printAcc2, ConfMatrix1D))
                x.testAcc = classification_results2[0]
                x.testTP = classification_results2[1]
                x.testFN = classification_results2[2]
                x.testFP = classification_results2[3]
                x.testTN = classification_results2[4]

                return x
        else:
            return x

    # Evaluate MLP classification model based on the training set
    trainClassification_results = evalNet.evaluateNetClassifier(
        x, trainInput, trainOutput, net)
    x.trainAcc = trainClassification_results[0]
    x.trainTP = trainClassification_results[1]
    x.trainFN = trainClassification_results[2]
    x.trainFP = trainClassification_results[3]
    x.trainTN = trainClassification_results[4]

    # Evaluate MLP classification model based on the testing set
    testClassification_results = evalNet.evaluateNetClassifier(
        x, testInput, testOutput, net)
    x.testAcc = testClassification_results[0]
    x.testTP = testClassification_results[1]
    x.testFN = testClassification_results[2]
    x.testFP = testClassification_results[3]
    x.testTN = testClassification_results[4]

    return x
コード例 #5
0
def selector(algo, func_details, popSize, Iter, trainDataset, testDataset):
    function_name = func_details[0]
    lb = func_details[1]
    ub = func_details[2]

    DatasetSplitRatio = 2 / 3

    dataTrain = "datasets/" + trainDataset
    dataTest = "datasets/" + testDataset

    Dataset_train = numpy.loadtxt(open(dataTrain, "rb"),
                                  delimiter=",",
                                  skiprows=0)
    Dataset_test = numpy.loadtxt(open(dataTest, "rb"),
                                 delimiter=",",
                                 skiprows=0)

    numRowsTrain = numpy.shape(Dataset_train)[
        0]  # number of instances in the train dataset
    numInputsTrain = numpy.shape(
        Dataset_train)[1] - 1  #number of features in the train dataset

    numRowsTest = numpy.shape(Dataset_test)[
        0]  # number of instances in the test dataset

    numInputsTest = numpy.shape(
        Dataset_test)[1] - 1  #number of features in the test dataset

    trainInput = Dataset_train[0:numRowsTrain, 0:-1]
    trainOutput = Dataset_train[0:numRowsTrain, -1]

    testInput = Dataset_test[0:numRowsTest, 0:-1]
    testOutput = Dataset_test[0:numRowsTest, -1]

    #number of hidden neurons
    HiddenNeurons = numInputsTrain * 2 + 1
    net = nl.net.newff([[0, 1]] * numInputsTrain, [HiddenNeurons, 1])

    dim = (numInputsTrain * HiddenNeurons) + (2 * HiddenNeurons) + 1

    if (algo == 0):
        x = pso.PSO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)
    if (algo == 1):
        x = mvo.MVO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)
    if (algo == 2):
        x = gwo.GWO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)
    if (algo == 3):
        x = mfo.MFO(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)
    if (algo == 4):
        x = cs.CS(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                  trainInput, trainOutput, net)
    if (algo == 5):
        x = bat.BAT(getattr(costNN, function_name), lb, ub, dim, popSize, Iter,
                    trainInput, trainOutput, net)

    # Evaluate MLP classification model based on the training set
    trainClassification_results = evalNet.evaluateNetClassifier(
        x, trainInput, trainOutput, net)
    x.trainAcc = trainClassification_results[0]
    x.trainTP = trainClassification_results[1]
    x.trainFN = trainClassification_results[2]
    x.trainFP = trainClassification_results[3]
    x.trainTN = trainClassification_results[4]

    # Evaluate MLP classification model based on the testing set
    testClassification_results = evalNet.evaluateNetClassifier(
        x, testInput, testOutput, net)
    x.testAcc = testClassification_results[0]
    x.testTP = testClassification_results[1]
    x.testFN = testClassification_results[2]
    x.testFP = testClassification_results[3]
    x.testTN = testClassification_results[4]

    return x
コード例 #6
0
def port_cont_procedure(asset_data, user_input, id_ticker_mapping,
                        ticker_id_mapping, factor_data):
    '''
		Start and end date in user_input is for user to select what range of data to look at
		If the user doesn't specify these two dates, default date will be used
		The end date for look back is "2018-09-30" for factor data availability
	'''
    if "investment_length" not in user_input or user_input[
            "investment_length"] > MAX_LOOK_BACK_L:
        user_input["investment_length"] = LOOK_BACK_L
    if "target_return" not in user_input: user_input["target_return"] = 0.1
    user_input["end_date"] = "2018-09-30"
    feasible_end_date_index = find_next_available_date_index(
        asset_data, user_input["end_date"], "SP500", -1)
    dates = asset_data["SP500"]["dates"]\
      [feasible_end_date_index - user_input["investment_length"]: feasible_end_date_index + 1]
    factor_matrix = prepare_factor_matrix(factor_data, 0,
                                          len(dates) - 1, dates)
    assets_included, asset_return_matrix = prepare_asset_return_matrix(asset_data, \
               dates[0], dates[-1], dates)
    expected_returns, covariance_matrix = factor_model.generate_factor(
        factor_matrix, asset_return_matrix)
    mvo_weight = MVO.get_weight_from_MVO(expected_returns, covariance_matrix, \
              user_input["target_return"])
    mvo_port = {"weight": {}, "start_date": dates[0], "end_date": "2018-10-31"}
    for i in range(len(assets_included)):
        if assets_included[i] in id_ticker_mapping:
            mvo_port["weight"][id_ticker_mapping[
                assets_included[i]]] = mvo_weight[i]
        else:
            mvo_port["weight"][assets_included[i]] = mvo_weight[i]
    mvo_port_back_test_res = back_testing_procedure(asset_data, mvo_port, \
                 id_ticker_mapping, ticker_id_mapping, False)
    cur_price = []
    for i in range(len(assets_included)):
        try:
            cur_price.append(asset_data[assets_included[i]]["price_his"]\
                 [asset_data[assets_included[i]]["dates"].index(dates[0])])
        except:
            import pdb
            pdb.set_trace()
    cvar_weight = cvar.get_optimal_weight_by_CVaR(expected_returns, covariance_matrix, cur_price, \
            10, int(user_input["investment_length"]), \
            int(user_input["investment_length"] / 4) )
    cvar_port = {
        "weight": {},
        "start_date": dates[0],
        "end_date": "2018-10-31"
    }
    for i in range(len(assets_included)):
        if assets_included[i] in id_ticker_mapping:
            cvar_port["weight"][id_ticker_mapping[
                assets_included[i]]] = cvar_weight[i]
        else:
            cvar_port["weight"][assets_included[i]] = cvar_weight[i]
    cvar_port_back_test_res = back_testing_procedure(asset_data, cvar_port, \
                 id_ticker_mapping, ticker_id_mapping, False)
    # import pdb; pdb.set_trace()
    if not os.path.exists("img"):
        os.mkdir("img")
    plot_and_save(dates + ["2018-10-31"], [cvar_port_back_test_res["portfolio_values"], \
              mvo_port_back_test_res["portfolio_values"]], \
         ["CVaR", "MVO"], "img/port_c_res.png")
    if cvar_port_back_test_res["stats"]["sharpe"] > mvo_port_back_test_res[
            "stats"]["sharpe"]:
        return {
            "port": cvar_port,
            "back_test": cvar_port_back_test_res,
            "taken": "CVaR",
            "objective": "c"
        }
    else:
        return {
            "port": mvo_port,
            "back_test": mvo_port_back_test_res,
            "taken": "MVO",
            "objective": "c"
        }
コード例 #7
0
def port_domi_procedure(asset_data, user_input, id_ticker_mapping,
                        ticker_id_mapping, factor_data):
    original_user_input = copy.deepcopy(user_input)
    if user_input["start_date"] < "2004-01-31":
        user_input["start_date"] = "2004-01-31"
    if user_input["end_date"] > "2018-10-31":
        user_input["end_date"] = "2018-10-31"
    user_port_res_whole = back_testing_procedure(asset_data, user_input,
                                                 id_ticker_mapping,
                                                 ticker_id_mapping, False)
    feasible_start_date_index = find_next_available_date_index(
        asset_data, user_input["start_date"], "SP500", +1)
    feasible_end_date_index = find_next_available_date_index(
        asset_data, user_input["end_date"], "SP500", -1)
    dates = asset_data["SP500"]["dates"][
        feasible_start_date_index:feasible_end_date_index + 1]
    dates = asset_data["SP500"][
        "dates"][feasible_start_date_index -
                 LOOK_BACK_L:feasible_start_date_index] + dates
    portfolio_values = [INITIAL_PORFOLIO_VALUE]
    cur_portfolio = {}
    cur_returns = []
    start_debug = False
    for i in range(LOOK_BACK_L, len(dates), 1):
        date = dates[i]
        user_input["start_date"] = dates[
            i - LOOK_BACK_L]  # Look back 4 years each time
        user_input["end_date"] = date
        user_port_res = back_testing_procedure(asset_data, user_input,
                                               id_ticker_mapping,
                                               ticker_id_mapping, False)
        factor_matrix = prepare_factor_matrix(factor_data, i - LOOK_BACK_L, i,
                                              dates)
        assets_included, asset_return_matrix = prepare_asset_return_matrix(asset_data, \
                  user_input["start_date"], user_input["end_date"], dates)
        expected_returns, covariance_matrix = factor_model.generate_factor(
            factor_matrix, asset_return_matrix)
        weight = MVO.get_weight_from_MVO(expected_returns, covariance_matrix, \
                 user_port_res["stats"]["mean_return"] * AMPLIFY_FACTOR)
        new_port_value = 0
        for asset_name in cur_portfolio.keys():
            if date in asset_data[asset_name]["dates"]:
                new_port_value += asset_data[asset_name]["price_his"][asset_data[asset_name]["dates"].index(date)] \
                    * cur_shares[asset_name]
            else:
                new_port_value += portfolio_values[-1] * cur_portfolio[
                    asset_name]
        if new_port_value != 0:
            portfolio_values.append(new_port_value)
        cur_portfolio = {}  # {asset_name: weight(decimal)}
        for ticker in user_input["weight"]:
            asset_name = ticker_id_mapping[ticker]
            cur_portfolio[
                asset_name] = user_input["weight"][ticker] * ORIGINAL_CONSTANT
        for j2 in range(len(weight)):
            if assets_included[j2] in cur_portfolio:
                cur_portfolio[assets_included[j2]] += weight[j2] * (
                    1 - ORIGINAL_CONSTANT)
            else:
                cur_portfolio[
                    assets_included[j2]] = weight[j2] * (1 - ORIGINAL_CONSTANT)
        cur_shares = {}  # {asset_name: #shares}
        for asset_name in cur_portfolio.keys():
            if date in asset_data[asset_name]["dates"]:
                cur_shares[asset_name] = portfolio_values[-1] * cur_portfolio[asset_name] / \
                   asset_data[asset_name]["price_his"][asset_data[asset_name]["dates"].index(date)]
        if len(portfolio_values) > 1:
            cur_returns.append(portfolio_values[-1] / portfolio_values[-2] - 1)
    stats = {}
    stats["total_return"] = portfolio_values[-1] / portfolio_values[0] - 1
    stats["mean_return"] = numpy.mean(cur_returns)
    stats["volitility"] = numpy.std(cur_returns) / (len(cur_returns)**0.5)
    stats["sharpe"] = stats["mean_return"] / stats["volitility"]
    if not os.path.exists("img"):
        os.mkdir("img")
    plot_and_save(user_port_res_whole["dates"], [user_port_res_whole["portfolio_values"], portfolio_values]\
        , ["user's portfolio", "improved portfolio"], "img/domi_res.png")
    return { "original_value": {"portfolio_values": user_port_res_whole["portfolio_values"], \
            "stats": user_port_res_whole["stats"]}, \
       "dominant": {"portfolio_values": portfolio_values, "stats": stats}, \
       "dates": user_port_res_whole["dates"], "objective": "d"}
コード例 #8
0
ファイル: selector.py プロジェクト: xm1490/EvoloPy-FS
def selector(algo, func_details, popSize, Iter, completeData):
    function_name = func_details[0]
    lb = func_details[1]
    ub = func_details[2]

    DatasetSplitRatio = 0.34  #Training 66%, Testing 34%

    DataFile = "datasets/" + completeData

    data_set = numpy.loadtxt(open(DataFile, "rb"), delimiter=",", skiprows=0)
    numRowsData = numpy.shape(data_set)[
        0]  # number of instances in the  dataset
    numFeaturesData = numpy.shape(
        data_set)[1] - 1  #number of features in the  dataset

    dataInput = data_set[0:numRowsData, 0:-1]
    dataTarget = data_set[0:numRowsData, -1]
    trainInput, testInput, trainOutput, testOutput = train_test_split(
        dataInput, dataTarget, test_size=DatasetSplitRatio, random_state=1)
    #

    #    numRowsTrain=numpy.shape(trainInput)[0]    # number of instances in the train dataset
    #    numFeaturesTrain=numpy.shape(trainInput)[1]-1 #number of features in the train dataset
    #
    #    numRowsTest=numpy.shape(testInput)[0]    # number of instances in the test dataset
    #    numFeaturesTest=numpy.shape(testInput)[1]-1 #number of features in the test dataset
    #

    dim = numFeaturesData

    if (algo == 0):
        x = pso.PSO(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize,
                    Iter, trainInput, trainOutput)
    if (algo == 1):
        x = mvo.MVO(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize,
                    Iter, trainInput, trainOutput)
    if (algo == 2):
        x = gwo.GWO(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize,
                    Iter, trainInput, trainOutput)
    if (algo == 3):
        x = mfo.MFO(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize,
                    Iter, trainInput, trainOutput)
    if (algo == 4):
        x = woa.WOA(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize,
                    Iter, trainInput, trainOutput)
    if (algo == 5):
        x = ffa.FFA(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize,
                    Iter, trainInput, trainOutput)
    if (algo == 6):
        x = bat.BAT(getattr(fitnessFUNs, function_name), lb, ub, dim, popSize,
                    Iter, trainInput, trainOutput)

    # Evaluate MLP classification model based on the training set
#    trainClassification_results=evalNet.evaluateNetClassifier(x,trainInput,trainOutput,net)
#   x.trainAcc=trainClassification_results[0]
#  x.trainTP=trainClassification_results[1]
# x.trainFN=trainClassification_results[2]
#x.trainFP=trainClassification_results[3]
#x.trainTN=trainClassification_results[4]

# Evaluate MLP classification model based on the testing set
#testClassification_results=evalNet.evaluateNetClassifier(x,testInput,testOutput,net)

    reducedfeatures = []
    for index in range(0, dim):
        if (x.bestIndividual[index] == 1):
            reducedfeatures.append(index)
    reduced_data_train_global = trainInput[:, reducedfeatures]
    reduced_data_test_global = testInput[:, reducedfeatures]

    knn = KNeighborsClassifier(n_neighbors=5)
    knn.fit(reduced_data_train_global, trainOutput)

    # Compute the accuracy of the prediction

    target_pred_train = knn.predict(reduced_data_train_global)
    acc_train = float(accuracy_score(trainOutput, target_pred_train))
    x.trainAcc = acc_train

    target_pred_test = knn.predict(reduced_data_test_global)
    acc_test = float(accuracy_score(testOutput, target_pred_test))
    x.testAcc = acc_test

    #print('Test set accuracy: %.2f %%' % (acc * 100))

    #x.testTP=testClassification_results[1]
    #x.testFN=testClassification_results[2]
    #x.testFP=testClassification_results[3]
    #x.testTN=testClassification_results[4]

    return x