# HG changeset patch
# User Yann Laigle-Chapuy <yannlaiglechapuy@gmail.com>
# Date 1285529522 -7200
# Node ID 9563c29e41dfd5b7841827b243565cb62cb789f9
# Parent 7f5668f0b5d22eea03c50010820ea7f308c90eff
#10019 big performance issue with variance
diff -r 7f5668f0b5d2 -r 9563c29e41df sage/stats/basic_stats.py
|
a
|
b
|
|
| 195 | 195 | if len(v) == 0: |
| 196 | 196 | # standard deviation of empty set defined as NaN |
| 197 | 197 | return NaN |
| 198 | | for i in range(len(v)): |
| 199 | | x += (v[i] - mean(v))**2 |
| 200 | | if bias: |
| 201 | | # population standard deviation |
| 202 | | if isinstance(x, (int,long)): |
| 203 | | return sqrt(x/ZZ(len(v))) |
| 204 | | return sqrt(x/len(v)) |
| 205 | | else: |
| 206 | | # sample standard deviation |
| 207 | | if isinstance(x, (int,long)): |
| 208 | | return sqrt(x/ZZ(len(v))) |
| 209 | | return sqrt(x/(len(v)-1)) |
| 210 | 198 | |
| 211 | | |
| 212 | | |
| | 199 | return sqrt(variance(v, bias=bias)) |
| 213 | 200 | |
| 214 | 201 | def variance(v, bias=False): |
| 215 | 202 | """ |
| … |
… |
|
| 260 | 247 | ... return 1 |
| 261 | 248 | sage: stats.variance(MyClass()) |
| 262 | 249 | 1 |
| | 250 | |
| | 251 | TESTS: |
| | 252 | |
| | 253 | The performance issue from #10019 is solved:: |
| | 254 | |
| | 255 | sage: variance([1] * 2^18) |
| | 256 | 0 |
| 263 | 257 | """ |
| 264 | 258 | if hasattr(v, 'variance'): return v.variance(bias=bias) |
| 265 | 259 | import numpy |
| … |
… |
|
| 274 | 268 | if len(v) == 0: |
| 275 | 269 | # variance of empty set defined as NaN |
| 276 | 270 | return NaN |
| 277 | | for i in range(len(v)): |
| 278 | | x += (v[i] - mean(v))**2 |
| | 271 | |
| | 272 | mu = mean(v) |
| | 273 | for vi in v: |
| | 274 | x += (vi - mu)**2 |
| 279 | 275 | if bias: |
| 280 | 276 | # population variance |
| 281 | 277 | if isinstance(x, (int,long)): |