How to combine two python matrices numpy - python

I have this CSV "TEMP2" full of data showed below.
1376460059,4,33.29,33.23,33.23,33.29,33.23,33.29,33.29,33.29,33.33,33.29,33.33,33.29,33.33,33.33,33.37,33.33,33.33,33.33,33.33,33.37,33.37,33.37,33.37
My work so far is this:
import csv
import numpy as np
import datetime
data = np.genfromtxt('TEMP2.csv', delimiter=',')[1:]
limite=data[0]
COLUMN_NUM = int(limite)
data = np.genfromtxt('TEMP2.csv', delimiter=',')[2:]
for x in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
tiempo = np.array(tiempo)
print tiempo
results = []
for i in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
results.append(tiempo)
print np.hstack(results)
if data.shape[0] % 4 == 0:
print data.reshape((-1, 4))
else:
data = np.pad(data, (0, COLUMN_NUM - len(data) % COLUMN_NUM), 'constant')
print (data)
print data.reshape((-1, COLUMN_NUM))
This part of the code will give me the time and miliseconds when each data was generated.
for x in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
tiempo = np.array(tiempo)
print tiempo
My question is, how can I set this results into a matrix looking something like this:
[[1376460059.0 1376460059.25 1376460059.5 1376460059.75]
[1376460060.0 1376460060.25 1376460060.5 1376460060.75]
[1376460061.0 1376460061.25 1376460061.5 1376460061.75]
[. . . . . . . . . . . . . . . . . . . and so on . .]
[1376460063.75 1376460064.0 1376460064.25 1376460064.5]]
Also, once I have it, is it possible to combine it with my other matrix to get a result like this:
[[33.29 33.23 33.23 33.29]
[1376460059.0 1376460059.25 1376460059.5 1376460059.75]
[33.23 33.29 33.29 33.29]
[1376460060.0 1376460060.25 1376460060.5 1376460060.75]
[33.33 33.29 33.33 33.29]
[1376460061.0 1376460061.25 1376460061.5 1376460061.75]
[ and so on. . . . .. . . . . .. . . . . . . .]]
I am really asking this because I have other source of help. But desperately to know. I have searched but Im not getting no where now. Thanks.

for the first part of your question, you just have to create a tab before your first iteration.
res = []
for x in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
tiempo = np.array(tiempo)
print tiempo
res.append(tiempo)
for the second part of your question, to combine the two matrix, you can use numpy.concatenate:
import numpy as np
np.concatenate((A, B))
matrix([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
I have not test my code, so I hope it will help you
I have work a little on the code, and now I have that :
import csv
import numpy as np
import datetime
data = np.genfromtxt('TEMP2.csv', delimiter=',')[1:]
limite=data[0]
COLUMN_NUM = int(limite)
data = np.genfromtxt('TEMP2.csv', delimiter=',')[2:]
results = np.zeros(shape=(len(data)))
for x in range(0, len(data)):
tiempo = (((x*1.)/COLUMN_NUM) + 1376460059)
results[x] = tiempo
if data.shape[0] % 4 == 0:
print(data.reshape((-1, 4)))
else:
data = np.pad(data, (0, COLUMN_NUM - len(data) % COLUMN_NUM), 'constant')
if results.shape[0] % 4 == 0:
print(results.reshape((-1, 4)))
else:
results = np.pad(results, (0, COLUMN_NUM - len(results) % COLUMN_NUM),
'constant')
data = data.reshape((-1, COLUMN_NUM))
results = results.reshape((-1, COLUMN_NUM))
final_matrix = np.concatenate((data, results))
final_matrix2 = []
for i in range(len(data)):
final_matrix2.append(list(data[i]))
final_matrix2.append(list(results[i]))
print(final_matrix)
print(final_matrix2)
and I have this result
[ 3.32900000e+01 3.32300000e+01 3.32300000e+01 3.32900000e+01]
[ 3.32300000e+01 3.32900000e+01 3.32900000e+01 3.32900000e+01]
[ 3.33300000e+01 3.32900000e+01 3.33300000e+01 3.32900000e+01]
[ 3.33300000e+01 3.33300000e+01 3.33700000e+01 3.33300000e+01]
[ 3.33300000e+01 3.33300000e+01 3.33300000e+01 3.33700000e+01]
[ 3.33700000e+01 3.33700000e+01 3.33700000e+01 0.00000000e+00]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 1.37646006e+09]
[ 1.37646006e+09 1.37646006e+09 1.37646006e+09 0.00000000e+00]]
[[33.289999999999999, 33.229999999999997, 33.229999999999997, 33.289999999999999], [1376460059.0, 1376460059.25, 1376460059.5, 1376460059.75], [33.229999999999997, 33.289999999999999, 33.289999999999999, 33.289999999999999], [1376460060.0, 1376460060.25, 1376460060.5, 1376460060.75], [33.329999999999998, 33.289999999999999, 33.329999999999998, 33.289999999999999], [1376460061.0, 1376460061.25, 1376460061.5, 1376460061.75], [33.329999999999998, 33.329999999999998, 33.369999999999997, 33.329999999999998], [1376460062.0, 1376460062.25, 1376460062.5, 1376460062.75], [33.329999999999998, 33.329999999999998, 33.329999999999998, 33.369999999999997], [1376460063.0, 1376460063.25, 1376460063.5, 1376460063.75], [33.369999999999997, 33.369999999999997, 33.369999999999997, 0.0], [1376460064.0, 1376460064.25, 1376460064.5, 0.0]]

Related

Gekko seems to ignore the single equation

I've been using Gekko for the last three years for optimization purposes. As a matter of fact, I'm using Gekko version 1.0.4. Recently, I've been trying to solve an ESG-MVP problem developed by (Vo, He et al. 2019):
Considering sum(wesg) should be equal to 1 and 0 ≤ wesg_i ≤ 1 for iε[0, 1, 2,..., 23]. In this model, I'm trying to
find the best values of wesg_i for the model above. Hence, according to the information provided, I have to find the value of 24 variables through an optimization model. My code is available for you to test on your computer with all the vital data needed.
Libraries
from gekko import GEKKO
import numpy as np
import pandas as pd
Data Section
Make sure copy it all.
esg_scores_df = pd.DataFrame({'Score 2019': {'MSFT': 7,'PEP': 7,'TSLA': 4,'AMZN': 6,'LKQ': 3,'ABMD': 3,'MSI': 10,'PH': 8,'NKE': 6,'TM': 7,'EOG': 7,'GOOGL': 5,'NFLX': 4,'GS': 6,'EQIX': 9,'EA': 7,'AAP': 6,'TEL': 9,'DG': 6,'EXR': 5,'MDLZ': 6,'FIS': 8,'CRL': 8,'RCL': 9},
'Score 2020': {'MSFT': 6,'PEP': 11,'TSLA': 4,'AMZN': 6,'LKQ': 4,'ABMD': 4,'MSI': 8,'PH': 8,'NKE': 6,'TM': 7,'EOG': 7,'GOOGL': 5,'NFLX': 3,'GS': 6,'EQIX': 9,'EA': 6,'AAP': 7,'TEL': 9,'DG': 6,'EXR': 4,'MDLZ': 6,'FIS': 8,'CRL': 8,'RCL': 7}})
predicted_return_dataframe = pd.DataFrame({'MSFT': 1.0982472257593677e-15,'PEP': 4.567069595849647e-09,'TSLA': 7.439258841202596e-10,'AMZN': 3.176883309676764e-07,'LKQ': 4.825709334830293e-05,'ABMD': 2.0608058642685837e-05,'MSI': -3.1789250961959136e-12,'PH': -2.257237871892785e-07,'NKE': -4.737530217609426e-07,'TM': 9.951932427172896e-07,'EOG': 1.184074639824261e-08,'GOOGL': -1.7027206923083418e-10,'NFLX': 2.948344729885545e-05,'GS': 1.458862302453713e-07,'EQIX': 4.620207503091301e-07,'EA': -1.297137640100873e-06,'AAP': -3.493153382990658e-07,'TEL': 8.202463568291799e-08,'DG': 2.743802312024829e-05,'EXR': 1.971994818465128e-11,'MDLZ': 1.578772827915881e-08,'FIS': 1.3314663987166631e-08,'CRL': 4.112083587242872e-05,'RCL': -1.470829206425942e-09} , index = [0])
r = pd.DataFrame({'MSFT': -0.029838842874655123,'PEP': -0.012265555866403165,'TSLA': -0.053599428210690636,'AMZN': -0.02614876342440773,'LKQ': -0.026658034482507052,'ABMD': -0.017274321994365294,'MSI': -0.018624198080929168,'PH': -0.02871952268633362,'NKE': -0.028820168231794074,'TM': -0.011208781085200492,'EOG': -0.04606883798249617,'GOOGL': -0.027983366801701926,'NFLX': -0.0070609860042608165,'GS': -0.02429261616692462,'EQIX': -0.015181566244607758,'EA': -0.012009295653284258,'AAP': -0.011979871239295382,'TEL': -0.021782414925879602,'DG': -0.009820085124020328,'EXR': -0.012233190959318829,'MDLZ': -0.015917631061436933,'FIS': -0.023877815976094417,'CRL': -0.020169902356185498,'RCL': -0.06831607178882856} , index = [0])
sigma = pd.DataFrame(np.array([[ 1.02188994e-04, 3.16399586e-05, 8.54243667e-05,
7.14148287e-05, 3.55313650e-05, 5.44796564e-05,
3.39128448e-05, 6.65003271e-05, 5.42932808e-05,
2.29333906e-05, 4.26407605e-05, 7.92791756e-05,
9.28910574e-05, 6.36770764e-05, 3.75024646e-05,
5.13720672e-05, 1.33646720e-05, 6.21865710e-05,
4.72888751e-05, 2.35546749e-06, 2.49700413e-05,
6.74503236e-05, 7.08948785e-05, 5.55096135e-05],
[ 3.16399586e-05, 6.47824823e-05, -2.57461009e-05,
1.81931187e-05, 2.11795049e-05, 1.01708683e-05,
3.33739243e-05, 1.23893822e-05, 2.74825418e-05,
1.35954411e-05, 2.37108310e-06, 2.34208728e-05,
1.96972793e-05, 1.09335123e-05, 4.26042775e-05,
2.76849308e-05, 6.01821651e-06, 8.04387109e-06,
3.10892437e-05, 2.79970364e-05, 3.70572613e-05,
3.08215527e-05, 2.57244480e-05, 8.72002781e-06],
[ 8.54243667e-05, -2.57461009e-05, 8.84138653e-04,
6.16478906e-05, -1.78063009e-05, 3.62712388e-05,
-6.15200573e-05, 8.63597848e-05, 2.12571109e-05,
3.21749681e-05, 6.54693912e-05, 7.95298035e-05,
1.53917874e-04, 8.06306277e-05, -5.27893372e-06,
2.60899474e-05, 4.29017379e-05, 6.13689353e-05,
2.30097086e-06, -4.45963115e-05, -2.83669072e-06,
2.95395217e-05, 4.53889239e-05, 3.11615533e-05],
[ 7.14148287e-05, 1.81931187e-05, 6.16478906e-05,
1.19357840e-04, 5.48763301e-05, 5.55161292e-05,
2.77288469e-05, 6.25231790e-05, 5.19494452e-05,
1.93193913e-05, 4.28946368e-05, 7.00210371e-05,
9.92756795e-05, 6.54630027e-05, 1.73935405e-05,
4.46819063e-05, 2.94199398e-05, 5.76749375e-05,
3.75634244e-05, -5.53847280e-06, 9.72193169e-06,
4.42756733e-05, 4.21469376e-05, 4.35344231e-05],
[ 3.55313650e-05, 2.11795049e-05, -1.78063009e-05,
5.48763301e-05, 3.49607174e-04, 1.58156275e-04,
-1.64565378e-05, 5.95602984e-05, 3.36138721e-05,
2.25095973e-05, 4.90144491e-05, 5.60207136e-05,
3.73119885e-05, 8.65010497e-05, -1.68774498e-05,
1.06928038e-05, 3.53732120e-05, 8.46071106e-05,
3.89200328e-05, -1.10740797e-05, 4.20238479e-06,
2.07371361e-05, 2.27640364e-05, 7.39017715e-05],
[ 5.44796564e-05, 1.01708683e-05, 3.62712388e-05,
5.55161292e-05, 1.58156275e-04, 1.12266763e-03,
2.49786917e-05, 5.60053839e-05, -2.57686022e-05,
3.00855419e-05, 2.09079416e-04, 1.21797841e-04,
3.80689080e-06, 1.17305259e-04, -4.95192897e-05,
5.56059321e-05, 2.87765760e-06, 1.13640096e-04,
-2.73690072e-05, 2.17458024e-05, -1.24009254e-05,
6.11287657e-05, 1.12849096e-04, 1.17234141e-04],
[ 3.39128448e-05, 3.33739243e-05, -6.15200573e-05,
2.77288469e-05, -1.64565378e-05, 2.49786917e-05,
1.84271573e-04, -2.99455473e-06, 3.56885701e-05,
1.53451236e-05, 3.20222217e-06, 3.00619247e-05,
5.17902613e-05, 1.69851832e-05, 7.75007583e-05,
7.49839347e-06, -3.78485373e-05, -1.28942631e-05,
4.36657573e-05, 3.97590833e-05, 4.04704969e-05,
4.05201790e-05, 4.01384618e-05, 4.82818631e-06],
[ 6.65003271e-05, 1.23893822e-05, 8.63597848e-05,
6.25231790e-05, 5.95602984e-05, 5.60053839e-05,
-2.99455473e-06, 2.86568704e-04, 6.55278686e-05,
4.08938883e-05, 1.18822482e-04, 9.32577766e-05,
7.92498191e-05, 1.38967662e-04, -1.64128182e-05,
4.16393158e-05, 1.06300676e-04, 1.31672888e-04,
6.64839109e-05, -5.14108669e-05, -1.03044572e-05,
1.59107041e-05, 7.65457600e-05, 1.31611693e-04],
[ 5.42932808e-05, 2.74825418e-05, 2.12571109e-05,
5.19494452e-05, 3.36138721e-05, -2.57686022e-05,
3.56885701e-05, 6.55278686e-05, 1.36374334e-04,
1.36781102e-05, 2.38544176e-05, 5.90104054e-05,
6.20754315e-05, 5.84082445e-05, 3.96422393e-05,
2.96064418e-05, 5.54456912e-05, 5.69396332e-05,
7.28867485e-05, 7.92870807e-06, 3.79528145e-05,
3.38036564e-05, 3.47015652e-05, 3.86545249e-05],
[ 2.29333906e-05, 1.35954411e-05, 3.21749681e-05,
1.93193913e-05, 2.25095973e-05, 3.00855419e-05,
1.53451236e-05, 4.08938883e-05, 1.36781102e-05,
5.30619573e-05, 4.42693706e-05, 2.92937393e-05,
1.15184525e-05, 3.04141154e-05, 1.11981894e-06,
1.29388215e-05, 1.41913521e-05, 2.37704063e-05,
1.27983719e-05, -3.53963791e-06, 5.41505031e-06,
1.21874870e-05, 2.04099948e-05, 2.89151466e-05],
[ 4.26407605e-05, 2.37108310e-06, 6.54693912e-05,
4.28946368e-05, 4.90144491e-05, 2.09079416e-04,
3.20222217e-06, 1.18822482e-04, 2.38544176e-05,
4.42693706e-05, 5.01946804e-04, 7.79572195e-05,
7.61563242e-05, 1.31597522e-04, -2.43789107e-05,
6.77962026e-05, 5.39745318e-05, 9.05223133e-05,
-1.88433584e-06, -1.63784490e-05, -1.04001201e-05,
1.79450881e-05, 5.33951974e-05, 1.07102393e-04],
[ 7.92791756e-05, 2.34208728e-05, 7.95298035e-05,
7.00210371e-05, 5.60207136e-05, 1.21797841e-04,
3.00619247e-05, 9.32577766e-05, 5.90104054e-05,
2.92937393e-05, 7.79572195e-05, 1.10688236e-04,
8.13294795e-05, 8.72981821e-05, 2.13737337e-05,
4.33439061e-05, 3.06090710e-05, 7.60853329e-05,
4.44363057e-05, 3.33458671e-06, 1.60908360e-05,
4.09067616e-05, 5.72051871e-05, 6.05239478e-05],
[ 9.28910574e-05, 1.96972793e-05, 1.53917874e-04,
9.92756795e-05, 3.73119885e-05, 3.80689080e-06,
5.17902613e-05, 7.92498191e-05, 6.20754315e-05,
1.15184525e-05, 7.61563242e-05, 8.13294795e-05,
3.77348891e-04, 8.99709029e-05, 2.58732673e-05,
8.24015922e-05, 2.77333081e-05, 4.74146019e-05,
3.76467363e-05, -5.60031564e-06, 2.25653918e-05,
6.22679966e-05, 3.92394407e-05, 6.47067147e-05],
[ 6.36770764e-05, 1.09335123e-05, 8.06306277e-05,
6.54630027e-05, 8.65010497e-05, 1.17305259e-04,
1.69851832e-05, 1.38967662e-04, 5.84082445e-05,
3.04141154e-05, 1.31597522e-04, 8.72981821e-05,
8.99709029e-05, 1.54802097e-04, -1.57072336e-05,
3.58129141e-05, 5.52667399e-05, 1.02065825e-04,
4.96942712e-05, -2.44193897e-05, -5.66188177e-06,
2.35884764e-05, 5.15584350e-05, 9.98376706e-05],
[ 3.75024646e-05, 4.26042775e-05, -5.27893372e-06,
1.73935405e-05, -1.68774498e-05, -4.95192897e-05,
7.75007583e-05, -1.64128182e-05, 3.96422393e-05,
1.11981894e-06, -2.43789107e-05, 2.13737337e-05,
2.58732673e-05, -1.57072336e-05, 1.54605175e-04,
1.07085192e-05, -1.14274871e-05, -1.26204468e-05,
2.50810750e-05, 5.60340845e-05, 4.60520336e-05,
4.29696767e-05, 5.33004299e-05, -1.56063893e-05],
[ 5.13720672e-05, 2.76849308e-05, 2.60899474e-05,
4.46819063e-05, 1.06928038e-05, 5.56059321e-05,
7.49839347e-06, 4.16393158e-05, 2.96064418e-05,
1.29388215e-05, 6.77962026e-05, 4.33439061e-05,
8.24015922e-05, 3.58129141e-05, 1.07085192e-05,
1.32544808e-04, 3.15561503e-05, 3.62681876e-05,
1.76503394e-05, 1.90982407e-07, 1.02992114e-05,
2.81341342e-05, 2.81970597e-05, 3.94413355e-05],
[ 1.33646720e-05, 6.01821651e-06, 4.29017379e-05,
2.94199398e-05, 3.53732120e-05, 2.87765760e-06,
-3.78485373e-05, 1.06300676e-04, 5.54456912e-05,
1.41913521e-05, 5.39745318e-05, 3.06090710e-05,
2.77333081e-05, 5.52667399e-05, -1.14274871e-05,
3.15561503e-05, 2.71861487e-04, 6.04272650e-05,
3.55256428e-05, -2.37123843e-05, -1.99844011e-05,
-2.02943319e-07, -4.12746174e-07, 6.80701129e-05],
[ 6.21865710e-05, 8.04387109e-06, 6.13689353e-05,
5.76749375e-05, 8.46071106e-05, 1.13640096e-04,
-1.28942631e-05, 1.31672888e-04, 5.69396332e-05,
2.37704063e-05, 9.05223133e-05, 7.60853329e-05,
4.74146019e-05, 1.02065825e-04, -1.26204468e-05,
3.62681876e-05, 6.04272650e-05, 1.67171047e-04,
5.06392219e-05, -1.02673299e-05, 1.64715372e-05,
2.50897861e-05, 5.81836032e-05, 1.00294572e-04],
[ 4.72888751e-05, 3.10892437e-05, 2.30097086e-06,
3.75634244e-05, 3.89200328e-05, -2.73690072e-05,
4.36657573e-05, 6.64839109e-05, 7.28867485e-05,
1.27983719e-05, -1.88433584e-06, 4.44363057e-05,
3.76467363e-05, 4.96942712e-05, 2.50810750e-05,
1.76503394e-05, 3.55256428e-05, 5.06392219e-05,
1.99771178e-04, 4.70710752e-06, 3.76160380e-05,
2.30014580e-05, 3.12554634e-05, 5.94948441e-06],
[ 2.35546749e-06, 2.79970364e-05, -4.45963115e-05,
-5.53847280e-06, -1.10740797e-05, 2.17458024e-05,
3.97590833e-05, -5.14108669e-05, 7.92870807e-06,
-3.53963791e-06, -1.63784490e-05, 3.33458671e-06,
-5.60031564e-06, -2.44193897e-05, 5.60340845e-05,
1.90982407e-07, -2.37123843e-05, -1.02673299e-05,
4.70710752e-06, 8.86508330e-05, 4.02039134e-05,
2.39025489e-05, -5.94660474e-06, -1.92301274e-05],
[ 2.49700413e-05, 3.70572613e-05, -2.83669072e-06,
9.72193169e-06, 4.20238479e-06, -1.24009254e-05,
4.04704969e-05, -1.03044572e-05, 3.79528145e-05,
5.41505031e-06, -1.04001201e-05, 1.60908360e-05,
2.25653918e-05, -5.66188177e-06, 4.60520336e-05,
1.02992114e-05, -1.99844011e-05, 1.64715372e-05,
3.76160380e-05, 4.02039134e-05, 8.23927778e-05,
3.21948208e-05, 3.50482953e-05, -1.97971783e-07],
[ 6.74503236e-05, 3.08215527e-05, 2.95395217e-05,
4.42756733e-05, 2.07371361e-05, 6.11287657e-05,
4.05201790e-05, 1.59107041e-05, 3.38036564e-05,
1.21874870e-05, 1.79450881e-05, 4.09067616e-05,
6.22679966e-05, 2.35884764e-05, 4.29696767e-05,
2.81341342e-05, -2.02943319e-07, 2.50897861e-05,
2.30014580e-05, 2.39025489e-05, 3.21948208e-05,
1.20680211e-04, 5.96710279e-05, 3.14411495e-05],
[ 7.08948785e-05, 2.57244480e-05, 4.53889239e-05,
4.21469376e-05, 2.27640364e-05, 1.12849096e-04,
4.01384618e-05, 7.65457600e-05, 3.47015652e-05,
2.04099948e-05, 5.33951974e-05, 5.72051871e-05,
3.92394407e-05, 5.15584350e-05, 5.33004299e-05,
2.81970597e-05, -4.12746174e-07, 5.81836032e-05,
3.12554634e-05, -5.94660474e-06, 3.50482953e-05,
5.96710279e-05, 1.65974972e-04, 5.42453625e-05],
[ 5.55096135e-05, 8.72002781e-06, 3.11615533e-05,
4.35344231e-05, 7.39017715e-05, 1.17234141e-04,
4.82818631e-06, 1.31611693e-04, 3.86545249e-05,
2.89151466e-05, 1.07102393e-04, 6.05239478e-05,
6.47067147e-05, 9.98376706e-05, -1.56063893e-05,
3.94413355e-05, 6.80701129e-05, 1.00294572e-04,
5.94948441e-06, -1.92301274e-05, -1.97971783e-07,
3.14411495e-05, 5.42453625e-05, 1.92061080e-04]]))
sigma_bar = pd.DataFrame(np.array([[ 1.26820873e-09, 9.99357360e-01, 9.96834344e-01,
9.99572167e-01, 9.42281243e-01, 9.97900189e-01,
9.99540337e-01, 9.98601652e-01, 9.99273813e-01,
9.97018616e-01, -9.73801139e-01, 9.99572102e-01,
9.99493833e-01, 9.98516049e-01, 9.99016459e-01,
9.99032878e-01, -9.44718270e-01, 9.99102498e-01,
9.99273586e-01, 9.97964072e-01, 9.98809893e-01,
9.99306302e-01, 9.98670142e-01, 9.89075401e-01],
[ 9.99357360e-01, 8.85756152e-04, 9.96279483e-01,
9.99197947e-01, 9.41407791e-01, 9.97899466e-01,
9.99417552e-01, 9.98836192e-01, 9.99109838e-01,
9.97212584e-01, -9.74070678e-01, 9.99506469e-01,
9.99304970e-01, 9.98532839e-01, 9.99435799e-01,
9.99191149e-01, -9.45834544e-01, 9.99087732e-01,
9.99237110e-01, 9.98261304e-01, 9.98920402e-01,
9.99254574e-01, 9.98828941e-01, 9.88935866e-01],
[ 9.96834344e-01, 9.96279483e-01, 4.28636430e-07,
9.96600181e-01, 9.47870932e-01, 9.95399154e-01,
9.96381458e-01, 9.96592822e-01, 9.96914826e-01,
9.93862938e-01, -9.68327765e-01, 9.96760950e-01,
9.96379221e-01, 9.96672585e-01, 9.96674621e-01,
9.95069722e-01, -9.40676110e-01, 9.96340223e-01,
9.96354345e-01, 9.94003174e-01, 9.96112855e-01,
9.96745892e-01, 9.95997518e-01, 9.89905446e-01],
[ 9.99572167e-01, 9.99197947e-01, 9.96600181e-01,
1.21966822e-09, 9.43053169e-01, 9.97880532e-01,
9.99500369e-01, 9.98410401e-01, 9.99173244e-01,
9.96775797e-01, -9.74367127e-01, 9.99362203e-01,
9.99367414e-01, 9.98297331e-01, 9.98880383e-01,
9.98663753e-01, -9.44650667e-01, 9.98960337e-01,
9.99311633e-01, 9.97876202e-01, 9.98462419e-01,
9.99263944e-01, 9.98810150e-01, 9.89492504e-01],
[ 9.42281243e-01, 9.41407791e-01, 9.47870932e-01,
9.43053169e-01, 5.87433525e-09, 9.47101615e-01,
9.43035090e-01, 9.51819965e-01, 9.46546373e-01,
9.35729856e-01, -8.83958006e-01, 9.43231419e-01,
9.39614529e-01, 9.50658526e-01, 9.44337124e-01,
9.41768971e-01, -8.57348147e-01, 9.46361648e-01,
9.43788045e-01, 9.35138095e-01, 9.43924294e-01,
9.46696871e-01, 9.46932752e-01, 9.74895278e-01],
[ 9.97900189e-01, 9.97899466e-01, 9.95399154e-01,
9.97880532e-01, 9.47101615e-01, 8.52516849e-08,
9.98162029e-01, 9.97693085e-01, 9.97494890e-01,
9.94870487e-01, -9.71281207e-01, 9.97825507e-01,
9.97609029e-01, 9.97078699e-01, 9.97937996e-01,
9.97886353e-01, -9.39807793e-01, 9.97323488e-01,
9.98086971e-01, 9.96890683e-01, 9.97306126e-01,
9.98328683e-01, 9.98019225e-01, 9.90401184e-01],
[ 9.99540337e-01, 9.99417552e-01, 9.96381458e-01,
9.99500369e-01, 9.43035090e-01, 9.98162029e-01,
1.97026096e-09, 9.98697236e-01, 9.99167466e-01,
9.96534267e-01, -9.74797106e-01, 9.99405117e-01,
9.99312797e-01, 9.98338095e-01, 9.99222108e-01,
9.99012700e-01, -9.45433385e-01, 9.99002384e-01,
9.99391574e-01, 9.98227196e-01, 9.98655866e-01,
9.99450287e-01, 9.98871183e-01, 9.89348747e-01],
[ 9.98601652e-01, 9.98836192e-01, 9.96592822e-01,
9.98410401e-01, 9.51819965e-01, 9.97693085e-01,
9.98697236e-01, 3.81563526e-10, 9.98687080e-01,
9.97121814e-01, -9.69119242e-01, 9.98787443e-01,
9.98289054e-01, 9.98866436e-01, 9.98833399e-01,
9.98412389e-01, -9.40737501e-01, 9.99081172e-01,
9.98609560e-01, 9.96732620e-01, 9.98290946e-01,
9.98808945e-01, 9.98467379e-01, 9.92893798e-01],
[ 9.99273813e-01, 9.99109838e-01, 9.96914826e-01,
9.99173244e-01, 9.46546373e-01, 9.97494890e-01,
9.99167466e-01, 9.98687080e-01, 1.63952743e-09,
9.97076275e-01, -9.71833154e-01, 9.99346179e-01,
9.99129781e-01, 9.98683650e-01, 9.99143749e-01,
9.98628992e-01, -9.42716179e-01, 9.99193054e-01,
9.99051762e-01, 9.97592295e-01, 9.98279757e-01,
9.99206541e-01, 9.98709951e-01, 9.91294961e-01],
[ 9.97018616e-01, 9.97212584e-01, 9.93862938e-01,
9.96775797e-01, 9.35729856e-01, 9.94870487e-01,
9.96534267e-01, 9.97121814e-01, 9.97076275e-01,
4.14508406e-09, -9.69602110e-01, 9.97209084e-01,
9.97269710e-01, 9.96130871e-01, 9.96598901e-01,
9.96945711e-01, -9.41737281e-01, 9.97547921e-01,
9.96623630e-01, 9.95604803e-01, 9.96053414e-01,
9.96252977e-01, 9.96293594e-01, 9.87014134e-01],
[-9.73801139e-01, -9.74070678e-01, -9.68327765e-01,
-9.74367127e-01, -8.83958006e-01, -9.71281207e-01,
-9.74797106e-01, -9.69119242e-01, -9.71833154e-01,
-9.69602110e-01, 7.91579754e-08, -9.72627284e-01,
-9.74397877e-01, -9.66816215e-01, -9.74055921e-01,
-9.72152413e-01, 9.49931280e-01, -9.70349967e-01,
-9.74543996e-01, -9.77042256e-01, -9.72510826e-01,
-9.72753685e-01, -9.70977647e-01, -9.49332577e-01],
[ 9.99572102e-01, 9.99506469e-01, 9.96760950e-01,
9.99362203e-01, 9.43231419e-01, 9.97825507e-01,
9.99405117e-01, 9.98787443e-01, 9.99346179e-01,
9.97209084e-01, -9.72627284e-01, 9.49759898e-10,
9.99396168e-01, 9.98616747e-01, 9.99231469e-01,
9.99092707e-01, -9.43690714e-01, 9.99236560e-01,
9.99125265e-01, 9.97720253e-01, 9.98657360e-01,
9.99322811e-01, 9.98924819e-01, 9.89982094e-01],
[ 9.99493833e-01, 9.99304970e-01, 9.96379221e-01,
9.99367414e-01, 9.39614529e-01, 9.97609029e-01,
9.99312797e-01, 9.98289054e-01, 9.99129781e-01,
9.97269710e-01, -9.74397877e-01, 9.99396168e-01,
6.10314496e-08, 9.98282510e-01, 9.98908676e-01,
9.99022688e-01, -9.44584256e-01, 9.99076968e-01,
9.99242844e-01, 9.98014936e-01, 9.98393894e-01,
9.99093343e-01, 9.98535127e-01, 9.88011557e-01],
[ 9.98516049e-01, 9.98532839e-01, 9.96672585e-01,
9.98297331e-01, 9.50658526e-01, 9.97078699e-01,
9.98338095e-01, 9.98866436e-01, 9.98683650e-01,
9.96130871e-01, -9.66816215e-01, 9.98616747e-01,
9.98282510e-01, 1.39566686e-09, 9.98517176e-01,
9.98377797e-01, -9.41181012e-01, 9.99085880e-01,
9.98417332e-01, 9.95895305e-01, 9.98089538e-01,
9.98786233e-01, 9.98273278e-01, 9.92133050e-01],
[ 9.99016459e-01, 9.99435799e-01, 9.96674621e-01,
9.98880383e-01, 9.44337124e-01, 9.97937996e-01,
9.99222108e-01, 9.98833399e-01, 9.99143749e-01,
9.96598901e-01, -9.74055921e-01, 9.99231469e-01,
9.98908676e-01, 9.98517176e-01, 3.91145914e-09,
9.98762801e-01, -9.44712685e-01, 9.98882295e-01,
9.99167722e-01, 9.98365735e-01, 9.98539526e-01,
9.99349674e-01, 9.98682774e-01, 9.90282263e-01],
[ 9.99032878e-01, 9.99191149e-01, 9.95069722e-01,
9.98663753e-01, 9.41768971e-01, 9.97886353e-01,
9.99012700e-01, 9.98412389e-01, 9.98628992e-01,
9.96945711e-01, -9.72152413e-01, 9.99092707e-01,
9.99022688e-01, 9.98377797e-01, 9.98762801e-01,
6.12438062e-09, -9.43278583e-01, 9.98714615e-01,
9.98765094e-01, 9.97518255e-01, 9.98234814e-01,
9.98833239e-01, 9.98556185e-01, 9.88600916e-01],
[-9.44718270e-01, -9.45834544e-01, -9.40676110e-01,
-9.44650667e-01, -8.57348147e-01, -9.39807793e-01,
-9.45433385e-01, -9.40737501e-01, -9.42716179e-01,
-9.41737281e-01, 9.49931280e-01, -9.43690714e-01,
-9.44584256e-01, -9.41181012e-01, -9.44712685e-01,
-9.43278583e-01, 1.85097555e-08, -9.42274581e-01,
-9.43930225e-01, -9.44416759e-01, -9.44123084e-01,
-9.43611879e-01, -9.43058469e-01, -9.20277725e-01],
[ 9.99102498e-01, 9.99087732e-01, 9.96340223e-01,
9.98960337e-01, 9.46361648e-01, 9.97323488e-01,
9.99002384e-01, 9.99081172e-01, 9.99193054e-01,
9.97547921e-01, -9.70349967e-01, 9.99236560e-01,
9.99076968e-01, 9.99085880e-01, 9.98882295e-01,
9.98714615e-01, -9.42274581e-01, 4.63172335e-10,
9.99055447e-01, 9.96678367e-01, 9.97947641e-01,
9.99023756e-01, 9.98655609e-01, 9.90680163e-01],
[ 9.99273586e-01, 9.99237110e-01, 9.96354345e-01,
9.99311633e-01, 9.43788045e-01, 9.98086971e-01,
9.99391574e-01, 9.98609560e-01, 9.99051762e-01,
9.96623630e-01, -9.74543996e-01, 9.99125265e-01,
9.99242844e-01, 9.98417332e-01, 9.99167722e-01,
9.98765094e-01, -9.43930225e-01, 9.99055447e-01,
1.49968419e-08, 9.98019738e-01, 9.98407433e-01,
9.99222857e-01, 9.98652394e-01, 9.89226980e-01],
[ 9.97964072e-01, 9.98261304e-01, 9.94003174e-01,
9.97876202e-01, 9.35138095e-01, 9.96890683e-01,
9.98227196e-01, 9.96732620e-01, 9.97592295e-01,
9.95604803e-01, -9.77042256e-01, 9.97720253e-01,
9.98014936e-01, 9.95895305e-01, 9.98365735e-01,
9.97518255e-01, -9.44416759e-01, 9.96678367e-01,
9.98019738e-01, 7.47650147e-10, 9.97617693e-01,
9.97870740e-01, 9.97367170e-01, 9.86577350e-01],
[ 9.98809893e-01, 9.98920402e-01, 9.96112855e-01,
9.98462419e-01, 9.43924294e-01, 9.97306126e-01,
9.98655866e-01, 9.98290946e-01, 9.98279757e-01,
9.96053414e-01, -9.72510826e-01, 9.98657360e-01,
9.98393894e-01, 9.98089538e-01, 9.98539526e-01,
9.98234814e-01, -9.44123084e-01, 9.97947641e-01,
9.98407433e-01, 9.97617693e-01, 9.94482237e-10,
9.98596112e-01, 9.98000736e-01, 9.89489744e-01],
[ 9.99306302e-01, 9.99254574e-01, 9.96745892e-01,
9.99263944e-01, 9.46696871e-01, 9.98328683e-01,
9.99450287e-01, 9.98808945e-01, 9.99206541e-01,
9.96252977e-01, -9.72753685e-01, 9.99322811e-01,
9.99093343e-01, 9.98786233e-01, 9.99349674e-01,
9.98833239e-01, -9.43611879e-01, 9.99023756e-01,
9.99222857e-01, 9.97870740e-01, 9.98596112e-01,
7.44695142e-09, 9.99016834e-01, 9.90734007e-01],
[ 9.98670142e-01, 9.98828941e-01, 9.95997518e-01,
9.98810150e-01, 9.46932752e-01, 9.98019225e-01,
9.98871183e-01, 9.98467379e-01, 9.98709951e-01,
9.96293594e-01, -9.70977647e-01, 9.98924819e-01,
9.98535127e-01, 9.98273278e-01, 9.98682774e-01,
9.98556185e-01, -9.43058469e-01, 9.98655609e-01,
9.98652394e-01, 9.97367170e-01, 9.98000736e-01,
9.99016834e-01, 1.79765233e-09, 9.91199840e-01],
[ 9.89075401e-01, 9.88935866e-01, 9.89905446e-01,
9.89492504e-01, 9.74895278e-01, 9.90401184e-01,
9.89348747e-01, 9.92893798e-01, 9.91294961e-01,
9.87014134e-01, -9.49332577e-01, 9.89982094e-01,
9.88011557e-01, 9.92133050e-01, 9.90282263e-01,
9.88600916e-01, -9.20277725e-01, 9.90680163e-01,
9.89226980e-01, 9.86577350e-01, 9.89489744e-01,
9.90734007e-01, 9.91199840e-01, 9.41720628e-08]]))
Algorithm Section
N = 24
model = GEKKO(remote=False)
wesg = model.Array(model.Var , N , value = 1/N , lb=0 , ub=1)
model.Equation = (sum(wesg) == 1)
def obj(rf = 0.02):
esg_portfo = 0
for i in range(N):
esg_portfo += model.Intermediate(wesg[i] * ((esg_scores_df["Score 2019"][i] + esg_scores_df["Score 2020"][i])/2))
return_portfo = 0
for i in range(N):
return_portfo += model.Intermediate(wesg[i]*((predicted_return_dataframe.iloc[0 , i] + r.iloc[0 ,i])/2))
sigma_portfo = 0
for i in range(N):
for j in range(N):
sigma_portfo += model.Intermediate(wesg[i] * ((sigma.iloc[i,j] + sigma_bar.iloc[i,j])/2) * wesg[j])
return -esg_portfo * ((return_portfo - rf)/sigma_portfo)
model.Minimize(obj())
model.solve(disp=False)
wesg = np.array( [item[0] for item in wesg], dtype=float)
print(wesg)
Result
The code results in:
[0.00000000e+00 1.31172076e-07 0.00000000e+00 0.00000000e+00
1.88847768e-01 1.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 1.00000000e+00 0.00000000e+00 0.00000000e+00
1.00000000e+00 0.00000000e+00 0.00000000e+00 1.00000000e+00
0.00000000e+00 0.00000000e+00 1.00000000e+00 1.00000000e+00
9.07168980e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00]
But, there are many ones in the result!! I have a constraint of sum(wesg) == 1! But the result indicates that somehow, Gekko doesn't consider the constraint. If you try print(wesg.sum()), then it will result:
7.096016879722077
Which I expected to be exact 1!
How can I fix this issue, and where is the problem? Any help would be appreciated.
The error is with model.Equation = (sum(wesg) == 1) that redefines the model.Equation function as False. Use this instead:
model.Equation(sum(wesg) == 1)
The new result enforces the constraint to add to one with sum(wesg)=1.000000000001.
[0. 0. 0. 0. 0.1425506 0.24959448
0. 0. 0. 0. 0. 0.0281217
0.32053308 0. 0. 0. 0. 0.
0.05860444 0.18554967 0.01504603 0. 0. 0. ]
Here are a couple additional suggestions and options:
For larger portfolios use model.sum() instead for better performance.
To select the top 3 stocks as integers (purchase or don't purchase) then use integer=True with the APOPT solver.
wesg = model.Array(model.Var, N, value = 1/N, lb=0, ub=1, integer=True)
model.Equation(model.sum(wesg) == 3)
model.options.SOLVER=1
Here is the complete code.
from gekko import GEKKO
import numpy as np
import pandas as pd
esg_scores_df = pd.DataFrame({'Score 2019': {'MSFT': 7,'PEP': 7,'TSLA': 4,'AMZN': 6,'LKQ': 3,'ABMD': 3,'MSI': 10,'PH': 8,'NKE': 6,'TM': 7,'EOG': 7,'GOOGL': 5,'NFLX': 4,'GS': 6,'EQIX': 9,'EA': 7,'AAP': 6,'TEL': 9,'DG': 6,'EXR': 5,'MDLZ': 6,'FIS': 8,'CRL': 8,'RCL': 9},
'Score 2020': {'MSFT': 6,'PEP': 11,'TSLA': 4,'AMZN': 6,'LKQ': 4,'ABMD': 4,'MSI': 8,'PH': 8,'NKE': 6,'TM': 7,'EOG': 7,'GOOGL': 5,'NFLX': 3,'GS': 6,'EQIX': 9,'EA': 6,'AAP': 7,'TEL': 9,'DG': 6,'EXR': 4,'MDLZ': 6,'FIS': 8,'CRL': 8,'RCL': 7}})
predicted_return_dataframe = pd.DataFrame({'MSFT': 1.0982472257593677e-15,'PEP': 4.567069595849647e-09,'TSLA': 7.439258841202596e-10,'AMZN': 3.176883309676764e-07,'LKQ': 4.825709334830293e-05,'ABMD': 2.0608058642685837e-05,'MSI': -3.1789250961959136e-12,'PH': -2.257237871892785e-07,'NKE': -4.737530217609426e-07,'TM': 9.951932427172896e-07,'EOG': 1.184074639824261e-08,'GOOGL': -1.7027206923083418e-10,'NFLX': 2.948344729885545e-05,'GS': 1.458862302453713e-07,'EQIX': 4.620207503091301e-07,'EA': -1.297137640100873e-06,'AAP': -3.493153382990658e-07,'TEL': 8.202463568291799e-08,'DG': 2.743802312024829e-05,'EXR': 1.971994818465128e-11,'MDLZ': 1.578772827915881e-08,'FIS': 1.3314663987166631e-08,'CRL': 4.112083587242872e-05,'RCL': -1.470829206425942e-09} , index = [0])
r = pd.DataFrame({'MSFT': -0.029838842874655123,'PEP': -0.012265555866403165,'TSLA': -0.053599428210690636,'AMZN': -0.02614876342440773,'LKQ': -0.026658034482507052,'ABMD': -0.017274321994365294,'MSI': -0.018624198080929168,'PH': -0.02871952268633362,'NKE': -0.028820168231794074,'TM': -0.011208781085200492,'EOG': -0.04606883798249617,'GOOGL': -0.027983366801701926,'NFLX': -0.0070609860042608165,'GS': -0.02429261616692462,'EQIX': -0.015181566244607758,'EA': -0.012009295653284258,'AAP': -0.011979871239295382,'TEL': -0.021782414925879602,'DG': -0.009820085124020328,'EXR': -0.012233190959318829,'MDLZ': -0.015917631061436933,'FIS': -0.023877815976094417,'CRL': -0.020169902356185498,'RCL': -0.06831607178882856} , index = [0])
sigma = pd.DataFrame(np.array([[ 1.02188994e-04, 3.16399586e-05, 8.54243667e-05,
7.14148287e-05, 3.55313650e-05, 5.44796564e-05,
3.39128448e-05, 6.65003271e-05, 5.42932808e-05,
2.29333906e-05, 4.26407605e-05, 7.92791756e-05,
9.28910574e-05, 6.36770764e-05, 3.75024646e-05,
5.13720672e-05, 1.33646720e-05, 6.21865710e-05,
4.72888751e-05, 2.35546749e-06, 2.49700413e-05,
6.74503236e-05, 7.08948785e-05, 5.55096135e-05],
[ 3.16399586e-05, 6.47824823e-05, -2.57461009e-05,
1.81931187e-05, 2.11795049e-05, 1.01708683e-05,
3.33739243e-05, 1.23893822e-05, 2.74825418e-05,
1.35954411e-05, 2.37108310e-06, 2.34208728e-05,
1.96972793e-05, 1.09335123e-05, 4.26042775e-05,
2.76849308e-05, 6.01821651e-06, 8.04387109e-06,
3.10892437e-05, 2.79970364e-05, 3.70572613e-05,
3.08215527e-05, 2.57244480e-05, 8.72002781e-06],
[ 8.54243667e-05, -2.57461009e-05, 8.84138653e-04,
6.16478906e-05, -1.78063009e-05, 3.62712388e-05,
-6.15200573e-05, 8.63597848e-05, 2.12571109e-05,
3.21749681e-05, 6.54693912e-05, 7.95298035e-05,
1.53917874e-04, 8.06306277e-05, -5.27893372e-06,
2.60899474e-05, 4.29017379e-05, 6.13689353e-05,
2.30097086e-06, -4.45963115e-05, -2.83669072e-06,
2.95395217e-05, 4.53889239e-05, 3.11615533e-05],
[ 7.14148287e-05, 1.81931187e-05, 6.16478906e-05,
1.19357840e-04, 5.48763301e-05, 5.55161292e-05,
2.77288469e-05, 6.25231790e-05, 5.19494452e-05,
1.93193913e-05, 4.28946368e-05, 7.00210371e-05,
9.92756795e-05, 6.54630027e-05, 1.73935405e-05,
4.46819063e-05, 2.94199398e-05, 5.76749375e-05,
3.75634244e-05, -5.53847280e-06, 9.72193169e-06,
4.42756733e-05, 4.21469376e-05, 4.35344231e-05],
[ 3.55313650e-05, 2.11795049e-05, -1.78063009e-05,
5.48763301e-05, 3.49607174e-04, 1.58156275e-04,
-1.64565378e-05, 5.95602984e-05, 3.36138721e-05,
2.25095973e-05, 4.90144491e-05, 5.60207136e-05,
3.73119885e-05, 8.65010497e-05, -1.68774498e-05,
1.06928038e-05, 3.53732120e-05, 8.46071106e-05,
3.89200328e-05, -1.10740797e-05, 4.20238479e-06,
2.07371361e-05, 2.27640364e-05, 7.39017715e-05],
[ 5.44796564e-05, 1.01708683e-05, 3.62712388e-05,
5.55161292e-05, 1.58156275e-04, 1.12266763e-03,
2.49786917e-05, 5.60053839e-05, -2.57686022e-05,
3.00855419e-05, 2.09079416e-04, 1.21797841e-04,
3.80689080e-06, 1.17305259e-04, -4.95192897e-05,
5.56059321e-05, 2.87765760e-06, 1.13640096e-04,
-2.73690072e-05, 2.17458024e-05, -1.24009254e-05,
6.11287657e-05, 1.12849096e-04, 1.17234141e-04],
[ 3.39128448e-05, 3.33739243e-05, -6.15200573e-05,
2.77288469e-05, -1.64565378e-05, 2.49786917e-05,
1.84271573e-04, -2.99455473e-06, 3.56885701e-05,
1.53451236e-05, 3.20222217e-06, 3.00619247e-05,
5.17902613e-05, 1.69851832e-05, 7.75007583e-05,
7.49839347e-06, -3.78485373e-05, -1.28942631e-05,
4.36657573e-05, 3.97590833e-05, 4.04704969e-05,
4.05201790e-05, 4.01384618e-05, 4.82818631e-06],
[ 6.65003271e-05, 1.23893822e-05, 8.63597848e-05,
6.25231790e-05, 5.95602984e-05, 5.60053839e-05,
-2.99455473e-06, 2.86568704e-04, 6.55278686e-05,
4.08938883e-05, 1.18822482e-04, 9.32577766e-05,
7.92498191e-05, 1.38967662e-04, -1.64128182e-05,
4.16393158e-05, 1.06300676e-04, 1.31672888e-04,
6.64839109e-05, -5.14108669e-05, -1.03044572e-05,
1.59107041e-05, 7.65457600e-05, 1.31611693e-04],
[ 5.42932808e-05, 2.74825418e-05, 2.12571109e-05,
5.19494452e-05, 3.36138721e-05, -2.57686022e-05,
3.56885701e-05, 6.55278686e-05, 1.36374334e-04,
1.36781102e-05, 2.38544176e-05, 5.90104054e-05,
6.20754315e-05, 5.84082445e-05, 3.96422393e-05,
2.96064418e-05, 5.54456912e-05, 5.69396332e-05,
7.28867485e-05, 7.92870807e-06, 3.79528145e-05,
3.38036564e-05, 3.47015652e-05, 3.86545249e-05],
[ 2.29333906e-05, 1.35954411e-05, 3.21749681e-05,
1.93193913e-05, 2.25095973e-05, 3.00855419e-05,
1.53451236e-05, 4.08938883e-05, 1.36781102e-05,
5.30619573e-05, 4.42693706e-05, 2.92937393e-05,
1.15184525e-05, 3.04141154e-05, 1.11981894e-06,
1.29388215e-05, 1.41913521e-05, 2.37704063e-05,
1.27983719e-05, -3.53963791e-06, 5.41505031e-06,
1.21874870e-05, 2.04099948e-05, 2.89151466e-05],
[ 4.26407605e-05, 2.37108310e-06, 6.54693912e-05,
4.28946368e-05, 4.90144491e-05, 2.09079416e-04,
3.20222217e-06, 1.18822482e-04, 2.38544176e-05,
4.42693706e-05, 5.01946804e-04, 7.79572195e-05,
7.61563242e-05, 1.31597522e-04, -2.43789107e-05,
6.77962026e-05, 5.39745318e-05, 9.05223133e-05,
-1.88433584e-06, -1.63784490e-05, -1.04001201e-05,
1.79450881e-05, 5.33951974e-05, 1.07102393e-04],
[ 7.92791756e-05, 2.34208728e-05, 7.95298035e-05,
7.00210371e-05, 5.60207136e-05, 1.21797841e-04,
3.00619247e-05, 9.32577766e-05, 5.90104054e-05,
2.92937393e-05, 7.79572195e-05, 1.10688236e-04,
8.13294795e-05, 8.72981821e-05, 2.13737337e-05,
4.33439061e-05, 3.06090710e-05, 7.60853329e-05,
4.44363057e-05, 3.33458671e-06, 1.60908360e-05,
4.09067616e-05, 5.72051871e-05, 6.05239478e-05],
[ 9.28910574e-05, 1.96972793e-05, 1.53917874e-04,
9.92756795e-05, 3.73119885e-05, 3.80689080e-06,
5.17902613e-05, 7.92498191e-05, 6.20754315e-05,
1.15184525e-05, 7.61563242e-05, 8.13294795e-05,
3.77348891e-04, 8.99709029e-05, 2.58732673e-05,
8.24015922e-05, 2.77333081e-05, 4.74146019e-05,
3.76467363e-05, -5.60031564e-06, 2.25653918e-05,
6.22679966e-05, 3.92394407e-05, 6.47067147e-05],
[ 6.36770764e-05, 1.09335123e-05, 8.06306277e-05,
6.54630027e-05, 8.65010497e-05, 1.17305259e-04,
1.69851832e-05, 1.38967662e-04, 5.84082445e-05,
3.04141154e-05, 1.31597522e-04, 8.72981821e-05,
8.99709029e-05, 1.54802097e-04, -1.57072336e-05,
3.58129141e-05, 5.52667399e-05, 1.02065825e-04,
4.96942712e-05, -2.44193897e-05, -5.66188177e-06,
2.35884764e-05, 5.15584350e-05, 9.98376706e-05],
[ 3.75024646e-05, 4.26042775e-05, -5.27893372e-06,
1.73935405e-05, -1.68774498e-05, -4.95192897e-05,
7.75007583e-05, -1.64128182e-05, 3.96422393e-05,
1.11981894e-06, -2.43789107e-05, 2.13737337e-05,
2.58732673e-05, -1.57072336e-05, 1.54605175e-04,
1.07085192e-05, -1.14274871e-05, -1.26204468e-05,
2.50810750e-05, 5.60340845e-05, 4.60520336e-05,
4.29696767e-05, 5.33004299e-05, -1.56063893e-05],
[ 5.13720672e-05, 2.76849308e-05, 2.60899474e-05,
4.46819063e-05, 1.06928038e-05, 5.56059321e-05,
7.49839347e-06, 4.16393158e-05, 2.96064418e-05,
1.29388215e-05, 6.77962026e-05, 4.33439061e-05,
8.24015922e-05, 3.58129141e-05, 1.07085192e-05,
1.32544808e-04, 3.15561503e-05, 3.62681876e-05,
1.76503394e-05, 1.90982407e-07, 1.02992114e-05,
2.81341342e-05, 2.81970597e-05, 3.94413355e-05],
[ 1.33646720e-05, 6.01821651e-06, 4.29017379e-05,
2.94199398e-05, 3.53732120e-05, 2.87765760e-06,
-3.78485373e-05, 1.06300676e-04, 5.54456912e-05,
1.41913521e-05, 5.39745318e-05, 3.06090710e-05,
2.77333081e-05, 5.52667399e-05, -1.14274871e-05,
3.15561503e-05, 2.71861487e-04, 6.04272650e-05,
3.55256428e-05, -2.37123843e-05, -1.99844011e-05,
-2.02943319e-07, -4.12746174e-07, 6.80701129e-05],
[ 6.21865710e-05, 8.04387109e-06, 6.13689353e-05,
5.76749375e-05, 8.46071106e-05, 1.13640096e-04,
-1.28942631e-05, 1.31672888e-04, 5.69396332e-05,
2.37704063e-05, 9.05223133e-05, 7.60853329e-05,
4.74146019e-05, 1.02065825e-04, -1.26204468e-05,
3.62681876e-05, 6.04272650e-05, 1.67171047e-04,
5.06392219e-05, -1.02673299e-05, 1.64715372e-05,
2.50897861e-05, 5.81836032e-05, 1.00294572e-04],
[ 4.72888751e-05, 3.10892437e-05, 2.30097086e-06,
3.75634244e-05, 3.89200328e-05, -2.73690072e-05,
4.36657573e-05, 6.64839109e-05, 7.28867485e-05,
1.27983719e-05, -1.88433584e-06, 4.44363057e-05,
3.76467363e-05, 4.96942712e-05, 2.50810750e-05,
1.76503394e-05, 3.55256428e-05, 5.06392219e-05,
1.99771178e-04, 4.70710752e-06, 3.76160380e-05,
2.30014580e-05, 3.12554634e-05, 5.94948441e-06],
[ 2.35546749e-06, 2.79970364e-05, -4.45963115e-05,
-5.53847280e-06, -1.10740797e-05, 2.17458024e-05,
3.97590833e-05, -5.14108669e-05, 7.92870807e-06,
-3.53963791e-06, -1.63784490e-05, 3.33458671e-06,
-5.60031564e-06, -2.44193897e-05, 5.60340845e-05,
1.90982407e-07, -2.37123843e-05, -1.02673299e-05,
4.70710752e-06, 8.86508330e-05, 4.02039134e-05,
2.39025489e-05, -5.94660474e-06, -1.92301274e-05],
[ 2.49700413e-05, 3.70572613e-05, -2.83669072e-06,
9.72193169e-06, 4.20238479e-06, -1.24009254e-05,
4.04704969e-05, -1.03044572e-05, 3.79528145e-05,
5.41505031e-06, -1.04001201e-05, 1.60908360e-05,
2.25653918e-05, -5.66188177e-06, 4.60520336e-05,
1.02992114e-05, -1.99844011e-05, 1.64715372e-05,
3.76160380e-05, 4.02039134e-05, 8.23927778e-05,
3.21948208e-05, 3.50482953e-05, -1.97971783e-07],
[ 6.74503236e-05, 3.08215527e-05, 2.95395217e-05,
4.42756733e-05, 2.07371361e-05, 6.11287657e-05,
4.05201790e-05, 1.59107041e-05, 3.38036564e-05,
1.21874870e-05, 1.79450881e-05, 4.09067616e-05,
6.22679966e-05, 2.35884764e-05, 4.29696767e-05,
2.81341342e-05, -2.02943319e-07, 2.50897861e-05,
2.30014580e-05, 2.39025489e-05, 3.21948208e-05,
1.20680211e-04, 5.96710279e-05, 3.14411495e-05],
[ 7.08948785e-05, 2.57244480e-05, 4.53889239e-05,
4.21469376e-05, 2.27640364e-05, 1.12849096e-04,
4.01384618e-05, 7.65457600e-05, 3.47015652e-05,
2.04099948e-05, 5.33951974e-05, 5.72051871e-05,
3.92394407e-05, 5.15584350e-05, 5.33004299e-05,
2.81970597e-05, -4.12746174e-07, 5.81836032e-05,
3.12554634e-05, -5.94660474e-06, 3.50482953e-05,
5.96710279e-05, 1.65974972e-04, 5.42453625e-05],
[ 5.55096135e-05, 8.72002781e-06, 3.11615533e-05,
4.35344231e-05, 7.39017715e-05, 1.17234141e-04,
4.82818631e-06, 1.31611693e-04, 3.86545249e-05,
2.89151466e-05, 1.07102393e-04, 6.05239478e-05,
6.47067147e-05, 9.98376706e-05, -1.56063893e-05,
3.94413355e-05, 6.80701129e-05, 1.00294572e-04,
5.94948441e-06, -1.92301274e-05, -1.97971783e-07,
3.14411495e-05, 5.42453625e-05, 1.92061080e-04]]))
sigma_bar = pd.DataFrame(np.array([[ 1.26820873e-09, 9.99357360e-01, 9.96834344e-01,
9.99572167e-01, 9.42281243e-01, 9.97900189e-01,
9.99540337e-01, 9.98601652e-01, 9.99273813e-01,
9.97018616e-01, -9.73801139e-01, 9.99572102e-01,
9.99493833e-01, 9.98516049e-01, 9.99016459e-01,
9.99032878e-01, -9.44718270e-01, 9.99102498e-01,
9.99273586e-01, 9.97964072e-01, 9.98809893e-01,
9.99306302e-01, 9.98670142e-01, 9.89075401e-01],
[ 9.99357360e-01, 8.85756152e-04, 9.96279483e-01,
9.99197947e-01, 9.41407791e-01, 9.97899466e-01,
9.99417552e-01, 9.98836192e-01, 9.99109838e-01,
9.97212584e-01, -9.74070678e-01, 9.99506469e-01,
9.99304970e-01, 9.98532839e-01, 9.99435799e-01,
9.99191149e-01, -9.45834544e-01, 9.99087732e-01,
9.99237110e-01, 9.98261304e-01, 9.98920402e-01,
9.99254574e-01, 9.98828941e-01, 9.88935866e-01],
[ 9.96834344e-01, 9.96279483e-01, 4.28636430e-07,
9.96600181e-01, 9.47870932e-01, 9.95399154e-01,
9.96381458e-01, 9.96592822e-01, 9.96914826e-01,
9.93862938e-01, -9.68327765e-01, 9.96760950e-01,
9.96379221e-01, 9.96672585e-01, 9.96674621e-01,
9.95069722e-01, -9.40676110e-01, 9.96340223e-01,
9.96354345e-01, 9.94003174e-01, 9.96112855e-01,
9.96745892e-01, 9.95997518e-01, 9.89905446e-01],
[ 9.99572167e-01, 9.99197947e-01, 9.96600181e-01,
1.21966822e-09, 9.43053169e-01, 9.97880532e-01,
9.99500369e-01, 9.98410401e-01, 9.99173244e-01,
9.96775797e-01, -9.74367127e-01, 9.99362203e-01,
9.99367414e-01, 9.98297331e-01, 9.98880383e-01,
9.98663753e-01, -9.44650667e-01, 9.98960337e-01,
9.99311633e-01, 9.97876202e-01, 9.98462419e-01,
9.99263944e-01, 9.98810150e-01, 9.89492504e-01],
[ 9.42281243e-01, 9.41407791e-01, 9.47870932e-01,
9.43053169e-01, 5.87433525e-09, 9.47101615e-01,
9.43035090e-01, 9.51819965e-01, 9.46546373e-01,
9.35729856e-01, -8.83958006e-01, 9.43231419e-01,
9.39614529e-01, 9.50658526e-01, 9.44337124e-01,
9.41768971e-01, -8.57348147e-01, 9.46361648e-01,
9.43788045e-01, 9.35138095e-01, 9.43924294e-01,
9.46696871e-01, 9.46932752e-01, 9.74895278e-01],
[ 9.97900189e-01, 9.97899466e-01, 9.95399154e-01,
9.97880532e-01, 9.47101615e-01, 8.52516849e-08,
9.98162029e-01, 9.97693085e-01, 9.97494890e-01,
9.94870487e-01, -9.71281207e-01, 9.97825507e-01,
9.97609029e-01, 9.97078699e-01, 9.97937996e-01,
9.97886353e-01, -9.39807793e-01, 9.97323488e-01,
9.98086971e-01, 9.96890683e-01, 9.97306126e-01,
9.98328683e-01, 9.98019225e-01, 9.90401184e-01],
[ 9.99540337e-01, 9.99417552e-01, 9.96381458e-01,
9.99500369e-01, 9.43035090e-01, 9.98162029e-01,
1.97026096e-09, 9.98697236e-01, 9.99167466e-01,
9.96534267e-01, -9.74797106e-01, 9.99405117e-01,
9.99312797e-01, 9.98338095e-01, 9.99222108e-01,
9.99012700e-01, -9.45433385e-01, 9.99002384e-01,
9.99391574e-01, 9.98227196e-01, 9.98655866e-01,
9.99450287e-01, 9.98871183e-01, 9.89348747e-01],
[ 9.98601652e-01, 9.98836192e-01, 9.96592822e-01,
9.98410401e-01, 9.51819965e-01, 9.97693085e-01,
9.98697236e-01, 3.81563526e-10, 9.98687080e-01,
9.97121814e-01, -9.69119242e-01, 9.98787443e-01,
9.98289054e-01, 9.98866436e-01, 9.98833399e-01,
9.98412389e-01, -9.40737501e-01, 9.99081172e-01,
9.98609560e-01, 9.96732620e-01, 9.98290946e-01,
9.98808945e-01, 9.98467379e-01, 9.92893798e-01],
[ 9.99273813e-01, 9.99109838e-01, 9.96914826e-01,
9.99173244e-01, 9.46546373e-01, 9.97494890e-01,
9.99167466e-01, 9.98687080e-01, 1.63952743e-09,
9.97076275e-01, -9.71833154e-01, 9.99346179e-01,
9.99129781e-01, 9.98683650e-01, 9.99143749e-01,
9.98628992e-01, -9.42716179e-01, 9.99193054e-01,
9.99051762e-01, 9.97592295e-01, 9.98279757e-01,
9.99206541e-01, 9.98709951e-01, 9.91294961e-01],
[ 9.97018616e-01, 9.97212584e-01, 9.93862938e-01,
9.96775797e-01, 9.35729856e-01, 9.94870487e-01,
9.96534267e-01, 9.97121814e-01, 9.97076275e-01,
4.14508406e-09, -9.69602110e-01, 9.97209084e-01,
9.97269710e-01, 9.96130871e-01, 9.96598901e-01,
9.96945711e-01, -9.41737281e-01, 9.97547921e-01,
9.96623630e-01, 9.95604803e-01, 9.96053414e-01,
9.96252977e-01, 9.96293594e-01, 9.87014134e-01],
[-9.73801139e-01, -9.74070678e-01, -9.68327765e-01,
-9.74367127e-01, -8.83958006e-01, -9.71281207e-01,
-9.74797106e-01, -9.69119242e-01, -9.71833154e-01,
-9.69602110e-01, 7.91579754e-08, -9.72627284e-01,
-9.74397877e-01, -9.66816215e-01, -9.74055921e-01,
-9.72152413e-01, 9.49931280e-01, -9.70349967e-01,
-9.74543996e-01, -9.77042256e-01, -9.72510826e-01,
-9.72753685e-01, -9.70977647e-01, -9.49332577e-01],
[ 9.99572102e-01, 9.99506469e-01, 9.96760950e-01,
9.99362203e-01, 9.43231419e-01, 9.97825507e-01,
9.99405117e-01, 9.98787443e-01, 9.99346179e-01,
9.97209084e-01, -9.72627284e-01, 9.49759898e-10,
9.99396168e-01, 9.98616747e-01, 9.99231469e-01,
9.99092707e-01, -9.43690714e-01, 9.99236560e-01,
9.99125265e-01, 9.97720253e-01, 9.98657360e-01,
9.99322811e-01, 9.98924819e-01, 9.89982094e-01],
[ 9.99493833e-01, 9.99304970e-01, 9.96379221e-01,
9.99367414e-01, 9.39614529e-01, 9.97609029e-01,
9.99312797e-01, 9.98289054e-01, 9.99129781e-01,
9.97269710e-01, -9.74397877e-01, 9.99396168e-01,
6.10314496e-08, 9.98282510e-01, 9.98908676e-01,
9.99022688e-01, -9.44584256e-01, 9.99076968e-01,
9.99242844e-01, 9.98014936e-01, 9.98393894e-01,
9.99093343e-01, 9.98535127e-01, 9.88011557e-01],
[ 9.98516049e-01, 9.98532839e-01, 9.96672585e-01,
9.98297331e-01, 9.50658526e-01, 9.97078699e-01,
9.98338095e-01, 9.98866436e-01, 9.98683650e-01,
9.96130871e-01, -9.66816215e-01, 9.98616747e-01,
9.98282510e-01, 1.39566686e-09, 9.98517176e-01,
9.98377797e-01, -9.41181012e-01, 9.99085880e-01,
9.98417332e-01, 9.95895305e-01, 9.98089538e-01,
9.98786233e-01, 9.98273278e-01, 9.92133050e-01],
[ 9.99016459e-01, 9.99435799e-01, 9.96674621e-01,
9.98880383e-01, 9.44337124e-01, 9.97937996e-01,
9.99222108e-01, 9.98833399e-01, 9.99143749e-01,
9.96598901e-01, -9.74055921e-01, 9.99231469e-01,
9.98908676e-01, 9.98517176e-01, 3.91145914e-09,
9.98762801e-01, -9.44712685e-01, 9.98882295e-01,
9.99167722e-01, 9.98365735e-01, 9.98539526e-01,
9.99349674e-01, 9.98682774e-01, 9.90282263e-01],
[ 9.99032878e-01, 9.99191149e-01, 9.95069722e-01,
9.98663753e-01, 9.41768971e-01, 9.97886353e-01,
9.99012700e-01, 9.98412389e-01, 9.98628992e-01,
9.96945711e-01, -9.72152413e-01, 9.99092707e-01,
9.99022688e-01, 9.98377797e-01, 9.98762801e-01,
6.12438062e-09, -9.43278583e-01, 9.98714615e-01,
9.98765094e-01, 9.97518255e-01, 9.98234814e-01,
9.98833239e-01, 9.98556185e-01, 9.88600916e-01],
[-9.44718270e-01, -9.45834544e-01, -9.40676110e-01,
-9.44650667e-01, -8.57348147e-01, -9.39807793e-01,
-9.45433385e-01, -9.40737501e-01, -9.42716179e-01,
-9.41737281e-01, 9.49931280e-01, -9.43690714e-01,
-9.44584256e-01, -9.41181012e-01, -9.44712685e-01,
-9.43278583e-01, 1.85097555e-08, -9.42274581e-01,
-9.43930225e-01, -9.44416759e-01, -9.44123084e-01,
-9.43611879e-01, -9.43058469e-01, -9.20277725e-01],
[ 9.99102498e-01, 9.99087732e-01, 9.96340223e-01,
9.98960337e-01, 9.46361648e-01, 9.97323488e-01,
9.99002384e-01, 9.99081172e-01, 9.99193054e-01,
9.97547921e-01, -9.70349967e-01, 9.99236560e-01,
9.99076968e-01, 9.99085880e-01, 9.98882295e-01,
9.98714615e-01, -9.42274581e-01, 4.63172335e-10,
9.99055447e-01, 9.96678367e-01, 9.97947641e-01,
9.99023756e-01, 9.98655609e-01, 9.90680163e-01],
[ 9.99273586e-01, 9.99237110e-01, 9.96354345e-01,
9.99311633e-01, 9.43788045e-01, 9.98086971e-01,
9.99391574e-01, 9.98609560e-01, 9.99051762e-01,
9.96623630e-01, -9.74543996e-01, 9.99125265e-01,
9.99242844e-01, 9.98417332e-01, 9.99167722e-01,
9.98765094e-01, -9.43930225e-01, 9.99055447e-01,
1.49968419e-08, 9.98019738e-01, 9.98407433e-01,
9.99222857e-01, 9.98652394e-01, 9.89226980e-01],
[ 9.97964072e-01, 9.98261304e-01, 9.94003174e-01,
9.97876202e-01, 9.35138095e-01, 9.96890683e-01,
9.98227196e-01, 9.96732620e-01, 9.97592295e-01,
9.95604803e-01, -9.77042256e-01, 9.97720253e-01,
9.98014936e-01, 9.95895305e-01, 9.98365735e-01,
9.97518255e-01, -9.44416759e-01, 9.96678367e-01,
9.98019738e-01, 7.47650147e-10, 9.97617693e-01,
9.97870740e-01, 9.97367170e-01, 9.86577350e-01],
[ 9.98809893e-01, 9.98920402e-01, 9.96112855e-01,
9.98462419e-01, 9.43924294e-01, 9.97306126e-01,
9.98655866e-01, 9.98290946e-01, 9.98279757e-01,
9.96053414e-01, -9.72510826e-01, 9.98657360e-01,
9.98393894e-01, 9.98089538e-01, 9.98539526e-01,
9.98234814e-01, -9.44123084e-01, 9.97947641e-01,
9.98407433e-01, 9.97617693e-01, 9.94482237e-10,
9.98596112e-01, 9.98000736e-01, 9.89489744e-01],
[ 9.99306302e-01, 9.99254574e-01, 9.96745892e-01,
9.99263944e-01, 9.46696871e-01, 9.98328683e-01,
9.99450287e-01, 9.98808945e-01, 9.99206541e-01,
9.96252977e-01, -9.72753685e-01, 9.99322811e-01,
9.99093343e-01, 9.98786233e-01, 9.99349674e-01,
9.98833239e-01, -9.43611879e-01, 9.99023756e-01,
9.99222857e-01, 9.97870740e-01, 9.98596112e-01,
7.44695142e-09, 9.99016834e-01, 9.90734007e-01],
[ 9.98670142e-01, 9.98828941e-01, 9.95997518e-01,
9.98810150e-01, 9.46932752e-01, 9.98019225e-01,
9.98871183e-01, 9.98467379e-01, 9.98709951e-01,
9.96293594e-01, -9.70977647e-01, 9.98924819e-01,
9.98535127e-01, 9.98273278e-01, 9.98682774e-01,
9.98556185e-01, -9.43058469e-01, 9.98655609e-01,
9.98652394e-01, 9.97367170e-01, 9.98000736e-01,
9.99016834e-01, 1.79765233e-09, 9.91199840e-01],
[ 9.89075401e-01, 9.88935866e-01, 9.89905446e-01,
9.89492504e-01, 9.74895278e-01, 9.90401184e-01,
9.89348747e-01, 9.92893798e-01, 9.91294961e-01,
9.87014134e-01, -9.49332577e-01, 9.89982094e-01,
9.88011557e-01, 9.92133050e-01, 9.90282263e-01,
9.88600916e-01, -9.20277725e-01, 9.90680163e-01,
9.89226980e-01, 9.86577350e-01, 9.89489744e-01,
9.90734007e-01, 9.91199840e-01, 9.41720628e-08]]))
N = 24
model = GEKKO(remote=False)
wesg = model.Array(model.Var, N, value = 1/N, lb=0, ub=1)
model.Equation(m.sum(wesg) == 1)
def obj(rf = 0.02):
esg_portfo = 0
for i in range(N):
esg_portfo += model.Intermediate(wesg[i] * ((esg_scores_df["Score 2019"][i] + esg_scores_df["Score 2020"][i])/2))
return_portfo = 0
for i in range(N):
return_portfo += model.Intermediate(wesg[i]*((predicted_return_dataframe.iloc[0 , i] + r.iloc[0 ,i])/2))
sigma_portfo = 0
for i in range(N):
for j in range(N):
sigma_portfo += model.Intermediate(wesg[i] * ((sigma.iloc[i,j] + sigma_bar.iloc[i,j])/2) * wesg[j])
return -esg_portfo * ((return_portfo - rf)/sigma_portfo)
model.Minimize(obj())
model.solve(disp=True)
wesg = np.array( [item[0] for item in wesg], dtype=float)
print(wesg)
print(sum(wesg))
Thanks for the well-written question and for sharing the application.

How to visualize high-dimension vectors as points in 2D plane?

For example, there are three vectors as below.
[ 0.0377, 0.1808, 0.0807, -0.0703, 0.2427, -0.1957, -0.0712, -0.2137,
-0.0754, -0.1200, 0.1919, 0.0373, 0.0536, 0.0887, -0.1916, -0.1268,
-0.1910, -0.1411, -0.1282, 0.0274, -0.0781, 0.0138, -0.0654, 0.0491,
0.0398, 0.1696, 0.0365, 0.2266, 0.1241, 0.0176, 0.0881, 0.2993,
-0.1425, -0.2535, 0.1801, -0.1188, 0.1251, 0.1840, 0.1112, 0.3172,
0.0844, -0.1142, 0.0662, 0.0910, 0.0416, 0.2104, 0.0781, -0.0348,
-0.1488, 0.0129],
[-0.1302, 0.1581, -0.0897, 0.1024, -0.1133, 0.1076, 0.1595, -0.1047,
0.0760, 0.1092, 0.0062, -0.1567, -0.1448, -0.0548, -0.1275, -0.0689,
-0.1293, 0.1024, 0.1615, 0.0869, 0.2906, -0.2056, 0.0442, -0.0595,
-0.1448, 0.0167, -0.1259, -0.0989, 0.0651, -0.0424, 0.0795, -0.1546,
0.1330, -0.2284, 0.1672, 0.1847, 0.0841, 0.1771, -0.0101, -0.0681,
0.1497, 0.1226, 0.1146, -0.2090, 0.3275, 0.0981, -0.3295, 0.0590,
0.1130, -0.0650],
[-0.1745, -0.1940, -0.1529, -0.0964, 0.2657, -0.0979, 0.1510, -0.1248,
-0.1541, 0.1782, -0.1769, -0.2335, 0.2011, 0.1906, -0.1918, 0.1896,
-0.2183, -0.1543, 0.1816, 0.1684, -0.1318, 0.2285, 0.1784, 0.2260,
-0.2331, 0.0523, 0.1882, 0.1764, -0.1686, 0.2292]
How to plot them as three points in the same 2D plane like this picture below? Thanks!
I use PCA from sklearn, maybe this code help you:
import matplotlib.pyplot as plt
import numpy as np
from sklearn.decomposition import PCA
usa = [ 0.0377, 0.1808, 0.0807, -0.0703, 0.2427, -0.1957, -0.0712, -0.2137,
-0.0754, -0.1200, 0.1919, 0.0373, 0.0536, 0.0887, -0.1916, -0.1268,
-0.1910, -0.1411, -0.1282, 0.0274, -0.0781, 0.0138, -0.0654, 0.0491,
0.0398, 0.1696, 0.0365, 0.2266, 0.1241, 0.0176, 0.0881, 0.2993,
-0.1425, -0.2535, 0.1801, -0.1188, 0.1251, 0.1840, 0.1112, 0.3172,
0.0844, -0.1142, 0.0662, 0.0910, 0.0416, 0.2104, 0.0781, -0.0348,
-0.1488, 0.0129]
obama = [-0.1302, 0.1581, -0.0897, 0.1024, -0.1133, 0.1076, 0.1595, -0.1047,
0.0760, 0.1092, 0.0062, -0.1567, -0.1448, -0.0548, -0.1275, -0.0689,
-0.1293, 0.1024, 0.1615, 0.0869, 0.2906, -0.2056, 0.0442, -0.0595,
-0.1448, 0.0167, -0.1259, -0.0989, 0.0651, -0.0424, 0.0795, -0.1546,
0.1330, -0.2284, 0.1672, 0.1847, 0.0841, 0.1771, -0.0101, -0.0681,
0.1497, 0.1226, 0.1146, -0.2090, 0.3275, 0.0981, -0.3295, 0.0590,
0.1130, -0.0650]
nationality = [-0.1745, -0.1940, -0.1529, -0.0964, 0.2657, -0.0979, 0.1510, -0.1248,
-0.1541, 0.1782, -0.1769, -0.2335, 0.2011, 0.1906, -0.1918, 0.1896,
-0.2183, -0.1543, 0.1816, 0.1684, -0.1318, 0.2285, 0.1784, 0.2260,
-0.2331, 0.0523, 0.1882, 0.1764, -0.1686, 0.2292]
pca = PCA(n_components=1)
X = np.array(usa).reshape(2,len(usa)//2)
X = pca.fit_transform(X)
Y = np.array(obama).reshape(2,len(obama)//2)
Y = pca.fit_transform(Y)
Z = np.array(nationality).reshape(2,len(nationality)//2)
Z = pca.fit_transform(Z)
x_coordinates = [X[0][0], Y[0][0], Z[0][0]]
y_coordinates = [X[1][0], Y[1][0], Z[1][0]]
colors = ['r','g','b']
annotations=["U.S.A","Obama","Nationality"]
plt.figure(figsize=(8,6))
plt.scatter(x_coordinates, y_coordinates, marker=",", color=colors,s=300)
for i, label in enumerate(annotations):
plt.annotate(label, (x_coordinates[i], y_coordinates[i]))
plt.show()
output:

Convert a for loop from MATLAB into Python

I have tried to convert a MATLAB code into Python but not getting the appropriate results. The results of MATLAB are right but the results of Python code are wrong.
My MATLAB code is:
xc =[27.6851 27.6573 27.6358 27.6499 27.6448 27.6435 27.6480 27.6418 27.6556 27.6506 27.6066 27.6004
27.5988 27.5939 27.5892 27.5755 27.5774 27.5968 27.5907 27.5861 27.5776 27.5554 27.5509 27.5210 27.5094
27.5109 27.4952 27.4777 27.4617 27.4298 27.4275 27.4186 27.4338 27.4247 27.4132 27.4111 27.4014 27.3751
27.3411 27.3276 27.3043 27.2914 27.2840 27.2216 27.1806 27.1619 27.1599 27.1727 27.1644 27.1724 27.1722
27.1741 27.1807 27.1814 27.1791 27.1720 27.1653 27.1621 27.1590 27.1598 27.1645 27.1587 27.1500 27.1573
27.1635 27.1605 27.1503 27.1489 27.1509 27.1503 27.1454 27.1167 27.0813 27.0524 27.0040 26.9938 26.9731
26.9935 26.9870 26.9876 26.9887 26.9920 26.9851 26.9736 26.9906 27.0207 27.0270 27.0220 27.0224 27.0241
27.0213 27.0152 27.0182 27.0235 27.0431 27.0516 27.0493 27.0405 27.0424 27.0383 27.0353 27.0293 27.0288
27.0333 27.0403 27.0421 27.0449 27.0723 27.1126 27.1903 27.2248 27.2522 27.2646 27.2721 27.2726 27.2959
27.3411 27.3833 27.4242 27.4889 27.5974 27.6362 28.0603];
yc = [22.7222 22.7192 22.6895 22.6662 22.6459 22.6377 22.6498 22.6579 22.6530 22.6664 22.6903 22.6904
22.7133 22.7365 22.7414 22.7678 22.7831 22.7956 22.8158 22.8497 22.8633 22.8916 22.9066 22.9493
22.9893 23.0000 23.0118 23.0371 23.0545 23.0857 23.0978 23.1105 23.1083 23.1255 23.1350 23.1422
23.1623 23.1828 23.2156 23.2344 23.2622 23.2705 23.2906 23.3335 23.4233 23.4887 23.5721 23.6112
23.6368 23.6530 23.6813 23.7030 23.7298 23.7612 23.7900 23.8237 23.8459 23.8881 23.9266 23.9494
23.9866 24.0173 24.0344 24.0827 24.1190 24.1501 24.1796 24.1883 24.1985 24.2336 24.3291 24.3946
24.4475 24.4868 24.5159 24.5969 24.6645 24.7714 24.8320 24.8987 25.0400 25.1576 25.2470 25.3148
25.4278 25.5148 25.5629 25.5927 25.6133 25.6391 25.6453 25.6630 25.6738 25.6874 25.7278 25.7371
25.7590 25.7740 25.7956 25.8046 25.8755 25.8226 25.8929 25.8323 25.8631 25.7839 25.7516 25.7103
25.7021 25.6988 25.7069 25.7092 25.7137 25.7180 25.7247 25.6927 25.7097 25.7337 25.7497 25.7821
25.8509 25.9075 25.6166];
idx_start = [1,5,9,100,105,1];
idx_end = [5,9,100,105,123,123];
Length_idx=idx_end-idx_start+1;
for i=1:length(idx_start)
if Length_idx(i)>=12 %Minimum number of points to calculate MSD
for j=1:Length_idx(i)
msd(j)=mean((xc(idx_start(i)+j:1:idx_end(i))-xc(idx_start(i):1:idx_end(i)-j)).^2 + (yc(idx_start(i)+j:1:idx_end(i))-yc(idx_start(i):1:idx_end(i)-j)).^2 );
end
end
end
msd
I have tried to convert MATLAB code into Python. My Python code is:
import numpy as np
x = np.array([[27.685125],[27.657279],[27.635751],[27.649908],[27.64476 ],[27.643473],[27.648036],[27.641835],[27.655641],[27.65061 ],[27.606618],[27.600417],
[27.598779],[27.593865],[27.589185],[27.575496],[27.577368],[27.59679 ],[27.590706],[27.586143],[27.577602],[27.555372],[27.550926],[27.520974],
[27.509391],[27.510912],[27.495234],[27.477684],[27.461655],[27.429831],[27.427491],[27.418599],[27.433809],[27.424683],[27.413217],[27.411111],
[27.4014 ],[27.375075],[27.341145],[27.327573],[27.30429 ],[27.29142 ],[27.284049],[27.221571],[27.180621],[27.161901],[27.159912],[27.172665],
[27.164358],[27.172431],[27.172197],[27.174069],[27.180738],[27.18144 ],[27.1791 ],[27.171963],[27.165294],[27.162135],[27.158976],[27.159795],
[27.164475],[27.158742],[27.149967],[27.157338],[27.163539],[27.160497],[27.150318],[27.148914],[27.150903],[27.150318],[27.145404],[27.116739],
[27.081288],[27.052389],[27.003951],[26.993772],[26.973063],[26.993538],[26.986986],[26.987571],[26.988741],[26.992017],[26.985114],[26.973648],
[26.990613],[27.020682],[27.027 ],[27.021969],[27.022437],[27.024075],[27.021267],[27.015183],[27.018225],[27.02349 ],[27.043146],[27.05157 ],
[27.049347],[27.040455],[27.042444],[27.038349],[27.035307],[27.02934 ],[27.028755],[27.033318],[27.040338],[27.042093],[27.044901],[27.072279],
[27.112644],[27.190332],[27.224847],[27.252225],[27.264627],[27.272115],[27.272583],[27.295866],[27.341145],[27.383265],[27.424215],[27.488916],
[27.597375],[27.636219],[28.060344]])
y = np.array([[22.722219],[22.719177],[22.689459],[22.666176],[22.645935],[22.637745],[22.649796],[22.657869],[22.652955],[22.66641 ],[22.690278],[22.690395],
[22.713327],[22.736493],[22.741407],[22.767849],[22.783059],[22.795578],[22.815819],[22.849749],[22.863321],[22.891635],[22.906611],[22.949316],
[22.98933 ],[22.999977],[23.011794],[23.037066],[23.054499],[23.085738],[23.097789],[23.110542],[23.108319],[23.125518],[23.134995],[23.142249],
[23.162256],[23.182848],[23.215608],[23.234445],[23.262174],[23.270481],[23.290605],[23.333544],[23.423283],[23.488686],[23.572107],[23.611185],
[23.636808],[23.652954],[23.681268],[23.70303 ],[23.729823],[23.761179],[23.789961],[23.823657],[23.845887],[23.888124],[23.926617],[23.949432],
[23.986638],[24.017292],[24.034374],[24.082695],[24.118965],[24.150087],[24.179571],[24.188346],[24.198525],[24.233625],[24.329097],[24.394617],
[24.447501],[24.486813],[24.515946],[24.59691 ],[24.664536],[24.771357],[24.831963],[24.898653],[25.039989],[25.157574],[25.246962],[25.314822],
[25.427844],[25.514775],[25.562862],[25.592697],[25.613289],[25.639146],[25.645347],[25.663014],[25.673778],[25.68735 ],[25.727832],[25.737075],
[25.758954],[25.774047],[25.795575],[25.804584],[25.875486],[25.822602],[25.892919],[25.832313],[25.863084],[25.783875],[25.751583],[25.710282],
[25.702092],[25.698816],[25.706889],[25.709229],[25.713675],[25.718004],[25.724673],[25.692732],[25.709697],[25.733682],[25.749711],[25.78212 ],
[25.850916],[25.907544],[25.616565]])
idx_start = np.array([1,5,9,100,105,1])
idx_end = np.array([5,9,100,105,123,123])
length_idx = idx_end - idx_start + 1
for i in range(0, len(idx_start)):
msd = []
if length_idx[i] >= 12 :
for j in range(1, length_idx[i] + 1):
t1 = np.power(x[idx_start[i] + j: idx_end[i] , :] -
x[idx_start[i]: idx_end[i] - j , :], 2)
t2 = np.power(y[idx_start[i] + j: idx_end[i] , :] -
y[idx_start[i]: idx_end[i] - j , :], 2)
msd.append(np.mean(t1 + t2, axis=0)[0])
my_formatted_list = [ '%.4f' % elem for elem in msd ]
print(my_formatted_list)
where x and y are column vectors of shape (123,1)
Expected output is:
0.0045 0.0101 0.0195 0.0313 0.0457 0.0626
0.0820 0.1037 0.1279 0.1547 0.1843 0.2165
0.2510 0.2881 0.3269 0.3679 0.4110 0.4571
0.5054 0.5567 0.6098 0.6659 0.7240 0.7849
0.8483 0.9145 0.9835 1.0555 1.1301 1.2071
1.2871 1.3702 1.4570 1.5474 1.6413 1.7388
1.8401 1.9448 2.0513 2.1604 2.2724 2.3870
2.5028 2.6187 2.7348 2.8518 2.9672 3.0842
3.2018 3.3232 3.4471 3.5734 3.7012 3.8291
3.9595 4.0931 4.2304 4.3702 4.5125 4.6570
4.8028 4.9518 5.1040 5.2577 5.4142 5.5726
5.7326 5.8968 6.0646 6.2351 6.4043 6.5733
6.7430 6.9142 7.0873 7.2593 7.4274 7.5850
7.7358 7.8770 8.0065 8.1259 8.2378 8.3426
8.4342 8.5148 8.5897 8.6610 8.7312 8.7994
8.8684 8.9370 9.0059 9.0730 9.1317 9.1890
9.2402 9.2890 9.3325 9.3662 9.3730 9.3910
9.3818 9.3880 9.3741 9.3820 9.3972 9.4253
9.4538 9.4908 9.5201 9.5433 9.5653 9.5774
9.5797 9.6126 9.6359 9.6288 9.6105 9.5703
9.3530 8.5180 NaN
But I am getting this:
['0.0045', '0.0102', '0.0196', '0.0315', '0.0460', '0.0631',
'0.0826', '0.1046', '0.1289', '0.1560', '0.1859', '0.2184',
'0.2533', '0.2907', '0.3298', '0.3712', '0.4148', '0.4613',
'0.5101', '0.5619', '0.6154', '0.6720', '0.7305', '0.7919',
'0.8560', '0.9228', '0.9923', '1.0650', '1.1401', '1.2180',
'1.2988', '1.3830', '1.4708', '1.5622', '1.6573', '1.7558',
'1.8581', '1.9636', '2.0714', '2.1814', '2.2948', '2.4108',
'2.5270', '2.6427', '2.7591', '2.8762', '2.9928', '3.1109',
'3.2302', '3.3529', '3.4784', '3.6063', '3.7356', '3.8649',
'3.9966', '4.1318', '4.2704', '4.4115', '4.5559', '4.7019',
'4.8495', '5.0009', '5.1545', '5.3101', '5.4686', '5.6290',
'5.7925', '5.9603', '6.1306', '6.2997', '6.4687', '6.6380',
'6.8089', '6.9816', '7.1532', '7.3241', '7.4885', '7.6452',
'7.7942', '7.9251', '8.0454', '8.1575', '8.2637', '8.3568',
'8.4395', '8.5151', '8.5871', '8.6570', '8.7247', '8.7936',
'8.8610', '8.9297', '8.9982', '9.0597', '9.1184', '9.1722',
'9.2206', '9.2653', '9.3070', '9.3208', '9.3411', '9.3363',
'9.3441', '9.3384', '9.3498', '9.3692', '9.4034', '9.4409',
'9.4806', '9.5217', '9.5574', '9.5851', '9.6120', '9.6265',
'9.6660', '9.7075', '9.7310', '9.7276', '9.6802', '9.4572',
'8.5573', 'nan', 'nan']

Having issue when reading binary data of float

I want to use os module specifically to handle read/write binary files. I have an issue when reading values of data type that takes more than 1 byte such as int64, float32, ... etc. To illustrate my issue, let's see the following example I wrote. I generate random values of type np.float64 which is 8 byte each:
# Write
n = 10
dim = 2
fd = os.open('test.dat', os.O_CREAT | os.O_WRONLY)
data_w = np.random.uniform(low=0.5, high=13.3, size=(n,dim)).astype(np.float64)
print("Written Data are:\n%s\n" % data_w)
os.write(fd, data_w.tobytes())
os.close(fd)
print("------------------ \n")
# Read
start_read = 0 # 0 for now. Later I can read from any row!
total_num_to_read = n*dim
fd = os.open('test.dat', os.O_RDONLY)
os.lseek(fd, start_read, 0) # start_read from the beginning 0
raw_data = os.read(fd, total_num_to_read) # How many values to be read
data_r = np.fromiter(raw_data, dtype=np.float64).reshape(-1, dim)
print("Data Read are:\n%s\n" % data_r)
os.close(fd)
The reading is not correct. Look how it is returned:
Written Data are:
[[ 2.75763292 9.87883101]
[ 1.73752327 9.9633879 ]
[ 1.01616811 1.81174597]
[ 9.93904659 10.6757686 ]
[ 7.02452029 2.68652109]
[ 5.29766028 11.15384409]
[ 4.12499766 10.37214532]
[11.75811252 3.30378401]
[ 1.72738203 2.11228277]
[ 7.7321937 11.64298051]]
------------------
Data Read are:
[[250. 87.]
[227. 216.]
[161. 15.]
[ 6. 64.]
[162. 178.]
[ 59. 35.]
[246. 193.]
[ 35. 64.]
[218. 97.]
[ 81. 50.]]
I cannot retrieve it correctly! I thought np.fromiter(raw_data, dtype=np.float64).reshape(-1, dim) is supposed to take care of it but I don't know where the issue is. How can I read binary data in this case given that I know it is of particular data type (i.e., np.float64)?
You should use np.fromstring(raw_data) instead of fromiter(). Check documentation for the purpose of each function. In addition, when reading from file, read the correct number of bytes!!!: 8* total_num_to_read.
In [103]: # Write
...: n = 10
...: dim = 2
...: fd = os.open('test.dat', os.O_CREAT | os.O_WRONLY)
...: data_w = np.random.uniform(low=0.5, high=13.3, size=(n,dim)).astype(np.float64)
...: print("Written Data are:\n%s\n" % data_w)
...: os.write(fd, data_w.tobytes())
...: os.close(fd)
...: print("------------------ \n")
...:
...: # Read
...: start_read = 0 # 0 for now. Later I can read from any row!
...: total_num_to_read = n*dim
...: fd = os.open('test.dat', os.O_RDONLY)
...: os.lseek(fd, start_read, 0) # start_read from the beginning 0
...: raw_data = os.read(fd, 8*total_num_to_read) # How many values to be read
...: data_r = np.fromstring(raw_data, dtype=np.float64).reshape(-1, dim)
...: print("Data Read are:\n%s\n" % data_r)
...: os.close(fd)
...:
...:
Written Data are:
[[ 11.2465988 5.45304778]
[ 12.06466331 9.95717255]
[ 7.35402895 1.68972606]
[ 0.7259652 1.01265826]
[ 3.11340311 2.44725153]
[ 2.82109715 5.02768335]
[ 12.69054614 9.26028537]
[ 5.13785639 2.0780649 ]
[ 4.6796513 4.24710598]
[ 2.34859141 8.87224674]]
------------------
Data Read are:
[[ 11.2465988 5.45304778]
[ 12.06466331 9.95717255]
[ 7.35402895 1.68972606]
[ 0.7259652 1.01265826]
[ 3.11340311 2.44725153]
[ 2.82109715 5.02768335]
[ 12.69054614 9.26028537]
[ 5.13785639 2.0780649 ]
[ 4.6796513 4.24710598]
[ 2.34859141 8.87224674]]

Array only filling last value

I'm having a problem with my array only filling the last space with the calculated value. My code is below:
c_volume = 4.45e-5
c_flow_rate = 1.67e-6
acr = c_flow_rate/c_volume
t1 = [3600.0,18000.0, 36000.0]
air_conc_t1 = [6.42404968e+02, 2.74977722e+02, 1.45282562e+02]
t2 = [7200.0, 21600.0, 39600.0]
air_conc_t2 = [4.53346985e+02, 2.41359268e+02, 1.28038071e+02]
===============================================================
n_calc = np.zeros((len(t1),1), dtype='f')
def n(t1, air_conc_t1, t2, air_conc_t2):
return (1/(t2-t1))*(np.log(air_conc_t1/air_conc_t2))
for i in range(len(t1)):
n_calc[i] = n(t1[i], air_conc_t1[i], t2[i], air_conc_t2[i])
===============================================================
calc_f1 = np.zeros((len(t1),1), dtype='f')
calc_f2 = np.zeros((len(t1),1), dtype='f')
calc_N = np.zeros((len(t1),1), dtype='f')
def f1(acr, n_calc):
return (acr+n_calc)/n_calc
calc_f1[i] = f1(acr, n_calc[i])
def f2(acr, n_calc):
return (acr-n_calc)/n_calc
calc_f2[i] = f2(acr, n_calc[i])
def N(R, calc_root, m_thickness, calc_f1, calc_f2):
return (2*R*np.tan(calc_root*m_thickness))/(calc_root*m_thickness*
(calc_f1+calc_f2*calc_root*m_thickness*((1/np.tan(calc_root\
*m_thickness))+np.tan(calc_root*m_thickness))))
for i in xrange(len(t1)):
calc_N[i] = N(R, calc_root[i], m_thickness, calc_f1[i], calc_f2[i])
print calc_f1
print calc_f2
print calc_N
I'm getting the following printed:
[[ 0. ]
[ 0. ]
[ 1070.23657227]]
[[ 0. ]
[ 0. ]
[ 1068.2364502]]
[[ inf]
[ inf]
[ 3.55326119e-06]]
I'm not sure why the first two values of the array are not being filled, but the last one is. I've calculated them by hand and get values.
I'm very new to programming so any help would be appreciated.
Thanks,
Shane
your calls to f1 and f2 are not in a loop. Note that everything at the same level of indentation is included in the loop.
try this instead:
for i in xrange(len(t1)):
calc_f1[i] = f1(acr, n_calc[i])
calc_f2[i] = f2(acr, n_calc[i])
calc_N[i] = N(R, calc_root[i], m_thickness, calc_f1[i], calc_f2[i])
print calc_f1
print calc_f2
print calc_N
Lines
calc_f1[i] = f1(acr, n_calc[i])
calc_f2[i] = f2(acr, n_calc[i])
are outside any for loop (maybe it is only problem with indentions).

Categories

Resources