def exp_test(count): Y = list(([norm.ppf(x) for x in MKM(count)])) TMY, TDY = 0.0, norm.var() M_min, MY, M_max = M_with_interval(Y) D_min, DY, D_max = D_with_interval(Y) pylab.title( 'N={}: {:.3}<= M <={:.3} (M={:.3} TM={:.3})\n {:.3}<= D <={:.3} (D={:.3} TD={:.3})' .format(count, M_min, M_max, MY, TMY, D_min, D_max, DY, TDY)) pylab.hist(Y, normed=1) X = list([x * 0.01 for x in xrange(-400, 400)]) TY = list([norm.pdf(x) for x in X]) pylab.plot(X, TY) pylab.show() F = build_F(X, Y) TF = [norm.cdf(x) for x in X] # alpha = 0.01 & n = 1000 max_sigma = 0.86 max_delta = max([abs(f - ft) for f, ft in zip(F, TF)]) sigma = math.sqrt(count) * max_delta pylab.title( 'kolmogorov: max delta = {:.4} sigma = {:.4} < {:.4}, viborka is {}'. format(max_delta, sigma, max_sigma, 'OK' if sigma < max_sigma else 'NOT OK')) pylab.plot(X, F) pylab.plot(X, TF) pylab.show()
def __init__(self, mode=0, elem=None, sample=None): if mode == 0: self.mu = elem[0] self.sigma = elem[1] else: self.mu, self.sigma = norm.fit(sample) self.math_average = norm.mean(loc=self.mu, scale=self.sigma) self.dispersion = norm.var(loc=self.mu, scale=self.sigma)
def var(self, dist): return norm.var(*self._get_params(dist))
# -*- coding: utf-8 -*- #https://docs.scipy.org/doc/scipy/reference/stats.html #https://docs.scipy.org/doc/scipy/reference/tutorial/stats.html #import scipy #scipy.stats package is imported as from scipy import stats # individual objects are imported as from scipy.stats import norm norm.cdf(0) norm.cdf([-1., 0, 1]) import numpy as np norm.cdf(np.array([-1., 0, 1])) norm.mean(), norm.std(), norm.var() norm.stats(moments="mv") norm.ppf(0.5) norm.rvs(size=3) #random nos #drawing random numbers relies on generators from numpy.random package. In the example above, the specific stream of random numbers is not reproducible across runs. To achieve reproducibility, you can explicitly seed a global variable np.random.seed(1234) #Relying on a global state is not recommended though. A better way is to use the random_state parameter which accepts an instance of numpy.random.RandomState class, or an integer which is then used to seed an internal RandomState object: norm.rvs(size=5, random_state=1234) norm.rvs(5) #one no only #Shifting and Scaling¶ #All continuous distributions take loc and scale as keyword parameters to adjust the location and scale of the distribution, e.g. for the standard normal distribution the location is the mean and the scale is the standard deviation. norm.stats(loc=3, scale=4, moments="mv") #uniform distribution from scipy.stats import uniform
np.quantile(rv2,[.05,.95]) ############################################################################# ## Sampling Importance Resampling (SIR) ##################################### ############################################################################# #suppose that we have a posterior with a Gamma(3,3) format, but we dont know. #use gaussian in the sampling step #loc1, scale1 are from the Gaussian loc1=0 scale1=500 #scale is standard deviation norm.mean(loc=loc1, scale=scale1) norm.var(loc=loc1, scale=scale1) norm.std(loc=loc1, scale=scale1) np.random.seed(seed=123) rv1 = np.random.normal(loc=loc1, scale=scale1, size=10000) rv1[0:10] np.mean(rv1) np.std(rv1) np.var(rv1) np.max(rv1) np.min(rv1) b1, b2 = -5,5 x = np.linspace(b1,b2,100) y = norm.pdf(x=x, loc=loc1, scale=scale1) fig, ax = plt.subplots(1, 1) #it creates another plot
dist_continu = [d for d in dir(stats) if isinstance(getattr(stats, d), stats.rv_continuous)] dist_discrete = [d for d in dir(stats) if isinstance(getattr(stats, d), stats.rv_discrete)] n = np.empty(1000) for i in range(1000): n[i] = norm.rvs() plt.hist(n, bins = 50, density = 1) plt.hist(norm.pdf(n), bins = 50, density = 1) plt.hist(norm.cdf(n), bins = 50, density = 1) plt.hist(norm.moment(n.any()), bins = 50, density = 1) norm.mean() norm.var() norm.ppf(0.5) # median of 0-1 normal distribution norm.rvs(size = 3) norm.rvs(size = 3, random_state = 1234) norm.rvs(3) # the first arguement is the loc (mean, in case of normal distrubution) parameter, not size # Shifting and scaling x = norm.rvs(size = 5, loc = 3, scale = 4) norm.stats(x) norm.stats(loc = 3, scale = 4, moments = "mv") np.mean(norm.rvs(5, size = 500))
def var(self, n, p): var = norm.var(self, n, p) return var
#!/usr/bin/env python # -*- coding:utf-8 -*- from scipy.stats import norm print(norm.mean(), norm.std(), norm.var()) r = norm.rvs(size=100) print(r) print(r.mean(), r.std(), r.var())
a = np.array([[1, 2], [3, 4]]) print(a) b = np.array([[5], [6]]) print(b) print(linalg.inv(a).dot(b)) print(a.dot(linalg.inv(a).dot(b)) - b) print(np.linalg.solve(a, b)) print(a.dot(np.linalg.solve(a, b)) - b) print('-----') # Step 4. Common Methods in stats print(norm.cdf(0)) print(norm.cdf([-1., 0, 1])) print(norm.cdf(np.array([-1., 0, 1]))) print(norm.mean(), norm.std(), norm.var()) print(norm.stats(moments="mv")) print(norm.ppf(0.5)) print(norm.rvs(size=3)) print(np.random.seed(1234)) print(norm.rvs(size=5, random_state=1234)) print(norm.rvs(5)) print('-----') # Step 5. Broadcasting print(stats.t.isf([0.1, 0.05, 0.01], [[10], [11]])) print(stats.t.isf([0.1, 0.05, 0.01], 10)) print(stats.t.isf([0.1, 0.05, 0.01], [10, 11, 12])) print('-----') # Step 6. Anlysing one sample