Speed optimisation of consecutive loops

Very simple question, which for some reason I am unable to find a proper solution with Nest or NestWhile. My issue is speed.

The idea is simple.

1) Construct a list of length T according to a linear rule and a limitation (bottom loop) 2) Substitute into a linear rule, store numerical results into vectors. Repeat G times creating 4 lists of length G 3) Average and subtract the 4 lists, repeat D times

The sets I want to create are SampleJU, SampleNJU, SampleJW, SampleNJW who will be averaged and compared D times. Also please note that at the end of the middle loop I reset Y and V, so that the next repetition will collect new sets, without the old values.

The key issue is that during the construction of the first list (Y) some parameters need to be sampled and a quantity needs to be positive. This has been giving me issues. I am looking for a way, either via pure functions or Nest / NestWhile structures, to optimise this structure. The only result of interest is the final Count result at the end.

A very crude code that consists of 3 consecutive loops follows, to illustrate a working variation. It is horrible but it works. Suffice to say that any help and advice you may offer will be deeply appreciated.

Initialising

ClearAll["Global*"]
a := 0.0210543
m := 0.034035
r := -0.67
SampleJU := {}
SampleNJU := {}
SampleJW := {}
SampleNJW := {}
HR := {}
UDif := {}
Y := {}
V := {0.8427865991162602}
Gama := 5
f[x_] := a + x*Sqrt[V[[-1]]]
T:=100
A:=100
G:=100


And the body of the code

Do[{
Do[{
Do[{{
m = RandomVariate[UniformDistribution[{-1, 1}]],
EE = RandomVariate[BinormalDistribution[{0, 0}, {1, 1}, r]],

eV = EE[],
Vnew = f[eV];
While[Vnew < 0,
EE = RandomVariate[
BinormalDistribution[{0, 0}, {1, 1}, r]]; eV = EE[];
Vnew = f[eV]],
eY = EE[],
Ynew = m + eY*Sqrt[V[[-1]]]},
Y = Append[Y, Ynew],
V = Append[V, Vnew]}, {T}],

SumY = Total[Y],
JW = Exp[5 + SumY],
NJW = Exp[10 + SumY],
SampleJW = Append[SampleJW, JW],
SampleNJW = Append[SampleNJW, NJW],
JU = JW^(1 - Gama)/(1 - Gama),
NJU = NJW^(1 - Gama)/(1 - Gama),
SampleJU = Append[SampleJU, JU],
SampleNJU = Append[SampleNJU, NJU],
Y = {},
V = {0.8427865991162602}}, {G}],

UDif = Mean[SampleNJU] - Mean[SampleJU],
HR = Append[HR, UDif],
SampleJU:={},
SampleNJU:={},
SampleJW:={},
SampleNJW:={}
}, {A}]
Count[HR, _?Positive]

• Could you perhaps reduce this to a Minimum Working Example of your problem? It will make it easier and more likely for others to help you. – Quantum_Oli Sep 13 '16 at 11:03
• Thank you for the comment, I mis-clicked while writing. The question is now complete along with a working example. – Titus Sep 13 '16 at 11:36
• This example isn't quite minimal ... a few comments on making it clearer: use a; b; c for writing three statements. Do not use {a, b, c} if you don't want to create a list. It makes the code unreadable. Are you aware of the distinction between = and :=? You have unusual and unnecessary (albeit not incorrect) uses of :=. There's a lot of hard to read code here so I cannot immediately tell, but: are you sure you can't use Table? Repeated Append is slow and should not be used. Use Table whenever possible, use Sow/Reap otherwise. – Szabolcs Sep 13 '16 at 12:17
• Every single x = Append[x,y] reallocates the whole list x, leading to quadratic complexity in a loop. – Szabolcs Sep 13 '16 at 12:17
• Nesting of Do is unnecessary. Do, like Table, accepts multiple iterator statements, from outer to inner, e.g. Do[Do[Print[{i, j}], {j, 4}], {i, 4}] becomes Do[Print[{i, j}], {i, 4}, {j, 4}]`. – rcollyer Sep 13 '16 at 14:01