Perl: the Markov chain saw PerlMonks

### Faster Statistics for Discrete Data

by RhetTbull (Curate)
 on Feb 21, 2002 at 03:17 UTC ( #146691=CUFP: print w/replies, xml ) Need Help??

### Background

I use Statistics::Descriptive a fair bit. It's a great module that's been around a long time and is well tested. However, if you need to use the functionality in Statistics::Descriptive::Full, it stores your entire data set in an array. If you have, oh, 2 million plus data points, then that array gets rather large. On a recent real-world data set that I've been analyzing, I had several sets of data with 2.6 million data points. Statistics::Descriptive crunched the data ok but it took 10 minutes and more than 400MB of memory! Since I needed to analyze a lot of data I wanted to find a better way.

### There's got to be a better way

Although my data set is large, I happen to know an interesting thing about the data: it is satellite telemetry that's discretized (is that a word?) into a 4 bit word. That means there's at most 16 possible values for each data point. All of the statistics I'm interested in can be calculated if I know what values I saw and how many times I saw each value. Aha! Sounds like a job for a hash.

So, instead of storing every data point in an array, I only store the values I've seen and the number of times I've seen them in a hash.

### Implementation

Using the hash idea, I implemented this module (named Statistics::Descriptive::Discretized for now). The data is stored in a hash instead of an array. This works very well if you have a limited number of discrete values in your data set. I've tested this with simulated 16 bit output (meaning 2^16 possible values) and it scales quite well, even with 1 million+ data points with 65,536 possible values. If your input data is not limited to discrete values then this will probably perform worse than the array method used by Statistics::Descriptive.

I've tried to keep the interface as close as possible to the Statistics::Descriptive interface. This is a rough draft and all of the routines in Statistics::Descriptive are not fully implemented yet. (Indeed, any that depend on the original order of the data can't be implemented with this method). For many purposes, this module should be a drop in replacement for Statistics::Descriptive.

### Results

I tested this module (using Statistics::Descriptive as a baseline) against several large real world data sets. Statistics::Descriptive::Discretized scales linearly and blows the socks off of Statistics::Descriptive. (I'm not knocking the excellent Statistics::Descriptive -- it's a great module! I just present an alternative that works better for certain data sets). Here are some results:
 Data points Run Time (sec)Statistics::Descriptive Run Time (sec)Discretized 100000 12 1.5 200000 24 3 300000 35 4 500000 59 7 700000 87 10 1000000 119 14 1500000 215 21 2000000 456 29 2600000 561 40

As you can see, after a million points, Statistics::Descriptive starts to scale somewhat exponentially but the Discretized version stays linear. For the test case with 2.6 million data points, this module is 14 times faster than the baseline (and it uses only a few MB of RAM while Statistics::Descriptive uses more than 400MB for this data set!)

### The Code

Here's a sample program that shows how to use it. If you have any suggestions, critiques, etc. please fire away. If this seems like a useful thing, I'll clean it up for the CPAN.

Replies are listed 'Best First'.
Re: Faster Statistics for Discrete Data
by Dog and Pony (Priest) on Feb 21, 2002 at 17:24 UTC
Indeed, any that depend on the original order of the data can't be implemented with this method

Maybe Tie::DxHash or Tie::IxHash could be of any help with this, in case it is an issue that should be dealt with?

You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.
Thanks for the suggestions. Unfortunately, even something like Tie::IxHash would defeat the purpose of this module. If you have to preserve order, you might as well use an array since you'd need to know where every data point came in. I suppose you could do something like run-length encoding if you had long runs of the same value but the hash overhead would probably eat up the savings for all but very limited data sets. Fortunately, there's very few statistical things (at least that I'm aware of) that depend on the order of the data (The least_squares_fit method of Statistics::Descriptive is the only one I can think of off the top of my head). There are some things that require the data to be in sorted order, and for that my method works quite well since all I have to sort is the hash keys not all the values.
Re: Faster Statistics for Discrete Data
by Everlasting God (Beadle) on Feb 21, 2002 at 04:08 UTC
My Perl skills are too rusty to comment on the code, but the proper word for 'discretized' is quantized.

'The fickle fascination of and Everlasting God' - Billy Corgan, The Smashing Pumpkins

Nope, discretized is the usual statistics terminology. Quantized has additional meanings which are not universally appropriate,

-- Anthony Staines
Really? Never heard of discretized before. Learn something every day I guess. Which additional meanings of quantized aren't appropriate here?

'The fickle fascination of and Everlasting God' - Billy Corgan, The Smashing Pumpkins
Re: Faster Statistics for Discrete Data
by rinceWind (Monsignor) on Feb 22, 2002 at 10:24 UTC
Couldn't you just tie the array and use the original package? Something like Tie::MmapArray could be used to map your array to a disk file.
Actually, tying to a file has some drawbacks. Foremost of which is that you have to have a file to tie to. I often pipe in data from another program or read in the data from a database. A tied file approach would require the overhead of writing to disk and reading back. Also, some statistics (such as median) require the data to be in sorted order -- that means you'd have to sort the file in memory or sort and write to disk again which defeats the purpose. The approach I used gets around all of those limitations -- you don't need an intermediate file and you don't have to sort the original data points.
(RhetTbull) Re: Faster Statistics for Discrete Data
by RhetTbull (Curate) on Mar 07, 2002 at 16:44 UTC
Well, after a bit of discussion here and on the module-authors list I settled on Statistics::Descriptive::Discrete. This module is now available on CPAN as Statistics::Descriptive::Discrete. I'd appreciate hearing if you find it useful (or if you find it buggy!)
--Rhet

Create A New User
Node Status?
node history
Node Type: CUFP [id://146691]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-04-18 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?