leng = (\xs -> foldl (\x y -> x+1) 0 xs) ex xs = (sum xs) / (leng xs) stddev xs = sqrt $ (/(leng xs)) $sum $ map (\x -> (x - (ex xs))^2) xs
mx: 0.777000, my: 0.701000, sx: 1.462176, sy: 1.324404, cov: 0.383190, cor: 0.197876 Prosta regresji: y = 0.561737x + 0.179232
from itertools import imap, product from math import sqrt coords = [3, 3.5, 4, 4.5, 5] fxy = {3:{}, 3.5:{}, 4:{}, 4.5:{}, 5:{}} points = ( (3, 3, 0.16), (3, 3.5, 0.12), (3, 4, 0.08), (3.5, 3.5, 0.04), (3.5, 4, 0.08), (3.5, 4.5, 0.12), (4, 4, 0.08), (4, 4.5, 0.06), (4, 5, 0.01), (4.5, 4.5, 0.05), (4.5, 5, 0.08), (5, 5, 0.08), ) for x, y, v in points: fxy[x][y] = v def f_xy((x, y)): return fxy[x].get(y, 0) def fx(y): fx = 0 for x in coords: fx += fxy[x].get(y, 0) return fx def fy(x): fy = 0 for y in coords: fy += fxy[x].get(y, 0) return fy def idty(x): return x def E(f, g, cs): ls = [g(c)*f(c) for c in cs] return sum(ls)/len(ls) mx = E(fx, idty, coords) my = E(fy, idty, coords) sx = sqrt(E(fx, lambda x: (x-mx)*(x-mx), coords)) sy = sqrt(E(fy, lambda y: (y-my)*(y-my), coords)) cov = E(f_xy, lambda (x, y): (x-mx)*(y-my), product(coords, repeat=2)) cor = cov/(sx*sy) print "mx: %f, my: %f, sx: %f, sy: %f, cov: %f, cor: %f" % (mx, my, sx, sy, cov, cor) # E([Y-(a+bX)]^2) - min # b = CXY / D^2X, a = EY - bEX b = cov / (sx*sx) a = my - b*mx print "Prosta regresji: y = %fx + %f" % (a, b)
Mała próbka.
\overline X = 333
S = 33.16323265304515
\displaystyle P \left( \overline{X} - t_{1 - \frac{\alpha}{2}} \frac{S}{\sqrt{n-1}} < m < \overline{X} + t_{1 - \frac{\alpha}{2}} \frac{S}{\sqrt{n-1}} \right) = 1 - \alpha
Mała próbka.
\overline X = 1.574
S = 0.043
\displaystyle P \left( \overline{X} - t_{1 - \frac{\alpha}{2}} \frac{S}{\sqrt{n-1}} < m < \overline{X} + t_{1 - \frac{\alpha}{2}} \frac{S}{\sqrt{n-1}} \right) = 1 - \alpha
Duża próbka.
\overline X = 37.5
S = 39.566399886772615
\displaystyle P \left( \overline{X} - u_{1-\frac{\alpha}{2}} \frac{S}{\sqrt{n}} < m < \overline{X} + u_{1-\frac{\alpha}{2}}
\frac{S}{\sqrt{n}} \right) = 1 - \alpha
Mała próbka.
\overline X = 100.0
S = 11.586630226256467
To jest dla wariancji, czyli pierwiastek, z obu stron:
\displaystyle P \left( \frac{nS^2}{\chi^{2}_{1 - \frac{\alpha}{2}, n - 1}} < \sigma^2 < \frac{nS^2}{\chi^{2}_{\frac{\alpha}{2}, n - 1}} \right)= 1 - \alpha
Duża próbka.
\displaystyle P \left( \frac{S}{1+ \frac{u_{\alpha}}{\sqrt{2n}}} < \sigma < \frac{S}{1 - \frac{u_{\alpha}}{\sqrt{2n}}} \right)= 1 - \alpha
Duża próbka.
Przedział:
p = \frac k n
\displaystyle \left(p - u_{1-\alpha/2} \sqrt{\frac {p(1-p)}n}, p + u_{1-\alpha/2} \sqrt{\frac {p(1-p)}n}\right)
5. [(312.96431003654271, 353.03568996345729), (308.36923762299926, 357.63076237700074), (297.96555773250361, 368.03444226749639)] 6. [(-9.7387607780359318, 12.886760778035931), (-12.059600199779362, 15.207600199779362), (-16.856231309036904, 20.004231309036907)] 7. [(33.742550387392015, 41.257449612607985), (33.022722633937505, 41.977277366062495), (31.615860122640775, 43.384139877359225)] 8. [(9.2700847290948065, 17.199689112976362), (8.8398005031199745, 18.520633113068985), (8.0922906651952005, 21.607026599096702)] 9. [(0.022687893099167609, 0.03178546605420815), (0.022082482141430701, 0.033055090136681231), (0.020987900459555682, 0.035854128204083208)] 10. [(0.089306808583436648, 0.19402652474989668), (0.079276047588324225, 0.20405728574500909), (0.059671494226666497, 0.22366183910666682)]
from scipy.stats import t, norm, chi2 from math import sqrt n = 10 m = 333 S = 33.16323265304515 conf_t = map(lambda x: t.ppf(1-x/2, n), [0.1, 0.05, 0.01]) print "5." print map(lambda conf: (m-conf*S/sqrt(n-1), m+conf*S/sqrt(n-1)), conf_t) print n = 26 m = 1.574 s = 0.043 conf_t = map(lambda x: t.ppf(1-x/2, n), [0.1, 0.05, 0.01]) print "6." print map(lambda conf: (m-conf*S/sqrt(n-1), m+conf*S/sqrt(n-1)), conf_t) print n = 300 m = 37.5 S = 39.566399886772615 conf_u = map(lambda x: norm.ppf(1-x/2), [0.1, 0.05, 0.01]) print "7." print map(lambda conf: (m-conf*S/sqrt(n), m+conf*S/sqrt(n)), conf_u) print n = 16 m = 100 S = 11.586630226256467 conf_chi = map(lambda a: (chi2.ppf(1-a/2, n-1), chi2.ppf(a/2,n-1)), [0.1, 0.05, 0.01]) print "8." print map(lambda (conf1, conf2): (sqrt(n*S*S/conf1), sqrt(n*S*S/conf2)), conf_chi) print n = 50 S = sqrt(0.00068) conf_u = map(lambda x: norm.ppf(1-x/2), [0.1, 0.05, 0.01]) print "9." print map(lambda conf: (S*sqrt(2*n)/(sqrt(2*n-3)+conf),S*sqrt(2*n)/(sqrt(2*n-3)-conf)), conf_u) print n = 120.0 k = 17.0 p = k/n print "10." print map(lambda conf: (p - conf*sqrt(p*(1-p)/n), p + conf*sqrt(p*(1-p)/n)), conf_u) print