Skip to content
Snippets Groups Projects
Commit 79432e83 authored by stepan's avatar stepan
Browse files

Implement dbeta

parent 75791d68
No related branches found
No related tags found
No related merge requests found
/*
* This material is distributed under the GNU General Public License
* Version 2. You may review the terms of this license at
* http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright (c) 2000--2014, The R Core Team
* Copyright (c) 2016, Oracle and/or its affiliates
*
* All rights reserved.
*/
// Acknowledgement from GnuR header:
// Author: Catherine Loader, catherine@research.bell-labs.com, October 23, 2000.
package com.oracle.truffle.r.library.stats;
import static com.oracle.truffle.r.library.stats.LBeta.lbeta;
import com.oracle.truffle.r.library.stats.StatsFunctions.Function3_1;
public class DBeta implements Function3_1 {
@Override
public double evaluate(double x, double a, double b, boolean log) {
/* NaNs propagated correctly */
if (Double.isNaN(x) || Double.isNaN(a) || Double.isNaN(b)) {
return x + a + b;
}
if (a < 0 || b < 0) {
return RMath.mlError();
}
if (x < 0 || x > 1) {
return (DPQ.rd0(log));
}
// limit cases for (a,b), leading to point masses
if (a == 0 || b == 0 || !Double.isFinite(a) || !Double.isFinite(b)) {
if (a == 0 && b == 0) { // point mass 1/2 at each of {0,1} :
if (x == 0 || x == 1) {
return Double.POSITIVE_INFINITY;
} else {
return DPQ.rd0(log);
}
}
if (a == 0 || a / b == 0) { // point mass 1 at 0
if (x == 0) {
return Double.POSITIVE_INFINITY;
} else {
return DPQ.rd0(log);
}
}
if (b == 0 || b / a == 0) { // point mass 1 at 1
if (x == 1) {
return Double.POSITIVE_INFINITY;
} else {
return DPQ.rd0(log);
}
}
// else, remaining case: a = b = Inf : point mass 1 at 1/2
if (x == 0.5) {
return Double.POSITIVE_INFINITY;
} else {
return DPQ.rd0(log);
}
}
if (x == 0) {
if (a > 1) {
return DPQ.rd0(log);
}
if (a < 1) {
return Double.POSITIVE_INFINITY;
}
/* a == 1 : */
return DPQ.rdval(b, log);
}
if (x == 1) {
if (b > 1) {
return DPQ.rd0(log);
}
if (b < 1) {
return Double.POSITIVE_INFINITY;
}
/* b == 1 : */
return (DPQ.rdval(a, log));
}
double lval;
if (a <= 2 || b <= 2) {
lval = (a - 1) * Math.log(x) + (b - 1) * Math.log1p(-x) - lbeta(a, b);
} else {
lval = Math.log(a + b - 1) + Dbinom.dbinomRaw(a - 1, a + b - 2, x, 1 - x, true);
}
return DPQ.rdexp(lval, log);
}
}
......@@ -79,7 +79,8 @@ public final class DPQ {
return lowerTail ? 0.5 - p + 0.5 : p; /* 1 - p */
}
public static double dval(double x, boolean logP) {
// R_D_val
public static double rdval(double x, boolean logP) {
return logP ? Math.log(x) : x; /* x in pF(x,..) */
}
......
......@@ -45,6 +45,7 @@ import com.oracle.truffle.r.library.stats.Chisq;
import com.oracle.truffle.r.library.stats.CompleteCases;
import com.oracle.truffle.r.library.stats.CovcorNodeGen;
import com.oracle.truffle.r.library.stats.CutreeNodeGen;
import com.oracle.truffle.r.library.stats.DBeta;
import com.oracle.truffle.r.library.stats.DPois;
import com.oracle.truffle.r.library.stats.Dbinom;
import com.oracle.truffle.r.library.stats.DoubleCentreNodeGen;
......@@ -304,6 +305,8 @@ public class CallAndExternalFunctions {
return StatsFunctionsFactory.Function2_1NodeGen.create(new DGeom());
case "dpois":
return StatsFunctionsFactory.Function2_1NodeGen.create(new DPois());
case "dbeta":
return StatsFunctionsFactory.Function3_1NodeGen.create(new DBeta());
case "dt":
return StatsFunctionsFactory.Function2_1NodeGen.create(new Dt());
case "rmultinom":
......
......@@ -30,7 +30,7 @@ import com.oracle.truffle.r.test.TestBase;
* Common tests for functions implemented using {@code StatsFunctions} infrastructure.
*/
public class TestStatFunctions extends TestBase {
private static final String[] FUNCTION3_1_NAMES = {"dgamma"};
private static final String[] FUNCTION3_1_NAMES = {"dgamma", "dbeta"};
private static final String[] FUNCTION3_1_PARAMS = {
"10, 10, 10, log=TRUE",
"3, 3, 3, log=FALSE",
......
......@@ -73,6 +73,7 @@ com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RChisq.java,
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Exp.java,gnu_r_ihaka_core.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Geom.java,gnu_r_ihaka_core.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Dt.java,gnu_r.core.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DBeta.java,gnu_r.core.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/DPois.java,gnu_r.core.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/RNchisq.java,gnu_r.copyright
com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/stats/Wilcox.java,gnu_r.core.copyright
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment