Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7055
| Date | 2011-06-05 12:17 -0700 |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Subject | Re: Standard Deviation One-liner |
| References | <isb75r$80h$1@speranza.aioe.org> <58f86ea2-b654-4240-a5f5-c5c7e503bcf1@s41g2000prb.googlegroups.com> <4debbc27$0$29996$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2472.1307301527.9059.python-list@python.org> (permalink) |
Steven D'Aprano wrote: > On Fri, 03 Jun 2011 13:09:43 -0700, Raymond Hettinger wrote: > >> On Jun 3, 10:55 am, Billy Mays <no...@nohow.com> wrote: >>> I'm trying to shorten a one-liner I have for calculating the standard >>> deviation of a list of numbers. I have something so far, but I was >>> wondering if it could be made any shorter (without imports). >>> >>> Here's my function: >>> >>> a=lambda d:(sum((x-1.*sum(d)/len(d))**2 for x in >>> d)/(1.*(len(d)-1)))**.5 >>> >>> The functions is invoked as follows: >>> >>> >>> a([1,2,3,4]) >>> 1.2909944487358056 >> Besides trying to do it one line, it is also interesting to write an >> one-pass version with incremental results: >> >> http://mathcentral.uregina.ca/QQ/database/QQ.09.06/h/murtaza2.html > > I'm not convinced that's a good approach, although I haven't tried it. In > general, the so-called "computational formula" for variance is optimized > for pencil and paper calculations of small amounts of data, but is > numerically unstable. > > See > > http://www.johndcook.com/blog/2008/09/26/comparing-three-methods-of- > computing-standard-deviation/ > > http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance > > > > I'll also take this opportunity to plug my experimental stats package, > which includes coroutine-based running statistics, including standard > deviation: > >--> s = stats.co.stdev() >--> s.send(3) > nan Look! A NaN in the wild! :) ~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Standard Deviation One-liner Billy Mays <noway@nohow.com> - 2011-06-03 13:55 -0400
Re: Standard Deviation One-liner Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-06-03 20:50 +0200
Re: Standard Deviation One-liner Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-06-03 21:10 +0200
Re: Standard Deviation One-liner Raymond Hettinger <python@rcn.com> - 2011-06-03 13:09 -0700
Re: Standard Deviation One-liner Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-05 17:26 +0000
Re: Standard Deviation One-liner Ethan Furman <ethan@stoneleaf.us> - 2011-06-05 12:17 -0700
csiph-web