本文簡要介紹 python 語言中 scipy.stats.rv_histogram.expect
的用法。
用法:
rv_histogram.expect(func=None, args=(), loc=0, scale=1, lb=None, ub=None, conditional=False, **kwds)#
通過數值積分計算函數相對於分布的期望值。
函數
f(x)
相對於分布dist
的期望值定義為:ub E[f(x)] = Integral(f(x) * dist.pdf(x)), lb
其中
ub
和lb
是參數,x
具有dist.pdf(x)
分布。如果邊界lb
和ub
對應於分布的支持,例如[-inf, inf]
在默認情況下,則積分是f(x)
的無限製期望。此外,函數f(x)
可以被定義為使得f(x)
是有限區間之外的0
,在這種情況下,在有限範圍[lb, ub]
內計算期望。- func: 可調用的,可選的
為其計算積分的函數。隻接受一個論點。默認值為恒等映射 f(x) = x。
- args: 元組,可選
分布的形狀參數。
- loc: 浮點數,可選
位置參數(默認 = 0)。
- scale: 浮點數,可選
比例參數(默認值=1)。
- lb, ub: 標量,可選
積分的下限和上限。默認設置為發行版的支持。
- conditional: 布爾型,可選
如果為 True,則通過積分區間的條件概率校正積分。返回值是函數的期望值,條件是在給定的區間內。默認為假。
- Additional keyword arguments are passed to the integration routine.:
- expect: 浮點數
計算出的期望值。
參數 ::
返回 ::
注意:
此函數的集成行為繼承自
scipy.integrate.quad
。這個函數和scipy.integrate.quad
都不能驗證積分是否存在或有限。例如cauchy(0).mean()
返回np.nan
和cauchy(0).expect()
返回0.0
。同樣,結果的準確性也沒有經過函數驗證。
scipy.integrate.quad
對於數值上有利的積分通常是可靠的,但不能保證所有可能的區間和被積函數都收斂到正確的值。提供此函數是為了方便;對於關鍵應用程序,請對照其他集成方法檢查結果。該函數未矢量化。
例子:
要了解積分邊界的影響,請考慮
>>> from scipy.stats import expon >>> expon(1).expect(lambda x: 1, lb=0.0, ub=2.0) 0.6321205588285578
這是接近
>>> expon(1).cdf(2.0) - expon(1).cdf(0.0) 0.6321205588285577
如果
conditional=True
>>> expon(1).expect(lambda x: 1, lb=0.0, ub=2.0, conditional=True) 1.0000000000000002
與 1 的微小偏差是由於數值積分。
通過將
complex_func=True
傳遞給scipy.integrate.quad
,可以將被積數視為 complex-valued 函數。>>> import numpy as np >>> from scipy.stats import vonmises >>> res = vonmises(loc=2, kappa=1).expect(lambda x: np.exp(1j*x), ... complex_func=True) >>> res (-0.18576377217422957+0.40590124735052263j)
>>> np.angle(res) # location of the (circular) distribution 2.0
相關用法
- Python SciPy rv_histogram.entropy用法及代碼示例
- Python SciPy rv_histogram.fit用法及代碼示例
- Python SciPy rv_continuous.entropy用法及代碼示例
- Python SciPy rv_continuous.fit用法及代碼示例
- Python SciPy rv_continuous.expect用法及代碼示例
- Python SciPy rv_discrete.entropy用法及代碼示例
- Python SciPy interpolate.make_interp_spline用法及代碼示例
- Python SciPy stats.anderson用法及代碼示例
- Python SciPy ClusterNode.pre_order用法及代碼示例
- Python SciPy stats.iqr用法及代碼示例
- Python SciPy FortranFile.read_record用法及代碼示例
- Python SciPy ndimage.correlate用法及代碼示例
- Python SciPy special.exp1用法及代碼示例
- Python SciPy special.expn用法及代碼示例
- Python SciPy signal.czt_points用法及代碼示例
- Python SciPy interpolate.krogh_interpolate用法及代碼示例
- Python SciPy ndimage.morphological_gradient用法及代碼示例
- Python SciPy distance.sokalmichener用法及代碼示例
- Python SciPy linalg.eigvalsh_tridiagonal用法及代碼示例
- Python SciPy linalg.cdf2rdf用法及代碼示例
- Python SciPy csc_array.diagonal用法及代碼示例
- Python SciPy fft.idctn用法及代碼示例
- Python SciPy linalg.LaplacianNd用法及代碼示例
- Python SciPy linalg.solve_circulant用法及代碼示例
- Python SciPy hierarchy.ward用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.stats.rv_histogram.expect。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。