1
$\begingroup$

My code is

m = RandomVariate[NormalDistribution[], 100];
c = 0.1;
f1 = RandomVariate[NormalDistribution[], 100];
F = Sqrt[c]*m + Sqrt[1 - c]*f1;

I would like to calculate F in the first step with the first element of m and the first element of f1; in the second step with the second element of m and the second element of f1 and so on, for all 100 random numbers.

$\endgroup$
7
  • $\begingroup$ and how can i do if i have 100 random numerbs and wouldl ike to calculate F1 for m[1] and f1[1].....m[100] and f1[100] $\endgroup$ – Sarah Mar 13 '19 at 11:59
  • 2
    $\begingroup$ I do not understand the question. The vector F contains all 100 results. $\endgroup$ – Szabolcs Mar 13 '19 at 14:10
  • 5
    $\begingroup$ If I'm interpreting you right, the code that you've written down should work. If it doesn't do what you want, either quit the kernel and try again, or edit your post to explain what it is you need. $\endgroup$ – march Mar 13 '19 at 16:23
  • 4
    $\begingroup$ Like the commenters above remarked: there is no need to iterate the calculation of F over 100 indices. You can just add and multiply lists in Mathematica. $\endgroup$ – Sjoerd Smit Mar 13 '19 at 16:57
  • $\begingroup$ I was just finishing up what I think is a good answer, when this question snapped closed. I think the OP's intent is clear and I would to see this question reopened. $\endgroup$ – m_goldberg Mar 13 '19 at 22:16
1
$\begingroup$

I think it would be useful for you to experiment with your computation so you can better understand how Mathematica's numeric functions work with arrays of values. I also recommend that you read this tutorial. It describes computing with functions that have the Listable attribute. Such functions automatically act on all the elements of any array they are given.

An easy way to make the the kind of experiments I'm recommending is to transform your computation into a function, which is very easy to do.

f[n_] :=
   Module[{c = 0.1, m, f1},
   {f1, m} = RandomVariate[NormalDistribution[], {2, n}];
   Sqrt[c] m + Sqrt[1 - c] f1]

Note that no explicit element-by-element operations appear in the definition of f. Nor are any such operations needed since all the arithmetic operators in the definition have the Listable attribute.

Now that we have f, we can experiment with samples of arbitrary size. We will see that they, too, can used as if they were scalar expressions.

It is best to start with small n.

SeedRandom[1]; f4 = f[4]

{0.949105, 0.602169, 0.289773, 0.950457}

We can get the root-mean-square without writing any explicit element-by-element code.

Sqrt[Mean[f4^2]]

0.750124

Generating a large sample is very quick.

AbsoluteTiming[SeedRandom[42]; sample = f[100000]][[1]]

0.007525

I got a sample of 100000 variates in just a bit over 7.5 milliseconds from my eight-year-old and not very fast iMac. Let's look into how the sample is distributed.

{Mean[#], StandardDeviation[#]}&[sample]

{-0.0006108, 1.00239}

h = Histogram[sample, Automatic, "Probability"];
With[{μ = 0, σ = 1}, p = Plot[(E^(-(x - μ)^2/(2 σ^2))/5), {x, -5, 5}]]
Show[h, p]

pdf-plot

The sample seems to be distributed as if it were directly sampled form NormalDistribution[].

As a final example, let's approximate the cumulative distribution of a sample returned by f.

SeedRandom[42];
Module[{n = 1000, sample, x, y},
  sample = f[n];
  x = Sort[sample];
  y = Range[n]/N[n];
  ListPlot[Transpose[{x, y}]]]

cdf_plot

I hope this answers convinces you that the Wolfram Language is designed so that almost anything you might want to do with a sample made by f can be done without loops and indexing into elements of the sample.

$\endgroup$
-1
$\begingroup$

for your question in the comments use

 Sqrt[c]*m[[#]] + Sqrt[1 - c]*f1[[#]] & /@ Range[100]   

or use the function

F1[x_]:=Sqrt[c]*m[[x]] + Sqrt[1 - c]*f1[[x]]

and you can get F1[1],F1[2]...F1[100]

or F1/@Range[100] to get all of them

$\endgroup$
1
  • 3
    $\begingroup$ You don't even need to do that. All these functions are Listable, so just evaluating the code as the OP has written it does the same thing. $\endgroup$ – march Mar 13 '19 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.