# Ticket #10019: trac10019-variance_perf_issue.patch

File trac10019-variance_perf_issue.patch, 1.6 KB (added by ylchapuy, 3 years ago)
• ## sage/stats/basic_stats.py

```# 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 if len(v) == 0: # standard deviation of empty set defined as NaN return NaN for i in range(len(v)): x += (v[i] - mean(v))**2 if bias: # population standard deviation if isinstance(x, (int,long)): return sqrt(x/ZZ(len(v))) return sqrt(x/len(v)) else: # sample standard deviation if isinstance(x, (int,long)): return sqrt(x/ZZ(len(v))) return sqrt(x/(len(v)-1)) return sqrt(variance(v, bias=bias)) def variance(v, bias=False): """ ...        return 1 sage: stats.variance(MyClass()) 1 TESTS: The performance issue from #10019 is solved:: sage: variance([1] * 2^18) 0 """ if hasattr(v, 'variance'): return v.variance(bias=bias) import numpy if len(v) == 0: # variance of empty set defined as NaN return NaN for i in range(len(v)): x += (v[i] - mean(v))**2 mu = mean(v) for vi in v: x += (vi - mu)**2 if bias: # population variance if isinstance(x, (int,long)):