BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
I'm comparing several billion long strings for uniqueness. A brute force cross comparison is out of the question. Building an inmemory hash or suffix tree is impractical. And diskbased solutions too slow.
The mechanism chosen for the task is as follows:
 Calculate the 128bit MD5 hash of each string.
 Break that into 4 x 32bit integers.
 Use those four integers to set bits in each of 4 corresponding 2**32bit vectors.
The question is: What is the probability of getting false positives?
That is, the odds of finding all 4 bits (1 in each vector) set, when the MD5 hash being tested has not previously be set into the vectors.
Ignoring the fact that any hashing mechanism that maps a large range (10e44 in this case) to a smaller smaller range (2**128 / 3e38) will produce duplicate mappings, and assuming that the MD5 hashing algorithm, and its subdivision into 4x 32bit hashes provide perfect distribution.
 When setting the first value into the vectors, the odds of a false collision are obviously 0.
 When setting the second value in, the odds of all 4 sub hashes being the same, whilst the MD5 is different are again zero.
 When setting the third value in, the odds that each of the 4 sub hashes will match 1 of the two sub hashes previously set into that vector are still very small, but tangible.
 And as each new quad of sub hashes is added to the vectors, the odds of a false positive increase over the previous addition.
How to calculate those odds for each new hash, as the number of hashes already set into the vectors increases?
Further to that, if the number of sub hashes and vectors is increased to 10, by permuting each of the four sub hashes against the other 3 and XORing, does that substantially decrease the odds of false matches? (Or increase them? Or leave them the same?)
The mechanism is a variation on the theme of Bloom filters. The statistics of bloom filters is documented, but goes over my head.
I believe that using separate vectors for the sub hashes should improve (decrease) the odds of false positives, but my math is insufficient to verify that belief.
Can these probabilities be calculated in a way that a stats layman (me) will understand?


Replies are listed 'Best First'.  

Re: [OT] The statistics of hashing.
by graff (Chancellor) on Apr 01, 2012 at 00:49 UTC  
In my linguistic background, "several" usually means "more than 4, less than 10". If that's what it means for you as well, and if syphilis has provided a valid metric, then once you're done processing the first 4 billion strings, each new string will definitely be counted as a duplicate, regardless of whether it's a true or false positive. (Perhaps that's not exactly an absolute certainty, but it seems close enough.)  [reply]  
by BrowserUk (Pope) on Apr 01, 2012 at 08:26 UTC  
If you're just looking for an estimate, why not work on a manageable and reasonably sampled subset of the data? Because, according to (my application of) the formulae I found on line, I should be seeing false positive after low 10s of millions of inserts. But my testing showed none after 1e6, 10e6, 100e6, 200e6 ... Ie. My sampling showed that the calculations were wrong, but provided no information upon which to draw any further conclusions. The nature of the beast is that I needed to run the experiment for 3/4 of a billion iterations in order to get my first piece of actual information from it. This start out being about verify my gut with respect to the big strings. That lead to looking for a mechanism capable of verifying uniqueness with the huge numbers involved. That lead  through various documented mechanisms that proved inadequate  to coming up with the current schema that appears to work substantially better than all the documented mechanisms. This post is about trying to come up with math that fits the experimental evidence. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply]  
Re: The statistics of hashing.
by tobyink (Canon) on Mar 31, 2012 at 22:58 UTC  
Seems almost certain to me, but my reasoning may well be faulty. Stats were never my forte. You're effectively running four different 32bit hash functions on each string. According to Wikipedia for a 32bit hash you only need about 110,000 hashes to get a 75% chance of a collision. Thus with 110,000 strings, getting a collision in all four stands at 75%**4, about 30% chance. That's with 110,000 strings. You have "several billion". With several billion strings you are going to have a very close to 100% chance of a collision on a 32bit hash function. With four 32bit hash functions, that's still close to 100% chance of a collision. Extending this to having 10 effective hash functions, this cannot increase the chance of collisions. Worst case scenario it has no effect. Here I think it does decrease the chance (difficult , but not enough to be useful.
perl E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]>[3])=~s{::}{ }and$monkey}}"Monkey say">Monkey::do'
 [reply]  
by BrowserUk (Pope) on Mar 31, 2012 at 23:26 UTC  
Seems almost certain to me, That's a given. As with bloom filters, false positives are a fact of life. The question is how likely, or more to the point, how frequently, they will occur. According to Wikipedia for a 32bit hash you only need about 110,000 hashes to get a 75% chance of a collision. Unfortunately, a good deal of what you read on wikipedia is less than reliable. The following is empirical evidence from an ongoing experiment. After 3/4 of a billion trials, there were zero (possible) dups. After 1.2 billion, there are only 61. These are the positions at which those possible dups were detected:
As you can see, having filled 1/4 quarter of the total space available, false positives are just beginning to occur. And as the vector space fills, they are (as expected) becoming more frequent. (Of course, some of those may be actual duplicates. It will take a second run to determine that.) But the question remains, how to calculate the probabilities of the mechanism. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply] [d/l]  
by tobyink (Canon) on Apr 01, 2012 at 09:13 UTC  
Indeed, but the errors on Wikipedia are not evenly distributed. On a subject such as the birthday attack I'd expect Wikipedia's article to be on par with any other authority (shortlived events of vandalism notwithstanding).
The exact calculation involves some big numbers. But assuming that c($x, $y) is the "pick $y from $x" function used in combinatorics, then the probability of a collision for $n strings and an evenly distributed 32bit hash function should be:
Big numbers. Horrible to calculate. Can be approximated though...
Calculating $p is still horrible, but calculating $t is easier. If $t is above 20 then $p is 1.00000 when rounded to 6 significant figures. Thus you can effectively be sure to have a collision with a 32bit hash function once $t is above 20. You can figure out an $n which triggers $t to be 20 using:
It's about 414,000. So with 414,000 strings, you are effectively certain to get collision on a 32bit hash function. Where I think my reasoning and tye's differ (and tye is almost certainly correct here  blame it on me answering late at night) is that I was then looking at the probabilities that you will have had collisions in all four (or ten) hash functions at the end of the entire run. With even half a million strings, that is a given. What you're actually doing is looking at events where a single string triggers a simultaneous collision in all the hash functions. I defer to tye's calculations for that.
perl E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]>[3])=~s{::}{ }and$monkey}}"Monkey say">Monkey::do'
 [reply] [d/l] [select]  
by BrowserUk (Pope) on Apr 01, 2012 at 09:34 UTC  
by tye (Sage) on Apr 01, 2012 at 07:54 UTC  
Wikipedia is correct (well, agrees quite closely with some of my calculations). But you have extrapolated from that quite incorrectly. 0.75**4 is the odds of, after inserting 1.1e5 strings, that there has been at least one collision in each of the four hashes. It is not even close to the odds of there being a single string that produced a simultaneous collision in all 4 hashes. Those odds would be more like 0.75*(0.75/1.1e5)**3 or about 2e16. My calculations came up with an upper bound of 9.5e15. I believe the 9.5e15 number is much, much more accurate. It only discounts the possibility of there being fewer than 1.1e5 bits set. I suspect the ratio of "expected bits set"/1.1e5 to be very close to 1. While the 2e16 number is based on there being a 75% chance of exactly one collision. But there is a 25% chance of 0 collisions and a 75% chance of 1 or more collisions. And the possibility of 2 collisions approximately doubles the odds for some subset of the possible outcomes and so has a much bigger impact on the final total than does the possibility of there being 1.1e51 bits set instead of 1.1e5. For example, consider tossing a coin twice. There is a 75% chance of getting at least one "head". Do 4 such trials. For each head, assign it a random position in 1.1e5 slots (but only one head per slot per trial). The odds of the 4 trials producing 4 heads in one of those slots (one from each trial) one might estimate as:
Where x==1.1e5. Or, 2.3e16. But to find the accurate odds you have to realize that there is a 50% chance of one "head" (per trial) and a 25% change two "heads" (per trial). So the actual odds are found via:
Where x1==1.1e51 and x12==1/(1.1e51)+1/(1.1e52). That raises the odds by a factor of 2.37 to 5.6e16. That type of magnification of the odds will be much more pronounced in the case originally discussed and I bet would result in nearly the 48fold increase required to get to the odds of my upper bound calculation.  tye  [reply] [d/l] [select]  
by JavaFan (Canon) on Apr 01, 2012 at 11:17 UTC  
Thus with 110,000 strings, getting a collision in all four stands at 75%**4, about 30% chance. That's with 110,000 strings. You have "several billion".If, after 110,000 strings you get a 75% on a collision, 75%^{4} gives you the chance 4 sets of 110,000 each give you a collision. But that's nowhere near the probability the same pair gives you a collision. Which is what BrowserUK needs. It would take me too much time to dig up the math required to do the calculation. But what BrowserUK may try to do: generate a billion strings that are all unique. Run the proposed algorithm. See how many collisions are reported: they're all false positives. Repeat several times (with different strings). You may even want to calculate a confidence interval. Of course, I've no idea how feasible this is. If it takes a week to process a data set this size, you may not want to do so.  [reply]  
Re: [OT] The statistics of hashing.
by syphilis (Bishop) on Mar 31, 2012 at 23:13 UTC  
Update: Following figure is not right. (Corrected figure provided in following post.) IIUC it's just (N/4294967296)**4, where N is the number of MD5 hashes that have already been entered into the bit vector. But that's assuming that MD5 hashes distribute evenly, and I don't know if that has been established (or disproved). If they don't distribute evenly, then the odds of hitting a false positive will increase. Not sure what affect your "Bloom Filters" variation would have. Cheers, Rob  [reply]  
by syphilis (Bishop) on Mar 31, 2012 at 23:55 UTC  
I believe the figure I should have provided is: (1  ((4294967295/4294967296) ** N)) ** 4 And if that's not right, I give up. Update: The logic is simple. (Danger, Will Robinson ;) If you're picking numbers at random in the range (1 .. 4294967296), then the probability that the N+1th pick has already come up is: 1 minus the probability that it has *not* already come up and the probability that it has *not* already come up is (4294967295/4294967296) ** N So that gives us the 1  ((4294967295/4294967296) ** N) But because we're looking for the case where all *4* numbers have already come up, that probability needs to be raised to the 4th power ... which yields the final figure. Cheers, Rob  [reply]  
by BrowserUk (Pope) on Apr 01, 2012 at 09:04 UTC  
Thanks syphilis. Your calculations make sense to me. But I'm not sure that it gels with the actual data? Assuming I've coded your formula correctly (maybe not!), then using 10 hashes & vectors, I get the odds of having seen a dup after 1e9 inserts as (1  ((4294967295/4294967296)**1e9) ) **10 := 0.00000014949378123. By that point I had actually seen 13 collisions: <Reveal this spoiler or all in this thread>
And looking at the figure for 4e9 := 0.00667569553892502, by which time the 10 vectors will be almost fully populated, it looks way too low to me? I would have expected that calculation (for N=4e9) to have yielded odds of almost 1? With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply] [d/l]  
by syphilis (Bishop) on Apr 01, 2012 at 19:33 UTC  
by BrowserUk (Pope) on Apr 01, 2012 at 20:06 UTC  
 
Re: [OT] The statistics of hashing. (birthday)
by tye (Sage) on Apr 01, 2012 at 04:03 UTC  
Although this sounds similar to the birthday problem, it is mostly much simpler (and partly much harder). The odds of a collision at each step are quite simply $setBits/2**32. So the odds of having 4 collisions in a row are prod(@setBits)/2**(4*32). So, if after inserting 100 items, your 2**32bit vectors have, respectively, 99, 100, 100, and 98 bits set, then the odds of a 4fold collision is 99*100*100*98/2**(32*4) or about 1 in 1e31. Now, the odds of there being 100 bits set after setting 100 of the bits, that is the same as the birthday problem. So, if you wanted to predict the likely odds of a collision after inserting, say, 1e4 strings, then you would first compute the odds of the full 1e4 different bits being set. The birthday problem tells us that the odds are about 98.84% that all 1e4 bits would be set in a given vector. Or that there is a 1.16% chance of at least one collision (in a given vector).
As calculated using Math::BigApprox:
So, when you insert 1e6 strings, you are not going to end up with 1e6 bits set. But getting a precise value for "expected number of bits set" is rather complex. But I wouldn't bother. I'd just count the number of bits that are actually set as you go along. Then, when one goes to insert the next string, just calculate $b1*$b2*$b3*$b4/2**(32*4) in order to get the odds of a false positive on that insertion.
But inserting about If you want to calculate the odds of there having been at least one false positive across the entire sequence of 1e6 inserts, then you'd compute 1prod(@odds) where @odds contains the odds of no false positive at each step. So $odds[$i] would be 1  $b1*$b2*$b3*$b4/2**(32*4). So you can calculate such that way, by just keeping a running product of that value. Note, however, that prod(@odds) will stay exactly 1.0 when using floating point until you get to the point that $b1*$b2*$b3*$b4/2**(32*4) creeps above about 1/2**$mantissaBits, (which won't happen until about 1/2 million inserts for typical 'double'). I'm not sure how much of an impact that will have on the accuracy when getting to the only interesting odds in the late stages. But it seems it might be quite significant. But you can compensate for this by instead tracking 1prod(@odds). That starts out simply as 1*1*1*1/2**(32*4) after the first insert. To effectively calculate 1prod(@odds[0..$n]) when given 1prod(@odds[0..$n1]) and $odds[$n] isn't hard. It is just $prod += $odds$prod*$odds (and that last term doesn't matter for a very long time). Just to be clear, $odds is just $b1*$b2*$b3*$b4/2**(32*4) where $b1 is the number of bits set in the first vector. So, doing that calculation as you go will give you the odds of having had at least one false positive so far. After 1e5 inserts it will only be about 6e15, depending on how many bit collisions you run into by then. You can also get an upper bound on it for however many strings you have by just running through the 1e9+ calculations assuming no singlebit collisions ever happen. I won't wait for that to finish running here. But it turns out that I don't have to:
 tye  [reply] [d/l] [select]  
by BrowserUk (Pope) on Apr 01, 2012 at 08:13 UTC  
By those values, the odds against not having seen a duplicate by the time you reached 100 million inserts are so low as to be a pretty damn good definition of 'impossible'. And yet, empirically, none had been seen by the time I reached 779,967,210. And after 1.5 billion inserts, that calculation suggests that the odds of finding a value that doesn't match would be minuscule, and the "possible dups" count should be growing at almost the same rate as the new inserts are being tested. The reality is that I've only had 1323 collisions after 1.5 billion inserts. These are the last 8:
They are coming much more regularly now, but they are actually still within the bounds of usability. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply] [d/l] [select]  
by tye (Sage) on Apr 01, 2012 at 19:00 UTC  
You appear to have not fully understood several parts of what I wrote. (Which is not an insult.) And after 1.5 billion inserts, that calculation suggests that the odds of finding a value that doesn't match would be minuscule, and the "possible dups" count should be growing at almost the same rate as the new inserts are being tested. That actually is not a valid conclusion from anything I wrote. The odds of the next insert being a collision after about 1.5e9 inserts is about (1.5e9/2**32)**4 or under 1.5%, and 98.5% is not "miniscule" (and about 1/100th as fast is not "almost the same rate"). But that is also ignoring that there won't actually be 1.5e9 bits set. But you make no mention of how many bits are set which is how you'd get an accurate calculations so I'm curious if you perhaps didn't fully appreciate that point (as opposed to some other reason like it perhaps not being easy to query the number of bits set in your current run, etc.). Probably much more important is that in some replies you hint that you are using 10 hashes not 4 hashes now. I suspect that is the case in the numbers you show it your reply above, because you don't show collisions happening on the order of every 1e2 inserts. With 10 hashes, the above odds go to around 2.7e5. And that is in the ballpark of what you report seeing. If you need some help redoing the calculations for 10 hashes instead of 4, then please speak up. I was not exhaustive in my explanations nor expansions of formulae, so I would expect more clarification could be required. I can also understand you not taking the time to seriously study what I wrote yet given how the numbers seem so far off from your observations.  tye  [reply]  
by BrowserUk (Pope) on Apr 01, 2012 at 19:39 UTC  
by tye (Sage) on Apr 01, 2012 at 21:32 UTC  
 
Re: [OT] The statistics of hashing.
by moritz (Cardinal) on Apr 01, 2012 at 10:44 UTC  
I'm not sure I understood the question correctly, but I hope my answer is still helpful for you. Further to that, if the number of sub hashes and vectors is increased to 10, by permuting each of the four sub hashes against the other 3 and XORing, does that substantially decrease the odds of false matches? (Or increase them? Or leave them the same?) Statistically, the only thing that matters for the probability of collisions is the total entropy of the hash. If you create a 128bit hash over a string, you have 128 bits of entropy. If you create two 128bit hashes, one over the first part and one over the second part of the string, you have 256 bits of entropy. If you XOR the two hashes, and use the results, you have 128 bits again. If you use one 128bit hash, the probabilities for getting no collision on inserting the nth number is
I'm pretty sure that the number of collisions follows (at least approximately) a Poisson distribution, though I haven't yet figured out how to calculate the expectation value.  [reply] [d/l]  
by BrowserUk (Pope) on Apr 01, 2012 at 11:30 UTC  
Hm. Attempting calculate 1  1/2**128 using my calculator just returns 1. So then I tried Math::BigFloat, and still got just 1. What Am I doing wrong?
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply] [d/l] [select]  
by moritz (Cardinal) on Apr 01, 2012 at 15:31 UTC  
You'd need a floatingpoint number with a mantissa of about 128 bits to hold the value of (11/2**128) Luckily there are other ways to evaluate the expression. If we set x = 1/2**128, we can do a taylor series to evaluate (1x)**n around x = 0 (and since x is very small, it's a good approximation:
Where higher orders can be neglected. If you chose an n that is high enough, you can get non1 values as the result. Though I'm not sure if that's really helpful. What you'd really need is the expectation value of the number of collisions (the lambda symbol in Poisson Distribution, then you could easily calculate everything you wanted.  [reply] [d/l] [select]  
Re: [OT] The statistics of hashing.
by flexvault (Monsignor) on Apr 01, 2012 at 13:52 UTC  
This is an observation sometime from 20052008. My handle on PM is because of working on this project at the time I joined, but I have little to due with the project now. It was a project to backup Terabytes of encrypted files to geographically distant offsite computer centers using 'rsync' in close to 'realtime'. The client's goal was that 'rsync' not work with the real files, but only with encrypted files which sort of negates the value of using 'rsync'. While we looked at several methods for getting a value for naming the encrypted files, we ran tests of several different sets of 1 million encrypted files and the following table is an average.
I can't say these results would be the same anywhere else, but when I started testing, I was sure that MD5 would be the most unique, and that's why we ran the tests several times with different file sets. I don't know if the encryption algorithm affected the results and don't know it the unencrypted files would give similiar results or not. I just didn't expect the results! I only bring this up because I think you started out with the same assumption about MD5, but maybe there is a better starting point and maybe not. Thank you "Well done is better than well said."  Benjamin Franklin  [reply] [d/l]  
by BrowserUk (Pope) on Apr 01, 2012 at 16:23 UTC  
I only bring this up because I think you started out with the same assumption about MD5, ... That is a very good point. The distribution of any (perfect(*)) hashing algorithm will only be fully utilised if the inputs can take on all possible values for any given input length. For example, ASCII or ISO text, certain parts of the input range  many of the control characters <32; and characters >126  will not appear in the inputs, so for any given length, the range of inputs to the hashing algorithm is biased. It stand to reason that the outputs will similarly biased and the full range of the hash values will not be produced. It is also the case that for any given input language, there are some character combinations that will never appear in normal text  eg. ZXZXZXZXZXZXZXZXZXZ  hence the inputs, and thus outputs will be further biased. For this particular part of the exercise, I'm more concerned with the collision detection mechanism than the vagaries of the hashing. But thank you for raising the issue. I may well contact you later for the details of your SimCRC64 algorithm. (*)Note: I am aware that MD5 is probably far from perfect. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply]  
by flexvault (Monsignor) on Apr 02, 2012 at 08:45 UTC  
BrowserUk,
As far as the 'SimCRC64' algorithm, it was just a simple CRC routine. I don't have the actual code, but it was something like this.
It was part of the test to see what the worst case would be. As I previously said, I didn't expect the results. Thank you "Well done is better than well said."  Benjamin Franklin  [reply] [d/l]  
by BrowserUk (Pope) on Apr 02, 2012 at 12:41 UTC  
by flexvault (Monsignor) on Apr 03, 2012 at 11:18 UTC  
Re: [OT] The statistics of hashing.
by roboticus (Chancellor) on Apr 02, 2012 at 13:57 UTC  
That problem sure was fun! I've got a handle on it, but I didn't do the math rigorously, but it agrees with my simulations. Anyway, here's what I think is going on: 1) The density of 1 bits is:
2) The probability of the next item colliding is:
If that's true, then I expect that the number of hits is just the integral of p(hit) over x in (0..records). (I haven't actually done the integration, but I don't foresee any hitches.) I found an online symbolic integrator that you can use to determine it. Anyway, if you want to play with it, here's the simulator code along with the sample output: Read more... (7 kB)
...roboticus When your only tool is a hammer, all problems look like your thumb.  [reply] [d/l] [select]  
by roboticus (Chancellor) on Apr 02, 2012 at 17:45 UTC  
I'm done with the program now. I added a column to show the predicted number of false positives. The code is nearly unchanged. Basically, I moved:
outside of the reporting section, and then accumulate it:
It's not very fast, so I only ran it a couple of times, but it seems like it gives reasonable agreement:
The updated code: Read more... (4 kB)
Both runs are still underway Update: Added rest of the tables. As you can see from the two sample runs, there's good agreement between the actual false positive count and the expected count. ...roboticus When your only tool is a hammer, all problems look like your thumb.  [reply] [d/l] [select]  
by roboticus (Chancellor) on Apr 02, 2012 at 22:50 UTC  
OK, I think I'm done with this diversion. I've built a program that will compute the expected number of collisions with no looping. Example output:
The code is straightforward: Read more... (2 kB)
Notes:
...roboticus When your only tool is a hammer, all problems look like your thumb.  [reply] [d/l] [select]  
by BrowserUk (Pope) on Apr 02, 2012 at 23:59 UTC  
Thank you roboticus. I ran your algorithm against the results of my 60hour run, and the correlation is simply stunning!
And that still includes the possibility  though I believe it to be remote  that there are 1 or more actual dups in there. My only regret now is that I wish I'd allowed the run to go to the 3/4 point instead of stopping it half way. The number of false positive would still have been easily manageable for the second pass:
That is an order of magnitude less that my own crude attempt was predicting, hence why I stopped the run. The only way I can repay you is to assure you that I will do my very best to try and understand the formula  which I do not currently. And of course, give you credit, when they world comes knocking at my door for my eminently patentable  at least if you take Apple as your guide  algorithm :) With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply] [d/l] [select]  
by tye (Sage) on Apr 03, 2012 at 04:47 UTC  
by BrowserUk (Pope) on Apr 03, 2012 at 15:59 UTC  
 
by BrowserUk (Pope) on Apr 03, 2012 at 12:39 UTC  
With the 'cancelling out' you've performed on the constants in your ex_*() subs, I couldn't see the pattern by which they were derived. Undoing that, I now see that they come (directly or not) from Pascal's Triangle. I guess at some point I should review some teaching materials to understand why those constants are used here, but for now it is enough to know that I can now write a generic ex_*() function (generator). Thanks. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply] [d/l]  
by roboticus (Chancellor) on Apr 03, 2012 at 13:32 UTC  
by BrowserUk (Pope) on Apr 03, 2012 at 15:01 UTC  
 
by roboticus (Chancellor) on Apr 02, 2012 at 14:07 UTC  
Oh, something I forgot to mention: I tried using a constant number of bits but varying the vector size/quantity to see how things scaled. In other words, I compared:
I found more smaller vectors works better until the number of samples matches the number if bits in the smaller vector. Plotting the functions:
using a graphing calculator shows that's where the curves cross: ...roboticus When your only tool is a hammer, all problems look like your thumb.  [reply] [d/l]  
by tye (Sage) on Apr 02, 2012 at 21:55 UTC  
I should review my transcendental function definitions as power series so I can recognize these correlations in future. I knew how to precisely measure the odds but had no clue how to search to see if there were any way to collapse it to some transcendental function (the exact measurement is quite impractical for very large numbers of inserts). But having 1exp($inserts/$slots) shown to me, I was able to play with that approximation to see how accurate it is. For this application, it is actually an impressive fit. If 1exp(1/$slots) starts out very close to 1/$slots (the correct starting value for the odds of a singlebit collision), then the accuracy never decreases as $inserts grows. For small $slots, 1exp(1/$slots) is not very accurate but 1exp($inserts/$slots) slowly becomes more and more accurate as $inserts increases. For $slots == 2**32, 1exp($inserts/$slots) starts out (at 1==$inserts) so accurate that a 'double' can't even measure the error. So you can consider the calculations based on it "perfect" rather than an approximation. :)  tye  [reply]  
Re: [OT] The statistics of hashing.
by sundialsvc4 (Abbot) on Apr 02, 2012 at 13:33 UTC  
What is the time interval that you have available to come up with a solution given “billions of” records? If the order of the records cannot be controlled, then a meaningful result can only be obtained by processing the entire dataset, which is not possible. Therefore, I truly see no alternative but that of performing multiple passes through the data, processing a mutually exclusive subset of the data each time. The results you describe in reply #2 seem to point to a reasonable compromisesolution: if 300K records produce 61 maybes, and if (say...) 3 million records at a time produce a few thousand, you have reduced the problem for that pass to a serviceable level. However, as the algorithm begins to become saturated, the inkblots start to run together and you’re left with a black piece of paper and not a picture. This will force you into a possibly large number of passes, that can be processed in parallel say on a cluster box. You say that “a disk based solution isn’t feasible,” but a fair amount of that is going to happen regardless. The informationcontent of any hash key, however derived, is considerably less than that of the record, and the mere fact that you haven’t yet seen what appears to be a duplicate of a particular record does not usefully predict that you won’t stumble into one with the very next record not yet seen. There will unfortunately be an extremely narrow “sweet spot” in which the discriminating power of the algorithm will be usefully preserved.  [reply]  
by BrowserUk (Pope) on Apr 02, 2012 at 15:02 UTC  
You say that “a disk based solution isn’t feasible,” Using my hashing/vectors algorithm, my script processes 10,000 strings/second. To process 2**32 strings (the absolute limit of the hashes) would require 2**32 / 10000 / 3600 = 119 hours. IE. 0.000001s/string. Using any sort of diskbased mechanism  diskbased hash, btree, etc.  would require at least 1 read and 1 write per string. Using the fastest (conventional) disks available that means adding at least 0.004 seconds for the read and 0.005 seconds for the write, to the processing time for every string. So, that's 0.000001 + 0.004 + 0.005 = 0.009001 * 2**32 / (3600) = 10738 hrs or 447 days. A little under 1 1/4 years. And the reality is that it will likely require at least 10 reads & 10 writes for every record. I'll leave that math to you. but a fair amount of that is going to happen regardless. When I stopped the process this morning, it had been running just under 62.5 hours. In that time it had processed 2,375,798,283 strings and detected just 52,286 possible dups. The cost of writing those 52,000 strings/positions to disk during the 62.5 hours of runtime is negligible; unmeasurable. Lost in the noise of processor variability due to variations in mains voltage. Ziltch. Running a second pass to verify those 52,000 possible dups as false or real will require less time as a simple hash lookup can be used, so there is no need to maintain the vectors. In any case, I'll trade 125 hours of runtime, for 1 1/4 years, every day of the week. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply]  
by sundialsvc4 (Abbot) on Apr 02, 2012 at 15:40 UTC  
Obviously, you assumed that I was just making “my usual suggestion” when I am not. Your algorithm is quite plainly at least the beginnings of the only feasible solution in this case ... especially given that the actual number of duplicates is known to be negligible. (0.0022% ...) If the data volumes are in the 2billion range and you are certain that you could only be committing a Type2 error with no possibility of Type1, then you have solved the problem right now. You produced a highly sensitive firstpass filter. 99.98% of the records have been excluded. That’s as solved as you can get ... and what you already know about the expected distribution (that is it is overwhelmingly likely that the records are unique) makes it so. In effect your algorithm is to calculate a digitaldigest of the entire record and then to look for possible collisions among those digests. The discriminating ability of the algorithm will be that of MD5 and nothing more or less. Taking the MD5 result and using the left/rightmost n bits would be statistically stronger than applying a separate reducing calculation to the entire result. MD5 and its brethren scatter entropy fairlyuniformly across all of the bits at once as they are designed to do. The distribution of values ought to be very flat, and any subset of the total bits in the vector, if not combined with others, will be equally flattened and therefore equally conducive to your hash.  [reply]  
by BrowserUk (Pope) on Apr 02, 2012 at 16:46 UTC 