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 just want to generate one stochastic process, whereas I want to be able to manipulate the start value and the volatility. Furthermore, the process should stop when it reaches 5

However, as soon as I use the SeedRandom[seed] function some error occurs. Hope you can help me. I also want the function to start at 0 and not at 1 Definition function of: (* x[t+1]=x[t]+μ (drift) + shock[t]; which stops at threshold*)

My code:

σ = 0.2
SeedRandom[85]; shock1 = 
RandomVariate[NormalDistribution[0, σ],(*time interval/Length of paths*) 10]

μ = 0.1 

 step[x_, μ_, shock1_] := 
 If[x == 5, 5, If[x + μ + shock1 < 5, 5, x + μ + shock1]]


p1 = FoldList[step, 10, shock1]

ListLogPlot[p1, Joined -> True]

This Works perfectly, however, as soon as I enter the following i get some Problems:

Manipulate[SeedRandom[seed]; 
 shock1 = RandomVariate[NormalDistribution[0, σ1], 10]; μ =
   0.1; step[x_, μ_, shock1_] := 
  If[x == 5, 5, If[x + μ + shock1 < 5, 5, x + μ + shock1]]; 
 ListLogPlot[FoldList[step, S, shock1], Joined -> True], {S, 10, 
  50}, {σ1, 0.1, 1}]

I get the following error-message: "Argument seed in SeedRandom[seed] should be an integer or a string. "

But when I enter:

Manipulate[SeedRandom[85]; 
 shock1 = RandomVariate[NormalDistribution[0, σ1], 10]; μ =
   0.1; step[x_, μ_, shock1_] := 
  If[x == 5, 5, If[x + μ + shock1 < 5, 5, x + μ + shock1]]; 
 ListLogPlot[FoldList[step, S, shock1], Joined -> True], {S, 10, 
  50}, {σ1, 0.1, 1}]

and try to manipulate the volatility just nothing happens.

do you have any solutions for my problem?

share|improve this question
    
When you say that the first part works perfectly. It doesn't for me when I copy and paste that first section of code. If you quite the kernel, does the first part still work correctly? –  Jonathan Shock Jun 5 '13 at 14:34
    
The Initialization option might be useful here... –  J. M. Jun 5 '13 at 14:34
1  
You might want to use Clip in place of the two nested If. –  b.gatessucks Jun 5 '13 at 14:40
    
@Jonathan Shock I alos have your problem now. I do not know why. Although I enter Remove["Global`*"] before I enter everything. just before it worked, no other notebooks are open. –  Milan Ivica Jun 5 '13 at 14:45
1  
@MilanIvica It would be easier to simply generate a matrix of processes and then choose those which satisfy your condition, IMO. –  Rod Jun 6 '13 at 15:22
show 8 more comments

1 Answer 1

up vote 1 down vote accepted

There are a couple of details that make your function misbehave. Here is a version that takes in {currentx, mu} and a term of the shock list and outputs {newx, mu}. I changed your Ifs to a single Which. The manipulate now seems to work fine. I also moved the function definition outside the Manipulate, which is more efficient (there's no need to define the function at each pass through the Manipulate).

step[{x_, mu_}, s_] := {Which[x == 5, 5, x + mu + s > 5, 5, True, x + mu + s], mu};

Manipulate[sig = 0.2; mu = 0.05; len = 500; SeedRandom[seed]; start=1;
  shock = RandomVariate[NormalDistribution[0, sig], len];
  p1 = FoldList[step, {start, mu}, shock];
  ListLogPlot[p1[[All, 1]], Joined -> True], {seed, 1, 85, 1}]

enter image description here

There are a couple of subtleties in this problem. For instance, one of the comments to the OP suggested replacing the logic of the Which (or If in the OP) with Clip. This might not be a good idea because the goal is to freeze the process once it reaches 5. When clipping, the process might go above 5 (and be clipped) but later drop below 5 again. It is also more tricky than it seems to generate a lot of paths and then choose the ones you want: again, the paths would need to be processed to get the proper clipping.

share|improve this answer
    
thank you for your help an remarks –  Milan Ivica Jun 7 '13 at 6:49
add comment

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.