Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (1.)Inverse Transform Method:
- <br>$$F(y) = \int_1^y \frac{1}{x^2} dx = (-\frac{1}{x}) \left. \right|_{x=1}^{y} = 1 - \frac{1}{y}$$
- <br>$$u = F(x) = 1 - \frac{1}{x}$$
- <br>$$\frac{1}{x} = 1 - u $$
- <br>$$x = \frac{1}{1-u}$$
- ```{r}
- X = NULL
- for(i in 1:10000)
- {
- u = runif(1)
- x = 1/(1-u)
- X = c(X,x)
- }
- max(X)
- binsize = 2
- x = seq(1,50,.5)
- Y = 1/x^2
- hist(X, breaks=seq(0,max(X)+binsize, binsize), xlim=c(0,50), freq=FALSE)
- lines(x, Y, col='blue')
- ```
- (2a.)
- ```{r}
- ONE = NULL
- a = 1
- count = 0
- while (length(ONE) < 1000)
- {
- X = rnorm(1)
- count = count + 1
- if(X >= a)
- {
- ONE = c(ONE, X)
- }
- }
- round(count/1000)
- ```
- ```{r}
- TWO = NULL
- a = 2
- count = 0
- while (length(TWO) < 1000)
- {
- X = rnorm(1)
- count = count + 1
- if(X >= a)
- {
- TWO = c(TWO, X)
- }
- }
- round(count/1000)
- ```
- ```{r}
- THREE = NULL
- a = 3
- count = 0
- while (length(THREE) < 1000)
- {
- X = rnorm(1)
- count = count + 1
- if(X >= a)
- {
- ONE = c(THREE, X)
- }
- }
- round(count/1000)
- ```
- ```{r}
- FOUR = NULL
- a = 4
- count = 0
- while (length(FOUR) < 1000)
- {
- X = rnorm(1)
- count = count + 1
- if(X >= a)
- {
- ONE = c(FOUR, X)
- }
- }
- round(count/1000)
- ```
- (2b.)
- ```{r}
- a = 1
- ONE = NULL
- count = 0
- while(length(ONE) < 1000)
- {
- x = rexp(1,rate=a)
- X = x + a
- U = runif(1)
- count = count + 1
- if(U <= exp(-(X-a)^2/2))
- {
- ONE = c(ONE,X)
- }
- }
- round(count/1000, digits = 3)
- ```
- ```{r}
- a = 2
- ONE = NULL
- count = 0
- while(length(ONE) < 1000)
- {
- x = rexp(1,rate=a)
- X = x + a
- U = runif(1)
- count = count + 1
- if(U <= exp(-(X-a)^2/2))
- {
- ONE = c(ONE,X)
- }
- }
- round(count/1000, digits = 3)
- ```
- ```{r}
- a = 3
- ONE = NULL
- count = 0
- while(length(ONE) < 1000)
- {
- x = rexp(1,rate=a)
- X = x + a
- U = runif(1)
- count = count + 1
- if(U <= exp(-(X-a)^2/2))
- {
- ONE = c(ONE,X)
- }
- }
- round(count/1000, digits = 3)
- ```
- ```{r}
- a = 4
- ONE = NULL
- count = 0
- while(length(ONE) < 1000)
- {
- x = rexp(1,rate=a)
- X = x + a
- U = runif(1)
- count = count + 1
- if(U <= exp(-(X-a)^2/2))
- {
- ONE = c(ONE,X)
- }
- }
- round(count/1000, digits = 3)
- ```
- ```{r}
- a = 5
- ONE = NULL
- count = 0
- while(length(ONE) < 1000)
- {
- x = rexp(1,rate=a)
- X = x + a
- U = runif(1)
- count = count + 1
- if(U <= exp(-(X-a)^2/2))
- {
- ONE = c(ONE,X)
- }
- }
- round(count/1000, digits = 3)
- ```
- ```{r}
- a = 6
- ONE = NULL
- count = 0
- while(length(ONE) < 1000)
- {
- x = rexp(1,rate=a)
- X = x + a
- U = runif(1)
- count = count + 1
- if(U <= exp(-(X-a)^2/2))
- {
- ONE = c(ONE,X)
- }
- }
- round(count/1000, digits = 3)
- ```
- All of these values round to the values on the table on pg 25.
- (3a.)
- <br>$$f(x) = \frac{\Gamma(3+2)}{\Gamma(3)\Gamma(2)} x^2*(1-x)$$
- <br> $$f(x) = 12x^2(1-x)$$
- <br>$$\frac{d}{dx}x^2(1-x) = 0$$
- <br> $$2x(1-x)^2-x^2 = 0$$
- <br> $$2(1-x)-x = 0$$
- <br>$$x = \frac{2}{3}$$
- <br>$$y = 12*(\frac{2}{3})^2(1-\frac{2}{3}) = \frac{16}{9}$$
- ```{r}
- f = function(x)
- {
- 12*x^2*(1-x)
- }
- N = 1000
- n = 1
- y = 16/9
- X = NULL
- while(n <= N)
- {
- Y = runif(1)
- U = runif(1)
- if(U < f(Y)/y)
- {
- X[n] = Y
- n = n + 1
- }
- }
- hist(X,prob = TRUE, col = "green", main = "Beta Distribution")
- curve(dbeta(x,3,2),add=TRUE,col="blue")
- ```
- ```{r}
- qqplot(X, rbeta(1000,3,2))
- #This quantile-quantile plot is straight. Although, it does contain a few outliers on each end of the line. This means that the two distributions are relatively close/about the same.
- ```
- ```{r}
- runtime = function()
- {
- N = 1000
- n = 1
- y = 16/9
- X = NULL
- while(n <= N)
- {
- Y = runif(1)
- U = runif(1)
- if(U < f(Y)/y)
- {
- X[n] = Y
- n = n + 1
- }
- }
- }
- system.time(for(i in 1:1000) rbeta(500,3,2))
- system.time(for(i in 1:1000) runtime())
- ```
- For different choices of a and b, the results will be different. As these numbers increase, the maximum point of the curve will also increase. Therefore, a larger "box" will be needed in order to cover the entire crve. More values will be rejected and it will therefore, take longer to get values that are accepted, increasing the runtime.
- (4.)
- $$y = 1 - e^{-(\alpha x)^{\beta}}$$
- $$F^{-1}(y) = (-\frac{\ln(1-y)}{\alpha})^{\frac{1}{\beta}}$$
- ```{r}
- alpha = 1
- beta = 5
- X = NULL
- for(i in 1:1000)
- {
- U = runif(1)
- y = ((-log(U)/alpha)^(1/beta))
- X = c(X,y)
- }
- y = seq(0,1,.001)
- hist(X,prob = TRUE, col = "green", main = "Weibull Distribution")
- curve(dweibull(x,5,1),add=TRUE,col="blue")
- ```
- (5.)
- <br>$$F(x) = \int_0^x f(t)dt = \frac{1 -e^{-x}}{1-e{^{-0.05}}}$$
- <br>$$F^{-1}(x) = -\ln(1-(1-e^{-0.05})x)$$
- ```{r}
- X = NULL
- for(i in 1:1000){
- u = runif(1)
- x = -log(1-(1-exp(-0.05)) * u)
- X = c(X,x)
- }
- mean(X)
- ```
- #This value is very close to the true value of 0.02479.
- (6.)
- ```{r}
- count = 0
- for(i in 1:100)
- {
- claims = 0
- for(i in 1:1000)
- {
- u = runif(1)
- if(u <= 0.05)
- {
- y = rexp(1, rate = 1/800)
- claims = claims + y
- }
- }
- if (claims > 50000)
- {
- count = count + 1
- }
- }
- count / 100
- ```
- (7.)
- <br>$$f(x) = \frac{(x+1)e^{-x}}{2}$$
- <br>$$f^{'}(x) = \frac{-xe^{-x}}{2}$$
- <br>$$f^{'}(x) = 0, x = 0$$
- <br>$$f^{''}(x) = \frac{xe^{-x}}{2} - \frac{e^{-x}}{2}$$
- <br>$$f^{''}(0) = -\frac{1}{2}$$
- <br>$$g(x) = \frac{2}{\sqrt{2\pi}}e^{\frac{-x^{2}}{2}}$$
- <br>$$\frac{f(x)}{g(x)} = \frac{\frac{(x+1)e^{-x}}{2}}{\frac{2}{\sqrt{2\pi}}e^{\frac{-x^{2}}{2}}} = \frac{\sqrt{2\pi}}{4}(x+1)e^{\frac{x^{2}}{2}-x}$$
- <br>$$\frac{d(\frac{f(x)}{g(x)})}{dx} = \frac{\sqrt{2\pi}}{4}e^{(\frac{x^{2}}{2}-x)}x^{2}$$
- <br>$$\frac{d(\frac{f(x)}{g(x)})}{dx} = 0$$
- <br>$$z = \frac{f(0)}{g(0)} = \frac{\sqrt{2\pi}}{4}$$
- <br>$$\frac{f(x)}{zg(x)} = e^{(\frac{x^{2}}{2}-x)}$$
- ```{r}
- X = NULL
- for(i in 1:1000)
- {
- Y = rexp(1)
- z = exp(((Y^2)/2) - Y)
- U = runif(1)
- if(U <= z)
- {
- X = c(X,Y)
- }
- }
- hist(X)
- ```
- (8.)
- ```{r}
- #(a.)
- fans=c()
- for(i in 1:1000)
- {
- fans1 = 0
- n.bus = rpois(1, 5)
- for ( i in 1:n.bus){
- n.people = floor(runif(1,20,41))
- fans1 = fans1 + n.people
- }
- fans = c(fans, fans1)
- }
- hist(fans)
- #(b.)
- mean(fans)
- var(fans)
- #(c.)
- count = 0
- for (i in 1:1000)
- {
- if (fans[i] == 150)
- {
- count = count + 1
- }
- }
- count / 1000
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement