Hi, I've written some C code for density evaluation of the non-central t distribution. It works with the R-1.7.1 source code if placed in the src/nmath directory and after appropriate changes are made to Makefiles, to the dt function in src/library/base/R/distn.R etc. I haven't read a lot of R source code so it may need some R-ification, but I've tried to use the dt.c file as a standard. Use and abuse. Claus ===> File dnt.c <== /* * AUTHOR * Claus Ekstr?m, ekstrom@dina.kvl.dk * July 15, 2003. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * * * NOTE * * Requires the following auxiliary routines: * * lgammafn(x) - log gamma function * pnt(x, df, ncp) - the distribution function for the non-central t distribution * * * DESCRIPTION * * The non-central t density is * * f(x, df, ncp) = df^(df/2)*exp(-.5*ncp^2)/(sqrt(pi)*gamma(df/2)*(df+x^2)^((df+1)/2))*sum_{k=0}^Infinity gamma((df + k + df)/2)*ncp^k/prod(1:k)*(2*x^2/(df+x^2))^(k/2) * * The functional relationship * * f(x, df, ncp) = df/x*(F(sqrt((df+2)/df)*x, df+2, ncp) - F(x, df, ncp)) * * is used to evaluate the density at x != 0 and * * f(0, df, ncp) = exp(-.5*ncp^2) / (sqrt(pi)*sqrt(df)*gamma(df/2))*gamma((df+1)/2) * * is used for x=0. * * All calculations are done on log-scale to increase stability. * */ #include "nmath.h" #include "dpq.h" double dnt(double x, double df, double ncp, int give_log) { double u; #ifdef IEEE_754 if (ISNAN(x) || ISNAN(df)) return x + df; #endif // If non-positive df then error if (df <= 0) ML_ERR_return_NAN; // If x is infinite then return 0 if(!R_FINITE(x)) return R_D__0; // If infinite df then the density is identical to a // normal distribution with mean = ncp if(!R_FINITE(df)) return dnorm(x, ncp, 1., give_log); // Consider two cases: x==0 or not // Do calculations on log scale to stabilize if (x!=0) { u = log(df)-log(fabs(x)) + log(fabs(pnt(x*sqrt((df+2)/df), df+2, ncp, 1, 0) - pnt(x, df, ncp, 1, 0))); } else { u = lgammafn((df+1)/2) - lgammafn(df/2) - .5*(log(M_PI) + log(df) + ncp*ncp); } return (give_log ? u : exp(u)); } -- ***************************************** Claus Thorn Ekstr?m <ekstrom@dina.kvl.dk> Dept of Mathematics and Physics, KVL Thorvaldsensvej 40 DK-1871 Frederiksberg C Denmark Phone:[+45] 3528 2341 Fax: [+45] 3528 2350
Martin Maechler
2003-Aug-19 19:00 UTC
[Rd] Density function for non-central t distribution
>>>>> "Claus" == Claus Ekstroem <claus@ekstroem.dk> >>>>> on Wed, 16 Jul 2003 09:35:50 +0200 writes:Claus> Hi, I've written some C code for density evaluation Claus> of the non-central t distribution. It works with the Claus> R-1.7.1 source code if placed in the src/nmath Claus> directory and after appropriate changes are made to Claus> Makefiles, to the dt function in Claus> src/library/base/R/distn.R etc. I haven't read a lot Claus> of R source code so it may need some R-ification, but Claus> I've tried to use the dt.c file as a standard. Claus> Use and abuse. Claus> Claus So, this is a donation to the R project, right? I'm about to incorporate it into R-devel which will become R 1.8.0 in October. Claus> <<<<< file dnt.c -- skipped here -- >>>>> That looks pretty good. We'll need some experiments to check if it's ok. I'll try some plausibility checks and then plan to compare pt(*, ncp= NN) with integrate("dt(*, ncp=NN)"). Thanks again for your contribution -- in the name of the R community! Claus> ***************************************** Claus> Claus Thorn Ekstr?m <ekstrom@dina.kvl.dk> Claus> Dept of Mathematics and Physics, KVL Claus> Thorvaldsensvej 40 Claus> DK-1871 Frederiksberg C Claus> Denmark Claus> Phone:[+45] 3528 2341 Claus> Fax: [+45] 3528 2350 Martin Maechler <maechler@stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <><
At 19:01 19-08-2003 +0200, Martin Maechler wrote:>So, this is a donation to the R project, right?Yep. That's exactly right.>We'll need some experiments to check if it's ok. >I'll try some plausibility checks and then plan to compare > pt(*, ncp= NN) with integrate("dt(*, ncp=NN)").Sure. Let me know if I can help in any way with constructing verification tests. Claus