訊息
|
- http://docs.scipy.org/doc/numpy/reference/routines.random.html
>>> np.mean(norm.rvs(5, size=500))
5.0254388366059688
>>> norm.rvs(5, size=100)
array([ 4.88363571, 4.9783015 , 4.08023589, 2.60696834, 5.07494184,
6.3111226 , 5.25700041, 6.04859859, 6.34235947, 5.44747348,
5.10592405, 4.63352744, 4.76001393, 3.64064404, 5.20489133,
4.83547866, 4.27259565, 6.07599844, 5.41002382, 6.6393537 ,
3.79091894, 4.50867322, 7.39967181, 3.64539272, 3.54451587,
5.24390518, 4.45622388, 4.70111655, 5.08043604, 5.23200434,
5.45682607, 5.53410458, 4.61222858, 4.32717762, 5.21983814,
5.33851384, 4.95238356, 4.03696058, 6.39972666, 5.47657374,
3.68368343, 3.98343698, 4.34009993, 3.83446237, 4.22036739,
5.58736975, 5.40692971, 4.86075061, 4.94603916, 3.835091 ,
5.71308147, 4.18278252, 7.14273596, 3.69966545, 4.91655355,
4.44996585, 5.33516299, 3.75917699, 6.18048523, 5.22022599,
4.72945058, 5.83469038, 5.1907251 , 4.65454664, 4.79568997,
4.63601872, 4.31892206, 3.62749124, 3.92378283, 4.91946834,
6.3443639 , 4.14517826, 5.35457789, 4.20911722, 4.38563896,
3.3930009 , 4.98393273, 5.35191551, 4.14238873, 4.15288579,
4.03820951, 5.65228128, 4.9840298 , 5.56714946, 4.67255474,
4.41683468, 5.4985144 , 6.06227204, 5.48989022, 5.31283911,
5.56854501, 4.73108723, 5.29675417, 5.5826896 , 2.83055984,
4.58273752, 4.85750386, 4.57019993, 6.35366729, 7.04079357])
>>> x = norm.rvs(5, size=100)
>>> np.mean(x)
4.91089654295914
>>> np.random.randn(100)
array([-0.52669783, -0.43059565, -0.60163783, -0.33475725, 0.95076907,
-0.15891786, -0.39878561, 2.34994531, -0.67223118, 1.57756535,
-0.17777502, -1.30144799, -0.90320329, 0.50434695, -0.83762419,
0.50069263, 0.97393217, -0.02334353, -0.30013538, -0.4198748 ,
-2.81146268, 0.18593411, 1.29728752, -1.18067422, 0.69092996,
1.3047599 , 1.2759695 , -1.39792853, 0.10269709, 0.14639689,
-0.94562381, 0.75542125, -0.10436422, -0.75724586, 1.01504831,
0.46656111, 0.70487961, 0.06911601, -0.23311595, -1.3343933 ,
0.15737459, -0.68656165, -2.46382459, 0.96142485, 1.43133643,
1.36843099, -1.26405395, -1.87790327, 0.65614779, -0.15521435,
0.38228966, 0.34117137, 0.08804055, 1.40917146, 0.14515672,
1.08489442, 1.35961909, 1.43162635, -1.19115191, -0.94866108,
-1.13228027, -1.1634924 , 0.82483222, 0.72301515, -1.13394606,
-0.30028234, 1.06243828, 0.76210998, 0.41847067, 0.35063356,
1.03619171, -1.05659314, 0.25840251, -1.54711583, -0.96786253,
2.05253961, -2.03643481, -1.0665637 , -0.13370291, 2.26780951,
0.9749539 , -0.31425988, 0.76189877, -0.18352354, 0.01866784,
-0.99173735, -1.302177 , 0.29901317, -0.69137632, -0.41383655,
-0.17950252, -1.12752884, -0.61966073, 1.47855494, 0.7860571 ,
0.98255187, -1.88120203, -2.11139993, -1.18113712, -1.3153758 ])
>>> np.random.rand(3,2)
array([[ 0.11209679, 0.86602561],
[ 0.88506569, 0.24564682],
[ 0.54538707, 0.96246955]])
>>> np.random.chisquare(2,4)
array([ 0.65743048, 2.48098974, 0.66438784, 0.1291731 ])
>>> np.random.chisquare(3,4)
array([ 1.54423197, 4.62086488, 6.34194595, 1.63875195])
>>> np.random.chisquare(5,8)
array([ 5.05393905, 13.24837786, 2.39024689, 1.72687928,
2.72188692, 0.72192836, 8.50371804, 5.85481933])
>>> mu, sigma = 0, 0.1
>>> s = np.random.normal(mu, sigma, 1000)
>>> abs(mu - np.mean(s)) < 0.01
True
>>> abs(sigma - np.std(s, ddof=1)) < 0.01
True
>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s, 30, normed=True)
File "<pyshell#80>", line 1
count, bins, ignored = plt.hist(s, 30, normed=True)
^
IndentationError: unexpected indent
>>> count, bins, ignored = plt.hist(s, 30, normed=True)
>>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
... np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
... linewidth=2, color='r')
SyntaxError: invalid syntax
>>> plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
np.exp( - (bins - mu)**2 / (2 * sigma**2) ),linewidth=2, color='r')
[<matplotlib.lines.Line2D object at 0x04200790>]
>>> plt.show()

- http://docs.scipy.org/doc/scipy/reference/tutorial/stats.html
>>> from scipy import stats
SyntaxError: invalid syntax
>>> from scipy import stats
>>> from scipy.stats import norm
>>> print norm.__doc_
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
print norm.__doc_
AttributeError: 'norm_gen' object has no attribute '__doc_'
>>> print norm.__doc_
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
print norm.__doc_
AttributeError: 'norm_gen' object has no attribute '__doc_'
>>> print norm._doc_
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
print norm._doc_
AttributeError: 'norm_gen' object has no attribute '_doc_'
>>> print norm.__doc__
A normal continuous random variable.
The location (loc) keyword specifies the mean.
The scale (scale) keyword specifies the standard deviation.
Continuous random variables are defined from a standard form and may
require some shape parameters to complete its specification. Any
optional keyword parameters can be passed to the methods of the RV
object as given below:
Methods
-------
rvs(loc=0, scale=1, size=1)
Random variates.
pdf(x, loc=0, scale=1)
Probability density function.
logpdf(x, loc=0, scale=1)
Log of the probability density function.
cdf(x, loc=0, scale=1)
Cumulative density function.
logcdf(x, loc=0, scale=1)
Log of the cumulative density function.
sf(x, loc=0, scale=1)
Survival function (1-cdf --- sometimes more accurate).
logsf(x, loc=0, scale=1)
Log of the survival function.
ppf(q, loc=0, scale=1)
Percent point function (inverse of cdf --- percentiles).
isf(q, loc=0, scale=1)
Inverse survival function (inverse of sf).
moment(n, loc=0, scale=1)
Non-central moment of order n
stats(loc=0, scale=1, moments='mv')
Mean('m'), variance('v'), skew('s'), and/or kurtosis('k').
entropy(loc=0, scale=1)
(Differential) entropy of the RV.
fit(data, loc=0, scale=1)
Parameter estimates for generic data.
expect(func, loc=0, scale=1, lb=None, ub=None, conditional=False, **kwds)
Expected value of a function (of one argument) with respect to the distribution.
median(loc=0, scale=1)
Median of the distribution.
mean(loc=0, scale=1)
Mean of the distribution.
var(loc=0, scale=1)
Variance of the distribution.
std(loc=0, scale=1)
Standard deviation of the distribution.
interval(alpha, loc=0, scale=1)
Endpoints of the range that contains alpha percent of the distribution
Parameters
----------
x : array_like
quantiles
q : array_like
lower or upper tail probability
loc : array_like, optional
location parameter (default=0)
scale : array_like, optional
scale parameter (default=1)
size : int or tuple of ints, optional
shape of random variates (default computed from input arguments )
moments : str, optional
composed of letters ['mvsk'] specifying which moments to compute where
'm' = mean, 'v' = variance, 's' = (Fisher's) skew and
'k' = (Fisher's) kurtosis. (default='mv')
Alternatively, the object may be called (as a function) to fix the shape,
location, and scale parameters returning a "frozen" continuous RV object:
rv = norm(loc=0, scale=1)
- Frozen RV object with the same methods but holding the given shape,
location, and scale fixed.
Notes
-----
The probability density function for `norm` is::
norm.pdf(x) = exp(-x**2/2)/sqrt(2*pi)
Examples
--------
>>> from scipy.stats import norm
>>> numargs = norm.numargs
>>> [ ] = [0.9,] * numargs
>>> rv = norm()
Display frozen pdf
>>> x = np.linspace(0, np.minimum(rv.dist.b, 3))
>>> h = plt.plot(x, rv.pdf(x))
Check accuracy of cdf and ppf
>>> prb = norm.cdf(x, )
>>> h = plt.semilogy(np.abs(x - norm.ppf(prb, )) + 1e-20)
Random number generation
>>> R = norm.rvs(size=100)
參考文獻
- 電子書:Think Stats : Probability and Statistics for Programmers, by Allen B. Downey, published by O'Reilly Media.
- StatPy: Statistical Computing with Python
- Statistics for Python
- Think Stats: Probability and Statistics for Programmers, Allen B. Downey
- Python as a statistics workbench
- http://scienceoss.com/rpy-statistics-in-r-from-python/
- Statistical functions (scipy.stats)
Facebook
|
Post preview:
Close preview