2
$\begingroup$

I have a number of possible outcomes $R=\{r_i\}$, with $r_i\in\mathbb{Z}$ and corresponding probabilities of occurrence $P=\{p_i \}$, with $p_i \in [0,1]$ and $\sum_i p_i=1$.

I am wondering whether one can construct some animation, e.g. in a DiscretePlot, where at each step one adds to the previous point a number randomly chosen from $R$ with probabilities from $P$. Of course, that would be a type of generalised random walk.

Does Mathematica allow for such a thing? Any help appreciated!

$\endgroup$
2
  • $\begingroup$ You might want to look up RandomChoice[]. $\endgroup$ Mar 25, 2016 at 0:43
  • $\begingroup$ Thanks for the reply. Indeed with RandomChoice[] I can create a list of elements from $R$, chosen in a way respecting the assigned probabilities of occurrence, so my problem is now reduced to finding a way to animate this. $\endgroup$
    – AG1123
    Mar 25, 2016 at 0:48

2 Answers 2

2
$\begingroup$
R = {1, 2, 3, -1, -2, -3};
P = {0.1, 0.15, 0.25, 0.05, 0.05, 0.4};
n = 20 (*number of time steps*)
x = RandomChoice[P -> R, n];
Animate[ListPlot[Accumulate[x[[1 ;; a]]], Filling -> Axis], {a, 1, n, 
  1}, AnimationRunning -> False]
$\endgroup$
7
  • $\begingroup$ This is great, thanks! There is only one thing missing now. I would like the time axis to remain the same, so in your example be from 0 to 20 from the start, instead of constantly expanding. How can I do this? $\endgroup$
    – AG1123
    Mar 25, 2016 at 1:05
  • 1
    $\begingroup$ @AG1123 You should be able to use PlotRange to set a static plot range throughout the animation. $\endgroup$
    – MarcoB
    Mar 25, 2016 at 1:06
  • $\begingroup$ Thanks @MarcoB. Indeed I could use PlotRange to set a static range but the problem I have is that I want only the "time" axis to be static. Not sure how I can do that with PlotRange. $\endgroup$
    – AG1123
    Mar 25, 2016 at 1:14
  • 1
    $\begingroup$ I can think of this right now: Animate[ListPlot[Accumulate[x[[1 ;; a]]], Filling -> Axis, PlotRange -> {{0, n}, {Min[Accumulate[x]], Max[Accumulate[x]]}}], {a, 1, n, 1}, AnimationRunning -> False] Anyone has a better opinion? $\endgroup$
    – MathX
    Mar 25, 2016 at 1:16
  • 1
    $\begingroup$ Oh you want ONLY t to be static? Maybe PlotRange -> {{0, n}, {Full, Full}} $\endgroup$
    – MathX
    Mar 25, 2016 at 1:22
1
$\begingroup$

I am not sure why you are assigning probabilities if according to you - and correct me if i am wrong - a point could just randomly be selected. I think you need a Metropolis Acceptance Criterion for determining if you can move to a new position or stay at the current location

here is a little code that may help however, I do not know if you are looking for a solution in 3D:

points = RandomReal[{-1, 1}, {100, 3}];
probabilities = Flatten@RandomReal[{0, 1}, {100, 1}];
site = RandomChoice@points;
pos = First @@ Position[points, site];
plot = Last@Reap@Table[newsite = 
   First @@ (RandomChoice@probabilities // 
      Position[probabilities, #] &);
  If[RandomReal[] < (probabilities[[newsite]]/probabilities[[pos]]), 
   pos = newsite]; Sow[points[[pos]]] ;
  , {i, 500}] // #[[1]] &;
Animate[Graphics3D[{Line[Accumulate@plot[[1 ;; a]]]}, 
Axes -> True], {a, 1, Length@plot, 1}]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks for taking the time to reply to my question. However, my aim is to make a generalised 2d random walk with multiple possibilities at each step, with different probabilities of occurrence. The answer by @Behzad Nazari, gives an example of what I want to achieve. $\endgroup$
    – AG1123
    Mar 26, 2016 at 2:16
  • $\begingroup$ I am not familiar with the Metropolis Acceptance Criterion, so I can not comment on this part of your reply. $\endgroup$
    – AG1123
    Mar 26, 2016 at 2:17
  • $\begingroup$ let's assume you are standing at crossroads and there are four directions each direction with a particular probability associated with it. The point where you are currently standing also has a certain probability. You roll a dice to decide where to move. The dice tells you to move East. Now you calculate the Min(1 , probability(east)/probability(center)). If the ratio comes out to be > 1 then the Min is 1 so you accept to move to the east with a probability of 1. If the ratio is < 1 than the minimum will be the ratio and therefore you must stay at the center. You roll the dice again to repeat $\endgroup$
    – Ali Hashmi
    Mar 26, 2016 at 6:08
  • $\begingroup$ There is no point in assigning probabilities to all the points in space otherwise. In your case you will just move to the new point (whatever the dice shows you) without considering that the probabilities associated with the new point and the current point in space allow you to make the move $\endgroup$
    – Ali Hashmi
    Mar 26, 2016 at 6:10

Your Answer

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

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