core icon indicating copy to clipboard operation
core copied to clipboard

Migrate bayes.js/jStat.js/beta.js implementations to Java

Open victoralfaro-dotcms opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe.

We need to implement our own version of Bayesian for A/B Testing feature. To find one that suit our needs doesn't work very well and therefore we need to create a solution of our own.

Describe the solution you'd like

We have found a descent Javascript implementation which we believe we can base on and migrate to Java code using the Apache commons Math library. Although we need to come up with some modifications specifically with the Beta distribution since it has its own that works better for our purposes than the Apache's. The Github project where this can be found is https://github.com/ssaw/BayesianTestJS.

There are 3 main files, one as source: https://github.com/ssaw/BayesianTestJS/blob/master/src/scripts/bayes.js and 2 libraries: https://github.com/ssaw/BayesianTestJS/blob/master/src/scripts/libs/jstat.js and https://github.com/ssaw/BayesianTestJS/blob/master/node_modules/beta-js/beta.js.

The way of how both solutions structure its functions is similar so it will be likely that we can replace/override classes like the BetaDistribution Java class with what is found in BetaModel Javascript object. This is the function mapping:

BetaDistribution.prototype.lp  = x -> BetaDistribution.logDensity(x);
BetaDistribution.prototype.pdf = x -> BetaDistribution.density(x)
BetaDistribution.prototype.rv = BetaDistribution.sample()
BetaDistribution.prototype.rvs(n) = Stream.generate(() -> betaDistribution.sample()).limit(n).collect(Collectors.toList());

Another method that we need to migrate is the jStat.quantiles() or reuse this one found in Apache's Commons Math (https://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/org/apache/commons/math3/stat/descriptive/rank/Percentile.html). Probably will need some tweaks since it's not an exact match.

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

Additional context

Bayesian formulas: https://www.evanmiller.org/bayesian-ab-testing.html Bayesian in A/B Testing: https://towardsdatascience.com/bayesian-a-b-testing-with-python-the-easy-guide-d638f89e0b8a Bayesian test calculator: https://making.lyst.com/bayesian-calculator/

To test the results our Java version we can calculate the probability that B beats A from:

𝛼𝐴 is one plus the number of successes for A
𝛽𝐴 is one plus the number of failures for A
𝛼𝐵 is one plus the number of successes for B
𝛽𝐵 is one plus the number of failures for B

function probability_B_beats_A(α_A, β_A, α_B, β_B)
    total = 0.0
    for i = 0:(α_B-1)
        total += exp(logbeta(α_A+i, β_B+β_A)
            - log(β_B+i) - logbeta(1+i, β_B) - logbeta(α_A, β_A))
    end
    return total
end

victoralfaro-dotcms avatar Sep 07 '22 03:09 victoralfaro-dotcms

Hey team! Please add your planning poker estimate with Zenhub @oidacra @freddyucv @dsilvam

victoralfaro-dotcms avatar Sep 07 '22 17:09 victoralfaro-dotcms