-
Notifications
You must be signed in to change notification settings - Fork 0
/
hwk2.4.r
51 lines (44 loc) · 901 Bytes
/
hwk2.4.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
##Q 4##
#Inputs: P is the cdf vector, A is numbers from 1:10^6, N is the desired number of A's
A <- seq(1:10^6)
CDF <- pgeom(A,10^-5)
N <- 10
discrete <- function(A,CDF,N) {
M <- length(A)
U <- runif(N)
X <- numeric(N)
P <- numeric(M)
for(i in 1:M){
if (i == 1) {
P[i] = CDF[1]
W <- which(U<= P[i])
} else {
P[i]<-CDF[i]
W <- which(P[i-1]<U & U<= P[i])
}
X[W]=A[i]
}
return(X)
}
discrete(A,CDF,N)
#a = a list of numbers
#p = the probabilities of drawing each number; these sum to 1
a <- 1:10^6
p <- 1e-5 *(.99999)^(a-1)
rvsampler <- function(a,p,n) {
x <- runif(1)
y <- numeric(n)
P <- numeric(length(a))
for(i in 1:length(a)) {
if(i==1) {
P[i] = p[1]
w <- which(x <= P[i])
} else {
p[i] <- sum(p[1:i])
w <- which(P[i-1]<x & x<=P[i])
}
y[w] <- a[i]
}
return(y)
}
rvsampler(a,p,1)