You are on page 1of 42

SYIT - COST

AIM: CHI-SQUARED TEST

A. FIND CRITICAL VALUE

PROBLEM STATEMENT:
Find the 95th percentile of the Chi-Squared distribution with 7 degrees of freedom.

CODE:
> qchisq(.95,df=7)

OUTPUT:
[1] 14.06714

B. CHI-SQUARE TEST OF INDEPENDENCE

PROBLEM STATEMENT 1:
In the built-in data set survey, the Smoke column records the students smoking habit, while the
Exer column records their exercise level. The allowed values in Smoke are “Heavy”, “Regul”,
“Occas” and “Never”. As for Exer, they are “Freq”, “Some” and “None”. Test the hypothesis
whether the students smoking habit is independent of their exercise level at 0.05 significance
level.

CODE:
> library(MASS)
> survey

> tbl=table(survey$Smoke,survey$Exer)
> tbl

> chisq.test(tbl)

> ctbl=cbind(tbl[,"Freq"],tbl[,"None"]+tbl[,"Some"])
> ctbl

> chisq.test(ctbl)

> qchisq(.95,df=3)

> chical=chisq.test(ctbl)
> chical$statistic

> H0="Smoking habit doesn't depend on exercise level"


> H1="Smoking habit depends on exercise level"
> chitab=qchisq(.95,df=3)
> if(chical$statistic<chitab)H0 else H1
OUTPUT:

       Sex Wr.Hnd NW.Hnd W.Hnd    Fold Pulse Clap Exer Smoke Height      M.I
1   Female   18.5 18.0 Right  R on L 92 Left Some Never 173.00   Metric
2     Male 19.5   20.5 Left R on L   104 Left None Regul 177.80 Imperial
3     Male 18.0   13.3 Right L on R    87 Neither None Occas     NA <NA>

Freq None Some


  Heavy    7 1 3
  Never   87 18   84
  Occas   12 3   4
  Regul    9 1 7

        Pearson's Chi-squared test

data:  tbl
X-squared = 5.4885, df = 6, p-value = 0.4828

      [,1] [,2]
Heavy    7 4
Never   87 102
Occas   12 7
Regul    9 8

        Pearson's Chi-squared test

data:  ctbl
X-squared = 3.2328, df = 3, p-value = 0.3571

[1] 7.814728

X-squared 
 3.232818 

[1] "Smoking habit doesn't depend on exercise level"

PROBLEM STATEMENT 2:
Effectiveness of a Drug Treatment
To test the effectiveness of a drug for a certain medical condition, we will consider a
hypothetical case.

Suppose we have 105 patients under study and 50 of them were treated with the drug. Moreover,
the remaining 55 patients were kept under control samples. Thus, the health condition of all
patients was checked after a week.
With the following table, we can asses if their condition has improved or not. By observing this
table, can one tell if the drug had positive effect on the patients?
Here in this example, we can see that 35 out of the 50 patients showed improvement. Suppose if
the drug had no effect, the 50 will split the same proportion of patients who were not given the
treatment. Here, in this case, improvement of the controlled case is high as about 70% of patients
showed improvement, since both categorical variables which we have already defined must have
only two levels. Also, it was sort of perceptive today that the drug treatment and health condition
are dependent.

CODE:
> setwd("C:/Ankita/cost")
> getwd()

> data_frame <- read.csv("treatement.csv")


> table(data_frame$treatement,data_frame$improvement)

> chical=chisq.test(data_frame$treatement,data_frame$improvement,correct=FALSE)
> H0="drug had a positive effect on patient"
> H1="drug did not have a positive effect on patient"
> chitab=qchisq(0.95,df=1)
> chitab

> if(chical$statistic<chitab)H0 else H1

OUTPUT:
[1] "C:/Ankita/cost"

       improved not-improved


not-treated    26 29
treated    35 15

[1] 3.841459

[1] "drug did not have a positive effect on patient"

C. CHI-SQUARE GOODNESS OF FIT TEST

PROBLEM STATEMENT 1:
Suppose the campus smoking statistics is as below. Determine whether the sample data in survey
supports it at 0.05 significance level.
Heavy Never Occas Regul
4.5% 79.5% 8.5% 7.5%

CODE:
> library(MASS)
> levels(survey$Smoke)

> smoke.freq=table(survey$Smoke)
> smoke.freq

> smoke.prob=c(.045,.795,.085,.075)
> smoke.prob

> chisq.test(smoke.freq,smoke.prob)

> H0='sample data supports the campus smoking stats'


> H1="sample data doesn't support the campus smoking stats"
> qchisq(.95,df=3)

> chitab=qchisq(.95,df=3)
> if(chical$statistic<chitab)H0 else H1

OUTPUT:
[1] "Heavy" "Never" "Occas" "Regul"

Heavy Never Occas Regul 


   11   189   19 17 

[1] 0.045 0.795 0.085 0.075

        Pearson's Chi-squared test

data:  smoke.freq and smoke.prob


X-squared = 12, df = 9, p-value = 0.2133

[1] 7.814728

[1] "sample data supports the campus smoking stats"

PROBLEM STATEMENT 2:
We collected wild tulips and found that 81 were Red, 50 were Yellow and 27 were White.

Question 1: Are these colors equally common?


CODE:
> tulip<-c(81,50,27)
> res<-chisq.test(tulip,p=c(1/3,1/3,1/3))
> res

> res$expected

> H0="they are equally common"


> H1="they are not equally common"
> qchisq(.95,df=2)

> chitab=qchisq(.95,df=2)
> if(res$statistic<chitab)H0 else H1

OUTPUT:
Chi-squared test for given probabilities

data:  tulip
X-squared = 27.886, df = 2, p-value = 8.803e-07

[1] 52.66667 52.66667 52.66667

[1] 5.991465

[1] "they are not equally common"

Question 1: Suppose that in the region, where you collected the data, the ratio of Red, Yellow
and White tulip is 3:2:1 (3+2+1=6). This means that the expected proportion are:
 3/6 (=1/2) for Red
 2/6 (=1/3) for Yellow
 1/6 for White

CODE:
> tulips <- c(81,50,27)
> res<-chisq.test(tulip,p=c(1/2,1/3,1/6))
> res

> res$expected

> H0="the colors are in proportion 2:3:6"


> H1="the colors are not in proportion 2:3:6"
> chitab=qchisq(.95,df=2)
> chitab

> if(res$statistic<chitab)H0 else H1

OUTPUT:
Chi-squared test for given probabilities

data:  tulip
X-squared = 0.20253, df = 2, p-value = 0.9037

[1] 79.00000 52.66667 26.33333

[1] 5.991465

[1] " the colors are in proportion 2:3:6"

Problem statement: . 5 fruits were collected and were found that 50 are apples, 30 were
mangoes, 40 were strawberries, 35 were pineapples and 45 were grapes

Q1) are these fruits equally common?


Code
> fruit<-c(50,30,40,35,45)
> chk<-chisq.test(fruit,p=c(1/5,1/5,1/5,1/5,1/5))
> ck=chk
> ck$statistic
X-squared
6.25
> ch=qchisq(0.95,df=1)
> h0="Fruits are equal"
> h1="Fruits are not equal"
> if(ck$statistic<ch) h0 else h1
[1] "Fruits are not equal"

Q2) suppose in the region where you collected the data, the ration of red, yellow and white
tulip is 1: 1: 1: 1:1. Check whether the fruits are in same proportion

Code
> fruit<-c(50,30,40,35,45)
> chk<-chisq.test(fruit,p=c(1/5,1/5,1/5,1/5,1/5))
> ck=chk
> ck$expected
[1] 40 40 40 40 40
> r=qchisq(0.95,df=1)
> h0="Same as the proportion"
> h1="Not Same as the proportion"
> if(ck$statistic<r)h0 else h1
[1] "Not Same as the proportion"

AIM: Chi-Square test of fit test

Q3. Suppose the campus smoking statistics is as below. Determine whether the sample data in
survey supports it at 0.05 significance level

Heavy Never Occas Regul

4.5% 79.5% 8.5% 7.5%

Code
> library(MASS)
> levels(survey$Smoke)
[1] "Heavy" "Never" "Occas" "Regul"
> smoke.freq=table(survey$Smoke)
> smoke.freq

Heavy Never Occas Regul


11 189 19 17
> smoke.prob=c(0.045,0.795,.085,.075)
> c=chisq.test(smoke.freq,p=smoke.prob)
> ch=qchisq(0.95,df=1)
> if(c$statistic<ch)"campus data supports smoking" else "campus data supports smoking"
[1] "campus data supports smoking"

AIM: Find critical value


Q4. Find the 95th percentile of the Chi-Squared distribution with 7 degrees of freedom

Code
> qchisq(0.99,df=10)
[1] 23.20925

AIM: Chi-Squared Test of Independence


Q5. In the Built in dataset survey, the Smoke column records the students smoking habit, while
the Exer column records their exercise level
The allowed values in smoke are "heavy", "Regul"(regularly), "occas" (occassionally), and
"never".
As for Exer, they are "freq" (frequently), "Some" and "none"
test the hypothesis whether the student smoking habit is independent of their exercise level at
0.01 significance level

Code
> library(MASS)
> tbl=table(survey$Smoke,survey$Exer)
> tbl

Freq None Some


Heavy 7 1 3
Never 87 18 84
Occas 12 3 4
Regul 9 1 7
> chisq.test(tbl)

Pearson's Chi-squared test

data: tbl
X-squared = 5.4885, df = 6, p-value = 0.4828
Warning message:
In chisq.test(tbl) : Chi-squared approximation may be incorrect
> ctbl=cbind(tbl[,"Freq"],tbl[,"None"]+tbl[,"Some"])
> ctbl
[,1] [,2]
Heavy 7 4
Never 87 102
Occas 12 7
Regul 9 8
> c=chisq.test(ctbl)
> qchisq(0.99,df=3)
[1] 11.34487
> c$statistic
X-squared
3.232818
> h0="smoking habits are not dependent on exercise level"
> h1="smoking habits are dependent on exercise level"
> chitab=qchisq(0.99,df=3)
> if(c$statistic<chitab) h0 else h1
[1] "smoking habits are not dependent on exercise level"

AIM: Chi-squared test of independence


Problem Statement:

1.Is gender independent of education level? A random sample of 395 people were surveyed and
each person was asked to report the highest education level they obtained. The data that resulted
from the survey is summarized in the following table:

High School Bachelors Masters Ph.d. Total

Female 60 54 46 41 201

Male 40 44 53 57 194

Total 100 98 99 98 395

Are gender and education level dependent at 5% level of significance? In other words, given the
data collected above, is there a relationship between the gender of an individual and the level of
education that they have obtained?

Code:

> x<-matrix(c(60,46,54,44,46,53,41,57),nrow=2)

>x

[,1] [,2] [,3] [,4]

[1,] 60 54 46 41

[2,] 46 44 53 57

> chisq.test(x)

Pearson's Chi-squared test


data: x

X-squared = 5.9742, df = 3, p-value = 0.1129

> x<-matrix(c(60,40,54,44,46,53,41,57),nrow=2)

>x

[,1] [,2] [,3] [,4]

[1,] 60 54 46 41

[2,] 40 44 53 57

> chisq.test(x)

Pearson's Chi-squared test

data: x

X-squared = 8.0061, df = 3, p-value = 0.04589

> qchisq(.95,df=3)

[1] 7.814728

> h0="the gender of an individual and the level of education that they have obtained is
dependent"

> h1="the gender of an individual and the level of education that they have obtained is
independent"

> chitab=qchisq(.95,df=3)

> chical=chisq.test(x)

> chical$statistic
X-squared

8.006066

> if(chical$statistic<chitab) h0 else h1

Output:

[1] "the gender of an individual and the level of education that they have obtained is
independent"

Problem Statement:

2. A public opinion poll surveyed a simple random sample of 1000 voters. Respondents
were classified by gender (male or female) and by voting preference (Republican,
Democrat, or Independent). Results are shown in the table below.

Voting Preferences

Rep Dem Ind Row total

Male 200 150 400

Female 250 300 600

Column 450 450 1000


total
Is there a gender gap? Do the men's voting preferences differ significantly from the women's
preferences? Use a 0.05 level of significance.

Code:

>x<-matrix(c(200,250,150,300,50,50),nrow=2)

>x

[,1] [,2] [,3]

[1,] 200 150 50

[2,] 250 300 50

> chical=chisq.test(x)

> chical

Pearson's Chi-squared test

data: x

X-squared = 16.204, df = 2, p-value = 0.000303

> chitab=qchisq(0.95,df=1)

> chitab

[1] 3.841459

> h0="voting preference is independent of gender"

> h1="voting preference is dependent on gender"

> chical$statistic

X-squared

16.2037

> if(chical$statistic<chitab) h0 else h1


Output:

[1] "voting preference is dependent on gender"

Problem Statement:

3. Pretend the C&C of Honolulu wants to make their lifeguards more effective at preventing
ocean rescues. They have lifeguards patrol the beaches on days with dangerous surf and counsel
some people not to enter the water. It is a preventative measure. For every person they persuade
not to enter the water, that is potential ocean rescues avoided. So they think that bodyboarders
will tend to listen best to lifeguards who are bodyborders, and that shortboardes will tend to
listen best to lifeguards who also ride shortboards. Finally, they think longboardes will tend to
listen best to lifeguards who also ride longboards. There are four lifeguarding districts on Oahu
that correspond to each side of the island: North Shore, West Shore, South Shore and East Shore.
So if a certain side of the island has more shortboarders, then they want to stations more
shortboarding lifeguards in that part of the island.

So they are going to do a study of surfers to see if side of the island where people surf (the
neighborhood if you will) is related to or dependent upon their favorite way to surf (longboard,
shortboard, or boogieboard).

Favorite South Shore North Shore West Side Row total

Shortboard 33 75 20 128

Bodyboard 67 25 45 137

Longboard 20 20 55 95

Column total 120 120 120 360

(at 5% LOS)

Code:
> matrix(c(33,67,20,75,25,20,20,45,55),nrow=3)

[,1] [,2] [,3]

[1,] 33 75 20

[2,] 67 25 45

[3,] 20 20 55

> x<-matrix(c(33,67,20,75,25,20,20,45,55),nrow=3)

> chical=chisq.test(x)

> chical

Pearson's Chi-squared test

data: x

X-squared = 83.852, df = 4, p-value < 2.2e-16

> chitab=qchisq(0.95,df=4)

> chitab

[1] 9.487729

> h0="Neighborhood is dependent on the people's preferred surfing technique"

> h1="Neighborhood is independent of the people's preferred surfing technique"

> chical$statistic

X-squared

83.85232

> if(chical$statistic<chitab) h0 else h1

Output:
[1] "Neighborhood is independent of the people's preferred surfing technique"

Goodness of fit test


Problem Statement:

4. Acme Toy Company prints baseball cards. The company claims that 30% of the cards are
rookies, 60% veterans but not All-Stars, and 10% are veteran All-Stars.Suppose a random
sample of 100 cards has 50 rookies, 45 veterans, and 5 All-Stars. Is this consistent with Acme's
claim? Use a 0.05 level of significance.

Solution:

> x<-matrix(c(50,45,5))

> p<-c(.30,.60,.10)

> chical<-chisq.test(x,p=p)

> chical

Chi-squared test for given probabilities

data: x

X-squared = 19.583, df = 2, p-value = 5.592e-05

> chical$expected

[1] 30 60 10

> chitab=qchisq(0.95,df=2)
> chitab

[1] 5.991465

> h0="The cards are in the given proportion"

> h1="The cards are not in the given proportion"

> if(chical$statistic<chitab) h0 else h1

Output:

[1] "The cards are not in the given proportion"

Problem Statement:

5. In the game rock,paper,scissors,Kenny expects to win,tie and lose with equal frequency.Kenny
plays rock,paper,scissors often but he suspected his own games were not following that pattern
so he took a random sample of 24 games and recorded their outcomes. The table shows his
results:

Outcomes Win Loss Tie

Games 4 13 7

Do the distribution of his outcomes disagree with an even distribution with level of significance
0.05.

Code:

>x<-matrix(c(4,13,7))

> p<-c(1/3,1/3,1/3)

> chical<-chisq.test(x,p=p)

> chical
Chi-squared test for given probabilities

data: x

X-squared = 5.25, df = 2, p-value = 0.07244

> chical$expected

[1] 8 8 8

> chitab=qchisq(0.95,df=2)

> chitab

[1] 5.991465

>h0="The distribution of Kennys outcomes disagree with even distribution"

> h1="The distribution of Kennys outcomes agree with even distribution"

> if(chical$statistic<chitab) h0 else h1

Output:

[1] "The distribution of Kennys outcomes disagree with even distribution"

AIM: to find the Chi-square test

Problem statement: . 5 fruits were collected and were found that 50 are apples, 30 were
mangoes, 40 were strawberries, 35 were pineapples and 45 were grapes

Q1) are these fruits equally common?

Code:
> fruit<-c(50,30,40,35,45)
> chk<-chisq.test(fruit,p=c(1/5,1/5,1/5,1/5,1/5))
> ck=chk
> ck$statistic
> ch=qchisq(0.95,df=1)
> h0="Fruits are equal"
> h1="Fruits are not equal"
> if(ck$statistic<ch) h0 else h1
Output

AIM: to find the Chi-square test

Problem statement: . 5 fruits were collected and were found that 50 are apples, 30 were
mangoes, 40 were strawberries, 35 were pineapples and 45 were grapes

Q2) suppose in the region where you collected the data, the ration of red, yellow and white
tulip is 1: 1: 1: 1:1. Check whether the fruits are in same proportion
Code
> fruit<-c(50,30,40,35,45)
> chk<-chisq.test(fruit,p=c(1/5,1/5,1/5,1/5,1/5))
> ck=chk
> ck$expected
> r=qchisq(0.95,df=1)
> h0="Same as the proportion"
> h1="Not Same as the proportion"
> if(ck$statistic<r)h0 else h1
Output

AIM: Chi-Square test of fit test


Problem Statement: Suppose the campus smoking statistics is as below. Determine whether the
sample data in survey supports it at 0.05 significance level

Heavy Never Occas Regul


4.5% 79.5% 8.5% 7.5%

Code
> library(MASS)
> levels(survey$Smoke)
> smoke.freq=table(survey$Smoke)
> smoke.freq
> smoke.prob=c(0.045,0.795,.085,.075)
> c=chisq.test(smoke.freq,p=smoke.prob)
> ch=qchisq(0.95,df=1)
> if(c$statistic<ch)"campus data supports smoking" else "campus data supports smoking"

Output

AIM: Finding Critical Value


Problem Statement:
Find the 99th percentile of the Chi-Squared distribution with 10 degrees of freedom
Code
qchisq(0.99,df=10)
Output

AIM: Chi-Square Test for Independence


Problem Statement:
In the Built in dataset survey, the Smoke column records the students smoking habit, while the
Exer column records their exercise level
The allowed values in smoke are "heavy", "Regul"(regularly), "occas" (occassionally), and
"never".
As for Exer, they are "freq" (frequently), "Some" and "none"
test the hypothesis whether the student smoking habit is independent of their exercise level at
0.01 significance level
Code
library(MASS)
tbl=table(survey$Smoke,survey$Exer)
tbl
chisq.test(tbl)
ctbl=cbind(tbl[,"Freq"],tbl[,"None"]+tbl[,"Some"])
ctbl
c=chisq.test(ctbl)
qchisq(0.99,df=3)
c$statistic
h0="smoking habits are not dependent on exercise level"
h1="smoking habits are dependent on exercise level"
chitab=qchisq(0.99,df=3)
if(c$statistic<chitab) h0 else h1

Output:
AIM: Newton’s Forward Interpolation

1) PROBLEM STATEMENT
X=[0, 1, 2, 3, 4, 5]
Y=[1, 7, 23, 55, 109]

CODE OUTPUT
function [yest]=newtonfor(x, y, xest);
n=length(y);
h=x(2)-x(1);
r=(xest-x(1))/h;
for i=1:n-1
d(i,1)=y(i+1)-y(i);
end
for j=2:n-1
for i=1:n-j
d(i,j)=d(i+1,j-1)-d(i,j-1);
end
end
e(1)=r;
for j=2:n-1
e(j)=e(j-1)*(r+1-j)/j;
end
yest=0;
for i=1:n-1
yest=yest+e(i)*d(1,i);
end
yest=yest+y(1);
printf('\nEstimated value of y=%g when
value of x=%g',yest,xest);
endfunction
x=[0,1,2,3,4,5];
y=[1,7,23,55,109];
xest=0.5;
newtonfor(x,y,xest);
2) PROBLEM STATEMENT
X=[0, 2, 4, 6, 8, 10]
Y=[0, 4, 56, 204, 496, 980]

CODE OUTPUT
function [yest]=newtonfor(x, y, xest);
n=length(y);
h=x(2)-x(1);
r=(xest-x(1))/h;
for i=1:n-1
d(i,1)=y(i+1)-y(i);
end
for j=2:n-1
for i=1:n-j
d(i,j)=d(i+1,j-1)-d(i,j-1);
end
end
e(1)=r;
for j=2:n-1
e(j)=e(j-1)*(r+1-j)/j;
end
yest=0;
for i=1:n-1
yest=yest+e(i)*d(1,i);
end
yest=yest+y(1);
printf('\nEstimated value of y=%g when
value of x=%g',yest,xest);
endfunction
x=[0,2,4,6,8,10];
y=[0,4,56,204,496,980];
xest=0.5;
newtonfor(x,y,xest);
3) PROBLEM STATEMENT
X = [0.1, 0.2, 0.3, 0.4, 0.5]
Y = [20.025021, 10.050130, 6.742110, 5.101050, 4.127060]

CODE OUTPUT
function [yest]=newtonfor(x, y, xest);
n=length(y);
h=x(2)-x(1);
r=(xest-x(1))/h;
for i=1:n-1
d(i,1)=y(i+1)-y(i);
end
for j=2:n-1
for i=1:n-j
d(i,j)=d(i+1,j-1)-d(i,j-1);
end
end
e(1)=r;
for j=2:n-1
e(j)=e(j-1)*(r+1-j)/j;
end
yest=0;
for i=1:n-1
yest=yest+e(i)*d(1,i);
end
yest=yest+y(1);
printf('\nEstimated value of y=%g when value of
x=%g',yest,xest);
endfunction
x=[0.1,0.2,0.3,0.4,0.5];
y=[20.025021,10.050130,6.742110,5.101050,4.127
060];
xest=0.15;
newtonfor(x,y,xest);
AIM: Newton’s Backward Interpolation

4) PROBLEM STATEMENT
X =[1891, 1901, 1911, 1921, 1931]
Y =[46, 66, 81, 93, 101]

CODE OUTPUT
function[yest]=newtonback(x,y,xest);
n=length(y);
for i=2:n
d(i,1)=y(i)- y(i-1);
end
for i=2:n-1
for j=(i+1):n
d(j,i)= d(j,i-1)-d(j-1,i-1);
end
end
h=x(2)-x(1);
r=(xest-x(n))/h;
e(1)=r;
for i=2:n-1
e(i)= e(i-1)*(r+i-1)/i;
end
yest=0;
for i=1:n-1
yest=yest+e(i)*d(n,i);
end
yest=yest+y(n);
printf('\n Estimated value of y= %g when
value of x= %g',yest,xest);
endfunction
x=[1891,1901,1911,1921,1931];
y=[46,66,81,93,101];
xest=1925;
newtonback(x,y,xest);
5) PROBLEM STATEMENT
X =[1, 2, 3, 4, 5, 6, 7, 8]
Y =[1, 8, 27, 64, 125, 216, 343, 512]

CODE OUTPUT
function[yest]=newtonback(x,y,xest);
n=length(y);
for i=2:n
d(i,1)=y(i)- y(i-1);
end
for i=2:n-1
for j=(i+1):n
d(j,i)= d(j,i-1)-d(j-1,i-1);
end
end
h=x(2)-x(1);
r=(xest-x(n))/h;
e(1)=r;
for i=2:n-1
e(i)= e(i-1)*(r+i-1)/i;
end
yest=0;
for i=1:n-1
yest=yest+e(i)*d(n,i);
end
yest=yest+y(n);
printf('\n Estimated value of y= %g when
value of x= %g',yest,xest);
endfunction
x=[1,2,3,4,5,6,7,8];
y=[1,8,27,64,125,216,343,512];
xest=7.5;
newtonback(x,y,xest);
AIM: Hypothetical Testing

Problem Statement 1:

In a large city A. 20 % of random sample of 900 school children had defective eye sight. In other
large city B. 15% of random sample of 1600 children had the same defect. Is this difference b/w
the two proportions significant. Obtain 95% confidence limit for the difference in the
population proportion.

CODE-

n1=900
n2=1600
x1=180
x2=240
p1=x1/n1
p1
p2=x2/n2
p2
a=p1*n1
a
b=p2*n2
b
p0=(a+b)/(n1+n2)
p0
q0=1-p0
q0
d=1/n1
e=1/n2
z=(p1-p2)/sqrt(p0*q0*(d+e))
z
h0="There is no difference between the two proportions."
h1="There is difference between the two proportions."
alpha=0.05
z.alpha=qnorm(1-alpha)
z.alpha
if(z<(z.alpha))
print (h0) else (h1)
se=sqrt(p0*q0*(d+e))
confidence_limit=(p1-p2)+(z.alpha*se)
confidence_limit
confidence_limit=(p1-p2)-(z.alpha*se)
confidence_limit
Output-

Problem Statement 2:

A company has the head office at Kolkata & branch at Mumbai. The personal director wanted
to know if the workers at the two place would like the introduction of new plan of work & a
survey was conducted for this purpose. Out of a sample of 500 workers at Kolkata - 62%
favoured the new plan & at Mumbai out of a sample of 400 workers – 41% were against the
plan. It there any significant difference b/w the two groups in their attitude towards towards
the new plan at 5% LOS?

CODE-

n1=500
n2=400
x1=310
x2=236
p1=x1/n1
p1
p2=x2/n2
p2
a=p1*n1
a
b=p2*n2
b
p0=(a+b)/(n1+n2)
p0
q0=1-p0
q0
d=1/n1
e=1/n2
z=(p1-p2)/sqrt(p0*q0*(d+e))
z
h0="There is significant difference between the two groups in the altitude towards new plan."
h1="There is no significant difference between the two groups in the altitude towards new
plan."
alpha=0.05
z.alpha=qnorm(1-alpha)
z.alpha
if(z<(z.alpha))
print (h0) else (h1)

Output-

Problem statement 3:

The means of 2 single large samples of 1000 & 2000 members are 67.5 inches & 68.0 inches
respectively. Can the samples be regarded as drawn from the same population of S.D 2.5
inches. Test at 5% LOS.

CODE-

n1=1000
n2=2000
x1bar=67.5
x2bar=68.0
SD1=2.5
SD1=2.5
d=1/n1
e=1/n2
z=abs(x1bar-x2bar)/(SD1*sqrt(d+e))
z
h0="Both samples from same population."
h1="Both samples not from same population."
alpha=0.05
z.alpha=qnorm(1-alpha)
z.alpha
if(z<(z.alpha))
print (h0) else (h1)
OUTPUT –

Problem statement 4:

In a survey of shopping 400 women shoppers are selected randomly in super market
A located in a certain section of a city. Their average weekly food expenditure is Rs.250 with S.D
of Rs. 40. For 400 women shoppers selected at random in super market B in another section of
city the average weekly food expenditure is Rs. 220 with S.D of Rs.55. Test at 1% LOS whether
the average weekly food expenditure of the two populations of shoppers are equal.

Code-

n1=400
n2=400
x1bar=250
x2bar=220
SD1=40
SD2=55
d=1/n1
e=1/n2
z=abs(x1bar-x2bar)/sqrt((SD1*SD1/n1) +(SD2*SD2/n2))
z
h0="average weekly food expenditure of the two populations of shoppers are equal"
h1="average weekly food expenditure of the two populations of shoppers are not equal"
alpha=0.01
z.alpha=qnorm(1-alpha)
z.alpha
if(z<(z.alpha))
print (h0) else print (h1)

OUTPUT –

Problem statement 5:

A random sample of 500 apples was taken from a large consignment & 60 were found to be
bad. Obtain the 99% confidence limit for the percentage of bad apples in the consignment.

CODE-

n=500
a=60
p=a/n
q=1-p
alpha=0.01
z.alpha=qnorm(1-alpha)
z.alpha
confidence_limit=p+z.alpha*sqrt(p*q/n)
confidence_limit
confidence_limit=p-z.alpha*sqrt(p*q/n)
confidence_limit

OUTPUT –
Aim:- Test of significance for Population proportion.

Problem Statement:-
20 people were attacked by disease and only 18 survived. Will you reject the hypothesis that the
survival rates, if attacked by this disease; is 85% in favor of the hypothesis that it is more, at 5%
LOS?

Code:-

> n=20
> p=0.9
> p0=0.85
> q0=1-p0
> z=(p-p0)/sqrt(p0*q0/n)
>z
> alpha=0.05
> z.alpha=qnorm(1-alpha)
> z.alpha
> H0="The proportion of persons survived after attack by a disease in the lot is 85%"
> H1=" The proportion of persons survived after attack by a disease in the lot is above
85%”
> if(z<(z.alpha)) print(H0)else print(H1)

Output:-

Aim:- Test of significance for Population proportion.


Problem Statement:-
In a sample of 1000 people in Maharashtra, 540 are rice eaters and the rest are wheat eater. Can
we assume that both rice and wheat are equally popular in this state at 1% LOS?

Code:-

> n=1000
> p=0.54
> p0=0.5
> q0=1-p0
> H0="Rice and Wheat are equally popular in Maharashtra State"
> H1="Rice and Wheat are not equally popular in Maharashtra State"
> z=(p-p0)/sqrt(p0*q0/n)
>z
> z.alpha=qnorm(1-alpha)
> z.alpha
> if(z>(z.alpha)) print(H0)else print(H1)

Aim:- Test of significance for Population Mean.

Problem Statement:-
The mean weekly sell of soap bars in departmental store is 146.3 store, After an advertisement
campaign the mean weekly sell is 22 stores for a typical week increases to 153.7 and show sd of
17.2. Was the advertisement campaign successful?

Code:-

> xbar=153.7
> mu0=146.3
> n=22
> sd=17.2
> H0="There is no difference mean weekly sell between before & after adv. camp"
> H1="There is difference mean weekly sell between before & after adv. camp"
> z=(xbar-mu0)/(sd/sqrt(n))
>z
> alpha=0.05
> z.alpha=qnorm(1-alpha)
> z.alpha
> if(z>(z.alpha) && z<(z.alpha)) print(H0)else print(H1)

Output:-

Aim:- Test of significance for Population Mean.

Problem Statement:-
Random samples of 400 men and 600 women were asked whether they would like to have a
flyover near their residence. 200 men and 325 women were in favor of the proposal. Test the
hypothesis that proportion of men and women in favor of the proposal, are same against that they
are not, at 5% level.

Code:-

> n1=400
> n2=600
> x1=200
> x2=325
> p1=x1/n1
> p2=x2/n2
> p=(x1+x2)/(n1+n2)
>p
> q=1-p
>q
> H0="there is no significant difference between the opinions of men and women as far as
proposal of flyover is concerned."
> H1="there is significant difference between the opinions of men and women as far as
proposal of flyover is concerned."
> d=1/n1
> e=1/n2
> p0=p1-p2
> z=p0/sqrt(p*q*(d+e))
>z
> z.alpha=qnorm(1-alpha)
> z.alpha
> if(z<(z.alpha)) print(H0)else print(H1)

Output:-

Aim:- Test of significance for Population Mean.

Problem Statement:-
In a random sample of 100 men taken from village A, 60 were found to be consuming alcohol
and another sample of 200 men taken from village B, 100 were found to be consume alcohol. Do
the two villages to be dependent significantly in represent the proportion of mean who consume
alcohol.

Code:-

> n1=100
> x1=60
> n2=200
> x2=100
> p1=x1/n1
> p2=x2/n2
> a=n1*p1
> b=n2*p2
> p0=(a+b)/(n1+n2)
> p0
> q0=1-p0
> q0
> c=1/n1
> d=1/n2
> z=(p1-p2)/sqrt(p0*q0*(c+d))
>z
> alpha=0.05
> z.alpha=qnorm(1-alpha)
> z.alpha

> H0="Proportion of men who consume alcohol is dependent upon the population"
> H1=" Proportion of men who consume alcohol is not dependent upon the population"
> if(z<(z.alpha)) print(H0)else print(H1)

Output:-
Aim:- Test of significance for Population Mean.

Problem Statement:-
A sample of 900 members has a mean 3.4 cm and sd is 2.61 cm. Is the sample from large
population of mean is 3.25 cm and sd is 2.61 cm??Check

Code:-

> x=3.4
> mu0=3.25
> sd=2.61
> n=900
> z=(x-mu0)/sd*sqrt(n)
>z
> H0="mean of population is 3.25"
> H1="mean of population is not 3.25"
> alpha=0.05
> z.alpha=qnorm(1-alpha)
> z.alpha
> if(z<(z.alpha)) print(H0)else print(H1)

Output:-
AIM: Lagrange Interpolation
1. Problem statement:
Solve the following Lagrange interpolation X=[0 1 4] , Y=[3 2 11]

Code:
function [P]=lagrange(X,Y)
n=length(X);
x=poly(0,"x");
P=0;
for i=1:n
L=1;
for j=[1:i-1,i+1:n]
L=L*(x-X(j))/(X(i)-X(j));
end
P=P+L*Y(i)
end
endfunction
X=[0 1 4];
Y=[3 2 11];
P=lagrange(X,Y)
disp(P);

Output:

2. Problem statement:
Solve the following Lagrange X=[0 1 4], Y=[3 2 11], xest=2.5

Code:
function [P]=lagrange(X,Y, xest)
n=length(X);
x=poly(0,"x");
P=0;
for i=1:n
L=1;
for j=[1:i-1,i+1:n]
L=L*(xest-X(j))/(X(i)-X(j));
end
P=P+L*Y(i)
end
endfunction
xest=2.5
X=[0 1 4];
Y=[3 2 11];
P=lagrange(X,Y,xest)
disp(P);

Output:

3. Problem statement:
Solve the following Lagrange X=[ 2 3 5 12 8] , Y=[10 15 25 40 60] xest=4

Code:
function [P]=lagrange(X,Y, xest)
n=length(X);
x=poly(0,"x");
P=0;
for i=1:n
L=1;
for j=[1:i-1,i+1:n]
L=L*(xest-X(j))/(X(i)-X(j));
end
P=P+L*Y(i)
end
endfunction
xest=4
X=[2 3 5 8 12];
Y=[10 15 25 40 60];
P=lagrange(X,Y,xest)
disp(P);

Output:

4. Problem statement:
Solve the following Lagrange X=[ 5 6 9 11] , Y=[12 13 14 16] xest=10
Code:
function [P]=lagrange(X,Y, xest)
n=length(X);
x=poly(0,"x");
P=0;
for i=1:n
L=1;
for j=[1:i-1,i+1:n]
L=L*(xest-X(j))/(X(i)-X(j));
end
P=P+L*Y(i)
end
endfunction
xest=10
X=[ 5 6 9 11];
Y=[12 13 14 16];
P=lagrange(X,Y,xest)
disp(P);

Output:

5. Problem statement:
Solve the following Lagrange X=[ -2 1 3 7] , Y=[5 7 11 34] xest=0
Code:
function [P]=lagrange(X,Y, xest)
n=length(X);
x=poly(0,"x");
P=0;
for i=1:n
L=1;
for j=[1:i-1,i+1:n]
L=L*(xest-X(j))/(X(i)-X(j));
end
P=P+L*Y(i)
end
endfunction
xest=0
X=[ -2 1 3 7] ;
Y=[5 7 11 34] ;
P=lagrange(X,Y,xest)
disp(P);

Output:

You might also like