0% found this document useful (0 votes)
6 views6 pages

Assignment 2 by Muhammad Zeeshan

This document contains a student's assignment submission containing 7 exercises involving the use of random number generation and probability distribution functions in R. The assignment includes generating random numbers from uniform and multinomial distributions, plotting density plots, calculating binomial and Poisson probabilities, and summing binomial probabilities.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Assignment 2 by Muhammad Zeeshan

This document contains a student's assignment submission containing 7 exercises involving the use of random number generation and probability distribution functions in R. The assignment includes generating random numbers from uniform and multinomial distributions, plotting density plots, calculating binomial and Poisson probabilities, and summing binomial probabilities.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name Muhammad Zeeshan Nazar

Class CS-8(B)
Roll No 160612
Instructor Sir Saad
Assignment 2
Exercise 1 
[Link]​(1) 
random_numbers <- ​runif​(100, min = .5, max = 6.5) 
(​round​(random_numbers)) 

## [1] 2 3 4 6 2 6 6 4 4 1 2 2 5 3 5 3 5 6 3 5 6 2 4 1 2 3 1 3 6 3 3 4 3 2 5 5 5 
## [38] 1 5 3 5 4 5 4 4 5 1 3 5 5 3 6 3 2 1 1 2 4 4 3 6 2 3 2 4 2 3 5 1 6 3 6 3 3 
## [75] 3 6 6 3 5 6 3 5 3 2 5 2 5 1 2 1 2 1 4 6 5 5 3 3 5 4 

Exercise 2 
[Link]​(1) 
(dice_result <- ​rmultinom​(n=30, size = 1, prob = ​rep​(1​/6
​ ,6))) #
​ rolling dice 

## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] 
## [1,] 0 0 1 0 0 0 0 0 0 0 0 0 0 0 
## [2,] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 
## [3,] 0 0 0 0 1 1 1 0 1 0 1 0 0 0 
## [4,] 1 0 0 0 0 0 0 0 0 0 0 1 0 1 
## [5,] 0 0 0 0 0 0 0 0 0 0 0 0 1 0 
## [6,] 0 0 0 1 0 0 0 1 0 1 0 0 0 0 
## [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] 
## [1,] 0 0 0 0 0 0 0 1 0 0 0 0 
## [2,] 1 0 0 0 0 1 0 0 0 0 0 0 
## [3,] 0 0 0 0 0 0 0 0 1 0 0 0 
## [4,] 0 0 1 0 1 0 1 0 0 0 0 1 
## [5,] 0 0 0 0 0 0 0 0 0 1 0 0 
## [6,] 0 1 0 1 0 0 0 0 0 0 1 0 
## [,27] [,28] [,29] [,30] 
## [1,] 0 0 0 0 
## [2,] 0 0 0 1 
## [3,] 0 0 0 0 
## [4,] 0 0 0 0 
## [5,] 1 1 1 0 
## [6,] 0 0 0 0 

sum​(dice_result[6,]​==​1) #
​ checking total 6's 

## [1] 6 
Exercise 3 
apply​(dice_result, 1, f​ unction​(x){ 
​sum​(x​==​1) 
}) 

## [1] 2 4 6 7 5 6 

Exercise 4 
[Link]​(1) 
heights <- r​ norm​(n = 700, mean = 1.70, sd = .1) 
plot​(d
​ ensity​(heights), main = 'Simulating Heights') 
 
 
[Link]​(1) 
heights2 <- ​rnorm​(n = 10000, mean = 1.70, sd = .1) 
lines​(d​ ensity​(heights2), col = 'red', lwd = 2) 

Exercise 5 

Part A 
x <- ​seq​(0,50,by = 1) 
y <- ​dbinom​(x,50,0.5) 
plot​(x,y) 
 
a <- ​seq​(0,50,by = 1) 
b <- ​pbinom​(x,50,0.5) 
plot​(a,b) 
 

Exercise 6 
ppois​(12, lambda = 15) ​#lower tail 

## [1] 0.267611 

ppois​(12, lambda = 15, lower = FALSE) #


​ upper tail 

## [1] 0.732389 

Exercise 7 
#lambda <- rgamma(n = 1,shape = 20, scale = .5) 
#[Link](1) 
#rpois(n = 1, lambda = lambda) 
#---------------------------------------- 
a <- ​dbinom​(14, size = 22, prob = 0.7) 
print​(a) 

## [1] 0.1422919 

b <- ​pbinom​(13, size = 22, prob = 0.7) 


print​(b) 

## [1] 0.1864574 
c <- ​sum​(d
​ binom​(11​:​22, size = 22, prob = 0.7)) 
print​(c) 

## [1] 0.9859649 

#or 
d <- 1​-pbinom​(10, size = 22, prob = 0.7) 
print​(d) 

## [1] 0.9859649 

You might also like