File: ComparisonFFTandRtoDPQ.R

package info (click to toggle)
r-cran-distr 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,412 kB
  • sloc: ansic: 199; sh: 13; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,563 bytes parent folder | download | duplicates (2)
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
require(distr)
options("newDevice"=TRUE)
Pause <- function() { cat("Hit <enter> to continue..."); readline()}

#################################################
## Comparison 1 - Exact, FFT and RtoDPQ 
#################################################

N1 <- Norm(0,3)
N2 <- Norm(0,4)
rnew1 <- function(n) r(N1)(n) + r(N2)(n) 

#  calls exact formula -> N(0,5)
X <- N1 + N2 
# calls FFT
Y <- N1 + as(N2, "AbscontDistribution") 
# appoximation via RtoDPQ
Z <- new("AbscontDistribution", r = rnew1) 

# density-plot
x <- seq(-15,15,0.01)
plot(x, d(X)(x), type = "l", lwd = 3, xlab = "", ylab = "density",  
      main = "Comparison 1", col = "black")
lines(x, d(Y)(x), col = "yellow")
lines(x, d(Z)(x), col = "red")
legend("topleft",  legend = c("Exact", "FFT-Approximation", 
                                             "RtoDQP-Approximation"),
           fill = c("black", "yellow", "red"))
Pause()

#################################################
## Comparison 2 - "Exact" Formula and RtoDPQ
#################################################

B <- Binom(size = 6, prob = 0.5) * 10
N <- Norm()
rnew2 <- function(n) r(B)(n) + r(N)(n)

# calls "exact" fomula
Y <- B + N 
# appoximation via RtoDPQ
Z <- new("AbscontDistribution", r = rnew2) 

# density-plot
x  <- seq(-5,65,0.01)
plot(x, d(Y)(x), type = "l", xlab = "", ylab = "density",
      main = "Comparison 2", col = "black")
lines(x, d(Z)(x), col = "red")
legend("topleft", legend = c("Exact", "RtoDQP-Approximation"),
           fill = c("black", "red"))