从Python到Go的移植算法

时间:2014-06-01 12:32:42

标签: go

我正在尝试将此python代码移植到Go,但数学包中没有 beta()。我在哪里可以找到测试所需的测试版和其他功能?

from numpy import *
from scipy.stats import beta


class BetaBandit(object):
    def __init__(self, num_options=2, prior=(1.0,1.0)):
        self.trials = zeros(shape=(num_options,), dtype=int)
        self.successes = zeros(shape=(num_options,), dtype=int)
        self.num_options = num_options
        self.prior = prior

    def add_result(self, trial_id, success):
        self.trials[trial_id] = self.trials[trial_id] + 1
        if (success):
            self.successes[trial_id] = self.successes[trial_id] + 1

    def get_recommendation(self):
        sampled_theta = []
        for i in range(self.num_options):
            #Construct beta distribution for posterior
            dist = beta(self.prior[0]+self.successes[i],
                        self.prior[1]+self.trials[i]-self.successes[i])
            #Draw sample from beta distribution
            sampled_theta += [ dist.rvs() ]
        # Return the index of the sample with the largest value
        return sampled_theta.index( max(sampled_theta) )

1 个答案:

答案 0 :(得分:7)

如果您正在讨论numpy.random.beta,Beta分布是Dirichlet分布的一个特例,并且与Gamma分布有关,您可以检查项目gostat

它有beta.go source code来实现该功能。