How to use scipy.stats function to expect?

I expected

scipy.stats.norm.expect(loc = 55, scale = 1)

returns a distribution average of 55, instead it returns 3.9096876333292135e-108. Am I using this incorrectly? Can someone explain to me how this function works. Thanks

+4
source share
2 answers

I think this should be a mistake. You correctly understood the manual, but it turns out that if it is "loc"more than 35.6, then everything becomes abnormal. Otherwise, it works as expected.

+1
source

Problem:

import scipy.stats
#expect(func, loc=0, scale=1, lb=None, ub=None, conditional=False, **kwds)

for i in range(26, 55):
    print scipy.stats.norm.expect(loc=i,scale=1),

output:

26.0
27.0
28.0
29.0
30.0
31.0
32.0
33.0
34.0
35.0
3.36373206533e-10
1.24696133938e-13
1.7005527212e-17
8.53164410131e-22
1.57463854604e-26
1.06913991702e-31
2.670507049e-37
2.45390887262e-43
8.29523418554e-50
1.03158350625e-56
4.71938759035e-64
7.94277793838e-72
4.91773037097e-80
1.12011477121e-88
9.38568238273e-98
2.89317497775e-107
3.28087032536e-117
1.4932849185e-127
3.63870849201e-118

For a larger scale(standard deviation) “disconnect” behavior moves further, as one would expect.

problem can be 'fixed' by setting the lower and upper bounds explicitly, :

import numpy as np

for i in np.arange(5,100,5):
    print i,scipy.stats.norm.expect(loc=55,lb=-i,ub=i,scale=1) 

:

5 0.0
10 0.0
15 0.0
20 2.01210143973e-267
25 1.05364770562e-196
30 7.87517644756e-137
35 8.61623210583e-88
40 1.40277331283e-49
45 3.46495136419e-22
50 1.42791169386e-05
55 27.1010577196
60 54.9999827474
65 55.0
70 55.0
75 55.0
80 55.0
85 55.0
90 55.0
95 55.0

. (. init rv_continous), , Numpy 'inf'. +/- np.inf, , OP:

for i in np.arange(5,60,5):
    print i,scipy.stats.norm.expect(loc=i,lb=-np.inf,ub=np.inf,scale=1) 

:

5 5.0
10 10.0
15 15.0
20 20.0
25 25.0000000007
30 30.0
35 35.0
40 1.57463854604e-26
45 1.03158350625e-56
50 9.38568238273e-98
55 3.90968763333e-108

, expect :

    # Silence floating point warnings from integration.
    olderr = np.seterr(all='ignore')
    vals = integrate.quad(fun, lb, ub, **kwds)[0] / invfac

, , integrate.quad , +/- np.inf. Fortran, , ( , ) Wikipedia.

+1

All Articles