[英]How to calculate gammaln in Spark MLLib?
I am trying to implement Log likelihood function of Buytillyoudie model in Spark MLLib. 我正在尝试在Spark MLLib中实现Buytillyoudie模型的对数似然函数。
L(r, α, a, b |X = x, tx, T) = A1 · A2 · (A3 + δx>0 A4)
where 哪里
A1 = Γ(r + x)αr/Γ(r)
A2 = Γ(a + b)Γ(b + x)/(Γ(b)Γ(a + b + x))
A3 = ( 1/(α + T))^(r+x)
A4 = (a/(b+x-1))(1/(α + tx))^(r+x)
For this, I am using L-BFGS algorithm as given here https://spark.apache.org/docs/2.2.0/mllib-optimization.html 为此,我使用此处给出的L-BFGS算法https://spark.apache.org/docs/2.2.0/mllib-optimization.html
I am implementing a custom Gradient class to be passed on to L-BFGS method LBFGS.runLBFGS
. 我正在实现自定义的Gradient类,该类将传递给L-BFGS方法LBFGS.runLBFGS
。
I don't know how to calculate Γ(x)
in Spark. 我不知道如何在Spark中计算Γ(x)
。 Is there any method or another optimization library which I can use to calculate gamma or gamma log? 我可以使用任何方法或其他优化库来计算伽玛或伽玛对数吗? Please suggest. 请提出建议。
PS: I got a cue from comment section to use Breeze library of Scala. PS:我从评论部分获得了使用Scala的Breeze库的提示。 But since I am using Java, it is not possible. 但是由于我正在使用Java,所以不可能。 Can I use ND4J or Apache-commons math library in Java? 我可以在Java中使用ND4J或Apache通用数学库吗?
Reads in a command line input x and prints Gamma(x) and log Gamma(x). 读入命令行输入x并打印Gamma(x)和对数Gamma(x)。 The Gamma function is defined by : Gamma(x) = integral( t^(x-1) e^(-t), t = 0 .. infinity) Gamma函数的定义为:Gamma(x)=积分(t ^(x-1)e ^(-t),t = 0 .. infinity)
public class Gamma {
static double logGamma(double x) {
double tmp = (x - 0.5) * Math.log(x + 4.5) - (x + 4.5);
double ser = 1.0 + 76.18009173 / (x + 0) - 86.50532033 / (x + 1)
+ 24.01409822 / (x + 2) - 1.231739516 / (x + 3)
+ 0.00120858003 / (x + 4) - 0.00000536382 / (x + 5);
return tmp + Math.log(ser * Math.sqrt(2 * Math.PI));
}
static double gamma(double x) { return Math.exp(logGamma(x)); }
public static void main(String[] args) {
double x = Double.parseDouble(args[0]);
StdOut.println("Gamma(" + x + ") = " + gamma(x));
StdOut.println("log Gamma(" + x + ") = " + logGamma(x));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.