-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbd0.c
59 lines (56 loc) · 1.79 KB
/
bd0.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* AUTHOR
* Catherine Loader, [email protected].
* October 23, 2000.
*
* Merge in to R:
* Copyright (C) 2000, The R Core Team
*
* 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, a copy is available at
* http://www.r-project.org/Licenses/
*
*
* DESCRIPTION
* Evaluates the "deviance part"
* bd0(x,M) := M * D0(x/M) = M*[ x/M * log(x/M) + 1 - (x/M) ] =
* = x * log(x/M) + M - x
* where M = E[X] = n*p (or = lambda), for x, M > 0
*
* in a manner that should be stable (with small relative error)
* for all x and M=np. In particular for x/np close to 1, direct
* evaluation fails, and evaluation is based on the Taylor series
* of log((1+v)/(1-v)) with v = (x-np)/(x+np).
*/
#include "nmath.h"
double attribute_hidden bd0(double x, double np)
{
double ej, s, s1, v;
int j;
if(!R_FINITE(x) || !R_FINITE(np) || np == 0.0) ML_ERR_return_NAN;
if (fabs(x-np) < 0.1*(x+np)) {
v = (x-np)/(x+np);
s = (x-np)*v;/* s using v -- change by MM */
ej = 2*x*v;
v = v*v;
for (j=1; ; j++) { /* Taylor series */
ej *= v;
s1 = s+ej/((j<<1)+1);
if (s1==s) /* last term was effectively 0 */
return(s1);
s = s1;
}
}
/* else: | x - np | is not too small */
return(x*log(x/np)+np-x);
}