# averaging over large number of random realizations

I want to run following for few hundreds of a random variable set. This calculates:

1. generate normal random variable
2. find gain with squaring
3. calculate performance metric
4. find max of min of possible M+1 performance metric when a changes (0,1). I have other case with another parameter b which changes (0,1).
5. find the average for 100 or 1000 random realizations
6. find same for different p values

Finally I want to save it as a table with different p and M.

variable[M_] := RandomVariate[NormalDistribution[0, 1], M + 1];
gain[M_, d_, t_] := variable[M]^2/(d/(M + 1))^t;
perform[p_, a_, m_, M_, d_, t_] :=
1/(M + 1) p ((1 - a)/(2 - a)) a^(
m - 1) (Product[gain[M, d, t][[i]], {i, 1, m}]);
maxminperform[p_, M_, d_, t_] :=
Max[Table[
Min[Table[perform[p, a, m, M, d, t], {m, 1, M + 1, 1}]], {a,
0.00001, 0.999, 0.001}]];
avgperform[p_, M_, d_, t_, itr_] :=
Mean[Table[maxminperform[p, M, d, t], {i, 1, itr}]];



Module[{n = 0.001, η = 0.7, σ = 1, k = 7, d = 6, t = 3,
itr = 5},
Export["table.xls", {Table[{avgperform[p, 2, d, t, itr],
avgperform[p, 4, d, t, itr], avgperform[p, 6, d, t, itr]}, {p,
1, 10, 1}] // N}]]

I understand that this takes really long time when I run.

Can someone help me to faster this code?

• General advice to be found here. This looks like it should benefit from Memoization a great deal, e.g. do you need SetDelayed for variable[M_]? – gwr Jul 25 '16 at 11:22
• when you compute Product[gain[][[i]]] , each factor in the product is computed using a newly generated random distribution. Is that your intent? You might want in there Product@@gain[][[;;m]] to compute it only once. It will be faster but obviously change the result. – george2079 Jul 25 '16 at 15:24