Manoj Kumar
Manoj Kumar

Reputation: 73

Understanding scipy.stats.norm.rvs()?

In scipy.stats.norm.rvs() the argument scale denotes standard deviation but in the below piece of code sigma_list refers to an array. How does the code actually work?

Where sigma_list is obtained by following code:

sigma=0.06

mask=(x > 0.65) & (x < 0.8)

sigma_list=sigma+mask*0.03

sigma_list

y = sp.stats.norm.rvs(scale=sigma_list, size=200)

Even the standard deviations of both sigma_list and y are also not matching

I want to know the working of the above scipy module

sorry, i didn't mention that x is an array of values between 0 and 1

Upvotes: 7

Views: 35866

Answers (2)

marcio-menezes
marcio-menezes

Reputation: 11

First you should have created a variable called x. The size of this variable should be 200, since this is the size of the generated random variable y.

import numpy as np
x = np.linspace(0, 1, 200)

Then the mask is selecting every sample of x that is greater than 0.65 and less than 0.8. The variable mask will be a Boolean vector with the same size as x, i.e, 200 samples. This mask will have samples with values True or False. For each sample of the array x that satisfies the condition (0.65 < x < 0.8), the value of the corresponding sample of the mask will be True, otherwise it will be False.

When you multiply a Boolean by a number, the Boolean behaves as an integer with values 0 (False) or 1 (True). So the multiplication mask * 0.03 results in 0.03 where 0.65 < x < 0.8, and 0 otherwise.

So this code does simply that:

  • For 0.65 < x < 0.8 the standard deviation will be 0.06 + 0.03, i.e., 0.09;
  • Otherwise the standard deviation will be 0.06

Upvotes: 0

tupui
tupui

Reputation: 6528

In your code, the mask will be either True or False here. So if you do some addition or subtraction, it is respectively translated into 1 or 0.

Then the result of sigma_list is not a list nor an array but a floating value. Looking at the documentation, you can see its usage.

rvs(loc=0, scale=1, size=1, random_state=None)

If you look at the code (line 2771) you have:

loc : array_like, optional Location parameter (default=0).

size : int or tuple of ints, optional Defining number of random variates (Default is 1). Note that size has to be given as keyword, not as positional argument.

random_state : None or int or np.random.RandomState instance, optional If int or RandomState, use it for drawing the random variates. If None, rely on self.random_state. Default is None.

Upvotes: 4

Related Questions