# 不确定性和分布 (astropy.uncertainty ）¶

astropy.uncertainty is relatively new (astropy v3.1), and thus it is possible there will be API changes in upcoming versions of astropy. If you have specific ideas for how it might be improved, please let us know on the astropy-dev mailing list 或者在http://feedback.astropy.org。

## 介绍¶

astropy 提供了一个 Distribution 对象表示统计分布的形式，该形式充当 Quantity 对象或常规 numpy.ndarray . 以这种方式使用， Distribution 以额外计算为代价提供不确定性传播。例如，它还可以更一般地表示蒙特卡罗计算技术中的抽样分布。

## 入门¶

>>> import numpy as np
>>> from astropy import units as u
>>> from astropy import uncertainty as unc
>>> np.random.seed(12345)  # ensures reproducible example numbers


>>> a = unc.normal(1*u.kpc, std=30*u.pc, n_samples=10000)
>>> b = unc.normal(2*u.kpc, std=40*u.pc, n_samples=10000)


>>> c = a + b
>>> c
<QuantityDistribution [...] kpc with n_samples=10000>
>>> c.pdf_mean()
<Quantity 2.99970555 kpc>
>>> c.pdf_std().to(u.pc)
<Quantity 50.07120457 pc>


>>> d = unc.uniform(center=3*u.kpc, width=800*u.pc, n_samples=10000)
>>> e = unc.Distribution(((np.random.beta(2,5, 10000)-(2/7))/2 + 3)*u.kpc)
>>> f = (c * d * e) ** (1/3)
>>> f.pdf_mean()
<Quantity 2.99786227 kpc>
>>> f.pdf_std()
<Quantity 0.08330476 kpc>
>>> from matplotlib import pyplot as plt
>>> from astropy.visualization import quantity_support
>>> with quantity_support():
...     plt.hist(f.distribution, bins=50)


(png _, svgpdf

## 使用 astropy.uncertainty¶

### 创建分发¶

>>> import numpy as np
>>> from astropy import units as u
>>> from astropy import uncertainty as unc
>>> np.random.seed(123456)  # ensures "random" numbers match examples below
>>> unc.Distribution(np.random.poisson(12, (1000)))
NdarrayDistribution([..., 12,...]) with n_samples=1000
>>> pq = np.random.poisson([1, 5, 30, 400], (1000, 4)).T * u.ct # note the transpose, required to get the sampling on the *last* axis
>>> distr = unc.Distribution(pq)
>>> distr
<QuantityDistribution [[...],
[...],
[...],
[...]] ct with n_samples=1000>


>>> center = [1, 5, 30, 400]
>>> n_distr = unc.normal(center*u.kpc, std=[0.2, 1.5, 4, 1]*u.kpc, n_samples=1000)
>>> n_distr = unc.normal(center*u.kpc, var=[0.04, 2.25, 16, 1]*u.kpc**2, n_samples=1000)
>>> n_distr = unc.normal(center*u.kpc, ivar=[25, 0.44444444, 0.625, 1]*u.kpc**-2, n_samples=1000)
>>> n_distr.distribution.shape
(4, 1000)
>>> unc.normal(center*u.kpc, std=[0.2, 1.5, 4, 1]*u.kpc, n_samples=100).distribution.shape
(4, 100)
>>> unc.normal(center*u.kpc, std=[0.2, 1.5, 4, 1]*u.kpc, n_samples=20000).distribution.shape
(4, 20000)


>>> unc.poisson(center*u.count, n_samples=1000)
<QuantityDistribution [[...],
[...],
[...],
[...]] ct with n_samples=1000>
>>> uwidth = [10, 20, 10, 55]*u.pc
>>> unc.uniform(center=center*u.kpc, width=uwidth, n_samples=1000)
<QuantityDistribution [[...],
[...],
[...],
[...]] kpc with n_samples=1000>
>>> unc.uniform(lower=center*u.kpc - uwidth/2,  upper=center*u.kpc + uwidth/2, n_samples=1000)
<QuantityDistribution [[...],
[...],
[...],
[...]] kpc with n_samples=1000>


### 使用分布¶

>>> distr.shape
(4,)
>>> distr.size
4
>>> distr.unit
Unit("ct")
>>> distr.n_samples
1000
>>> distr.pdf_mean()
<Quantity [  0.998,   5.017,  30.085, 400.345] ct>
>>> distr.pdf_std()
<Quantity [ 0.97262326,  2.32222114,  5.47629208, 20.6328373 ] ct>
>>> distr.pdf_var()
<Quantity [  0.945996,   5.392711,  29.989775, 425.713975] ct2>
>>> distr.pdf_median()
<Quantity [   1.,   5.,  30., 400.] ct>
>>> distr.pdf_mad()  # Median absolute deviation
<Quantity [ 1.,  2.,  4., 14.] ct>
>>> distr.pdf_smad()  # Median absolute deviation, rescaled to match std for normal
<Quantity [ 1.48260222,  2.96520444,  5.93040887, 20.75643106] ct>
>>> distr.pdf_percentiles([10, 50, 90])
<Quantity [[  0. ,   2. ,  23. , 374. ],
[  1. ,   5. ,  30. , 400. ],
[  2. ,   8. ,  37.1, 427. ]] ct>
>>> distr.pdf_percentiles([.1, .5, .9]*u.dimensionless_unscaled)
<Quantity [[  0. ,   2. ,  23. , 374. ],
[  1. ,   5. ,  30. , 400. ],
[  2. ,   8. ,  37.1, 427. ]] ct>


>>> distr.distribution
<Quantity [[...1...],
[...5...],
[...27...],
[...405...]] ct>
>>> distr.distribution.shape
(4, 1000)


A Quantity distribution interacts naturally with non-Distribution Quantity objects, assuming the Quantity is a Dirac delta distribution:

>>> distr_in_kpc = distr * u.kpc/u.count  # for the sake of round numbers in examples
>>> distrplus = distr_in_kpc + [2000,0,0,500]*u.pc
>>> distrplus.pdf_median()
<Quantity [   3. ,   5. ,  30. , 400.5] kpc>
>>> distrplus.pdf_var()
<Quantity [  0.945996,   5.392711,  29.989775, 425.713975] kpc2>


>>> another_distr = unc.Distribution((np.random.randn(1000,4)*[1000,.01 , 3000, 10] + [2000, 0, 0, 500]).T * u.pc)
>>> combined_distr = distr_in_kpc + another_distr
>>> combined_distr.pdf_median()
<Quantity [  3.01847755,   4.99999576,  29.60559788, 400.49176321] kpc>
>>> combined_distr.pdf_var()
<Quantity [  1.8427705 ,   5.39271147,  39.5343726 , 425.71324244] kpc2>


### 分布协方差与离散抽样效应¶

>>> import numpy as np
>>> np.random.seed(12345)  # produce repeatable plots
>>> from astropy import units as u
>>> from astropy import uncertainty as unc
>>> from matplotlib import pyplot as plt
>>> n1 = unc.normal(center=0., std=1, n_samples=10000)
>>> n2 = unc.normal(center=0., std=2, n_samples=10000)
>>> plt.scatter(n1.distribution, n2.distribution, s=2, lw=0, alpha=.5)
>>> plt.xlim(-4, 4)
>>> plt.ylim(-4, 4)


(png _, svgpdf

>>> ncov = np.random.multivariate_normal([0, 0], [[1, .5], [.5, 2]], size=10000)
>>> n1 = unc.Distribution(ncov[:, 0])
>>> n2 = unc.Distribution(ncov[:, 1])
>>> plt.scatter(n1.distribution, n2.distribution, s=2, lw=0, alpha=.5)
>>> plt.xlim(-4, 4)
>>> plt.ylim(-4, 4)


(png _, svgpdf

>>> fe_abund = unc.normal(center=-2, std=.25, n_samples=10000)
>>> o_abund = unc.normal(center=-6., std=.5, n_samples=10000)
>>> h_abund = unc.normal(center=-0.7, std=.1, n_samples=10000)
>>> feh = fe_abund - h_abund
>>> ofe = o_abund - fe_abund
>>> plt.scatter(ofe.distribution, feh.distribution, s=2, lw=0, alpha=.5)
>>> plt.xlabel('[Fe/H]')
>>> plt.ylabel('[O/Fe]')


(png _, svgpdf

>>> n2_wrong = unc.Distribution(ncov[::-1, 1])  #reverse the sampling axis order
>>> plt.scatter(n1.distribution, n2_wrong.distribution, s=2, lw=0, alpha=.5)
>>> plt.xlim(-4, 4)
>>> plt.ylim(-4, 4)


(png _, svgpdf

## 参考/API¶

### 不确定性包裹¶

#### 功能¶

 normal(center, *[, std, var, ivar, cls]) 创建高斯/正态分布。 poisson(center, n_samples[, cls]) 创建泊松分布。 uniform(*[, lower, upper, center, width, cls]) 从上下限创建一个统一的分布。

#### Classes¶

 具有相关不确定性分布的标量值或数组值。