Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Thanks to “Rod Lm” I got some very nice input for the following problem.

My intention is to simulate within a Manipulate function a given number of possible stock price paths and to calculate their mean. So, if something is manipulated the “new mean” is also calculated automatically and both are shown by a line and numerically within the chart. I would also like to know how I can also calculate the mean outside the chart. I tried the following but it didn't work:

     GeometricBrownianMotionProcess[0.1, 0.2, 100], {0, 250, 0.05}][
    "Path"], {20}][[All, -1]]]

The next step -if possible- is to introduce a lower boundary. So for example if the initial stock price is 100 then the boundary could be at, let’s say, 70. First, I want the boundary to be shown by a line. Second, I would like to stop all stochastic processes that fall below this threshold and then receiving the mean from all remaining processes.

Alternatively, if it’s not possible to stop the processes that fall below the threshold, I also just want the mean of the remaining processes that are beyond the threshold. Third, I also would like to have a manipulable threshold. So, in short, I want to be able to switch the threshold between 20 and 90 and then receive the mean of the processes that remain beyond this boundary.

Is something like that possible? If yes, I would be really thankful if I would receive some suggestions how to solve my problem, or at least parts of it. So far I have done the following and is it even possible to modify this code in such a manner that my problem can be solved?

My code so far:

     GeometricBrownianMotionProcess[μ,σ,S0], {0, 250, 0.05}]["Path"], {P}], 
    Joined -> True, 
    AxesLabel -> {"Time", "St"}, 
    PlotLabel -> Style["Forecasted Stock Price\n (Brownian Motion)", Bold], 
    PlotRange -> All, 
    ImageSize -> 500, 
    PlotStyle -> Directive[{Thin, Lighter@Gray}]
  {{S0, 100, "Initial Stock Value"}, 100, 500, 0.05, Appearance -> "Labeled"}, 
  {{μ, 0.01, "Drift μ"}, 0.01, 1, 0.05, Appearance -> "Labeled"}, 
  {{σ, 0.01, "Standard Deviation σ"}, 0.01, 1, 0.05, Appearance -> "Labeled"}, 
  {{P, 1, "Paths"}, 1, 100, 1, Appearance -> "Labeled"}, 
  {{seed, 77777, "New Random Case"}, 10000, 999999, 1}, 
 Button["Set Initial Values", {S0 = 25, μ = 0.01, σ = 0.01}, ImageSize -> 150], 
 ControlPlacement -> Left]

enter image description here

share|improve this question
Variable name S_0 doesn't look correct to me. You can't use an underscore (= Blank) in a variable name. – Sjoerd C. de Vries Jun 2 '13 at 21:09
Changed better? – Milan Ivica Jun 2 '13 at 21:20
What are the problems you are running into? The more specific your question the more likely it is someone will help... – sebhofer Jun 3 '13 at 10:30
the Problems I am running into are that I simply want to calculate the mean of a specific number of paths at the end of the period and also introduce a visimble threshold let's say at 70 which can also be manipulated. and if possible I would like to calculate the mean of those paths which remain beyond this threshold. do you understand my problem? if no, please let me know so that I can be more precise. Thank you – Milan Ivica Jun 3 '13 at 10:43
I think I understand what you are trying to do, but what are the problems you have with the implementation? – sebhofer Jun 3 '13 at 11:00
up vote 3 down vote accepted

To correctly compute the mean, try this:

meanvector := Mean[assets];
assets = Table[RandomFunction[GeometricBrownianMotionProcess[μ, σ, S0], {0, time, 0.1}]["Path"], {P}];
G1 := ListLogPlot[assets, GridLines -> {{}, {watermark}}, GridLinesStyle -> Directive[Green, Thick], Joined -> True, AxesLabel -> {"Time", "St"}, PlotLabel -> Style["Forecasted Stock Price\n (Brownian Motion)", Bold], PlotRange -> All, PlotStyle -> Directive[{Thin, Lighter@Gray}]];
G2 := ListLogPlot[Mean[assets], Joined -> True, PlotStyle -> Directive[{Thick, Darker@Red}]];
Show[G1, G2],
{{S0, 25, "Initial Stock Value"}, 1, 500, 0.5, Appearance -> "Labeled"},
{{μ, 0.08, "Drift μ"}, 0.01, 0.2, 0.01, Appearance -> "Labeled"}, {{σ, 0.2, "Standard Deviation σ"}, 0.01, 1, 0.05, Appearance -> "Labeled"},
{{P, 6, "Paths"}, 1, 20, 1, Appearance -> "Labeled"},
{{time, 10, "Time t"}, 1, 20, 1, Appearance -> "Labeled"},
{{watermark, 25, "Watermark"}, 1, 500, Appearance -> "Labeled"},
{{seed, 1, "New Random Case"}, 1, 100, 1},Button["Set Initial Values", {S0 = 25, μ = 0.08, σ = 0.20, P = 6, time = 10, watermark = S0}, ImageSize -> 150],
ControlPlacement -> Left]


enter image description here

share|improve this answer
thank you a lot for your help. now it works perfectly! :) – Milan Ivica Jun 5 '13 at 6:19
@Ron Lm thak you a lot, I just slightly changed your code by making the time manipulable. However, I also wanted to introduce a line, which should symbolise a kind of threshold. I tried to do it, but I had some Problems. If you have time, could you please take a brief look at it?. I think the Problem is somewhere in G3 – Milan Ivica Jun 5 '13 at 11:56
@MilanIvica I edited your suggestion for some syntax error and misusing on functions (your original definition of G3). I also formatted the code for more readability. Please see the edit history for detail and let me know if I did something wrong. – Silvia Jun 5 '13 at 12:18
@Silvia and @MilanIvica You don't need to define debtlevel = Line[{{0, t}, {T, t}}] or threshold = Line[{{0, t}, {T, t}}]. You only have to add GridLines -> {{}, {watermark}} to the ListLogPlot[] function... – Rod Jun 5 '13 at 14:08
I only did a a minimal modification for correcting the error in @Milanlvica's edit suggestion on this answer. Its YOUR answer, so please feel free to edit it.:) But I personally think it would be better if you indent your code more for its readability. btw +1 – Silvia Jun 5 '13 at 14:42

Your Answer


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.