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.
Related
i'm doing a project that involves getting every pixel from an image than averaging it out, i'm having some problems
here's the code
for i in range(0, 3):
for j in range(0, 3):
img = Image.open("Row " + str(i + 1) + " Col " + str(j + 1) + ".png", "r")
width, height = img.size
pixel_values = list(img.getdata())
pixel_values = np.array(pixel_values).reshape((width, height, 3))
total = list(sum(pixel_values) / len(pixel_values))
print("TOTAL: " + str(total))
and the output
TOTAL: [array([150.73267327, 147.4950495 , 135.68316832]), array([157.04950495, 153.71287129, 137.23762376]), array([162.94059406, 160.89108911, 142.02970297]), array([138.05940594, 137.72277228, 129.43564356]), array([124.86138614, 126.15841584, 124.0990099 ]), array([138.1980198 , 139.33663366, 138.45544554]), array([119.24752475, 120.17821782, 119.96039604]), array([65.2970297 , 61.94059406, 61. ]), array([42.84158416, 33.3960396 , 31.3960396 ]), array([55.82178218, 38.18811881, 33.5049505 ]), array([93.87128713, 58.76237624, 45.26732673]), array([152.16831683, 90.93069307, 58.16831683]), array([187.72277228, 100.9009901 , 45.12871287]), array([191.84158416, 89.82178218, 22.6039604 ]), array([194.38613861, 87.99009901, 19.77227723]), array([196.00990099, 87.94059406, 19.2970297 ]), array([198.16831683, 88.9009901 , 19.36633663]), array([199.15841584, 88.64356436, 18.6039604 ]), array([198.68316832, 87.83168317, 17.48514851]), array([199.31683168, 87.86138614, 17.32673267]), array([199.44554455, 87.54455446, 17.21782178]), array([199.67326733, 87.93069307, 17.34653465]), array([199.78217822, 88.26732673, 17.26732673]), array([199.99009901, 88.14851485, 16.94059406]), array([199.74257426, 88.10891089, 17.05940594]), array([200.30693069, 88.66336634, 18.17821782]), array([200.37623762, 88.47524752, 17.74257426]), array([201.2970297 , 88.92079208, 17.79207921]), array([200.33663366, 87.71287129, 16.41584158]), array([201.11881188, 88.2970297 , 16.9009901 ]), array([201.4950495 , 88.72277228, 17. ]), array([200.65346535, 88.12871287, 16.45544554]), array([200.11881188, 87.73267327, 16.33663366]), array([200.10891089, 87.79207921, 16.28712871]), array([200.36633663, 87.91089109, 16.16831683]), array([200.58415842, 88.10891089, 16.30693069]), array([199.65346535, 87.17821782, 15.42574257]), array([200.14851485, 87.42574257, 16.12871287]), array([200.2970297 , 87.16831683, 15.88118812]), array([201.00990099, 88.0990099 , 16.85148515]), array([200.11881188, 87.55445545, 16.7029703 ]), array([200.37623762, 87.87128713, 16.21782178]), array([200.55445545, 87.89108911, 16.30693069]), array([200.48514851, 87.7029703 , 15.93069307]), array([200.77227723, 87.79207921, 16.33663366]), array([200.1980198 , 87.59405941, 16.94059406]), array([201.27722772, 88.76237624, 17.65346535]), array([200.51485149, 88.28712871, 16.61386139]), array([201.43564356, 88.33663366, 16.69306931]), array([200.52475248, 87.2970297 , 15.38613861]), array([200.6039604 , 87.75247525, 15.93069307]), array([200.67326733, 87.95049505, 16.04950495]), array([201.16831683, 88.75247525, 16.6039604 ]), array([200.43564356, 88.18811881, 16.17821782]), array([201.00990099, 88.14851485, 16.35643564]), array([200.83168317, 87.85148515, 15.98019802]), array([200.66336634, 87.73267327, 15.52475248]), array([201.13861386, 88.24752475, 16. ]), array([200.56435644, 87.63366337, 15.56435644]), array([200.89108911, 87.48514851, 15.79207921]), array([201.12871287, 87.66336634, 16.32673267]), array([201.64356436, 87.95049505, 16.78217822]), array([200.93069307, 87.33663366, 15.4950495 ]), array([201.12871287, 87.83168317, 15.97029703]), array([201.23762376, 88.01980198, 15.98019802]), array([200.55445545, 87.68316832, 15.92079208]), array([201.0990099 , 87.92079208, 15.97029703]), array([201.10891089, 87.91089109, 15.82178218]), array([201.40594059, 87.6039604 , 15.13861386]), array([201.44554455, 87.63366337, 15.55445545]), array([201.6039604 , 87.88118812, 16.18811881]), array([201.97029703, 87.65346535, 15.93069307]), array([200.94059406, 86.33663366, 14.52475248]), array([201.03960396, 86.63366337, 13.75247525]), array([200.74257426, 86.97029703, 13.8019802 ]), array([200.10891089, 86.5049505 , 13.52475248]), array([198.92079208, 85.92079208, 12.77227723]), array([197.98019802, 85.63366337, 13.38613861]), array([198.4950495 , 85.79207921, 13.55445545]), array([197.54455446, 85.46534653, 13.30693069]), array([197.69306931, 85.95049505, 13.67326733]), array([197.51485149, 85.92079208, 13.47524752]), array([197.56435644, 86.12871287, 13.81188119]), array([197. , 86.06930693, 14.08910891]), array([196.88118812, 85.97029703, 13.5049505 ]), array([196.35643564, 85.11881188, 12.71287129]), array([195.86138614, 85.0990099 , 12.48514851]), array([195.15841584, 84.81188119, 12.78217822]), array([194. , 84.83168317, 13.35643564]), array([190.63366337, 83.66336634, 13.41584158]), array([187.96039604, 82.63366337, 13.78217822]), array([180.31683168, 80.66336634, 15.93069307]), array([166.03960396, 77.46534653, 25.25742574]), array([88.46534653, 45.98019802, 27. ]), array([42.02970297, 31.15841584, 29.57425743]), array([33.92079208, 28.25742574, 27.22772277]), array([30.18811881, 25.94059406, 25.51485149]), array([29.85148515, 25.75247525, 25.22772277]), array([30.10891089, 25.84158416, 25.44554455]), array([30.15841584, 26. , 25.95049505]), array([27.71287129, 23.79207921, 23.67326733])]
TOTAL: [array([22.31683168, 19.92079208, 20.45544554]), array([23.45544554, 20.98019802, 21.76237624]), array([22.24752475, 19.87128713, 20.61386139]), array([31.42574257, 30.44554455, 31.05940594]), array([104.45544554, 104.33663366, 105.20792079]), array([32.20792079, 28.98019802, 29.34653465]), array([33.30693069, 28.3960396 , 28.79207921])...74257426]), array([ 18.63366337, 87.43564356,
TOTAL: [array([20.5049505 , 18.83168317, 19.06930693]), array([17.16831683, 16.03960396, 16.71287129]), array([20.77227723, 20.27722772, 21.65346535]), array([69.94059406, 69.6039604 , 69.75247525]), array([57.92079208, 56.02970297, 55.43564356]), array([36.75247525, 33.00990099, 30.84158416]), array([34.95049505, 30.06930693, 27.92079208]), array([32.78217822, 28.04950495, 25.94059406]), array([33.76237624, 28.66336634, 26.42574257]), array([35.14851485, 30.08910891, 26.65346535]), array([37.86138614, 32.0990099 , 25.94059406]), array([53.77227723, 48.00990099, 31.6039604 ]), array([155.0990099 , 148.82178218, 68.36633663]), array([155.71287129, 146.25742574, 20.42574257]), , 17.66336634]), array([159.64356436, 150.41584158, 16.73267327]), array([159.03960396, 149.76237624, 17.07920792]), array([159.0990099 , 149.61386139, 17.82178218]), array([158.32673267, 148.7029703 , 18.94059406]), array([156.68316832, 146.3960396 , 19.59405941]), array([154.52475248, 143.94059406, 19.91089109]), array([152.69306931, 142.43564356, 22.25742574]), array([148.95049505, 138.34653465, 23.66336634]), array([142.87128713, 132.27722772, 33.37623762]), array([117.61386139, 107.61386139, 39.94059406]), array([69.86138614, 61.52475248, 31.41584158]), array([33.42574257, 28.27722772, 21.08910891]), array([29.16831683, 26.42574257, 23.12871287]), array([29.93069307, 27.23762376, 23.78217822]), array([32.76237624, 30.36633663, 26.8019802 ]), array([38.38613861, 35.74257426, 32.25742574]), array([62.72277228, 58.68316832, 55.11881188])]
TOTAL: [array([148.99009901, 148.12871287, 140.7029703 ]), array([154.83168317, 158.13861386, 155.98019802]), array([163.48514851, 167.21782178, 167.51485149]), array([149.57425743, 151.25742574, 151.6039604 ]), array([80.68316832, 79.28712871, 80.31683168]), array([34.94059406, 27.83168317, 27.55445545]), array([32.51485149, 21.89108911, 20.56435644]), array([39.78217822, 23.83168317, 20.47524752]), array([63.4950495 , 36.03960396, 27.16831683]), array([120.8019802 , 64.12871287, 38.28712871]), array([170.3960396 , 82.43564356, 30.26732673]), array([180.21782178, 73.99009901, 13.34653465]), array([182.35643564, 70.14851485, 9.53465347]), array([184.56435644, 70.37623762, 9.03960396]), array([186.47524752, 71.05940594, 8.67326733]), array([186.48514851, 70.51485149, 8.56435644]), array([187.76237624, 71.68316832, 9.28712871]), array([188.55445545, 72.38613861, 9.0990099 ]), array([189.63366337, 73.26732673, 10.33663366]), array([189.06930693, 72.35643564, 10. ]), array([190.1980198 , 72.52475248, 9.23762376]), array([190.23762376, 72.07920792, 8.74257426]), array([189.91089109, 71.68316832, 8.30693069]), array([190.20792079, 71.94059406, 8.26732673]), array([190.00990099, 71.9009901 , 8.27722772]), array([190.69306931, 72.20792079, 8.25742574]), array([191.82178218, 72.5049505 , 7.87128713]), array([193.12871287, 72.78217822, 7.66336634]), array([193.41584158, 72.63366337, 7.40594059]), array([193.66336634, 72.75247525, 7.72277228]), array([193.95049505, 73. , 8.08910891]), array([192.89108911, 71.86138614, 6.61386139]), array([194.11881188, 72.52475248, 7.0990099 ]), array([194.5049505 , 72.37623762, 7.44554455]), array([194.73267327, 72.10891089, 7.51485149]), array([194.36633663, 71.99009901, 7.14851485]), array([194.32673267, 72. , 7.30693069]), array([193.76237624, 71.62376238, 6.75247525]), array([194.41584158, 72.14851485, 7.24752475]), array([195.05940594, 72.88118812, 7.73267327]), array([194.30693069, 72.16831683, 7.34653465]), array([194.8019802 , 72.67326733, 7.38613861]), array([194.31683168, 72.27722772, 6.99009901]), array([194.17821782, 72.2970297 , 6.76237624]), array([195.23762376, 72.97029703, 7.06930693]), array([195.34653465, 72.79207921, 7.25742574]), array([194.46534653, 72.27722772, 7.02970297]), array([195.38613861, 72.83168317, 7.65346535]), array([195.10891089, 72.69306931, 7.55445545]), array([195.23762376, 72.79207921, 6.82178218]), array([195.12871287, 72.87128713, 7.18811881]), array([195.12871287, 72.64356436, 7.18811881]), array([195.56435644, 72.71287129, 7.22772277]), array([195.45544554, 72.56435644, 7.22772277]), array([196.41584158, 73.76237624, 8.1980198 ]), array([195.59405941, 73. , 7.82178218]), array([195.77227723, 72.8019802 , 7.79207921]), array([196.48514851, 73.22772277,
TOTAL: [array([12.8019802 , 7.52475248, 7.75247525]), array([12.17821782, 6.92079208, 7.22772277]), array([12.38613861, 7.08910891, 7.34653465]), array([15.82178218, 10.1980198 , 10.43564356]), 129, 9.89108911, 13.16831683]), array([183.18811881, 10.23762376, 13.47524752]), array([184.86138614, 9.55445545, 12.99009901]), array([186.4950495 , 8.62376238, 12.28712871]), array([189.04950495, 9.26732673, 12.66336634]), array([190.5049505 , 8.68316832, 11.96039604]), array([191.40594059, 7.86138614, 11.26732673]), array([193.27722772, 7.78217822, 11.05940594]), array([193.8019802 , 7.20792079, 10.36633663]), array([194.83168317, 6.91089109, 10.24752475]), array([195.91089109, 6.8019802 , 10.46534653]), array([196.57425743, 6.77227723, 10.71287129]), array([197.5049505 , 6.84158416, 10.97029703]), array([197.44554455, 6.44554455, 10.65346535]), array([197.58415842, 6.46534653, 10.38613861]), array([197.2970297 , 5.7029703 , 9.33663366]), array([197.67326733, 5.51485149, 9.16831683]), array([198.08910891, 5.17821782, 9.04950495]), array([197.73267327, 4.78217822, 8.62376238]), array([198.51485149, 5.41584158, 9.24752475]), array([198.55445545, 5.2970297 , 8.99009901]), array([198.35643564, 5. , 8.76237624]), array([198.58415842, 5.03960396, 9.0990099 ]), array([198.18811881, 4.74257426, 8.69306931]), array([197.89108911, 4.89108911, 8.89108911]), array([197.40594059, 4.72277228, 8.62376238]), array([196.2970297 ,
TOTAL: [array([20.8019802 , 15.78217822, 15.25742574]), array([19.00990099, 14.95049505, 14.59405941]), array([18.24752475, 14.4950495 , 14.27722772]), array([16.6039604 , 12.84158416, 12.94059406]), array([10.55445545, 6.87128713, 7.68316832]), array([11.15841584, 7.43564356, 8.13861386]), array([11.69306931, 7.75247525, 8.47524752]), array([14.51485149, 10.54455446, 10.68316832]), array([23.2970297 , 18.97029703, 19.53465347]), array([29.82178218, 25.06930693, 26.03960396]), array([29.98019802, 24.91089109, 25.78217822]), array([32.20792079, 26.9009901 , 27.1980198 ]), array([35.47524752, 29.57425743, 29.79207921]), array([39.61386139, 31.83168317, 31.25742574]), array([42.83168317, 32.54455446, 30.44554455]), array([83.9009901 , 32.84158416, 32.33663366]), array([105.07920792, 25.48514851, 27.12871287]), array([120.76237624, 19.66336634, 22.62376238]), array([133.52475248, 17.25742574, 21.4950495 ]), array([144.63366337, 15.64356436, 21.24752475]), array([157.11881188, 15.71287129, 22.59405941]), array([163.94059406, 13.07920792, 20.86138614]), array([170.74257426, 13.66336634, 20.8019802 ]), array([176.43564356, 12.62376238, 20.15841584]), array([181.28712871, 11.5049505 , 19.5049505 ]), array([184.12871287, 10.20792079, 18.43564356]), array([187.16831683, 9.63366337, 18.22772277]), array([188.2970297 , 8.02970297, 16.79207921]), array([188.81188119, 7.63366337, 16.28712871]), array([189.22772277, 7.63366337, 16.18811881]), array([189.43564356, 7.41584158, 16.2970297 ]), array([189.98019802, 7.82178218, 16.9009901 ]), array([189.27722772, 7. , 16.00990099]), array([189.51485149, 7.25742574, 16.31683168]), array([189.15841584, 7.22772277, 16.28712871]), array([189.06930693, 7.13861386, 16.44554455]), array([189.27722772, 7.27722772, 16.4950495 ]), array([189.34653465, 7.28712871, 16.25742574]), array([189.64356436, 7.73267327, 16.64356436]), array([189.05940594, 7.20792079, 16.47524752]), array([189.59405941, 7.61386139, 16.83168317]), array([189.41584158, 7.47524752, 16.71287129]), array([189.45544554, 7.47524752, 16.78217822]), array([188.85148515, 7.17821782, 16.79207921]), array([189.06930693, 7.26732673, 16.75247525]), array([188.9009901 , 7.2970297 , 16.56435644]), array([189.46534653, 7.81188119, 17.12871287]), array([189.26732673, 7.68316832, 17.11881188]), array([188.69306931, 7.12871287, 16.20792079]), array([188.6039604 , 7.04950495, 16.22772277]), array([189.12871287, 7.44554455, 16.7029703 ]), array([188.73267327, 7.52475248, 16.68316832]), array([188.81188119, 7.57425743, 16.79207921]), array([188.89108911, 7.4950495 , 16.5049505 ]), array([188.88118812, 7.38613861, 16.26732673]), array([189.01980198, 7.62376238, 16.54455446]), array([188.86138614, 7.47524752, 16.71287129]), array([188.58415842, 7.38613861, 16.51485149]), array([188.51485149, 7.27722772, 16.51485149]), array([188.35643564, 7.22772277, 16.36633663]), array([188.61386139, 7.66336634, 16.72277228]), array([187.95049505, 7.31683168, 16.28712871]), array([188.53465347, 7.86138614, 16.81188119]), array([188.24752475, 7.58415842, 16.7029703 ]), array([187.95049505, 7.18811881, 16.3960396 ]), array([188.23762376, 7.92079208, 16.9009901 ]), array([188.55445545, 8.18811881, 17.34653465]), array([188.17821782, 7.75247525, 16.84158416]), array([188.01980198, 8.15841584, 17.22772277]), array([187.64356436, 7.82178218, 17.20792079]), 38614, 101.67326733, 149.53465347]), array([ 8.52475248, 101.11881188, 149.21782178]), array([ 8.45544554, 101.13861386, 149.27722772]), array([ 8.17821782, 100.71287129, 148.55445545]), array([ 8.89108911, 101.56435644, 149.43564356]), array([ 8.08910891, 101.05940594, 149.07920792]), array([ 8.32673267, 101.57425743, 149.20792079]), array([ 7.85148515, 100.93069307, 148.62376238]), array([ 8.43564356, 100.61386139, 148.34653465]), array([ 7.99009901, 100.55445545, 148.13861386]), array([ 8.01980198, 100.20792079, 147.93069307]), array([ 8.63366337, 99.72277228, 146.8019802 ]), array([ 8.25742574, 98.2970297 , 145.32673267]), array([ 9.5049505 , 98.53465347, 144.51485149]), array([ 8.98019802, 96.21782178, 141.11881188]), array([ 10.01980198, 94.2970297 , 137.24752475]), array([ 13.14851485, 88.84158416, 125.86138614]), array([19.30693069, 62.61386139, 87.16831683]), array([24.57425743, 35.27722772, 42.64356436])]
i wanted the sum of all of the numbers, but i'm not getting that... what am i doing wrong here?
You can use PIL (Python Imaging Library) to load the image, convert it to an array with numpy.asarray() then use numpy.sum():
from PIL import Image
import numpy as np
image_path = '/Users/xxx/Desktop/test.png'
img = Image.open(image_path)
img.load()
data = np.asarray(img, dtype="int32")
data.sum()
Output:
1715779623
References:
numpy.asarray
numpy.sum
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]]
I am trying to use differential_evolution from SciPy. I have three matrices: x, y and P - all of size (14,6). I have to use the following formula:
z= np.log10(g)+ np.log10(c)*np.log10(P)
to find the value of c (real number from 0 to 2) which minimize:
numpy.median(z**2)
this expression. What I try is this (I provide the data for the convenience):
import numpy as np
from scipy.optimize import differential_evolution
def func(c, args):
z = args[0] + np.log10(c)*np.log10(args[1])
return np.median(z**2)
if __name__ == '__main__':
bounds = [(0, 2)]
x = np.array([[126581.94951205, 97601.85624482, 59659.00330833,
27646.48551627, 9202.50377458, 4840.25789068],
[213571.84886437, 148750.52154776, 85979.81139937,
38757.37831212, 11775.99906427, 4619.32027948],
[195684.50299021, 131818.78542437, 74376.55189913,
32793.21715377, 10288.70838873, 4042.58093119],
[177598.13865746, 120942.50439911, 68866.09898276,
30819.5354775 , 10588.08746517, 5011.71808947],
[126433.18311483, 85863.57788065, 48923.64502157,
21828.60950911, 7907.37639781, 4410.61819399],
[103431.88029629, 67452.94418262, 37608.36861047,
16456.97701443, 6027.98704858, 3550.06927169],
[100689.06813945, 64380.21348052, 34764.02910376,
14849.85472635, 5607.19256065, 3605.5709208 ],
[ 96509.22946744, 63832.74512518, 36041.69174706,
15802.87650901, 6473.33232805, 4664.07058733],
[113078.63455882, 73227.02362359, 40861.09037499,
17385.89127848, 7074.98444924, 5136.84232454],
[121241.93118924, 78537.13681709, 44257.97654994,
18584.94999742, 7733.39219718, 5869.49536788],
[115948.06368262, 73995.07204278, 41536.21315507,
16851.59724901, 6736.25125909, 4851.5738275 ],
[115024.20359423, 72108.15245783, 40341.98473413,
15900.55422399, 6243.63777265, 4411.24859372],
[108754.83802899, 66210.25952459, 36485.42905112,
14577.73925124, 5553.23702141, 3736.5217322 ],
[ 95340.59125024, 58458.97552915, 32364.19705748,
13236.30114676, 4929.04023171, 3202.21731277]])
y = y=np.array([[118166.08 , 95784.692 , 68134.878 , 37119.959 , 17924.157 ,
7445.3083],
[ 99265.027 , 70679.135 , 43297.559 , 19822.017 , 8527.8497,
3404.7113],
[ 80227.797 , 50972.879 , 26648.604 , 11190.488 , 4836.6514,
2249.9172],
[ 68510.582 , 39288.19 , 19938.938 , 9312.6881, 4907.6661,
2681.2709],
[ 65193.15 , 36610.107 , 18612.181 , 9211.144 , 5416.1685,
3372.1282],
[ 67188.918 , 37227.699 , 20132.92 , 11663.275 , 7315.3472,
4648.1669],
[ 64802.06 , 38885.622 , 22008.537 , 13100.638 , 8043.0185,
5049.2097],
[ 68104.867 , 41212.89 , 23247.898 , 14134.707 , 8805.2547,
5526.1014],
[ 74180.595 , 41268.904 , 22868.016 , 13841.437 , 8660.1413,
5401.245 ],
[ 78920.685 , 42743.389 , 23932.305 , 13910.089 , 8439.3342,
5141.7051],
[ 91329.012 , 45733.772 , 25430.818 , 14144.185 , 8273.7953,
5016.5839],
[ 92217.594 , 44984.3 , 23353.596 , 13467.631 , 8099.728 ,
4948.26 ],
[ 94508.441 , 48114.879 , 24735.311 , 13358.097 , 7821.8587,
4806.7923],
[108211.73 , 53987.095 , 25872.772 , 13189.61 , 7552.7164,
4497.2611]])
P=10000*np.array([[0.6011,0.6011,0.6011,0.6011,0.6011,0.6011],
[0.9007,0.9007,0.9007,0.9007,0.9007,0.9007],
[1.1968,1.1968,1.1968,1.1968,1.1968,1.1968],
[1.4178,1.4178,1.4178,1.4178,1.4178,1.4178],
[1.5015,1.5015,1.5015,1.5015,1.5015,1.5015],
[1.439,1.439,1.439,1.439,1.439,1.439],
[1.2721,1.2721,1.2721,1.2721,1.2721,1.2721],
[1.0616,1.0616,1.0616,1.0616,1.0616,1.0616],
[0.8543,0.8543,0.8543,0.8543,0.8543,0.8543],
[0.6723,0.6723,0.6723,0.6723,0.6723,0.6723],
[0.5204,0.5204,0.5204,0.5204,0.5204,0.5204],
[0.3963,0.3963,0.3963,0.3963,0.3963,0.3963],
[0.2990,0.2990,0.2990,0.2990,0.2990,0.2990],
[0.2211,0.2211,0.2211,0.2211,0.2211,0.2211]])
g=np.log10(y) - np.log10(x)
args = (g,P)
result = differential_evolution(func, bounds, args=args)
print(func(bounds, args))
I get this error: TypeError: func() takes exactly 2 arguments (3 given) . Is there any way to fix this?
def func(c, g, P):
z = g + np.log10(c)*np.log10(P)
return np.median(z**2)
if __name__ == '__main__':
# Your arrays go here
g = np.log10(y) - np.log10(x)
args = (g, P)
result = differential_evolution(func, bounds, args=(g, P))
# will print the value of c and value of the optimized function
print (result.x, result.fun)
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]]
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).