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

I would like to simulate paths of a Geometric Brownian Motion till it hits one of two barriers, say 10 or 200.

data2 = RandomFunction[
  GeometricBrownianMotionProcess[0.05, .15, 100], {0, 10, .01}, 1]

enter image description here

data3 = %69["Path"]

data4 = {}

j = 1; While[data3[[j, 2]] < 200 && data3[[j, 2]] > 10, 
 data4 = Append[data4, {data3[[j, 1]], data3[[j, 2]]}]; j++]


enter image description here

This works but is clunky and inelegant. Is there someway I can do it better or maybe run the simulation till some barrier is hit instead of truncating the data post simulation?

share|improve this question
Please don't use %69 — your %69 is not the same for anyone else and they will not be able to run it (also, I save only 1 previous output)... – R. M. May 11 '13 at 0:25
No time to try this, but I wonder if Rescale or RescalingTransform might help? – Jagra May 11 '13 at 0:25
@rm-rf I have no clue what %69 even is. When I Shift Enter data2, MMA-9 prints: Temporaldata[1]. Then MMA-9 gives this window right below Temporaldata[1]. That window gave a drop down menu called properties which had path as an option. When I clicked on path, it ran the command %69["path"] and spat out the data. So I just re-used that command to catch that data. The documentation for % doesn't explain what it is so perhaps you can shed some light on it. – Amatya May 11 '13 at 2:43
@Amatya % is the previous input, %% 2 inputs prior, %n n inputs prior (all, as indicated by the In[n] and not by your current position) – R. M. May 11 '13 at 2:45
To be slightly more precise: %n is the nth input not n inputs before current. – Sjoerd C. de Vries May 11 '13 at 6:48
up vote 6 down vote accepted

I am not aware of possibilities to set up RandomFunction with barriers. However, your after-the-fact selection of data can be done somewhat more elegantly indeed:

data4 = TakeWhile[data3, 10 < #[[2]] < 200 &];
share|improve this answer

I didn't manage to make it work with RandomFunction, but if you would consider running simulations without it you could use TruncatedDistribution.

Define the parameters :

mu = 5/100; sigma = 15/100; x0 = 100;

xmin = 80; xmax = 200;

tmin = 1/100; tmax = 10; dt = 1/100;

Define the truncated distribution :

trDist[xmin_, xmax_, mu_, sigma_, x0_, t_] := TruncatedDistribution[{xmin, xmax}, 
   LogNormalDistribution[Log[x0] + (mu - 1/2 sigma^2) t, sigma Sqrt[t]]]

d = Transpose[RandomVariate[LogNormalDistribution[Log[x0] + (mu - 1/2 sigma^2) #, 
   sigma Sqrt[#]], 2] & /@ Range[tmin, tmax, dt]];
paths = Transpose[{Range[tmin, tmax, dt], #}] & /@ d;

d2 = Transpose[RandomVariate[trDist[xmin,xmax,mu,sigma,x0,#], 2] & /@ Range[tmin,tmax,dt]];
paths2 = Transpose[{Range[tmin, tmax, dt], #}] & /@ d2;

Check :

ListLinePlot[paths, PlotRange -> {0, All}, Epilog -> {Black, Line[{{tmin, xmin}, {tmax, xmin}}], Line[{{tmin, xmax}, {tmax, xmax}}]}]


ListLinePlot[paths2, PlotRange -> {0, All}, Epilog -> {Black, Line[{{tmin, xmin}, {tmax, xmin}}], Line[{{tmin, xmax}, {tmax, xmax}}]}]


share|improve this answer

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.