Advertisement
sirdi

prob

Apr 9th, 2023 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 14.90 KB | None | 0 0
  1. AIM:
  2. Write the R programming code for computing the mean, median, mode, quartile deviation, variance,
  3. standard deviation, co-efficient of variation, first four moments about the mean and Pearson’s coefficients for the following frequency distribution
  4. KEY WORDS:
  5. Example:
  6. M = Mean
  7. MD = Median
  8. Mode = mode
  9. Cl = cumulative frequency
  10. H = height of the class
  11. F = frequency
  12. V = variance
  13. SD = standard deviation
  14. CV = coefficient of variation
  15. B1 & B2 = Pearson’s Coefficients
  16. M1,M2,M3,M4 = Moments about mean
  17. R-CODE:
  18. h=10
  19. x=seq(175,245,h)
  20. f=c(53,68,85,92,100,95,70,28)
  21. N=sum(f)
  22.  Reg. No: XXXXX
  23.  Name: YYYYY
  24. M=(sum(x*f))/sum(f)
  25. M
  26. cl=cumsum(f)
  27. cl
  28. ml=min(which(cl>=N/2))
  29. ml
  30. F=f[ml]
  31. F
  32. c=cl[ml-1]
  33. c
  34. l=midx[ml]-h/2
  35. MD=l+(((N/2)-c)/f)*h
  36. MD
  37. m=which(f==max(f))
  38. m
  39. fm=f[m]
  40. fm
  41. f1=f[m-1]
  42. f2=f[m+1]
  43. f1
  44. f2
  45. l=midx[m]-h/2
  46. l
  47. mode=l+((fm-f1)/(2*fm-f1-f2))*h
  48. mode
  49. var=((1/N)*(sum(x^2*f)))-((1/N)*(sum(x*f)))^2
  50. var
  51. SD=sqrt(var)
  52. SD
  53. CV=100*(SD/M)
  54. CV
  55.  Reg. No: XXXXX
  56.  Name: YYYYY
  57. M1=sum(((x-M)^1)*f)/N
  58. M2=sum(((x-M)^2)*f)/N
  59. M3=sum(((x-M)^3)*f)/N
  60. M4=sum(((x-M)^4)*f)/N
  61. B1=(M3^2)/(M2^3)
  62. B2=(M4)/(M2^2)
  63. B1
  64. B2
  65. G1=sqrt(B1)
  66. G2=B2-3
  67. G1
  68. G2
  69. ________________________________________________________________________________________________
  70. DIGITAL ASSIGNMENT – 3
  71. PROBABILITY AND STATISTICS LAB
  72. Course Code: BMAT202P
  73. NAME :-Muhammad Idris
  74. REG NO – 21BCE37776
  75. CLASS NUMBER: VL2022230503586
  76. LAB SLOT: L5 + L6
  77. AIM :- SOLVING THE GIVEN QUESTIONS USING Binomial, poisson and Normal distributions.
  78. 21BCE3695
  79. Code
  80. It is known that probability of an item produced by a certain machine will be defective is
  81. 0.05. If the produced items are sent to the market in packets of 20, then write down the
  82. R code to find the number of packets containing at least, exactly and at most 2 defective
  83. items in a consignment of 1000 packets.
  84. # Exp 3 - Binomial, poisson and Normal distributions
  85. # Binomial Distribution
  86. n = 20
  87. p = 0.05
  88. q = 1 - p
  89. N = 1000
  90. #1. Number of items containing at least 2 defective items in a consignment
  91. k = 2
  92. N1 = round(N*(1 - pbinom(k-1,n,p)))
  93. N1
  94. #2. Number of packets containing exactly 2 defective itens in a
  95. consignment k = 2
  96. N2 = round(N*dbinom(k,n,p))
  97. N2
  98. #3. Number of packets containing at most 2 defective items in a consignment
  99. of k = 2
  100. N3 = round(N*(pbinom(k,n,p)))
  101. N3
  102. 21BCE3695
  103. Output:-
  104. Conclusion
  105. Hence,
  106. The probability of packets containing at least 2 defective items is264
  107. The probability of packets containing exactly 2 defective items is189
  108. The probability of packets containing at most 2 defective items is925
  109. 21BCE3695
  110. A car hire firm has 2 cars which it hires out day by day. The number of demands for a
  111. car on each day follows a Poisson distribution with mean 1.5. Write down the R code to
  112. compute the proportion of days on which (i). neither car is used, (ii). at most one car is
  113. used and (iii). some demand of car is not fulfilled
  114. Code:-
  115. # Poissons Distribution
  116. lam = 1.5
  117. #1. Propotional of days on which neither car is used
  118. x = 0
  119. P1 = dpois(x,lam)
  120. P1
  121. #2. Propotional of days on which atmost one car is
  122. used x = 1
  123. P2 = ppois(x,lam)
  124. P2
  125. #3. Propotional of days on which some demands of car is not fullfilled
  126. x = 2
  127. P3 = 1 - ppois(x,lam)
  128. P3
  129. 21BCE3695
  130. Output:-
  131. Conclusion:-
  132. Hence from the analysis,
  133. The proportion of days on which neither car is used – 0.2231302 The
  134. proportion of days on which at most 1 car is used – 0.5578254
  135. The proportion of days on which some demand of car is not fullfilled – 0.1911532.
  136. 21BCE3695
  137. The local corporation authorities in a certain city install 10,000 electric lamps in the
  138. streets of the city with the assumption that the life of lamps is normally distributed. If
  139. these lamps have an average life of 1,000 burning hours with a standard deviation of 200
  140. hours, then write down the R code to calculate the number of lamps might be expected
  141. to fail in the first 800 burning hours and also the number of lamps might be expected to
  142. fail between 800 and 1,200 burning hours.
  143. Code:-
  144. # Normal distribution
  145. Mu = 1000
  146. SD = 200
  147. N = 10000
  148. #1. Number of lamps might be expected to fail in the first 800 burning
  149. hours x1 = 800
  150. N1 = round(N*pnorm(x1,Mu,SD))
  151. N1
  152. #2. Number of lamps might be expected to fail between 800 to 1200 burning hours
  153. x1 = 800
  154. x2 = 1200
  155. N2 = round(N*(pnorm(x2,Mu,SD)-pnorm(x1,Mu,SD)))
  156. N2
  157. 21BCE3695
  158. Output:-
  159. Conclusion:-
  160. Hence,
  161. The number of lamps might be expected to fail in the first 800 burning hours – 1587
  162. The number of lamps might be expected to fail in between 800 to 1200 burning hours – 6827
  163.  
  164.  
  165. NAME : Muhammad Idris
  166. REG.NO : 21BCE3776
  167. SUBJECT CODE : BMAT202P
  168. SUBJECT TITLE : Probability and Statistics LAB
  169. SLOT : L5+L6
  170. SEMESTER : Winter Semester 2022-2023 GUIDED
  171. BY : Dr. MANIMARAN A
  172. Question 1)
  173. Experience has shown that 20% of a manufactured product is of top quality. In one day’s
  174. production of 400 articles, only 50 are of top quality. Write down the R programming
  175. code to test whether the production of the day chosen is a representative sample at 95%
  176. confidence level.
  177. p_hat <- 50/400
  178. p_null <- 0.2
  179. n <- 400
  180. z_score <- (p_hat - p_null) / sqrt(p_null * (1 - p_null) / n)
  181. p_value <- 2 * pnorm(-abs(z_score))
  182. alpha <- 0.05
  183. if (p_value < alpha) {
  184.  cat("Reject the null hypothesis, the production of the day chosen is not a representative sample.")
  185. } else {
  186.  cat("Fail to reject the null hypothesis, the production of the day chosen is a representative sample.")
  187. }
  188. Output:
  189. Reject the null hypothesis, the production of the day chosen is not a representative sample.
  190. Question 2)
  191. Before an increase in excise duty on tea, 800 people out of a sample of 1000 were
  192. consumers of tea. After the increase in duty, 800 people were consumers of tea in a
  193. sample of 1200 persons. Write down the R programming code to test whether the
  194. significant decrease in the consumption of tea after the increase in duty at 1 % level of
  195. significance.
  196. Print (“H0:= p1 = p2”)
  197. Print(“H1:= p1 != p2”)
  198. Alpha = 0.01
  199. ZTab = gnorm(1-alpha)
  200. ZTab
  201. N1 = 1000
  202. N2 = 1200
  203. P1 = 800/1000
  204. P2 = 800/1200
  205. P = ((n1 * p1) + (n2 * p2))/(n1 + n2)
  206. Q = 1 – P
  207. ZCal = ((p1 – p2) / sqrt(P * Q * (1/n1) + (1/n2)))
  208. ZCal
  209. If (abs(ZCal) < abs(ZTab)) {
  210. Print (“H0 is accepted and H1 is rejected”)
  211. } else {
  212. Print (“H0 is accepted and H1 is rejected”)
  213. }
  214. Question 3)
  215. A sample of 900 items is found to have a mean of 3.47 cm. Write down the R
  216. programming code to test whether it can be reasonably regarded as a simple sample
  217. from a population with mean 3.23 cm and SD 2.31 cm at 99% level of confidence.
  218. Print (“H0:= x0 = Mu”)
  219. Print (“H1:= x0 !- Mu”)
  220. Alpha = 0.01
  221. ZTab = gnorm(1 – alpha)
  222. ZTab
  223. Mu = 3.23
  224. Sigma = 2.31
  225. N = 900
  226. X0 = 3.47
  227. ZCal = (x0 – Mu)/ ( Sigma/ sqrt(n))
  228. ZCal
  229. If (abs(ZCal) < abs(ZTab)) {
  230. Print (“H0 is accepted and H1 is rejected”)
  231. } else {
  232. Print (“H0 is rejected and H1 is accepted”)
  233. }
  234. Question 4)
  235. The average mark scored by 32 boys is 72 with a standard deviation of 8, while that for
  236. 36 girls is 70 with a standard deviation of 6. Write down the R programming code to
  237. test whether the boys are performing better than girls on the basis of average mark at
  238. 5 % level of significance.
  239.  
  240. Print (“H0 := x1 = x2”)
  241. Print (“H1 := x1 != x2”)
  242. Alpha = 0.05
  243. ZTab = gnorm(1 – alpha)
  244. ZTab
  245. N1 = 32
  246. N2 = 36
  247. X1 = 72
  248. X2 = 70
  249. s1 = 8
  250. S2 = 6
  251. ZCal = (x1-x2) / sqrt(((s1^2) / n1) + ((s2 ^ 2)/n2))
  252. ZCal
  253. If(abs(ZCal) < abs(ZTab)) {
  254. Print (“H0 is accepted and H1 is rejected”)
  255. } else {
  256. Print (“H0 is rejected and H1 is accepted”)
  257. }
  258.  
  259.  
  260.  
  261.  
  262. NAME : Muhammad Idris
  263. REG.NO : 21BCE3776
  264. SUBJECT CODE : MAT202P
  265. SUBJECT TITLE : PROBABILITY AND STATISTICS LAB
  266. LAB SLOT : L5+L6
  267. SEMESTER : Winter Semester 2022-2023
  268. GUIDED BY : Dr. MANIMARAN A
  269. AIM:
  270. Finding the test statistic in t test, paired test and F test
  271. KEY WORDS:
  272. Mean
  273. Var=Variance
  274. QN 1)
  275. A random sample of 10 boys with the following IQs: 70, 120, 110, 101, 88, 83,
  276. 95, 98, 107, and 100. Write down the R programming code to test whether
  277. the data support the assumption of a population mean IQ of 100 at 5 % level
  278. of significance
  279. R-CODE:
  280. print("H0 := X0=Mu")
  281. print("H1 := x0!=Mu")
  282. alpha=0.05
  283. Mu=100
  284. n=10
  285. x=c(70,120,110,101,88,83,95,98,107,100)
  286. x0=mean(x)
  287. s=sqrt(var(x))
  288. tTab=qt((1-alpha),(n-1))
  289. tTab
  290. tCal=(x0-Mu)/(s/sqrt(n-1))
  291. tCal
  292. if(abs(tCal)<abs(tTab)){
  293. print("H0 is accepted and H1 is rejected")
  294. } else {
  295. print("H0 is rejected and H1 is accepted")
  296. }
  297. t.test(x,mu=Mu)
  298. CONCLUSION:
  299. tTab=1.833113
  300. tCal=-0.5885024
  301. H0 is accepted and H1 is rejected
  302. OUPUT SIMULATION AND R-CODE:
  303. QN2)
  304. The mean height and the standard deviation height of 8 randomly chosen soldiers are
  305. 166.9 cm and 8.29 cm respectively. The corresponding values of 6 randomly chosen sailors
  306. are 170.3 cm and 8.50 cm respectively. Write down the R programming code to test
  307. whether the soldiers are shorter than the sailors on the basis of average height.
  308. R-CODE:
  309. print("H0 := x1=x2")
  310. print("H1 := x1!=x2")
  311. alpha=0.01
  312. n1=8
  313. n2=6
  314. x1=166.9
  315. x2=170.3
  316. s1=8.29
  317. s2=8.50
  318. Sigma=sqrt(((n1*s1^2)+(n2*s2^2))/(n1+n2-2))
  319. tTab=qt((1-alpha),(n1+n2-2))
  320. tTab
  321. tCal=(x1-x2)/(Sigma*sqrt((1/n1)+(1/n2)))
  322. tCal
  323. if(abs(tCal)<abs(tTab)){
  324. print("H0 is accepted and H1 is rejected")
  325. } else {
  326. print("H0 is rejected and H1 is accepted")
  327. }
  328. CONCLUSION:
  329. tTab=2.680998
  330. tCal=-0.6954801
  331. H0 is accepted and H1 is rejected
  332. RCODE SIMULATION AND OUTPUT:
  333. Q3)
  334. The following data relate to the marks obtained by 11 students in two sets, one held at the
  335. beginning of a year and the other at the end of the year after intensive coaching. Write down the
  336. R programming code to test whether the data indicate that the students have benefited by
  337. coaching at 5 % level of significance?
  338. Test I : 19 23 16 24 17 18 20 18 21 19 20
  339. Test II : 17 24 20 24 20 22 20 20 18 22 19
  340. RCODE:
  341. print("H0 := x1=x2")
  342. print("H1 := x1!=x2")
  343. alpha=0.05
  344. S1=c(19,23,16,24,17,18,20,18,21,19,20)
  345. S2=c(17,24,20,24,20,22,20,20,18,22,19)
  346. D=S1-S2
  347. d=mean(D)
  348. n=length(D)
  349. s=sqrt(var(D))
  350. tTab=qt((1-alpha),(n-1))
  351. tTab
  352. tCal=d/(s/sqrt(n-1))
  353. tCal
  354. if(abs(tCal)<abs(tTab)){
  355. print("H0 is accepted and H1 is rejected")
  356. } else {
  357. print("H0 is rejected and H1 is accepted")
  358. }
  359. CONCLUSION:
  360. tTab=1.812461
  361. tCal=-1.313064
  362. H0 is accepted and H1 is rejected
  363. RCODE AND SIMLUATION:
  364. Q.4) Two random samples drawn from two normal populations with the following
  365. observations.
  366. Sample I : 21 24 25 26 27
  367. Sample II : 22 27 28 30 31 36
  368. Write down the R programming code to test whether the two populations have the same
  369. variance at 5 % level of significance.
  370. RCODE:
  371. print ("HO := s1=s2")
  372. print ("H1 := s1!=s2")
  373. alpha=0.05
  374. s1=c(21, 24, 25, 26, 27)
  375. s2=c(22, 27, 28, 30, 31, 36)
  376. n1=length(s1)
  377. n2=length(s2)
  378. x1=mean(s1)
  379. x2=mean(s2)
  380. s1=sqrt(var(s1))
  381. s2=sqrt(var(s2) )
  382. Var1=(n1/(n1-1))*(s1^2)
  383. Var2=(n2/(n2-1))*(s2^2)
  384. FTab=qf((1-alpha),(n1-1),(n2-1))
  385. FTab
  386. if (Var1>Var2) {
  387. FCal=Var1/Var2
  388. } else {
  389. FCal=Var2/Var1
  390. }
  391. FCal
  392. if(abs(FCal)<abs(FTab)) {
  393. print ("HO Is accepted and H1 is rejected")
  394. } else {
  395. print ("HO Is rejected and H1 is accepted")
  396. }
  397. CONCLUSION:
  398. FTab=5.192168
  399. FCal=3.912453
  400. HO Is accepted and H1 is rejected
  401. OUTPUT AND RCODE SIMULATION:
  402.  
  403.  
  404. Sss
  405. NAME : Muhammad Idris
  406. REG.NO : 21BCE3776
  407. SUBJECT CODE : BMAT202P
  408. SUBJECT TITLE : Probability and Statistics Lab
  409. LAB SLOT : L5+L6
  410. SEMESTER : Winter Semester 2022-2023
  411. GUIDED BY : Dr. MANIMARAN A
  412. AIM:
  413. Finding Measures of Central Tendency, Dispersion, Skewness and Kurtosis for the
  414. given data.
  415. KEY WORDS:
  416. Example:
  417. n=Sample Size
  418. alpha= Level of Significance
  419. OF= Given values
  420. Tot= Total of the given values
  421. EV= Calculated Mean
  422. EF= Mean Frequency
  423. ChiTab = Tabular Value
  424. ChiCal = Calculated Value
  425. Dim = Dimensions
  426. . The following table gives the number of fatal road accidents that occurred during the 7
  427. days of a week. Write down the R programming code to test whether the accidents are
  428. uniformly distributed over the week at 95 % level of confidence.
  429. R-CODE:
  430. message(sprintf("80 := The accidents are uniformly distributed over the week"))
  431. message(sprintf("80 := The accidents are not uniformly distributed over the
  432. week"))
  433. alpha=0.05
  434. OF=c(8, 14, 16, 12, 11, 14, 9)
  435. Tot= sum(OF)
  436. n=length(OF)
  437. EV=Tot/n
  438. EF=rep(EV,n)
  439. ChiTab=qchisq((1-alpha),(n-1))
  440. ChiTab
  441. ChiCal=sum(((OF-EF)^2)/EF)
  442. ChiCal
  443. if(abs(ChiCal)<abs(ChiTab)){
  444. message(sprintf("H0 is accepted and H1 is rejected"))
  445. }else{
  446. message(sprintf("H0 is rejected and H1 is accepted"))
  447. }
  448. message(sprintf("H0 := There is no association between gender and attitude"))
  449. message(sprintf("H0 := There is an association between gender and attitude"))
  450. A total number of 3759 individuals were interviewed according to gender and decision in
  451. a public opinion survey on a political proposal with the results as in the following table.
  452. Write down the R programming code to test the hypothesis that there is no association
  453. between gender and attitude 5 % level of significance.
  454. Favoured Opposed Undecided
  455. Male 1154 475 243
  456. Female 1103 442 342
  457. alpha=0.05
  458. OF=matrix(c(1154, 475, 243, 1103, 442, 342),nrow=2,ncol=3)
  459. Dim=dim(OF)
  460. m=Dim[1]
  461. n=Dim[2]
  462. EF=matrix(nrow=m,ncol=n)
  463. for(i in 1:Dim[1])
  464. {
  465. for(j in 1:Dim[2])
  466. {
  467. EF[i,j]=round(sum(OF[i,])*sum(OF[,j])/sum(OF))
  468. }
  469. }
  470. ChiTab=qchisq((1-alpha),(m-1)*(n-1))
  471. message(sprintf("Tabulated Chi-Square Value = %0.4f",ChiTab))
  472. ChiCal=sum(((OF-EF)^2)/EF)
  473. message(sprintf("Calculated Chi-Square Value = %0.4f",ChiCal))
  474. if((abs(ChiCal)<abs(ChiTab))){
  475. message(sprintf("H0 is accepted and H1 is rejected"))
  476. message(sprintf("Attributes are independent"))
  477. } else{
  478. message(sprintf("H0 is rejected and H1 is accepted"))
  479. message(sprintf("Attributes are nit independent"))
  480. }
  481. A random sample is selected from each of 3 makes of ropes (Type 1, Type 2 and Type
  482. 3) and their breaking strength (in certain units) are measured with the results in the
  483. following table.
  484. Type 1 : 70 72 75 80 83
  485. Type 2 : 60 65 57 84 87 73
  486. Type 3 : 100 110 108 112 113 120 107
  487. Write down the R programming code to test whether the breaking strengths of the ropes
  488. differ significantly at 5 % level of significance.
  489. #CRD - ANOVA for 1 way classification
  490. D1=c(70, 72, 75, 90, 83)
  491. D2=c(60, 65, 57, 84, 87, 73)
  492. D3=c(100, 110, 108, 112, 113, 120, 107)
  493. Data=c(D1,D2,D3)
  494. Data
  495. Type=c(rep("Type1",length(D1)),rep("Type2",length(D2)),rep("Type3",length(D
  496. 3)))
  497. Type
  498. ANOVA1=aov(Data~Type)
  499. summary(ANOVA1)
  500. #RBD - ANOVA for 2 way classification
  501. Data=c(36, 36, 21, 35, 28, 29, 31, 32, 26, 28, 29, 29)
  502. Data
  503. Season=c(rep("Summer",4),rep("Winter",4),rep("Monsoon",4))
  504. Season
  505. Salesman=c(rep(c("SalesA","SalesB","SalesC","SalesD"),3))
  506. Salesman
  507. ANOVA2=aov(Data~(Season+Salesman))
  508. summary(ANOVA2)
  509. #LSD - ANOVA for 3 way classification
  510. Data=c(16, 17, 20, 16, 21, 15, 15, 12, 13)
  511. Data
  512. Day=c(rep("Day1",3),rep("Day2",3),rep("Day3",3))
  513. Day
  514. Engine=c(rep(c("Eng1","Eng2","Eng3"),3))
  515. Engine
  516. Burner=c("B1","B2","B3","B2","B3","B1","B3","B1","B2")
  517. Burner
  518. ANOVA3=aov(Data~(Day+Engine+Burner))
  519. summary(ANOVA3)
  520. CONCLUSION:
  521. 1. H0 is accepted H1 is rejected
  522. 2. H0 is rejected and H1 is accepted
  523. The attributes are not independent
  524. 3.
  525. I. CRD:
  526. II. RBD:
  527. III. LSD:
  528.  
  529.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement