Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'package,': 0.03; 'hettinger': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'function:': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; '>>>': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'approach,': 0.16; 'billy': 0.16; 'calculating': 0.16; 'incremental': 0.16; 'nan': 0.16; 'numerically': 0.16; 'received:gateway01.websitewelcome.com': 0.16; 'shorten': 0.16; 'variance': 0.16; 'url:blog': 0.17; 'header :In-Reply-To:1': 0.21; "haven't": 0.22; 'raymond': 0.22; 'trying': 0.23; '(without': 0.23; 'fri,': 0.23; 'url:wiki': 0.23; 'tried': 0.27; "i'm": 0.27; 'wondering': 0.28; 'besides': 0.29; 'url:ca': 0.29; 'version': 0.29; 'far,': 0.30; 'so-called': 0.30; 'it.': 0.31; 'steven': 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; "i'll": 0.34; 'follows:': 0.34; 'header:User-Agent:1': 0.35; 'skip:" 10': 0.35; '-0700,': 0.35; "d'aprano": 0.35; 'numbers.': 0.35; 'data,': 0.36; 'running': 0.37; 'something': 0.37; 'url:en': 0.37; 'could': 0.38; 'url:org': 0.38; 'but': 0.38; 'url:h': 0.38; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'skip:1 10': 0.63; 'received:websitewelcome.com': 0.67; 'received:69.93': 0.67; 'paper': 0.70; 'subject:One': 0.73; '10:55': 0.84; 'calculations': 0.84; 'formula"': 0.84; 'url:database': 0.84 Date: Sun, 05 Jun 2011 12:17:46 -0700 From: Ethan Furman User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) MIME-Version: 1.0 To: python-list@python.org Subject: Re: Standard Deviation One-liner References: <58f86ea2-b654-4240-a5f5-c5c7e503bcf1@s41g2000prb.googlegroups.com> <4debbc27$0$29996$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <4debbc27$0$29996$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: c-67-170-168-84.hsd1.or.comcast.net ([192.168.74.5]) [67.170.168.84]:2681 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307301527 news.xs4all.nl 49178 [::ffff:82.94.164.166]:56997 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7055 Steven D'Aprano wrote: > On Fri, 03 Jun 2011 13:09:43 -0700, Raymond Hettinger wrote: > >> On Jun 3, 10:55 am, Billy Mays 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~