-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogisticNormalFunctions.R
173 lines (129 loc) · 3.13 KB
/
logisticNormalFunctions.R
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#' Logistic normal integral
#'
#' @param z
#' @param t
#' @param Nmax
#'
#' @return Returns a real value
#' @export
#'
#' @examples
phiLogisticNormal <- function(z,t,Nmax=5){
phi <- 1/2*exp(-z/2+t/8)*gRec(z-t/2,t,Nmax)
return(phi)
}
#-------------------------------------------------------------------------
#' Computes g(z,t) using the theta2 Poisson series (27)
#'
#' @param Nmax, the truncation order of the series. Defaults to 5.
#' @export
#' @examples
#' gPoisson(0.0,1.0) should return 0.901925
gPoisson <- function(x,t,Nmax=5){
PI <- 4.0*atan(1.0)
q <- exp(-0.5*t)
q1 <- exp(-2.0*PI*PI/t)
x <- abs(x)
# S1 = theta2(i*z/2, exp(-t/2))
S1 <- exp(-1/2*x)*q^0.25
S2 <- 0.0
S3 <- 0.0
for (k in 1:Nmax){
fact <- q^((k+0.5)^2)*exp(-(k+0.5)*x) + q^((-k+0.5)^2)*exp((k-0.5)*x)
S1 <- S1 + fact
}
for (j in 0:2*Nmax-1){
k <- j-Nmax+1
fact <- q1^(k*k-0.25)
den <- 1.0 - q1^(2*k-1)
S2 <- S2 + (-1)^k*cos(2*PI/t*(k-0.5)*x)*fact/den
}
# S3 is called S4 in the paper
for (k in -Nmax:Nmax){
fact = q^(k*k+k)
den = 1.0 + q^(2*k)
S3 <- S3 + exp(k*x)*fact/den
}
y <- (4.0*PI/t*S2 + 2.0*S3)/(S1)
return(y)
}
#------------------------------------------------------------------------------------
#' Computes the g(z,t) function by recursion
#' Does not work when x is a vector
#'
#' The function g(z,t) is computed by recursion from its values in the primitive cell (-1/2 t,1/2 t)
#'
#' @param Nmax is the truncation order in the evaluation of the Appell sums. Defaults to 5
#' @export
#' @examples
#' gRec(0.0,1.0)
gRec <- function(x,t,Nmax=5) {
x <- abs(x)
k <- floor(x/t)
x0 <- x - k*t
if (x0 > (0.5*t)) {
x0 <- x0 - t
k <- k+1
}
g0 <- gPoisson(x0,t,Nmax)
z <- x0
g <- g0
if (k > 0 )
{
for (j in 1:k){
g <- 2*exp(-1/2*z-3/8*t)- exp(-z-1/2*t)*g
z <- z + t
}
}
return (g)
}
#-------------------------------------------------------------------------------
#' exact evaluation of phi(k*t,t) with integer k, using (20) in JCAM
phiLNexact <- function(t, k){
ks <- k
k <- abs(k)
# print (k)
if (k==0) sum = 1/2
else if (k==1) sum = 1/2*exp(-1/2*t)
else
{
sum <- 0
for (j in 1:(k-1)){
term <- (-1)^(j+1)*exp(j*(1/2*j-k)*t)
sum <- sum + term
# print(j)
}
lterm <- 1/2*(-1)^(k-1)*exp(-1/2*k^2*t)
sum <- sum + lterm
}
sumn <- 1 - sum
if (ks>=0) return (sum)
if (ks <0) return (sumn)
}
#-------------------------------------------------
#computes phi(z,t) by linear interpolation between closest integer multiples of t.
# err < 1/8*t^2*max|phi''(z,t)|
###################################
phiLinInterp <- function(x, t){
xs <- x
x <- abs(x)
k = floor(x/t)
print(k)
if (x-k*t==0) {
y <- phiLNexact(t,k)
err <- 0
}
else{
y1 <- phiLNexact(t,k)
y2 <- phiLNexact(t,k+1)
y3 <- phiLNexact(t,k+2)
x1 <- k*t
x2 <- (k+1)*t
y <- y1 + (y2-y1)/(x2-x1)*(x-x1)
phi2 <- (y1+y3-2*y2)/t^2
err <- 1/8*t^2*phi2
}
if (xs < 0) y <- 1- y
yerr <- c(y,err)
return (yerr)
}