>{-# LANGUAGE BangPatterns #-}
So I've been playing with the median problem today. Not sure why, but it stuck in my head.
>import Data.List
>import Control.Monad.ST
>import Data.STRef
>import Control.Monad
>import Control.Applicative ((<$>))
>import qualified Data.Vector.Mutable as V
>import Debug.Trace
I've been using the hashing algorithm that I described last night, but it's quite slow. I must be missing something obvious. It really shouldn't be slow at all!
So I have a median function:
>median
First I assume you know the range of your data.
> (min,max)
Then I ask you to figure out how many buckets you want to create, and which of those buckets you actually want to fill. You should only fill the buckets near the middle, based on an educated guess of the distribution of your data, otherwise, you will end up wasting memory. I you guessed the middle correctly the function will return more quickly than if you didn't.
> numBuckets guessedMiddle
Then you are to pass it your list.
> list =
> let
First I seperate out the values into a list of buckets, each one holding values which are near to eachother. I can figure out the length of the list at the same time, since I have to go through the whole thing anyways.
> (myBuckets,length) =
> buckets
> (min,max)
> numBuckets
> guessedMiddle
> list
> halfLength = length `div` 2
The buckets are set up, so that the first bucket has the lowest values, and the last bucket has the highest values. Each bucket, is a tuple, of it's length and it's contents. We fold across the list of buckets, accumulating the "index so far", until we find the bucket in which the median must reside.
> Right ((_,medianBucketVals),stubLen,medianBucketIx) =
foo :: Either (Int,Int) ((a,Maybe [b]),a,Int)
foo =
> foldr
> (\thisBucket@(thisBucketLen,_)
> eitheriOrMedianBucket ->
> case eitheriOrMedianBucket of
> Left (i,bucketIx) ->
> let
> nextI = i + thisBucketLen
> in
> if nextI > halfLength
> then Right (thisBucket, thisBucketLen-(halfLength - i),(bucketIx-1))
> else (Left (nextI, (bucketIx + 1)))
> _ -> eitheriOrMedianBucket)
> (Left (0,0))
> myBuckets
> in
We then sort the bucket in which the median must reside, and we then find the median the normal way. This should be faster, since we only had to sort one bucket, rather than the entire list. It is not, so there must be something horrible going on.
> case medianBucketVals of
> Just medianBucket' ->
> sort medianBucket' `genericIndex` stubLen
> Nothing ->
> median
> (min,max)
> numBuckets
> (medianBucketIx,medianBucketIx)
> list
Here is my actual function for seperating the values out into buckets.
>buckets
> (min,max)
> numBuckets
> (guessedMiddleStart,guessedMiddleEnd)
> list =
> runST $ do
> lengthRef <- newSTRef 0
First we create a list of empty buckets. Since it would be a waste of memory to actually fill the buckets near the edges of our distribution(where we are not likely to find our median), our buckets contain Maybe lists, and the buckets which are outside of our guessed bucket range will be filled with Nothing.
> buckets' <- V.new numBuckets
> mapM
> (\n->
> V.write
> buckets'
> n
> (0,
> if n >= guessedMiddleStart && n <= guessedMiddleEnd
> then Just []
> else Nothing))
> [0..numBuckets-1]
Then we go through the buckets, figuring out which bucket to put a given value into. We calculate the length at the same time.
> let
> bottom =
> (fromIntegral (max - min))
> /
> (fromIntegral (numBuckets-1))
> forM_ list $ \number -> do
> let
Figure out which bucket to put this into.
> bucket =
> whichBucket
> (min,max)
> bottom
> numBuckets
> number
Increment length.
> modifySTRef
> lengthRef
> (+1)
However tests show that's not true.
Put the value into the appropriate bucket.
> (oldLen,oldVal) <- V.read
> buckets'
> bucket
> let !newLen = oldLen + 1
> !newVal = (number:) <$> oldVal
> V.write
> buckets'
> bucket
> (newLen, newVal)
> filledBuckets <-
> mapM
> (\index->V.read buckets' index)
> [0..numBuckets-1]
> length <- readSTRef lengthRef
> return (filledBuckets,length)
>whichBucket (min,max) bottom numBuckets number =
> floor((fromIntegral (number - min))
> /
> bottom)
So I created a little test scenario.
>someListNumBuckets = 100
>someListGuessedMiddle = (45,55)
>someListLength = genericLength someList
And found that this:
>realMedian = sort someList `genericIndex` (someListLength `div` 2)
Is actually faster ^_^ :O
*Main> realMedian
500
(7.18 secs, 2031543472 bytes)
Than this:
>someListMedian =
> median
> (someListMin,someListMax)
> someListNumBuckets
> someListGuessedMiddle
> someList
*Main> someListMedian
Just 500
(37.77 secs, 15376209200 bytes)
someListMin :: Integer
>someListMin = 0
someListMax :: Integer
>someListMax = 1000
someList :: [Integer]
>someList =
> concatMap
> (\n->intersperse n [someListMin..someListMax])
> [someListMax,someListMax-1..someListMin]
>main :: IO ()
>main = print someListMedian