Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.47 KB | None | 0 0
  1. n <- 30
  2. s <- 1.8
  3. r <- 1/2.2
  4. G <- 0.5
  5. E <- s*(1/r)
  6. D <- G^2 + s*(1/r^2)
  7. x <- seq(-20, 20, 0.1)
  8. t <- c(rnorm(n, 0, 0.5))+c(rgamma(n, shape=s, rate=r))
  9. dfun3 <- function(x) {
  10.   dfun <- function(g,y) {
  11.   dnorm(g, 0, 0.5) * dgamma(y-g, shape=s, rate=r)}
  12.   b <- x;
  13.   for (i in 1:length(x))
  14.     b[i] <- integrate(dfun, lower = -Inf, upper = Inf, y = x[i])$value
  15.   return(b)
  16. }
  17. pfun3 <- function(x){
  18. for (i in 1:length(x)) {
  19.   b[i] <- integrate(dfun3, -Inf, x[i])$value
  20.   return(b)
  21.   }
  22. }
  23. plot(x, dfun3(x), type='l', main='Normal+Gamma pdf', xlab='', ylab='')
  24. lines(rep(E,2), c(0, dfun3(E)), col='red', lty=2)
  25. lines(rep((E-sqrt(D)),2), c(0, dfun3((E-sqrt(D)))), col='blue', lty=2)
  26. lines(rep((E+sqrt(D)),2), c(0, dfun3((E+sqrt(D)))), col='blue', lty=2)
  27. legend('topleft', legend=c(sprintf( 'E = %s', round(E, 2) ),sprintf( 'D = %s', round(D, 2) )), inset=0.03, box.lty=0, col=c('red', 'blue'), lty=2)
  28. plot(x, pfun3, type='l', main='Normal+Gamma cdf', xlab='', ylab='')
  29. lines(rep(E,2), c(0, pfun3(E)), col='red', lty=2)
  30. lines(rep((E-sqrt(D)),2), c(0, pfun3(E-sqrt(D))), col='blue', lty=2)
  31. lines(rep((E+sqrt(D)),2), c(0, pfun3(E+sqrt(D))), col='blue', lty=2)
  32. legend('topleft', legend=c(sprintf( 'E = %s', round(E, 2) ),sprintf( 'D = %s', round(D, 2) )), inset=0.03, box.lty=0, col=c('red', 'blue'), lty=2)
  33. hist(t, freq=FALSE, main='30 random values', xlab='', ylab='')
  34. lines(x, dfun3(x), col='red')
  35. plot(ecdf(t), main = 'Empirical cdf', xlab='', ylab='')
  36. lines(x, pfun3, col = 'red')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement