Jump to content

User talk:Ledrug: Difference between revisions

Line 104:
If you want you can tell me if you see something that could be improved in the D version of the code. In some cases it's nice to receive a little of code review on Rosettacode.
: Since LZW deals with bit streams of variable-length token, a few casts are probably inevitable. I know nothing about D language, so I can't honestly comment on the D code, but since the C code is so messy, anything you did there would likely be an improvement in terms of safety and readability. If I get the time, it'd probably be a better idea for me to rewrite the C code cleanly instead. --[[User:Ledrug|Ledrug]] ([[User talk:Ledrug|talk]]) 17:36, 16 May 2014 (UTC)
 
== Finding pi using MonteCarlo method C Implementation ==
 
I didn't know if you were tracking the discussion page so I decided to copy our conversation. I hope you don't mind:
 
: Randomly throwing a point in a square, and it has chance p of being in the circle, while (1-p) chance of otherwise. If you throw N points and count the number of times n that they landed in the circle, n would follow [[wp:binomial distribution]] (look up the variance formula there). Here we are taking p = n/N as the ratio between areas of circle and square, but n is subject to statistical fluctuation. Assuming that we had the senses to throw a large enough N so n/N wouldn't be a completely bogus estimate of p, but we'd still like to know how far off it could be from p's true value. This is where the variance comes in: it tells you, given N and a rough knowledge of p, how much uncertainty of n (and p) one should expect.
 
: If you want to use the stddev formula, then each <math>x_i</math> takes the value of either 1 (landing in circle) or 0 (not). The average is <math>\mu = p</math> as mentioned above; now <math>\sigma^2 = {1\over N} \sum (x_i - p)^2</math>. Note that there are going to be about <math>Np</math> of those <math>x_i</math>s with value <math>1</math>, and <math>N(1-p)</math> with value <math>0</math>, so <math>\sum(x_i - p)^2 \approx Np(1-p)^2 + N(1-p)(0-p)^2 = Np(1-p)</math>. See how it comes back to the same formula? --[[User:Ledrug|Ledrug]] ([[User talk:Ledrug|talk]]) 06:17, 5 May 2014 (UTC)
 
:: Thank you very much that explains the origin of the formula. Even though following your reasoning the formula should be:
:: <math> \sum(x_i - p)^2 \approx Np(1-p)</math>,
:: But because we have a factor of <math> {1\over N} </math> we must take into account, then the resulting stddev should be:
:: <math>\sigma^2 = p(1-p)</math>,
:: Meaning that the formula implemented has an extra factor of <math> {1\over N} </math> inside the square root and a factor of <math> p </math> outside of the square root, meaning:
:: error = val * sqrt(val * (1 - val) / sampled) * 4, when it should be:
:: error = sqrt(val * (1 - val)) * 4;
:: Am I missing something else here? Sorry for the intrigue I'm no expert in probability, but I'm curious as to the implementation.-[[User:Chibby0ne|Chibby0ne]] ([[User talk:Chibby0ne|talk]]) 17:18, 17 May 2014 (UTC)
 
--00:17, 18 May 2014 (UTC)
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.