Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I've been playing with WeightedData and wanted a financial engineering example, a colleague suggested working with the fractional return (FinancialData["Name","FractionalChange") of a weighted portfolio.

Things were going well until I tried to build a Manipulate:

Manipulate[
Grid[
{{Style[DateString[fractionalChanges[[1, day, 1]], {"DayName", " ", "Day", " ", 
   "MonthName", " ", "Year"}], "Text"], SpanFromLeft},
{Style["Unweighted Mean Fractional Change:", "Text"], 
Style[ToString@Mean[fractionalChanges[[All, day, 2]]], "Text"]},
{Style["Weighted Mean Fractional Change:", "Text"], 
Style[ToString@Mean[allWeightedFChanges[[day]]], "Text"]},
{Pane[
 Plot[{PDF[HistogramDistribution[allWeightedFChanges[[day]], 10], 
    x], PDF[HistogramDistribution[
     allWeightedFChanges[[day]]["InputData"], 10], x]}, {x, -0.03,
    0.03}, Filling -> Axis, PlotStyle -> {Red, Blue},
  PlotLegends -> {"Weighted Fractional \nChange Distribution", 
    "Unweighted Fractional \nChange Distribution"}, 
  AxesLabel -> {"Fractional Change", " "}], {700, 280}, 
 ImageSizeAction -> "ResizeToFit", Alignment -> {Center, Center}],
 SpanFromLeft}}, Frame -> All], {day, 1, 253, 1},
Initialization :> (thirtyRandomStocks = 
RandomChoice[FinancialData["FTSE100", "Members"], 30];
fractionalChanges = 
Chop@Map[FinancialData[#, "FractionalChange", DatePlus[-360]] &, 
  thirtyRandomStocks];
stockWeights = RandomInteger[{100, 10000}, 30];
allFChange = fractionalChanges[[All, All, 2]];
allWeightedFChanges = 
Table[WeightedData[allFChange[[All, i]], stockWeights], {i, 1, 
  Min[Table[
    Length[allFChange[[i, All]]], {i, 1, 
     Length[allFChange]}]]}];)]

I get an "Initialization Time Out" error and can't figure out how to give the Manipulate more time to initialise.

Therefore, I have two questions:

  • How can I give Mathematica more time to initialise, or else ensure the Manipulate gets the necessary data for the calculations?
  • Note: This Manipulate will always be used within notebooks, so Free CDF limitations aren't important for me at this point.
  • Are there any suggestions you'd be willing to share to improve the code in general? I'm aware that if you look closely I'm not working with the data correctly; allWeightedFChanges doesn't really do what it should do.
share|improve this question
2  
First thing I'd do is to pay close attention to the reason of slow initialization. A quick guess is that this is due to FinancialData, so I'd start by caching it outside your Manipulate code if I were you. –  Leonid Shifrin Feb 2 '13 at 16:46
    
@LeonidShifrin, I'd like to be able to use this Manipulate without having Initialization Cells if at all possible. Cormulion's answer below is probably the fastest way to do this. –  Martin John Hadley Feb 3 '13 at 17:28
    
Sure. Mu suggestion was anyway coming from general common sense and not necessarily taking into account your specific situation. –  Leonid Shifrin Feb 3 '13 at 17:46
    
Understandable, I usually employ common sense when building things up and then it goes out the window when I attempt to dump everything within a Manipulate. DynamicModule would have provided me with more flexibility but I wanted a quick solution. Thanks again for commenting. –  Martin John Hadley Feb 3 '13 at 18:28
    
Sure, no problem. –  Leonid Shifrin Feb 3 '13 at 18:37

1 Answer 1

up vote 6 down vote accepted

Probably better to heed Leonid's advice to re-configure, but, with SynchronousInitialization -> False added I got this after a while:

graph

share|improve this answer
    
Thanks @cormullion, I guess I just need to adjust my DatePlus value so that "a while" is "a little while". –  Martin John Hadley Feb 3 '13 at 17:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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