question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Normal distribution: generation algorithm

See original GitHub issue

I’ve been using functions RandomNormal from DataGen.cs and Population from Population.cs in order to visualize the distribution of a given variable https://github.com/ScottPlot/ScottPlot/blob/0be0921e508656e19323642974a8c5d441bc686a/src/ScottPlot4/ScottPlot/Statistics/Population.cs#L40-L44

I’m no expert whatsoever in statistical algorithms, but it seems that both functions make use of RandomNormalValue where some kind of Box-Muller algorithm is implemented to generate gaussian numbers:

https://github.com/ScottPlot/ScottPlot/blob/0be0921e508656e19323642974a8c5d441bc686a/src/ScottPlot4/ScottPlot/DataGen.cs#L195-L200

However, the gaussian data generated seems to be sistematically underestimated as shown below, where the grey vertical line represents the mean and the bell curve appears to be slightly offsetted to the left. image

There’s an article in Wikipedia where the Box-Mueller algorithm is explained. It states that Suppose U1 and U2 are independent samples chosen from the uniform distribution on the unit interval (0, 1).

Since Random.NextDouble() does in fact return the value 0, the current computation of both u1 and u2 will eventually return some 1s.

Therefore, I’ve been using the following modification of RandomNormalValue:

public double SampleGaussian(Random random, double mean, double stdDev)
    {
        double u1 = NextDouble(random);
        double u2 = NextDouble(random);

        double y1 = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);    // Math.Cos is also fine
        return mean + stdDev * y1;

        double NextDouble(Random random)
        {
            return ((double)random.Next(1, Int32.MaxValue)) / Int32.MaxValue;    // random.Next includes 1 and exludes Int32MaxValue
        }
    }

This modification provides a more visually mean-centered gaussian curve: image

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
arthuritscommented, Oct 31, 2022

My bad @bclehmann! I totally messed the whole comments/review options. I’ll pay more attention the next time. Anyways, it’s good to see that @swharden already merged it.

1reaction
bclehmanncommented, Oct 31, 2022

Ah, since it says “Pending” it means it was part of a review, and it looks like you didn’t complete the review (there’s a menu with “Approve”, “Request Changes”, and “Reject” that you have to click on). Alternatively, you can leave a comment without starting a review, and then the comment will be applied immediately.

At any rate thank you for noticing that, I guess I was a little bit on autopilot 😕

Read more comments on GitHub >

github_iconTop Results From Across the Web

An Algorithm for Generating Random Numbers with ...
A new algorithm is suggested based on the central limit theorem for generating pseudo-random numbers with a specified normal or Gaussian probability density ......
Read more >
Box–Muller transform
The Box–Muller transform, by George Edward Pelham Box and Mervin Edgar Muller, is a random number sampling method for generating pairs of independent, ......
Read more >
Generation of N-dimensional normally distributed random ...
Ziggurat method is the fastest algorithm for generating normal random numbers. It is more efficient than Box-Muller method but is more complicated algorithm....
Read more >
Generate random numbers following a normal distribution ...
It correctly produces values with a normal distribution. The math is easy. You generate two (uniform) random numbers, and by applying an formula ......
Read more >
11 Gaussian Random Number Generators
Rapid generation of high quality Gaussian random numbers is a key capability for simulations across a wide range of disciplines. Advances in computing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found