Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

I am using the code from this answerthis answer, to optimize a portfolio of stocks. This is the code:

Portfolio = {"AAPL", "BA", "IBM", "BMW.DE", "DIS", "R", "PEP", 
   "BRBY.L", "AXP", "BTI"};
data = FinancialData[#, "Price", {{2004}, {2012}, "Month"}][[All, 
      2]] & /@ Portfolio;
Returns = Differences[Log[data[[#]]]] & /@ Range[10];
n = 10;
WeightsVector = Subscript[w, #] & /@ Range[n]
VarianceVector = Subsuperscript[\[Sigma], #, 2] & /@ Range[n]
SDVector = Subscript[\[Sigma], #] & /@ Range[n]
CovMatrix = 
  Array[\[Sigma], {n, n}] /. {\[Sigma][i_, j_] :> 
     Subscript[\[Sigma], ToString@j <> ToString@i] /; i > j, \[Sigma][
      i_, j_] :> 
     Subscript[\[Sigma], ToString@i <> ToString@j] /; i < j, \[Sigma][
      i_, j_] :> Subsuperscript[\[Sigma], i, 2] /; i == j};
PortfolioVariance := WeightsVector.CovMatrix.WeightsVector;
PortfolioMean := WeightsVector.MeanVector;
MeanVector = Mean@Transpose@Returns*12;
VarianceVector = Variance@Transpose@Returns*12;
SDVector = StandardDeviation@Transpose@Returns*Sqrt@12;
CovMatrix = Covariance@Transpose@Returns;
PortfolioVariance := WeightsVector.CovMatrix.WeightsVector;
MV = FindMinimum[{PortfolioVariance, Total[WeightsVector] == 1}, 
  WeightsVector]
BarChart[{Last[MV[[2, #]]] & /@ Range[n]}, ImageSize -> Large, 
 ChartLabels -> Portfolio]

This works perfectly, however, when I change the years in the data variable from 2004 and 2012 to 2004 and 2016, it errors, saying that the first two levels could not be transposed. I'm not exactly sure why this is happening, I've examined all the variables and they stay the same except of course the numbers change.

How can I fix this, and why is it happening?

I am using the code from this answer, to optimize a portfolio of stocks. This is the code:

Portfolio = {"AAPL", "BA", "IBM", "BMW.DE", "DIS", "R", "PEP", 
   "BRBY.L", "AXP", "BTI"};
data = FinancialData[#, "Price", {{2004}, {2012}, "Month"}][[All, 
      2]] & /@ Portfolio;
Returns = Differences[Log[data[[#]]]] & /@ Range[10];
n = 10;
WeightsVector = Subscript[w, #] & /@ Range[n]
VarianceVector = Subsuperscript[\[Sigma], #, 2] & /@ Range[n]
SDVector = Subscript[\[Sigma], #] & /@ Range[n]
CovMatrix = 
  Array[\[Sigma], {n, n}] /. {\[Sigma][i_, j_] :> 
     Subscript[\[Sigma], ToString@j <> ToString@i] /; i > j, \[Sigma][
      i_, j_] :> 
     Subscript[\[Sigma], ToString@i <> ToString@j] /; i < j, \[Sigma][
      i_, j_] :> Subsuperscript[\[Sigma], i, 2] /; i == j};
PortfolioVariance := WeightsVector.CovMatrix.WeightsVector;
PortfolioMean := WeightsVector.MeanVector;
MeanVector = Mean@Transpose@Returns*12;
VarianceVector = Variance@Transpose@Returns*12;
SDVector = StandardDeviation@Transpose@Returns*Sqrt@12;
CovMatrix = Covariance@Transpose@Returns;
PortfolioVariance := WeightsVector.CovMatrix.WeightsVector;
MV = FindMinimum[{PortfolioVariance, Total[WeightsVector] == 1}, 
  WeightsVector]
BarChart[{Last[MV[[2, #]]] & /@ Range[n]}, ImageSize -> Large, 
 ChartLabels -> Portfolio]

This works perfectly, however, when I change the years in the data variable from 2004 and 2012 to 2004 and 2016, it errors, saying that the first two levels could not be transposed. I'm not exactly sure why this is happening, I've examined all the variables and they stay the same except of course the numbers change.

How can I fix this, and why is it happening?

I am using the code from this answer, to optimize a portfolio of stocks. This is the code:

Portfolio = {"AAPL", "BA", "IBM", "BMW.DE", "DIS", "R", "PEP", 
   "BRBY.L", "AXP", "BTI"};
data = FinancialData[#, "Price", {{2004}, {2012}, "Month"}][[All, 
      2]] & /@ Portfolio;
Returns = Differences[Log[data[[#]]]] & /@ Range[10];
n = 10;
WeightsVector = Subscript[w, #] & /@ Range[n]
VarianceVector = Subsuperscript[\[Sigma], #, 2] & /@ Range[n]
SDVector = Subscript[\[Sigma], #] & /@ Range[n]
CovMatrix = 
  Array[\[Sigma], {n, n}] /. {\[Sigma][i_, j_] :> 
     Subscript[\[Sigma], ToString@j <> ToString@i] /; i > j, \[Sigma][
      i_, j_] :> 
     Subscript[\[Sigma], ToString@i <> ToString@j] /; i < j, \[Sigma][
      i_, j_] :> Subsuperscript[\[Sigma], i, 2] /; i == j};
PortfolioVariance := WeightsVector.CovMatrix.WeightsVector;
PortfolioMean := WeightsVector.MeanVector;
MeanVector = Mean@Transpose@Returns*12;
VarianceVector = Variance@Transpose@Returns*12;
SDVector = StandardDeviation@Transpose@Returns*Sqrt@12;
CovMatrix = Covariance@Transpose@Returns;
PortfolioVariance := WeightsVector.CovMatrix.WeightsVector;
MV = FindMinimum[{PortfolioVariance, Total[WeightsVector] == 1}, 
  WeightsVector]
BarChart[{Last[MV[[2, #]]] & /@ Range[n]}, ImageSize -> Large, 
 ChartLabels -> Portfolio]

This works perfectly, however, when I change the years in the data variable from 2004 and 2012 to 2004 and 2016, it errors, saying that the first two levels could not be transposed. I'm not exactly sure why this is happening, I've examined all the variables and they stay the same except of course the numbers change.

How can I fix this, and why is it happening?

edited tags
Link
Source Link
Nico A
  • 3.7k
  • 1
  • 16
  • 28

Transpose erroring

I am using the code from this answer, to optimize a portfolio of stocks. This is the code:

Portfolio = {"AAPL", "BA", "IBM", "BMW.DE", "DIS", "R", "PEP", 
   "BRBY.L", "AXP", "BTI"};
data = FinancialData[#, "Price", {{2004}, {2012}, "Month"}][[All, 
      2]] & /@ Portfolio;
Returns = Differences[Log[data[[#]]]] & /@ Range[10];
n = 10;
WeightsVector = Subscript[w, #] & /@ Range[n]
VarianceVector = Subsuperscript[\[Sigma], #, 2] & /@ Range[n]
SDVector = Subscript[\[Sigma], #] & /@ Range[n]
CovMatrix = 
  Array[\[Sigma], {n, n}] /. {\[Sigma][i_, j_] :> 
     Subscript[\[Sigma], ToString@j <> ToString@i] /; i > j, \[Sigma][
      i_, j_] :> 
     Subscript[\[Sigma], ToString@i <> ToString@j] /; i < j, \[Sigma][
      i_, j_] :> Subsuperscript[\[Sigma], i, 2] /; i == j};
PortfolioVariance := WeightsVector.CovMatrix.WeightsVector;
PortfolioMean := WeightsVector.MeanVector;
MeanVector = Mean@Transpose@Returns*12;
VarianceVector = Variance@Transpose@Returns*12;
SDVector = StandardDeviation@Transpose@Returns*Sqrt@12;
CovMatrix = Covariance@Transpose@Returns;
PortfolioVariance := WeightsVector.CovMatrix.WeightsVector;
MV = FindMinimum[{PortfolioVariance, Total[WeightsVector] == 1}, 
  WeightsVector]
BarChart[{Last[MV[[2, #]]] & /@ Range[n]}, ImageSize -> Large, 
 ChartLabels -> Portfolio]

This works perfectly, however, when I change the years in the data variable from 2004 and 2012 to 2004 and 2016, it errors, saying that the first two levels could not be transposed. I'm not exactly sure why this is happening, I've examined all the variables and they stay the same except of course the numbers change.

How can I fix this, and why is it happening?