1
$\begingroup$

I have a list in the form of prices below.

mcd = FinancialData["MCD", {{1990, 1, 1}, {2014, 1, 1}, "Month"}];
ibm = FinancialData["IBM", {{1990, 1, 1}, {2014, 1, 1}, "Month"}, 
 "Value"];
hpq = FinancialData["HPQ", {{1990, 1, 1}, {2014, 1, 1}, "Month"}, 
 "Value"];
prices = Table[
 Append[hpq[[i]]]@Append[ibm[[i]]]@mcd[[i]], 
 {i, First@Dimensions[ibm]}];

I have prices in this form and am not building it from FinancialData. From this I make a DataListLogPlot that I'd like to reindex to certain points using Manipulate.

Manipulate[
 DateListPlot[
  Table[
   Table[
    {
     prices[[i, 1]],
     Table[100 (prices[[m, n]]/prices[[indexLoc, n]]), 
      {m, 1, Dimensions[prices][[1]]}, {n, 2, 4}][[i, j]]
    },
    {i, Dimensions[prices][[1]]}],
   {j, Dimensions[prices][[2]] - 1}],
  GridLines -> {None, Automatic}, PlotLegends -> Automatic
 ],
{indexLoc, 1, Dimensions[prices][[1]], 12}]

This works. It reindexes the values to 100 at the month in the series that is selected by the slider. However, it takes a while (4 seconds or so) to calculate each time it is moved. I was hoping to have this scrollable with a vertical line at the month it is reindex. However, it is too clunky with this 4 second lag. Is there a way to speed this up? I looked at a few of the time series functions but nothing jumped out at me.

Thanks,

Edmund


Update

From Sander's suggestion below I was able to get to what I wanted. I need to keep it as a DateListLogPlot but was able to use Transpose to get there.

(*Using the "prices" variable from above*)
plotPrices =
  Table[
   Table[
    {
     prices[[i, 1]],
     Table[(prices[[m, n]]), {m, 1, Dimensions[prices][[1]]}, 
      {n, 2, 4}][[i, j]]
    },
   {i, Dimensions[prices][[1]]}],
  {j, Dimensions[prices][[2]] - 1}];

Manipulate[
 DateListLogPlot[
  Transpose[
   {#[[All, 1]], 100 #[[All, 2]]/#[[indexLoc, 2]]} & /@ plotPrices, 
   {1, 3, 2}], 
  GridLines -> {None, Automatic}, 
  PlotLegends -> Automatic],
 {indexLoc, 1, Dimensions[prices][[1]], 1}]
$\endgroup$
  • $\begingroup$ Changed step size to 12. Had it too large from when I was trying things to speed it up. $\endgroup$ – Edmund Oct 11 '14 at 16:17
1
$\begingroup$

The performance issue is that you are dynamically building several levels of tables into tables into a plot function into a manipulate.

This is very slow and what you are looking for is to build the table outside the plot and outside the manipulate and only do the indexation inside.

However (!!) it seems to me there is a very straightforward solution to what you want to achieve, and avoid the table creation altogether.

Performance on my Macbook Pro i7 is much faster: approx 0.0003 seconds per manipulation.

mcd = FinancialData["MCD", {{1990, 1, 1}, {2014, 1, 1}, "Month"}];
ibm = FinancialData["IBM", {{1990, 1, 1}, {2014, 1, 1}, "Month"}];
hpq = FinancialData["HPQ", {{1990, 1, 1}, {2014, 1, 1}, "Month"}];
Manipulate[ListLinePlot[100 #[[All, 2]]/#[[ix, 2]] & /@ {mcd, ibm, hpq}], {ix, 1, Length[mcd], 1}]

Hope this is what you are looking for. Please note I made the data format for mdc,ivm and hpq consistent.

$\endgroup$
  • $\begingroup$ This put me on track get the answer I was looking for. Thanks! I still need it to be a DateListPlot (actually a DateListLogPlot) but I was able to get there from here with Transpose. $\endgroup$ – Edmund Oct 13 '14 at 14:47
  • $\begingroup$ You are most welcome; I liked your question btw. $\endgroup$ – Sander Oct 13 '14 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.