You are on page 1of 13

WINTER SEMESTER 2022-2023

NAME: Chethan N V

REG.NO: 21BCE0427

Course Title: Probability and Statistics

Course Code: BMAT202P

Slot: L5+L6

EXPERIMENT NO : LAB ASSIGNMENT – 1

FACULTY NAME: Padigepati Naveen


Aim: To find the value of Y when X1=10 and X2=6 using least
square regression equation.
Code:
> #Name: Chethan N V
> #Reg No: 21BCE0427
>
> y=c(90,72,54,42,30,12,82,56,75,62,45,89)
> x1=c(3,5,6,8,12,14,15,23,17,12,18,23)
> x2=c(16,10,7,4,3,2,3,5,7,5,6,2)
> x12=x1^2
> x12
[1] 9 25 36 64 144 196 225 529 289 144 324 529
> x22=x2^2
> x22
[1] 256 100 49 16 9 4 9 25 49 25 36 4
> x1x2=x1*x2
> x1x2
[1] 48 50 42 32 36 28 45 115 119 60 108 46
> yx1=y*x1
> yx1
[1] 270 360 324 336 360 168 1230 1288 1275 744 810
2047
> yx2=y*x2
> yx2
[1] 1440 720 378 168 90 24 246 280 525 310 270
178
> sumy=sum(y)
> sumx1=sum(x1)
> sumx2=sum(x2)
> sumx12=sum(sumx1)
> sumx22=sum(sumx2)
> sumx1x2=sum(x1x2)
> sumyx1=sum(yx1)
> sumyx2=sum(yx2)
>
total=c(sumy,sumx1,sumx2,sumx12,sumx22,sumx1x2,sumyx1,sumyx2)
> dataframe=data.frame(y,x1,x2,x12,x22,x1x2,yx1,yx2)
> dataframe
y x1 x2 x12 x22 x1x2 yx1 yx2
1 90 3 16 9 256 48 270 1440
2 72 5 10 25 100 50 360 720
3 54 6 7 36 49 42 324 378
4 42 8 4 64 16 32 336 168
5 30 12 3 144 9 36 360 90
6 12 14 2 196 4 28 168 24
7 82 15 3 225 9 45 1230 246
8 56 23 5 529 25 115 1288 280
9 75 17 7 289 49 119 1275 525
10 62 12 5 144 25 60 744 310
11 45 18 6 324 36 108 810 270
12 89 23 2 529 4 46 2047 178
>
>
> #Substituting the required values in normal equations, we
get
> #sumy=nb0+b1sumx1+b2sumx2
> total
[1] 709 156 70 156 70 729 9212 4629
> #709=12b0+156b1+70b2
> #sumx1=b0sumx1+b1sumx12+b2sumx1x2
> #9212=156b0+2514b1+729b2
>
> #sumyx2= b0sum2+b1sum1x2
> #sumyx2= b0sumx2+b1sumx1x2
> #sumyx2= b0sumx2+b1sumx1x2+b2sum22
> #178= 70b0+729b1+582b2
> b0= array(c(120,156,70,156,2514,729,70,729,582),c(3,3))
> a=c(709,9212,178)
> solve(b0,a)
[1] 2.797312 5.495714 -6.914411
> #b0= 2.797312
> #b1=5.495714
> #b2=-6.914411
> #The regression equation with two independent variables is
given by
> #y= b0+b1x1+b2x2
> #given x1=10 and x2=6
> regy=(2.797312)+ 10*(5.495714) + 6*(-6.914411)
> regy
[1] 16.26799
> #The value of Y = 16.26799
Screenshot:
Aim: To show distribution in R programming

Code:
#Name: Chethan N V
> #Reg No: 21BCE0427
>
>
> p=1/3 #Proportion of population that consumes alcohol
> n=20 #Number of individuals investigated by each
investigator
> N=1000 #Total number of investigators
>
> # (a) How are individuals distributed?
> n_p=N*n #Total number of individuals in the population
> n_c=round(p*n_p) #Number of individuals who consume
alcohol
> n_c
[1] 6667
>
>
> # (b) Probability function for consumers
> p_c=dbinom(0:n, size=n, prob=p)
> p_c
[1] 3.007287e-04 3.007287e-03 1.428461e-02 4.285383e-02
9.106440e-02
[6] 1.457030e-01 1.821288e-01 1.821288e-01 1.479796e-01
9.865310e-02
[11] 5.425920e-02 2.466327e-02 9.248728e-03 2.845762e-03
7.114406e-04
[16] 1.422881e-04 2.223252e-05 2.615590e-06 2.179659e-07
1.147189e-08
[21] 2.867972e-10
>
>
> # Plot probability function
> plot(0:n, p_c, type="h", lwd=2, xlab="Number of consumers",
ylab="Probability", main="Probability Function for Consumers")
>
> # (c) Cumulative distribution function
> c_p_c=pbinom(0:n, size=n, prob=p)
> c_p_c
[1] 0.0003007287 0.0033080153 0.0175926266 0.0604464606
0.1515108579
[6] 0.2972138936 0.4793426882 0.6614714828 0.8094511284
0.9081042255
[11] 0.9623634289 0.9870267032 0.9962754310 0.9991211934
0.9998326340
[16] 0.9999749222 0.9999971547 0.9999997703 0.9999999882
0.9999999997
[21] 1.0000000000
>
>
> # Plot cumulative distribution function
> plot(0:n, c_p_c, type="s", lwd=2, xlab="Number of
consumers", ylab="Cumulative Probability", main="Cumulative
Distribution Function for Consumers")
>
>
> # (d) Expected number of investigators
> # (i) Three people were consumers
> expected_3_consumers <- N * pbinom(3, size=n, prob=p,
lower.tail=FALSE)
> e3c=N*pbinom(3, size=n, prob=p, lower.tail=FALSE)
> e3c
[1] 939.5535
>
>
> # (ii) Four people or less were consumers
> expected_4_or_less_consumers <- N * pbinom(4, size=n,
prob=p)
> e4c=N*pbinom(4, size=n, prob=p)
> e4c
[1] 151.5109
>
>
> # (iii) 6 to 8 people were consumers
> # (iii) 6 to 8 people were consumers
> e6to8=N * diff(pbinom(c(5,8), size=n, prob=p))
> e6to8
[1] 512.2372
>
>
> # (iv) At least 15 people were consumers
> e15ormore=N*pbinom(14, size=n, prob=p, lower.tail=FALSE)
> e15ormore
[1] 0.167366
>
>
>
Screenshots:
Aim: To Fit a binomial distribution

Code:
> #Name: Chethan N V

> #Reg No: 21BCE0427

>

> x=c(0,1,2,3,4,5,6)

> f=c(275,72,30,7,5,2,1)

> n=sum(f)

> p=sum(x*f)/(n*max(x))

> y=binom(x,n,p)

Error in binom(x, n, p) : could not find function "binom"

> y=dbinom(x,n,p)

> y

[1] 5.479327e-15 1.876802e-13 3.206052e-12 3.641826e-11 3.094668e-10

[6] 2.098365e-09 1.182622e-08

> plot(x,y)
Screenshots:

You might also like