Python Numpy
Numpy 练习
import numpy as np
import scipy.stats as ss
def case(n , mu , sigma , p , rep ):
m = np.zeros((rep, 4))
for i in range(rep):
norm = np.random.normal(loc = mu, scale = sigma, size = n)
xbar = np.mean(norm)
low = xbar - ss.norm.ppf(q = 1 - p) * (sigma / np.sqrt(n))
up = xbar + ss.norm.ppf(q = 1 - p) * (sigma / np.sqrt(n))
if (mu > low) & (mu < up):
rem = 1
else:
rem = 0
m[i, :] = [xbar, low, up, rem]
inside = np.sum(m[:, 3])
per = inside / rep
desc = "There are " + str(inside) + " confidence intervals that contain "
"the true mean (" + str(mu) + "), that is " + str(per) + " percent of the total CIs"
print( {"Matrix": m, "Decision": desc})
case(n = 10, mu = 3, sigma = np.sqrt(5), p = 0.025, rep = 100)
{'Matrix': array([[ 2.94244037, 1.55653654, 4.32834419, 1. ],
[ 2.7033498 , 1.31744597, 4.08925362, 1. ],
[ 4.7486197 , 3.36271587, 6.13452352, 0. ],
[ 3.41305929, 2.02715546, 4.79896311, 1. ],
[ 2.64787272, 1.2619689 , 4.03377655, 1. ],
[ 3.08967078, 1.70376695, 4.4755746 , 1. ],
[ 1.48585139, 0.09994757, 2.87175522, 0. ],
[ 3.53803986, 2.15213603, 4.92394368, 1. ],
[ 2.56695478, 1.18105095, 3.9528586 , 1. ],
[ 4.00780865, 2.62190482, 5.39371247, 1. ],
[ 2.84628264, 1.46037882, 4.23218647, 1. ],
[ 3.18124253, 1.79533871, 4.56714635, 1. ],
[ 4.69740877, 3.31150494, 6.08331259, 0. ],
[ 3.10123937, 1.71533555, 4.48714319, 1. ],
[ 1.66798754, 0.28208371, 3.05389136, 1. ],
[ 2.99611972, 1.6102159 , 4.38202355, 1. ],
[ 2.27878446, 0.89288064, 3.66468829, 1. ],
[ 2.49110791, 1.10520409, 3.87701174, 1. ],
[ 3.12825333, 1.74234951, 4.51415716, 1. ],
[ 1.73518129, 0.34927747, 3.12108512, 1. ],
[ 1.54507169, 0.15916787, 2.93097552, 0. ],
[ 2.99182215, 1.60591833, 4.37772598, 1. ],
[ 3.48756949, 2.10166567, 4.87347331, 1. ],
[ 2.89581822, 1.5099144 , 4.28172204, 1. ],
[ 2.80865214, 1.42274832, 4.19455597, 1. ],
[ 3.50052159, 2.11461777, 4.88642542, 1. ],
[ 4.14494862, 2.75904479, 5.53085244, 1. ],
[ 3.67753588, 2.29163205, 5.0634397 , 1. ],
[ 2.95616485, 1.57026103, 4.34206868, 1. ],
[ 4.53506363, 3.1491598 , 5.92096745, 0. ],
[ 2.72485067, 1.33894684, 4.11075449, 1. ],
[ 3.17907726, 1.79317344, 4.56498109, 1. ],
[ 3.21897371, 1.83306989, 4.60487753, 1. ],
[ 2.45852793, 1.0726241 , 3.84443175, 1. ],
[ 3.73128494, 2.34538111, 5.11718876, 1. ],
[ 3.0720078 , 1.68610398, 4.45791163, 1. ],
[ 3.59371559, 2.20781177, 4.97961942, 1. ],
[ 2.62224556, 1.23634173, 4.00814938, 1. ],
[ 2.93213703, 1.54623321, 4.31804086, 1. ],
[ 2.36009182, 0.97418799, 3.74599564, 1. ],
[ 3.32636664, 1.94046282, 4.71227046, 1. ],
[ 3.47970118, 2.09379735, 4.865605 , 1. ],
[ 2.22514533, 0.8392415 , 3.61104915, 1. ],
[ 2.89182878, 1.50592496, 4.27773261, 1. ],
[ 1.48909717, 0.10319335, 2.875001 , 0. ],
[ 4.50260023, 3.1166964 , 5.88850405, 0. ],
[ 2.64266112, 1.25675729, 4.02856494, 1. ],
[ 2.44128142, 1.0553776 , 3.82718525, 1. ],
[ 3.944326 , 2.55842217, 5.33022982, 1. ],
[ 2.64270758, 1.25680375, 4.0286114 , 1. ],
[ 3.28213223, 1.8962284 , 4.66803605, 1. ],
[ 2.45659846, 1.07069464, 3.84250229, 1. ],
[ 2.05437736, 0.66847353, 3.44028118, 1. ],
[ 1.7528231 , 0.36691928, 3.13872692, 1. ],
[ 3.03156704, 1.64566322, 4.41747087, 1. ],
[ 2.5957265 , 1.20982267, 3.98163032, 1. ],
[ 4.29652432, 2.91062049, 5.68242814, 1. ],
[ 0.61878333, -0.7671205 , 2.00468715, 0. ],
[ 1.24349692, -0.1424069 , 2.62940075, 0. ],
[ 2.87718212, 1.4912783 , 4.26308595, 1. ],
[ 2.30513816, 0.91923434, 3.69104199, 1. ],
[ 3.52140323, 2.13549941, 4.90730706, 1. ],
[ 2.06199962, 0.67609579, 3.44790344, 1. ],
[ 3.14329411, 1.75739028, 4.52919793, 1. ],
[ 3.01066419, 1.62476037, 4.39656802, 1. ],
[ 2.09523516, 0.70933134, 3.48113898, 1. ],
[ 4.80460475, 3.41870093, 6.19050858, 0. ],
[ 3.10590296, 1.71999914, 4.49180679, 1. ],
[ 2.10089448, 0.71499066, 3.48679831, 1. ],
[ 3.4028367 , 2.01693287, 4.78874052, 1. ],
[ 2.93177144, 1.54586762, 4.31767527, 1. ],
[ 2.88510097, 1.49919715, 4.2710048 , 1. ],
[ 3.72506026, 2.33915643, 5.11096408, 1. ],
[ 4.00821327, 2.62230944, 5.39411709, 1. ],
[ 2.79177951, 1.40587568, 4.17768333, 1. ],
[ 2.9758259 , 1.58992208, 4.36172973, 1. ],
[ 3.17610416, 1.79020033, 4.56200798, 1. ],
[ 2.69703102, 1.31112719, 4.08293484, 1. ],
[ 3.82051569, 2.43461187, 5.20641952, 1. ],
[ 2.87274776, 1.48684393, 4.25865158, 1. ],
[ 3.75466277, 2.36875894, 5.14056659, 1. ],
[ 2.41862718, 1.03272335, 3.804531 , 1. ],
[ 2.09078702, 0.70488319, 3.47669084, 1. ],
[ 3.5470385 , 2.16113468, 4.93294233, 1. ],
[ 3.29972583, 1.91382201, 4.68562965, 1. ],
[ 2.53316294, 1.14725912, 3.91906677, 1. ],
[ 2.43271501, 1.04681119, 3.81861884, 1. ],
[ 2.97562393, 1.58972011, 4.36152776, 1. ],
[ 2.59330321, 1.20739939, 3.97920704, 1. ],
[ 3.20420879, 1.81830497, 4.59011262, 1. ],
[ 1.82511119, 0.43920736, 3.21101501, 1. ],
[ 1.99640796, 0.61050413, 3.38231178, 1. ],
[ 3.20508829, 1.81918447, 4.59099211, 1. ],
[ 2.27549514, 0.88959131, 3.66139896, 1. ],
[ 2.73630813, 1.35040431, 4.12221196, 1. ],
[ 2.4695514 , 1.08364758, 3.85545523, 1. ],
[ 4.28041329, 2.89450947, 5.66631712, 1. ],
[ 1.97264979, 0.58674597, 3.35855362, 1. ],
[ 2.79447979, 1.40857597, 4.18038362, 1. ],
[ 2.58405171, 1.19814788, 3.96995553, 1. ]]),
'Decision': 'There are 90.0 confidence intervals that contain the true mean (3), that is 0.9 percent of the total CIs'}
import numpy as np
import scipy.stats as ss
def case2(n , mu , sigma , p = , rep ):
scaled_crit = ss.norm.ppf(q = 1 - p) * (sigma / np.sqrt(n))
norm = np.random.normal(loc = mu, scale = sigma, size = (rep, n))
xbar = norm.mean(1)
low = xbar - scaled_crit
up = xbar + scaled_crit
rem = (mu > low) & (mu < up)
m = np.c_[xbar, low, up, rem]
inside = np.sum(m[:, 3])
per = inside / rep
desc = "There are " + str(inside) + " confidence intervals that contain "
"the true mean (" + str(mu) + "), that is " + str(per) + " percent of the total CIs"
print( {"Matrix": m, "Decision": desc})
case2(n = 10, mu = 3, sigma = np.sqrt(5), p = 0.025, rep = 100)
{'Matrix': array([[ 3.11513239, 1.72922856, 4.50103621, 1. ],
[ 4.37741717, 2.99151335, 5.763321 , 1. ],
[ 3.50369495, 2.11779113, 4.88959878, 1. ],
[ 3.92328984, 2.53738602, 5.30919367, 1. ],
[ 4.05234486, 2.66644104, 5.43824869, 1. ],
[ 1.52382152, 0.13791769, 2.90972534, 0. ],
[ 3.01021434, 1.62431051, 4.39611816, 1. ],
[ 3.16610978, 1.78020595, 4.5520136 , 1. ],
[ 2.91210213, 1.5261983 , 4.29800595, 1. ],
[ 2.90473912, 1.51883529, 4.29064294, 1. ],
[ 2.02357197, 0.63766814, 3.40947579, 1. ],
[ 4.42493367, 3.03902985, 5.8108375 , 0. ],
[ 3.32230679, 1.93640297, 4.70821062, 1. ],
[ 3.20926919, 1.82336537, 4.59517302, 1. ],
[ 2.83092026, 1.44501643, 4.21682408, 1. ],
[ 2.64273426, 1.25683044, 4.02863808, 1. ],
[ 4.16830429, 2.78240046, 5.55420811, 1. ],
[ 3.36988996, 1.98398613, 4.75579378, 1. ],
[ 2.59097813, 1.20507431, 3.97688195, 1. ],
[ 2.61249845, 1.22659462, 3.99840227, 1. ],
[ 4.24629684, 2.86039301, 5.63220066, 1. ],
[ 4.60020971, 3.21430589, 5.98611354, 0. ],
[ 4.08479984, 2.69889601, 5.47070366, 1. ],
[ 3.33209001, 1.94618618, 4.71799383, 1. ],
[ 3.59251387, 2.20661005, 4.9784177 , 1. ],
[ 3.68444949, 2.29854567, 5.07035332, 1. ],
[ 3.00913259, 1.62322877, 4.39503642, 1. ],
[ 3.21919882, 1.83329499, 4.60510264, 1. ],
[ 3.20020829, 1.81430446, 4.58611211, 1. ],
[ 2.40560374, 1.01969992, 3.79150756, 1. ],
[ 3.71353537, 2.32763154, 5.09943919, 1. ],
[ 3.62509263, 2.2391888 , 5.01099645, 1. ],
[ 2.89022272, 1.5043189 , 4.27612654, 1. ],
[ 2.73909177, 1.35318794, 4.12499559, 1. ],
[ 2.17577935, 0.78987553, 3.56168318, 1. ],
[ 3.06606491, 1.68016108, 4.45196873, 1. ],
[ 1.81440876, 0.42850493, 3.20031258, 1. ],
[ 2.69955968, 1.31365585, 4.0854635 , 1. ],
[ 3.64872238, 2.26281856, 5.03462621, 1. ],
[ 2.53294803, 1.1470442 , 3.91885185, 1. ],
[ 2.5917853 , 1.20588148, 3.97768912, 1. ],
[ 2.08203396, 0.69613013, 3.46793778, 1. ],
[ 1.58372594, 0.19782212, 2.96962977, 0. ],
[ 3.53536882, 2.149465 , 4.92127265, 1. ],
[ 3.95965507, 2.57375125, 5.34555889, 1. ],
[ 2.72876271, 1.34285889, 4.11466653, 1. ],
[ 3.44364205, 2.05773822, 4.82954587, 1. ],
[ 2.17086461, 0.78496078, 3.55676843, 1. ],
[ 3.29803318, 1.91212935, 4.683937 , 1. ],
[ 1.11366509, -0.27223874, 2.49956891, 0. ],
[ 3.53452411, 2.14862029, 4.92042793, 1. ],
[ 2.22673629, 0.84083247, 3.61264012, 1. ],
[ 3.97041982, 2.584516 , 5.35632365, 1. ],
[ 3.41386928, 2.02796546, 4.7997731 , 1. ],
[ 3.23969271, 1.85378888, 4.62559653, 1. ],
[ 4.12605408, 2.74015026, 5.51195791, 1. ],
[ 2.55026349, 1.16435966, 3.93616731, 1. ],
[ 4.37087749, 2.98497366, 5.75678131, 1. ],
[ 2.00564413, 0.61974031, 3.39154796, 1. ],
[ 2.49962212, 1.1137183 , 3.88552594, 1. ],
[ 3.32357235, 1.93766853, 4.70947618, 1. ],
[ 2.98914117, 1.60323734, 4.37504499, 1. ],
[ 3.87868714, 2.49278331, 5.26459096, 1. ],
[ 2.24590758, 0.86000376, 3.6318114 , 1. ],
[ 3.17845029, 1.79254646, 4.56435411, 1. ],
[ 3.55283527, 2.16693144, 4.93873909, 1. ],
[ 3.52740655, 2.14150272, 4.91331037, 1. ],
[ 3.90036016, 2.51445633, 5.28626398, 1. ],
[ 3.32921542, 1.9433116 , 4.71511925, 1. ],
[ 2.55965139, 1.17374756, 3.94555521, 1. ],
[ 3.19253403, 1.80663021, 4.57843786, 1. ],
[ 1.96654953, 0.58064571, 3.35245336, 1. ],
[ 3.06105266, 1.67514883, 4.44695648, 1. ],
[ 3.04509584, 1.65919202, 4.43099967, 1. ],
[ 4.31985206, 2.93394824, 5.70575589, 1. ],
[ 2.78412884, 1.39822501, 4.17003266, 1. ],
[ 4.29800107, 2.91209724, 5.68390489, 1. ],
[ 3.24314385, 1.85724003, 4.62904768, 1. ],
[ 3.23501665, 1.84911282, 4.62092047, 1. ],
[ 3.82121344, 2.43530962, 5.20711727, 1. ],
[ 3.79165134, 2.40574752, 5.17755517, 1. ],
[ 4.02561175, 2.63970793, 5.41151558, 1. ],
[ 3.58370386, 2.19780004, 4.96960769, 1. ],
[ 2.07816926, 0.69226544, 3.46407309, 1. ],
[ 2.03592137, 0.65001755, 3.4218252 , 1. ],
[ 2.11000338, 0.72409956, 3.49590721, 1. ],
[ 2.36511892, 0.9792151 , 3.75102275, 1. ],
[ 2.54766344, 1.16175961, 3.93356726, 1. ],
[ 3.32621431, 1.94031049, 4.71211814, 1. ],
[ 4.1312444 , 2.74534058, 5.51714823, 1. ],
[ 2.47832421, 1.09242039, 3.86422804, 1. ],
[ 2.73456516, 1.34866133, 4.12046898, 1. ],
[ 3.01062978, 1.62472596, 4.39653361, 1. ],
[ 1.98022227, 0.59431844, 3.36612609, 1. ],
[ 2.54736365, 1.16145983, 3.93326748, 1. ],
[ 2.34329693, 0.9573931 , 3.72920075, 1. ],
[ 3.6893514 , 2.30344757, 5.07525522, 1. ],
[ 2.73055316, 1.34464934, 4.11645699, 1. ],
[ 3.00831459, 1.62241076, 4.39421841, 1. ],
[ 2.51669702, 1.13079319, 3.90260084, 1. ]]),
'Decision': 'There are 95.0 confidence intervals that contain the true mean (3), that is 0.95 percent of the total CIs'}