Post Reply 
[VA] SRC #012a - Then and Now: Probability
10-13-2022, 08:14 AM (This post was last modified: 10-13-2022 09:13 AM by J-F Garnier.)
Post: #30
RE: [VA] SRC #012a - Then and Now: Probability
More comments on Fernando's solution and mine.

The basic principle is the same iterative method, starting from the probability matrix at step N, it calculates the probability at step N+1.
Fernando scanned the previous matrix cells (step N-1) and distributes the probabilities to the next step matrix (N), with several statements like:
F=A(I,I)/6 @ B(I+1,I)=B(I+1,I)+F ...
On my side, I scanned the next matrix cells (step N) and computes the cell probability (in one go) from the previous cells (step N-1) with something like:
B(I,J)=(A(I-1,J-1)+A(I-1,J)+A(I,J-1)+A(I,J+1)+A(I+1,J)+A(I+1,J+1))/6

Both methods are equivalent, but Fernando's code is shorter.
However, I would have thought that my method was more accurate since most of the cell probabilities (the inner cells) are computed in one go, with only one division by 6.

But it's the contrary: Fernando's result is exact to 12 places, whereas my result had an "error" of 6 ULP.
To figure out why my answer was less accurate, I tried to mimic Fernando's calculation by not factoring the divisor term but distribute it to each cell term, that is replacing:
B(I,J)=(A(I-1,J-1)+A(I-1,J)+A(I,J-1)+A(I,J+1)+A(I+1,J)+A(I+1,J+1))/6
by
B(I,J)=A(I-1,J-1)/6+A(I-1,J)/6+A(I,J-1)/6+A(I,J+1)/6+A(I+1,J)/6+A(I+1,J+1)/6
In this way our methods sum exactly the same terms, but in a different order.

My result is now 9.51234350205E-6 with a difference of only 2 ULP (instead of 6) from the "exact" value.

I updated my code above accordingly.

Now, the question is: why is it better to distribute the divisions to each term, instead of factoring it?
Is it better just by chance?

J-F
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: [VA] SRC #012a - Then and Now: Probability - J-F Garnier - 10-13-2022 08:14 AM



User(s) browsing this thread: 1 Guest(s)