blckbird - 1 year ago 127
Python Question

# Generate data with normally distributed noise and mean function

I created a numpy array with n values from 0 to 2pi. Now, I want to generate n test data points deviating from sin(x) normally distributed.

So i figured I need to do something like this:

`t = sin(x) + noise`
. Where the noise must be something like this:
`noise = np.random.randn(mean, std)`
.

However, I do not know how I can calculate the noise when my mean is sin(x) (and not a constant).

Answer Source

The arguments to `numpy.random.randn` are not the mean and standard deviation. For that, you want `numpy.random.normal`. Its signature is

``````normal(loc=0.0, scale=1.0, size=None)
``````

To add noise to your sin function, simply use a mean of 0 in the call of `normal()`. The mean corresponds to the `loc` argument (i.e. "location"), which by default is 0. So, given that `x` is something like `np.linspace(0, 2*np.pi, n)`, you can do this:

``````t = np.sin(x) + np.random.normal(scale=std, size=n)
``````

You could use `numpy.random.randn`, but you have to scale it by `std`, because `randn` returns samples from the standard normal distribution, with mean 0 and standard deviation 1. To use `randn`, you would write:

``````t = np.sin(x) + std * np.random.randn(n)
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download