2

Consider :

dist = Parallelize[
   Table[RandomVariate[NormalDistribution[]], {100000}]];

How could I create a recursive function such that :

Subscript[d, 1] = dist[[1]]

Subscript[d, 2] = .95 Subscript[d, 1] + dist[[2]]

Subscript[d, 3] = .95 Subscript[d, 2] + dist[[3]]

And do this till Subscript[d, 100000]

Thank You.

It is surprisingly the first time I run into this.

2
  • Out of curiosity, should the dist[[i]] be 0.05*dist[[i]]? (If not, I'm curious as to the application for this.) Commented Oct 23, 2011 at 4:34
  • @Brett,you will see more, in the "contingency table" question I asked soon after. Simulating some market volatility :-) Commented Oct 23, 2011 at 11:51

2 Answers 2

6

Consider this:

dist = RandomVariate[NormalDistribution[], {100000}];

dist2 = Rest@FoldList[0.95 # + #2 &, 0, dist];

Subscript[d, x_] := dist2[[x]]

I don't normally use Subscript this way; I don't know what may break doing this. If you describe more of your problem, I may have an alternate suggestion.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank You ! The Subscript is just because I am working with my cousin and wrote as He spoke. How would you do ?
@500 I am not saying it's necessarily wrong, just that I am not comfortable with it. I would use something like d[x_] or func[d, x_] rather than attaching a definition to Subscript.
@Mr. Wizard, got it ! I have never dealt with this before, I will adopt your format !
+1 for using generating all 100000 values in one call, instead of using a (Parallel)Table.
@Brett, thank you. Why did you change it to RandomVariate? This is not in version 7. In version 8, is it faster than RandomReal?
|
2

How about using something like

In[1]:= dist = ParallelTable[RandomVariate[NormalDistribution[]], {100000}];//Timing

Out[1]= {0.15601, Null}

In[2]:= folded = FoldList[.95 #1 + #2 &, First@dist, Rest@dist]; // Timing

Out[2]= {0.056003, Null}

which you can compare to

In[3]:= Subscript[d, 1] = dist[[1]];
        Do[Subscript[d, n] = 0.95 Subscript[d, n - 1] + dist[[n]], 
           {n, 2, Length@dist}] // Timing

Out[3]= {1.09607, Null}

In[4]:= Table[Subscript[d, n], {n, 1, Length@dist}] === folded

Out[4]= True

2 Comments

(playing comment tag) -- I was sorry, because I first wrote "how is this different from my answer?" and then realized you acknowledged as much.
@Mr.Wizard: oh - I was wondering. Yeah, I got the notification that you had posted an answer just as I was cleaning up the In/Out numbering - so I decided to post anyway. After all, the Do loop I give does implement something like what the OP originally had in mind. (If it wasn't early Sunday morning, then I might have been quicker and beaten you!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.