beta.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package hyperloglog
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. var betaMap = map[uint8]func(float64) float64{
  7. 4: beta4,
  8. 5: beta5,
  9. 6: beta6,
  10. 7: beta7,
  11. 8: beta8,
  12. 9: beta9,
  13. 10: beta10,
  14. 11: beta11,
  15. 12: beta12,
  16. 13: beta13,
  17. 14: beta14,
  18. 15: beta15,
  19. 16: beta16,
  20. 17: beta17,
  21. 18: beta18,
  22. }
  23. func beta(p uint8, ez float64) float64 {
  24. f, ok := betaMap[p]
  25. if !ok {
  26. panic(fmt.Sprintf("invalid precision %d", p))
  27. }
  28. return f(ez)
  29. }
  30. /*
  31. p=4
  32. [-0.582581413904517,-1.935300357560050,11.07932375 8035073,-22.131357446444323,22.505391846630037,-12 .000723834917984,3.220579408194167,-0.342225302271 235]
  33. */
  34. func beta4(ez float64) float64 {
  35. zl := math.Log(ez + 1)
  36. return -0.582581413904517*ez +
  37. -1.935300357560050*zl +
  38. 11.079323758035073*math.Pow(zl, 2) +
  39. -22.131357446444323*math.Pow(zl, 3) +
  40. 22.505391846630037*math.Pow(zl, 4) +
  41. -12.000723834917984*math.Pow(zl, 5) +
  42. 3.220579408194167*math.Pow(zl, 6) +
  43. -0.342225302271235*math.Pow(zl, 7)
  44. }
  45. /*
  46. p=5
  47. [-0.7518999460733967,-0.9590030077748760,5.5997371 322141607,-8.2097636999765520,6.5091254894472037,- 2.6830293734323729,0.5612891113138221,-0.046333162 2196545]
  48. */
  49. func beta5(ez float64) float64 {
  50. zl := math.Log(ez + 1)
  51. return -0.7518999460733967*ez +
  52. -0.9590030077748760*zl +
  53. 5.5997371322141607*math.Pow(zl, 2) +
  54. -8.2097636999765520*math.Pow(zl, 3) +
  55. 6.5091254894472037*math.Pow(zl, 4) +
  56. -2.6830293734323729*math.Pow(zl, 5) +
  57. 0.5612891113138221*math.Pow(zl, 6) +
  58. -0.0463331622196545*math.Pow(zl, 7)
  59. }
  60. /*
  61. p=6
  62. [29.8257900969619634,-31.3287083337725925,-10.5942 523036582283,-11.5720125689099618,3.81887543739074 92,-2.4160130328530811,0.4542208940970826,-0.05751 55452020420]
  63. */
  64. func beta6(ez float64) float64 {
  65. zl := math.Log(ez + 1)
  66. return 29.8257900969619634*ez +
  67. -31.3287083337725925*zl +
  68. -10.5942523036582283*math.Pow(zl, 2) +
  69. -11.5720125689099618*math.Pow(zl, 3) +
  70. 3.8188754373907492*math.Pow(zl, 4) +
  71. -2.4160130328530811*math.Pow(zl, 5) +
  72. 0.4542208940970826*math.Pow(zl, 6) +
  73. -0.0575155452020420*math.Pow(zl, 7)
  74. }
  75. /*
  76. p=7
  77. [2.8102921290820060,-3.9780498518175995,1.31626800 41351582,-3.9252486335805901,2.0080835753946471,-0 .7527151937556955,0.1265569894242751,-0.0109946438726240]
  78. */
  79. func beta7(ez float64) float64 {
  80. zl := math.Log(ez + 1)
  81. return 2.8102921290820060*ez +
  82. -3.9780498518175995*zl +
  83. 1.3162680041351582*math.Pow(zl, 2) +
  84. -3.9252486335805901*math.Pow(zl, 3) +
  85. 2.0080835753946471*math.Pow(zl, 4) +
  86. -0.7527151937556955*math.Pow(zl, 5) +
  87. 0.1265569894242751*math.Pow(zl, 6) +
  88. -0.0109946438726240*math.Pow(zl, 7)
  89. }
  90. /*
  91. p=8
  92. [1.00633544887550519,-2.00580666405112407,1.643697 49366514117,-2.70560809940566172,1.392099802442225 98,-0.46470374272183190,0.07384282377269775,-0.00578554885254223]
  93. */
  94. func beta8(ez float64) float64 {
  95. zl := math.Log(ez + 1)
  96. return 1.00633544887550519*ez +
  97. -2.00580666405112407*zl +
  98. 1.64369749366514117*math.Pow(zl, 2) +
  99. -2.70560809940566172*math.Pow(zl, 3) +
  100. 1.39209980244222598*math.Pow(zl, 4) +
  101. -0.46470374272183190*math.Pow(zl, 5) +
  102. 0.07384282377269775*math.Pow(zl, 6) +
  103. -0.00578554885254223*math.Pow(zl, 7)
  104. }
  105. /*
  106. p=9
  107. [-0.09415657458167959,-0.78130975924550528,1.71514 946750712460,-1.73711250406516338,0.86441508489048 924,-0.23819027465047218,0.03343448400269076,-0.00 207858528178157]
  108. */
  109. func beta9(ez float64) float64 {
  110. zl := math.Log(ez + 1)
  111. return -0.09415657458167959*ez +
  112. -0.78130975924550528*zl +
  113. 1.71514946750712460*math.Pow(zl, 2) +
  114. -1.73711250406516338*math.Pow(zl, 3) +
  115. 0.86441508489048924*math.Pow(zl, 4) +
  116. -0.23819027465047218*math.Pow(zl, 5) +
  117. 0.03343448400269076*math.Pow(zl, 6) +
  118. -0.00207858528178157*math.Pow(zl, 7)
  119. }
  120. /*
  121. p=10
  122. [-0.25935400670790054,-0.52598301999805808,1.48933 034925876839,-1.29642714084993571,0.62284756217221615,-0.15672326770251041,0.02054415903878563,-0.00 112488483925502]
  123. */
  124. func beta10(ez float64) float64 {
  125. zl := math.Log(ez + 1)
  126. return -0.25935400670790054*ez +
  127. -0.52598301999805808*zl +
  128. 1.48933034925876839*math.Pow(zl, 2) +
  129. -1.29642714084993571*math.Pow(zl, 3) +
  130. 0.62284756217221615*math.Pow(zl, 4) +
  131. -0.15672326770251041*math.Pow(zl, 5) +
  132. 0.02054415903878563*math.Pow(zl, 6) +
  133. -0.00112488483925502*math.Pow(zl, 7)
  134. }
  135. /*
  136. p=11
  137. [-4.32325553856025e-01,-1.08450736399632e-01,6.091 56550741120e-01,-1.65687801845180e-02,-7.958293410 87617e-02,4.71830602102918e-02,-7.81372902346934e- 03,5.84268708489995e-04]
  138. */
  139. func beta11(ez float64) float64 {
  140. zl := math.Log(ez + 1)
  141. return -0.432325553856025*ez +
  142. -0.108450736399632*zl +
  143. 0.609156550741120*math.Pow(zl, 2) +
  144. -0.0165687801845180*math.Pow(zl, 3) +
  145. -0.0795829341087617*math.Pow(zl, 4) +
  146. 0.0471830602102918*math.Pow(zl, 5) +
  147. -0.00781372902346934*math.Pow(zl, 6) +
  148. 0.000584268708489995*math.Pow(zl, 7)
  149. }
  150. /*
  151. p=12
  152. [-3.84979202588598e-01,1.83162233114364e-01,1.3039 6688841854e-01,7.04838927629266e-02,-8.95893971464 453e-03,1.13010036741605e-02,-1.94285569591290e-03 ,2.25435774024964e-04]
  153. */
  154. func beta12(ez float64) float64 {
  155. zl := math.Log(ez + 1)
  156. return -0.384979202588598*ez +
  157. 0.183162233114364*zl +
  158. 0.130396688841854*math.Pow(zl, 2) +
  159. 0.0704838927629266*math.Pow(zl, 3) +
  160. -0.0089589397146453*math.Pow(zl, 4) +
  161. 0.0113010036741605*math.Pow(zl, 5) +
  162. -0.00194285569591290*math.Pow(zl, 6) +
  163. 0.000225435774024964*math.Pow(zl, 7)
  164. }
  165. /*
  166. p=13
  167. [-0.41655270946462997,-0.22146677040685156,0.38862 131236999947,0.45340979746062371,-0.36264738324476 375,0.12304650053558529,-0.01701540384555510,0.001 02750367080838]
  168. */
  169. func beta13(ez float64) float64 {
  170. zl := math.Log(ez + 1)
  171. return -0.41655270946462997*ez +
  172. -0.22146677040685156*zl +
  173. 0.38862131236999947*math.Pow(zl, 2) +
  174. 0.45340979746062371*math.Pow(zl, 3) +
  175. -0.36264738324476375*math.Pow(zl, 4) +
  176. 0.12304650053558529*math.Pow(zl, 5) +
  177. -0.01701540384555510*math.Pow(zl, 6) +
  178. 0.00102750367080838*math.Pow(zl, 7)
  179. }
  180. /*
  181. p=14
  182. [-3.71009760230692e-01,9.78811941207509e-03,1.8579 6293324165e-01,2.03015527328432e-01,-1.16710521803 686e-01,4.31106699492820e-02,-5.99583540511831e-03 ,4.49704299509437e-04]
  183. */
  184. func beta14(ez float64) float64 {
  185. zl := math.Log(ez + 1)
  186. return -0.371009760230692*ez +
  187. 0.00978811941207509*zl +
  188. 0.185796293324165*math.Pow(zl, 2) +
  189. 0.203015527328432*math.Pow(zl, 3) +
  190. -0.116710521803686*math.Pow(zl, 4) +
  191. 0.0431106699492820*math.Pow(zl, 5) +
  192. -0.00599583540511831*math.Pow(zl, 6) +
  193. 0.000449704299509437*math.Pow(zl, 7)
  194. }
  195. /*
  196. p=15
  197. [-0.38215145543875273,-0.89069400536090837,0.37602 335774678869,0.99335977440682377,-0.65577441638318 956,0.18332342129703610,-0.02241529633062872,0.001 21399789330194]
  198. */
  199. func beta15(ez float64) float64 {
  200. zl := math.Log(ez + 1)
  201. return -0.38215145543875273*ez +
  202. -0.89069400536090837*zl +
  203. 0.37602335774678869*math.Pow(zl, 2) +
  204. 0.99335977440682377*math.Pow(zl, 3) +
  205. -0.65577441638318956*math.Pow(zl, 4) +
  206. 0.18332342129703610*math.Pow(zl, 5) +
  207. -0.02241529633062872*math.Pow(zl, 6) +
  208. 0.00121399789330194*math.Pow(zl, 7)
  209. }
  210. /*
  211. p=16
  212. [-0.37331876643753059,-1.41704077448122989,0.407291 84796612533,1.56152033906584164,-0.99242233534286128,0.26064681399483092,-0.03053811369682807,0.00155770210179105]
  213. */
  214. func beta16(ez float64) float64 {
  215. zl := math.Log(ez + 1)
  216. return -0.37331876643753059*ez +
  217. -1.41704077448122989*zl +
  218. 0.40729184796612533*math.Pow(zl, 2) +
  219. 1.56152033906584164*math.Pow(zl, 3) +
  220. -0.99242233534286128*math.Pow(zl, 4) +
  221. 0.26064681399483092*math.Pow(zl, 5) +
  222. -0.03053811369682807*math.Pow(zl, 6) +
  223. 0.00155770210179105*math.Pow(zl, 7)
  224. }
  225. /*
  226. p=17
  227. [-0.36775502299404605,0.53831422351377967,0.769702 89278767923,0.55002583586450560,-0.745755882611469 41,0.25711835785821952,-0.03437902606864149,0.0018 5949146371616]
  228. */
  229. func beta17(ez float64) float64 {
  230. zl := math.Log(ez + 1)
  231. return -0.36775502299404605*ez +
  232. 0.53831422351377967*zl +
  233. 0.76970289278767923*math.Pow(zl, 2) +
  234. 0.55002583586450560*math.Pow(zl, 3) +
  235. -0.74575588261146941*math.Pow(zl, 4) +
  236. 0.25711835785821952*math.Pow(zl, 5) +
  237. -0.03437902606864149*math.Pow(zl, 6) +
  238. 0.00185949146371616*math.Pow(zl, 7)
  239. }
  240. /*
  241. p=18
  242. [-0.36479623325960542,0.99730412328635032,1.553543 86230081221,1.25932677198028919,-1.533259482091101 63,0.47801042200056593,-0.05951025172951174,0.0029 1076804642205]
  243. */
  244. func beta18(ez float64) float64 {
  245. zl := math.Log(ez + 1)
  246. return -0.36479623325960542*ez +
  247. 0.99730412328635032*zl +
  248. 1.55354386230081221*math.Pow(zl, 2) +
  249. 1.25932677198028919*math.Pow(zl, 3) +
  250. -1.53325948209110163*math.Pow(zl, 4) +
  251. 0.47801042200056593*math.Pow(zl, 5) +
  252. -0.05951025172951174*math.Pow(zl, 6) +
  253. 0.00291076804642205*math.Pow(zl, 7)
  254. }