Advertisement
Guest User

Untitled

a guest
Jun 15th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. # Claas Heuer, 2015
  2. # Sampling from a truncated normal
  3. # by simply only accepting normal samples
  4. # in the boundaries given by 'a' and 'b'
  5.  
  6. tnorm <- function(x = 1, a = -Inf, b = Inf, mean=0, sd=1) {
  7.  
  8. y <- rep(0,x)
  9.  
  10. for(i in 1:x) {
  11.  
  12. repeat {
  13.  
  14. u <- rnorm(1,mean,sd)
  15.  
  16. if(u >= a & u <= b) break
  17.  
  18. }
  19.  
  20. y[i] <- u
  21.  
  22. }
  23.  
  24. return(y)
  25.  
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement