1
$\begingroup$

I need to create the code that generates a sample based on an adjustable parameter and, later, interactively calculates statistics or builds a plot for the generated sample. Let's insert a slider

Slider[Dynamic[m1], {-2, 2}]

and perform the calculation

(* 1 *) Sample = Dynamic[RandomVariate[NormalDistribution[m1, 0.5], 10]]
(* 2 *) Dynamic[Mean[Sample]]
(* 3 *) Mean[Sample]
(* 4 *) Dynamic[Mean[RandomVariate[NormalDistribution[m1, 0.5], 10]]]

None of 4 items has the required performance. 1: only calculates a sample, 2: returns Mean[{...}] unevaluated for another sample, 3: the same, unevaluated for 3d sample, 4: does calculate Mean for 4th sample.

It's possible to use control for creating static variable (see my question), but now I need the dynamic behavior mentioned above.

$\endgroup$
1
$\begingroup$

Try this:

Manipulate[
 Module[{ms, mr},
  Sample = RandomVariate[NormalDistribution[m1, 0.5], 10];
  ms = Mean[Sample];
  
  mr = Mean[RandomVariate[NormalDistribution[m1, 0.5], 10]];
  {ms, mr}], {m1, -2, 2}]

enter image description here

Have fun!

$\endgroup$
3
  • $\begingroup$ I understood. Module inside Manipulate. Not exactly what I need (for some reasons), but as an intermediate solution, this works. Блвгодарю, Alex. $\endgroup$ – Konstantin Dec 17 '20 at 9:55
  • $\begingroup$ Module you only need if you want to scope the variables ms and mr. If you do not need to scope them you may remove Module. You can also do that using Dynamic but Manipulate is more straightforward. $\endgroup$ – Alexei Boulbitch Dec 17 '20 at 10:02
  • $\begingroup$ Sure, it's clear $\endgroup$ – Konstantin Dec 17 '20 at 10:09

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.