Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. (1.)Inverse Transform Method:
  2. <br>$$F(y) = \int_1^y \frac{1}{x^2} dx = (-\frac{1}{x}) \left. \right|_{x=1}^{y} = 1 - \frac{1}{y}$$
  3. <br>$$u = F(x) = 1 - \frac{1}{x}$$
  4. <br>$$\frac{1}{x} = 1 - u $$
  5. <br>$$x = \frac{1}{1-u}$$
  6. ```{r}
  7. X = NULL
  8. for(i in 1:10000)
  9. {
  10. u = runif(1)
  11. x = 1/(1-u)
  12. X = c(X,x)
  13. }
  14. max(X)
  15.  
  16. binsize = 2
  17. x = seq(1,50,.5)
  18. Y = 1/x^2
  19. hist(X, breaks=seq(0,max(X)+binsize, binsize), xlim=c(0,50), freq=FALSE)
  20. lines(x, Y, col='blue')
  21. ```
  22.  
  23. (2a.)
  24. ```{r}
  25. ONE = NULL
  26. a = 1
  27. count = 0
  28.  
  29. while (length(ONE) < 1000)
  30. {
  31. X = rnorm(1)
  32. count = count + 1
  33. if(X >= a)
  34. {
  35. ONE = c(ONE, X)
  36. }
  37. }
  38. round(count/1000)
  39. ```
  40.  
  41. ```{r}
  42. TWO = NULL
  43. a = 2
  44. count = 0
  45. while (length(TWO) < 1000)
  46. {
  47. X = rnorm(1)
  48. count = count + 1
  49. if(X >= a)
  50. {
  51. TWO = c(TWO, X)
  52. }
  53. }
  54. round(count/1000)
  55. ```
  56.  
  57. ```{r}
  58. THREE = NULL
  59. a = 3
  60. count = 0
  61.  
  62. while (length(THREE) < 1000)
  63. {
  64. X = rnorm(1)
  65. count = count + 1
  66. if(X >= a)
  67. {
  68. ONE = c(THREE, X)
  69. }
  70. }
  71. round(count/1000)
  72. ```
  73.  
  74. ```{r}
  75. FOUR = NULL
  76. a = 4
  77. count = 0
  78.  
  79. while (length(FOUR) < 1000)
  80. {
  81. X = rnorm(1)
  82. count = count + 1
  83. if(X >= a)
  84. {
  85. ONE = c(FOUR, X)
  86. }
  87. }
  88. round(count/1000)
  89. ```
  90.  
  91. (2b.)
  92. ```{r}
  93. a = 1
  94. ONE = NULL
  95. count = 0
  96. while(length(ONE) < 1000)
  97. {
  98. x = rexp(1,rate=a)
  99. X = x + a
  100. U = runif(1)
  101. count = count + 1
  102. if(U <= exp(-(X-a)^2/2))
  103. {
  104. ONE = c(ONE,X)
  105. }
  106. }
  107. round(count/1000, digits = 3)
  108. ```
  109.  
  110. ```{r}
  111. a = 2
  112. ONE = NULL
  113. count = 0
  114. while(length(ONE) < 1000)
  115. {
  116. x = rexp(1,rate=a)
  117. X = x + a
  118. U = runif(1)
  119. count = count + 1
  120. if(U <= exp(-(X-a)^2/2))
  121. {
  122. ONE = c(ONE,X)
  123. }
  124. }
  125. round(count/1000, digits = 3)
  126. ```
  127.  
  128. ```{r}
  129. a = 3
  130. ONE = NULL
  131. count = 0
  132. while(length(ONE) < 1000)
  133. {
  134. x = rexp(1,rate=a)
  135. X = x + a
  136. U = runif(1)
  137. count = count + 1
  138. if(U <= exp(-(X-a)^2/2))
  139. {
  140. ONE = c(ONE,X)
  141. }
  142. }
  143. round(count/1000, digits = 3)
  144. ```
  145.  
  146. ```{r}
  147. a = 4
  148. ONE = NULL
  149. count = 0
  150. while(length(ONE) < 1000)
  151. {
  152. x = rexp(1,rate=a)
  153. X = x + a
  154. U = runif(1)
  155. count = count + 1
  156. if(U <= exp(-(X-a)^2/2))
  157. {
  158. ONE = c(ONE,X)
  159. }
  160. }
  161. round(count/1000, digits = 3)
  162. ```
  163.  
  164. ```{r}
  165. a = 5
  166. ONE = NULL
  167. count = 0
  168. while(length(ONE) < 1000)
  169. {
  170. x = rexp(1,rate=a)
  171. X = x + a
  172. U = runif(1)
  173. count = count + 1
  174. if(U <= exp(-(X-a)^2/2))
  175. {
  176. ONE = c(ONE,X)
  177. }
  178. }
  179. round(count/1000, digits = 3)
  180. ```
  181.  
  182. ```{r}
  183. a = 6
  184. ONE = NULL
  185. count = 0
  186. while(length(ONE) < 1000)
  187. {
  188. x = rexp(1,rate=a)
  189. X = x + a
  190. U = runif(1)
  191. count = count + 1
  192. if(U <= exp(-(X-a)^2/2))
  193. {
  194. ONE = c(ONE,X)
  195. }
  196. }
  197. round(count/1000, digits = 3)
  198. ```
  199. All of these values round to the values on the table on pg 25.
  200. (3a.)
  201. <br>$$f(x) = \frac{\Gamma(3+2)}{\Gamma(3)\Gamma(2)} x^2*(1-x)$$
  202. <br> $$f(x) = 12x^2(1-x)$$
  203. <br>$$\frac{d}{dx}x^2(1-x) = 0$$
  204. <br> $$2x(1-x)^2-x^2 = 0$$
  205. <br> $$2(1-x)-x = 0$$
  206. <br>$$x = \frac{2}{3}$$
  207. <br>$$y = 12*(\frac{2}{3})^2(1-\frac{2}{3}) = \frac{16}{9}$$
  208. ```{r}
  209. f = function(x)
  210. {
  211. 12*x^2*(1-x)
  212. }
  213. N = 1000
  214. n = 1
  215. y = 16/9
  216. X = NULL
  217. while(n <= N)
  218. {
  219. Y = runif(1)
  220. U = runif(1)
  221. if(U < f(Y)/y)
  222. {
  223. X[n] = Y
  224. n = n + 1
  225. }
  226. }
  227.  
  228. hist(X,prob = TRUE, col = "green", main = "Beta Distribution")
  229. curve(dbeta(x,3,2),add=TRUE,col="blue")
  230. ```
  231. ```{r}
  232. qqplot(X, rbeta(1000,3,2))
  233. #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.
  234. ```
  235. ```{r}
  236. runtime = function()
  237. {
  238. N = 1000
  239. n = 1
  240. y = 16/9
  241. X = NULL
  242. while(n <= N)
  243. {
  244. Y = runif(1)
  245. U = runif(1)
  246. if(U < f(Y)/y)
  247. {
  248. X[n] = Y
  249. n = n + 1
  250. }
  251. }
  252. }
  253.  
  254. system.time(for(i in 1:1000) rbeta(500,3,2))
  255. system.time(for(i in 1:1000) runtime())
  256. ```
  257. 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.
  258.  
  259. (4.)
  260. $$y = 1 - e^{-(\alpha x)^{\beta}}$$
  261. $$F^{-1}(y) = (-\frac{\ln(1-y)}{\alpha})^{\frac{1}{\beta}}$$
  262. ```{r}
  263. alpha = 1
  264. beta = 5
  265. X = NULL
  266. for(i in 1:1000)
  267. {
  268. U = runif(1)
  269. y = ((-log(U)/alpha)^(1/beta))
  270. X = c(X,y)
  271. }
  272. y = seq(0,1,.001)
  273. hist(X,prob = TRUE, col = "green", main = "Weibull Distribution")
  274. curve(dweibull(x,5,1),add=TRUE,col="blue")
  275. ```
  276. (5.)
  277. <br>$$F(x) = \int_0^x f(t)dt = \frac{1 -e^{-x}}{1-e{^{-0.05}}}$$
  278. <br>$$F^{-1}(x) = -\ln(1-(1-e^{-0.05})x)$$
  279. ```{r}
  280. X = NULL
  281. for(i in 1:1000){
  282. u = runif(1)
  283. x = -log(1-(1-exp(-0.05)) * u)
  284. X = c(X,x)
  285. }
  286. mean(X)
  287. ```
  288. #This value is very close to the true value of 0.02479.
  289. (6.)
  290. ```{r}
  291. count = 0
  292. for(i in 1:100)
  293. {
  294. claims = 0
  295. for(i in 1:1000)
  296. {
  297. u = runif(1)
  298. if(u <= 0.05)
  299. {
  300. y = rexp(1, rate = 1/800)
  301. claims = claims + y
  302. }
  303. }
  304. if (claims > 50000)
  305. {
  306. count = count + 1
  307. }
  308. }
  309. count / 100
  310. ```
  311.  
  312. (7.)
  313. <br>$$f(x) = \frac{(x+1)e^{-x}}{2}$$
  314. <br>$$f^{'}(x) = \frac{-xe^{-x}}{2}$$
  315. <br>$$f^{'}(x) = 0, x = 0$$
  316. <br>$$f^{''}(x) = \frac{xe^{-x}}{2} - \frac{e^{-x}}{2}$$
  317. <br>$$f^{''}(0) = -\frac{1}{2}$$
  318. <br>$$g(x) = \frac{2}{\sqrt{2\pi}}e^{\frac{-x^{2}}{2}}$$
  319. <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}$$
  320. <br>$$\frac{d(\frac{f(x)}{g(x)})}{dx} = \frac{\sqrt{2\pi}}{4}e^{(\frac{x^{2}}{2}-x)}x^{2}$$
  321. <br>$$\frac{d(\frac{f(x)}{g(x)})}{dx} = 0$$
  322. <br>$$z = \frac{f(0)}{g(0)} = \frac{\sqrt{2\pi}}{4}$$
  323. <br>$$\frac{f(x)}{zg(x)} = e^{(\frac{x^{2}}{2}-x)}$$
  324. ```{r}
  325. X = NULL
  326. for(i in 1:1000)
  327. {
  328. Y = rexp(1)
  329. z = exp(((Y^2)/2) - Y)
  330. U = runif(1)
  331. if(U <= z)
  332. {
  333. X = c(X,Y)
  334. }
  335. }
  336. hist(X)
  337. ```
  338.  
  339. (8.)
  340. ```{r}
  341. #(a.)
  342. fans=c()
  343. for(i in 1:1000)
  344. {
  345. fans1 = 0
  346. n.bus = rpois(1, 5)
  347. for ( i in 1:n.bus){
  348. n.people = floor(runif(1,20,41))
  349. fans1 = fans1 + n.people
  350. }
  351. fans = c(fans, fans1)
  352. }
  353.  
  354. hist(fans)
  355.  
  356. #(b.)
  357. mean(fans)
  358. var(fans)
  359.  
  360. #(c.)
  361. count = 0
  362. for (i in 1:1000)
  363. {
  364. if (fans[i] == 150)
  365. {
  366. count = count + 1
  367. }
  368. }
  369. count / 1000
  370. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement