@@ -894,6 +894,13 @@ def _normal_dist_inv_cdf(p, mu, sigma):
894
894
return mu + (x * sigma )
895
895
896
896
897
+ # If available, use C implementation
898
+ try :
899
+ from _statistics import _normal_dist_inv_cdf
900
+ except ImportError :
901
+ pass
902
+
903
+
897
904
class NormalDist :
898
905
"Normal distribution of a random variable"
899
906
# https://en.wikipedia.org/wiki/Normal_distribution
@@ -1111,79 +1118,3 @@ def __hash__(self):
1111
1118
1112
1119
def __repr__ (self ):
1113
1120
return f'{ type (self ).__name__ } (mu={ self ._mu !r} , sigma={ self ._sigma !r} )'
1114
-
1115
- # If available, use C implementation
1116
- try :
1117
- from _statistics import _normal_dist_inv_cdf
1118
- except ImportError :
1119
- pass
1120
-
1121
-
1122
- if __name__ == '__main__' :
1123
-
1124
- # Show math operations computed analytically in comparsion
1125
- # to a monte carlo simulation of the same operations
1126
-
1127
- from math import isclose
1128
- from operator import add , sub , mul , truediv
1129
- from itertools import repeat
1130
- import doctest
1131
-
1132
- g1 = NormalDist (10 , 20 )
1133
- g2 = NormalDist (- 5 , 25 )
1134
-
1135
- # Test scaling by a constant
1136
- assert (g1 * 5 / 5 ).mean == g1 .mean
1137
- assert (g1 * 5 / 5 ).stdev == g1 .stdev
1138
-
1139
- n = 100_000
1140
- G1 = g1 .samples (n )
1141
- G2 = g2 .samples (n )
1142
-
1143
- for func in (add , sub ):
1144
- print (f'\n Test { func .__name__ } with another NormalDist:' )
1145
- print (func (g1 , g2 ))
1146
- print (NormalDist .from_samples (map (func , G1 , G2 )))
1147
-
1148
- const = 11
1149
- for func in (add , sub , mul , truediv ):
1150
- print (f'\n Test { func .__name__ } with a constant:' )
1151
- print (func (g1 , const ))
1152
- print (NormalDist .from_samples (map (func , G1 , repeat (const ))))
1153
-
1154
- const = 19
1155
- for func in (add , sub , mul ):
1156
- print (f'\n Test constant with { func .__name__ } :' )
1157
- print (func (const , g1 ))
1158
- print (NormalDist .from_samples (map (func , repeat (const ), G1 )))
1159
-
1160
- def assert_close (G1 , G2 ):
1161
- assert isclose (G1 .mean , G1 .mean , rel_tol = 0.01 ), (G1 , G2 )
1162
- assert isclose (G1 .stdev , G2 .stdev , rel_tol = 0.01 ), (G1 , G2 )
1163
-
1164
- X = NormalDist (- 105 , 73 )
1165
- Y = NormalDist (31 , 47 )
1166
- s = 32.75
1167
- n = 100_000
1168
-
1169
- S = NormalDist .from_samples ([x + s for x in X .samples (n )])
1170
- assert_close (X + s , S )
1171
-
1172
- S = NormalDist .from_samples ([x - s for x in X .samples (n )])
1173
- assert_close (X - s , S )
1174
-
1175
- S = NormalDist .from_samples ([x * s for x in X .samples (n )])
1176
- assert_close (X * s , S )
1177
-
1178
- S = NormalDist .from_samples ([x / s for x in X .samples (n )])
1179
- assert_close (X / s , S )
1180
-
1181
- S = NormalDist .from_samples ([x + y for x , y in zip (X .samples (n ),
1182
- Y .samples (n ))])
1183
- assert_close (X + Y , S )
1184
-
1185
- S = NormalDist .from_samples ([x - y for x , y in zip (X .samples (n ),
1186
- Y .samples (n ))])
1187
- assert_close (X - Y , S )
1188
-
1189
- print (doctest .testmod ())
0 commit comments