# Introduction of straight Line into Manipulate function

I just want to implement a line within my chart that starts from time point 0 to time point T and whose y-coordinates can be manipulated, which in my case is represented by D.

I tried to implement a function called Threshold but I am still struggling with it. Can anybody of you help me out?

My code so far:

 Manipulate[
SeedRandom[seed]; Column[{test2[μ_, σ_, S_, P_, T_] := Table[RandomFunction[
GeometricBrownianMotionProcess[μ, σ, S], {0, T, 0.05}]["Path"], {P}];

meanPaths = Mean[test2[μ, σ, S, P, T][[All]]];
threshold[t_, T_] := Line[{{0, t}, {T, t}}];

ListLogPlot[{meanPaths,Flatten[test2[μ, σ, S, P, T], 1], threshold},
Joined -> True, AxesLabel -> {"Time", "St"},
PlotLabel -> Style["Forecasted Stock Price\n (Brownian Motion)", Bold],
PlotRange -> {{0, T}, {0, 700}},
PlotStyle -> {Directive[{Thick, Red}], Directive[{Thin, Gray}]},
GridLines -> {{}, {t}},
ImageSize -> 500]}],

{{S, 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"},
{{T, 1, "Time"}, 1, 250, 1, Appearance -> "Labeled"},
{{t, 100, "thresh"}, 95, 105, Appearance -> "Labeled"},
{{seed, 77777, "New Random Case"}, 10000, 999999, 1},
Button["Set Initial Values",
{S = 100, μ = 0.01, σ = 0.01, T = 1, P = 1, t = 100},
ImageSize -> 150],
ControlPlacement -> Left]

-
Is the line vertical? Is it controlled via the mouse or by a controller? What is its purpose? – DavidC Jun 4 '13 at 14:25
@David Carraher this line is supposed to be horizontal and represent a Kind of threshold where I just want to consider the stochastic processes which are beyond this horizontal line. I want also to manipulate this line. so it should start (x-coordinates) at 0 to T and should be manipulable in ist y-coordinates. – Milan Ivica Jun 4 '13 at 14:31
Best to show the simplest code that demonstrates the problem. – Jagra Jun 4 '13 at 14:44
@MilanIvica D is a protected symbol in Mathematica. – Rod Jun 4 '13 at 15:04
@Jara what do you exactly mean with "Best to show the simplest code that demonstrates the Problem"? I just want to insert a straight horizontal line whoch starts at the coordinates (0/D) and Ends at (T/D). and I want to be able to manipulate D – Milan Ivica Jun 4 '13 at 15:04

## 3 Answers

To correctly compute the mean and show watermark, try this:

Manipulate[SeedRandom[seed];
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]


Result:

-
You are the best! :) – Milan Ivica Jun 5 '13 at 14:37
@ Rod Lm Hi, do you think that the watermark could be used as a threshold in the following way: as soon as the process hits the threshold (watermark) it stops and is also excluded from the calculation of the mean function. So all processes which equal or fall beneath the threshold should stop and all processes that remain beyond the threshold should continue and the mean should only be calculated from the processes that remain beyond the threshold. – Milan Ivica Jun 6 '13 at 13:03
@MilanIvica Yes, it's possible to do it... but you have to define some things first... for instance, if the process hits the watermark, the past values of the process (below the watermark) should be included in the computation of the mean or not? – Rod Jun 6 '13 at 13:49
@MilanIvica Or should the whole process be excluded from the computation of the mean? – Rod Jun 6 '13 at 13:50
@ Rod Lm I am thinking about the following: if we for example consider three different processes. If process 1-3 do not fall below the watermark at the end of T, all of them should be considered concerning the calculation of the mean. However, if only process 1 and 2 remain beyond the watermark up to T and 3 falls below the watermark or hits the watermark it should stop at the moment where process 3 is <= the watermark and only the mean of process 1 and 2 should be considered when the mean is calculated. I hope I was able to explain you my thoughts. – Milan Ivica Jun 6 '13 at 13:54

Try inserting the following slider:

{{t, 100, "thresh"}, 95, 105,  Appearance -> "Labeled"}


and add the following setting to ListLogPlot

GridLines -> {{}, {t}}


You'll likely need to play with the min and max of t.

-
Beat me by seconds. +1 – Jagra Jun 4 '13 at 14:55
@David Carraher Thanks it Looks better now. But I changed now the code above and is's working better now, but i still get the following error: SetDelayed::write: "Tag Line in Line[{{0,80},{29,80}}][t_,T_] is Protected" . have I set GridLines -> {{}, {t}} in the vorrect Position? thank you – Milan Ivica Jun 4 '13 at 15:17
@ David Carraher Does the same mistake also appear when you run the code? – Milan Ivica Jun 4 '13 at 15:26
@Milan I suspect there is an earlier definition of one of your variables that is causing the problem. ClearAll your variables before running the code. (No, I don't get that mistake.) I do get a continuous running of the code however. Perhaps someone can suggest how to clear up that problem. – DavidC Jun 4 '13 at 15:53
@ David Carraher thank you, bevore I ran the code I enterd Remove["Global*"], however, the Problem is still appearing. hopefully someone can help me out with a solution – Milan Ivica Jun 4 '13 at 15:58

It seems, that you tried to implement such a line with the function threshold[], did you, but it seems to do nothing in your code. The GridLine does the job, but it varies from 0 up to t. However, t is limited by a constant value 105, while your plot easily goes to much higher values. If I understood you right, I propose to take the t limit varying dynamically from the maximum value of the list that has been generated before this limit is evaluated. Which list to use I do not know, it depends upon the nature of your problem, and you should decide. As an example I just took the Flatten[test2[μ, σ, S, P, T], 1] list and introduced

Max[Transpose[Flatten[test2[μ, σ, S, P, T], 1]][[2]]]


instead of the maximal t value into the iterator of the Manipulate statement. It is not the best choice, since it does not really go now to the top of the plot, but is already close to that. I also removed the function threshold. The final code is as follows:

  Manipulate[SeedRandom[seed];
Column[{test2[μ_, σ_, S_, P_, T_] :=
Table[RandomFunction[
GeometricBrownianMotionProcess[μ, σ, S], {0, T,
0.05}]["Path"], {P}];
meanPaths = Mean[test2[μ, σ, S, P, T][[All]]];

ListLogPlot[{meanPaths,
Flatten[test2[μ, σ, S, P, T], 1]}, Joined -> True,
AxesLabel -> {"Time", "St"},
PlotLabel ->
Style["Forecasted Stock Price\n (Brownian Motion)", Bold],
PlotRange -> {{0, T}, {0, 700}},
PlotStyle -> {Directive[{Thick, Red}], Directive[{Thin, Gray}]},
GridLines -> {{}, {t}}, GridLinesStyle -> {Thick, Blue},
ImageSize -> 500],
Graphics[{Blue, Thick, Line[{{0, t}, {t, T}}]}]

}], {{S, 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"}, {{T, 1, "Time"}, 1, 100, 1,
Appearance -> "Labeled"}, {{t, 0, "thresh"}, 0,
Max[Transpose[Flatten[test2[μ, σ, S, P, T], 1]][[2]]],
Appearance -> "Labeled"}, {{seed, 77777, "New Random Case"}, 10000,
999999, 1},
Button["Set Initial Values", {S = 100, μ = 0.01, σ = 0.01,
T = 1, P = 1, t = 100}, ImageSize -> 150],
ControlPlacement -> Left]
`
-