Strange behaviour with Gaussian random distribution

Gabriel

I'm running a bit of code whose purpose is to take a list/array of floats and an associated list/array of the same length acting as an "error" and shuffle the first list around according to a Gaussian distribution.

This is a MWE of the code:

import random
import numpy as np

def random_data(N, a, b):
    # Generate some random data.
    return np.random.uniform(a, b, N).tolist()

# Obtain values for x.
x = random_data(100, 0., 1.)
# Obtain error/sigma values for x.
x_sigma = random_data(100, 0., 0.2)

# Generate new x values shuffling each float around a
# Gaussian distribution with a given sigma.
x_gauss = random.gauss(np.array(x), np.array(x_sigma))

print x-x_gauss

What I find is that the result of doing x-x_gauss is a list of floats that is always either positive or negative. This means the random.gauss call is always assigning either a larger new value for each float in x or a smaller one for all values in x.

I would expect the random.gauss call to shuffle the floats in x around its values both to the right and to the left, since this is a random process.

Why is this not happening? Am I understanding something wrong about the process?

unutbu

This is the definition of random.gauss:

def gauss(self, mu, sigma):
    random = self.random
    z = self.gauss_next
    self.gauss_next = None
    if z is None:
        x2pi = random() * TWOPI
        g2rad = _sqrt(-2.0 * _log(1.0 - random()))
        z = _cos(x2pi) * g2rad
        self.gauss_next = _sin(x2pi) * g2rad

    return mu + z*sigma

Notice that is is generating one value for z, and returning mu + z*sigma. Since mu and sigma are numpy arrays, this calculation is being done element-wise. Since sigma is positive, the shift z*sigma is either always positive or negative, depending on the sign of z


If you are using NumPy, unless there is a specific reason to do otherwise, I would use the np.random module to generate these values. It would be quicker than using a Python loop with calls to random.gauss:

import numpy as np

N = 100
x = np.random.uniform(0., 1., size=N)
x_sigma = np.random.uniform(0., 0.2, size=N)

z = np.random.normal(0, 1, size=N)
x_gauss = x + z*x_sigma

print x-x_gauss

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create a matrix ( mx n) with gaussian random distribution

From Dev

Elm - Random Number on init strange behaviour

From Dev

Elm - Random Number on init strange behaviour

From Dev

How to make random loss bits follow Gaussian distribution

From Dev

JavaScript Math.random Normal distribution (Gaussian bell curve)?

From Dev

What's causing this strange random number generator behaviour?

From Dev

Strange random-seed behaviour after create-<breeds>-with

From Dev

Strange behaviour when using random.shuffle on numpy.array

From Dev

Strange random-seed behaviour after create-<breeds>-with

From Dev

Strange behaviour when using random.shuffle on numpy.array

From Dev

Fitting an image to Gaussian distribution

From Dev

Multiple Gaussian distribution

From Dev

Dealing with normal (Gaussian) distribution

From Dev

How generate pseudo-random numbers in uniform and gaussian distribution without float/double numbers?

From Dev

How would I produce random numbers between two values with a Gaussian distribution

From Dev

How would I produce random numbers between two values with a Gaussian distribution

From Dev

Draw multivariate Gaussian distribution samples using Python numpy.random.randn

From Dev

Gaussian random generation with the condition

From Dev

Fail to generate gaussian distribution with seed

From Dev

Gaussian distribution for censored regression with cenreg

From Dev

Inverse of the cumulative gaussian distribution in R

From Dev

Multivariate Gaussian distribution formula implementation

From Dev

Variance using Gaussian distribution Python

From Dev

square of a number using gaussian distribution

From Dev

Strange addEventListener behaviour

From Dev

MATLAB subs(): Strange behaviour

From Dev

Strange behaviour of size utility

From Dev

IronPython strange behaviour

From Dev

htaccess issue - strange [OR] behaviour

Related Related

  1. 1

    Create a matrix ( mx n) with gaussian random distribution

  2. 2

    Elm - Random Number on init strange behaviour

  3. 3

    Elm - Random Number on init strange behaviour

  4. 4

    How to make random loss bits follow Gaussian distribution

  5. 5

    JavaScript Math.random Normal distribution (Gaussian bell curve)?

  6. 6

    What's causing this strange random number generator behaviour?

  7. 7

    Strange random-seed behaviour after create-<breeds>-with

  8. 8

    Strange behaviour when using random.shuffle on numpy.array

  9. 9

    Strange random-seed behaviour after create-<breeds>-with

  10. 10

    Strange behaviour when using random.shuffle on numpy.array

  11. 11

    Fitting an image to Gaussian distribution

  12. 12

    Multiple Gaussian distribution

  13. 13

    Dealing with normal (Gaussian) distribution

  14. 14

    How generate pseudo-random numbers in uniform and gaussian distribution without float/double numbers?

  15. 15

    How would I produce random numbers between two values with a Gaussian distribution

  16. 16

    How would I produce random numbers between two values with a Gaussian distribution

  17. 17

    Draw multivariate Gaussian distribution samples using Python numpy.random.randn

  18. 18

    Gaussian random generation with the condition

  19. 19

    Fail to generate gaussian distribution with seed

  20. 20

    Gaussian distribution for censored regression with cenreg

  21. 21

    Inverse of the cumulative gaussian distribution in R

  22. 22

    Multivariate Gaussian distribution formula implementation

  23. 23

    Variance using Gaussian distribution Python

  24. 24

    square of a number using gaussian distribution

  25. 25

    Strange addEventListener behaviour

  26. 26

    MATLAB subs(): Strange behaviour

  27. 27

    Strange behaviour of size utility

  28. 28

    IronPython strange behaviour

  29. 29

    htaccess issue - strange [OR] behaviour

HotTag

Archive