Python – Vectorize the normal distribution python

Vectorize the normal distribution python… here is a solution to the problem.

Vectorize the normal distribution python

Probably a stupid question so please forgive me, but,

from here :

N = 10000
x = 10 + 2*np.random.randn(N)
y = 5 + x + np.random.randn(N)

def neg_loglike(const,coef,std):
    mu = const + coef*x
    print(mu.shape)
    return -1*stats.norm(mu, std).logpdf(y).sum()

seed = np.array([1,1,1])
res = minimize(neg_loglike, seed, method = 'Nelder-Mead', 
            options={'disp': True})

mu is an array/vector in this case – does stats.norm generate a normal distribution for each value of x? What it means that a normal distribution has many ways… (apparently I didn’t understand this)

Finally, it is the correct interpretation of the optimal value in res.x
These parameters generate a set of normal distributions that maximize the probability of seeing y in the distribution…?

Solution

Yes, norm accepts a vector containing loc and scale parameters and treats each input as its own distribution. Note that you can enter a vector for one parameter and a scalar for another, as is the case in the link you are referencing (where scale is 1 and loc is vector x).

For example:

from scipy.stats import norm

norm(loc=[1,2,3], scale=1).logpdf([4,5,6])

Output:

array([-5.41893853, -5.41893853, -5.41893853])

Related Problems and Solutions