Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6960

Re: Standard Deviation One-liner

From Alain Ketterlin <alain@dpt-info.u-strasbg.fr>
Newsgroups comp.lang.python
Subject Re: Standard Deviation One-liner
Date 2011-06-03 20:50 +0200
Organization Universites Paris VI/Paris VII - France
Message-ID <87oc2erbpf.fsf@dpt-info.u-strasbg.fr> (permalink)
References <isb75r$80h$1@speranza.aioe.org>

Show all headers | View raw


Billy Mays <noway@nohow.com> writes:

> 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).

> a=lambda d:(sum((x-1.*sum(d)/len(d))**2 for x in d)/(1.*(len(d)-1)))**.5

You should make it two half-liners, because this one repeatedly computes
sum(d). I would suggest:

aux = lambda s1,s2,n: (s2 - s1*s1/n)/(n-1)
sv = lambda d: aux(sum(d),sum(x*x for x in d),len(d))

(after some algebra). Completely untested, assumes data come in as
floats. You get the idea.

-- Alain.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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