Cap. 9

Exercício 9.9

n=100;
(Subscript[p, 0])=0.08;
UCL=16.1;
dist=BinomialDistribution[n,(Subscript[p, 0])];
probfalsealarm=1-CDF[dist,UCL] (* because LCL is equal to zero! *)
incontrolARL=1/probfalsealarm

(Subscript[p, 1])=0.2;
dist=BinomialDistribution[n,(Subscript[p, 1])];
probvalidsignal=1-CDF[dist,UCL] (* because LCL is equal to zero! *)

dist=GeometricDistribution[probvalidsignal]  ;(* geometric distribution for the number of trials before the first success, where the probability of success in a trial is p *)
CDF[dist,4-1]

Exercício 9.10

n=50;
(Subscript[p, 0])=0.02;
LCL=Max[0,n×(Subscript[p, 0])-3×(Sqrt[n×(Subscript[p, 0])×(1-(Subscript[p, 0]))])] 
CL=n×(Subscript[p, 0])
UCL=n×(Subscript[p, 0])+3×(Sqrt[n×(Subscript[p, 0])×(1-(Subscript[p, 0]))])
(Subscript[p, 1])=0.04;
dist=BinomialDistribution[n,(Subscript[p, 1])];
probvalidsignal=1-CDF[dist,UCL]  (* because LCL is equal to zero! *)
dist=GeometricDistribution[probvalidsignal]  ;(* geometric distribution for the number of trials before the first success, where the probability of success in a trial is p *)
PDF[dist,1-1]
PDF[dist,4-1]

Exercício 9.11

n=100;
(Subscript[p, 0])=0.079;
LCL=Max[0,n×(Subscript[p, 0])-3×(Sqrt[n×(Subscript[p, 0])×(1-(Subscript[p, 0]))])]
CL=n×(Subscript[p, 0])
UCL=n×(Subscript[p, 0])+3×(Sqrt[n×(Subscript[p, 0])×(1-(Subscript[p, 0]))]);
UCL=16 (* the UCL value was incorrectly approximated in the lecture notes *)
dist=BinomialDistribution[n,(Subscript[p, 0])];
probfalsealarm=1-CDF[dist,UCL] (* because LCL is equal to zero! *)
incontrolARL=1/probfalsealarm

(Subscript[p, 1])=0.1;
dist=BinomialDistribution[n,(Subscript[p, 1])];
probvalidsignal=1-CDF[dist,UCL]  (* because LCL is equal to zero! *)
outofcontrolARL=1/probvalidsignal

(Subscript[p, 2])=0.05;
dist=BinomialDistribution[n,(Subscript[p, 2])];
probvalidsignal=1-CDF[dist,UCL]  (* because LCL is equal to zero! *)
outofcontrolARL=1/probvalidsignal

ξ[δ_]=1-CDF[BinomialDistribution[n,(Subscript[p, 0])+δ],UCL];
Plot[Log[(1/(ξ[δ]))],{δ,-(Subscript[p, 0])+0.01,1-(Subscript[p, 0])-0.01}]
Plot[Log[(1/(ξ[δ]))],{δ,0,(Subscript[p, 1])-(Subscript[p, 0])}]
Plot[Log[(1/(ξ[δ]))],{δ,(Subscript[p, 2])-(Subscript[p, 0]),(Subscript[p, 1])-(Subscript[p, 0])}]

Exercício 9.16

data={2,4,3,1,0,2,1,8,2,1,3,4,5,4,2,3};
(Subscript[λ, 0])=N[Mean[data],5];
LCL=Max[0,(Subscript[λ, 0])-3(Sqrt[(Subscript[λ, 0])])]
CL=(Subscript[λ, 0])
UCL=(Subscript[λ, 0])+3(Sqrt[(Subscript[λ, 0])])
list1=Table[LCL,{i,1,Length[data]}];
list2=Table[CL,{i,1,Length[data]}];
list3=Table[UCL,{i,1,Length[data]}];
ListPlot[
{list3,list2,data,list1},
AxesOrigin->{1,0},
AxesLabel->{"N",""},
PlotStyle->{Directive[Red,Thick],Directive[Green,Thick],Directive[Blue,Thick], Directive[Red]},
PlotRange->{{0,Length[data]+1},{-1,Max[Max[data],UCL]+1}},PlotLegends->Placed[{"UCL","CL","(Subscript[t, N])","LCL"},Right],Joined->{True,True,True,True}]

numberobsoutofcontrol=Length[data]×Mean[Table[If[data[[i]]<LCL ||  data[[i]]>UCL,1,0],{i,1,Length[data]}]];
If[numberobsoutofcontrol>0,Print["There is (are) ",numberobsoutofcontrol," out of control observation(s), thus we need to update the data and control limits."],Print["There are no out of control observations."]]

newdata={};
Table[If[data[[i]]≥LCL &&  data[[i]]≤UCL,newdata=Append[newdata,data[[i]]]],{i,1,Length[data]}];
newdata

(Subscript[λ, 0])=N[Mean[newdata],5];
LCL=Max[0,(Subscript[λ, 0])-3(Sqrt[(Subscript[λ, 0])])]
CL=(Subscript[λ, 0])
UCL=(Subscript[λ, 0])+3(Sqrt[(Subscript[λ, 0])])
list1=Table[LCL,{i,1,Length[newdata]}];
list2=Table[CL,{i,1,Length[newdata]}];
list3=Table[UCL,{i,1,Length[newdata]}];

ListPlot[
{list3,list2,newdata,list1},
AxesOrigin->{1,0},
AxesLabel->{"N",""},
PlotStyle->{Directive[Red,Thick],Directive[Green,Thick],Directive[Blue,Thick], Directive[Red]},
PlotRange->{{0,Length[newdata]+1}, {-1,Max[Max[data],UCL]+1}},PlotLegends->Placed[{"UCL","CL","(Subscript[t, N])","LCL"},Right],Joined->{True,True,True,True}]

(Subscript[λ, 1])=5.;
dist=PoissonDistribution[(Subscript[λ, 1])];
probvalidsignal=1-(CDF[dist,UCL] -CDF[dist,Ceiling[LCL]-0.001]);
outofcontrolARL=1/probvalidsignal
outofcontrolSDRL=((Sqrt[1-probvalidsignal])/probvalidsignal)

Ceiling[((Log[1-0.5])/(Log[1-probvalidsignal]))] (* median of ARL *)
(((1-probvalidsignal))^(Floor[outofcontrolARL])) (* probability of more than outofcontrolARL observations until a valid signal*)

Exercise 9.18

data={0,1,1,0,2,1,1,3,2,1,0,3,2,5,1,2,1,1};
(Subscript[λ, 0])=N[Mean[data],5];
LCL=Max[0,(Subscript[λ, 0])-3(Sqrt[(Subscript[λ, 0])])]
CL=(Subscript[λ, 0])
UCL=(Subscript[λ, 0])+3(Sqrt[(Subscript[λ, 0])])
list1=Table[LCL,{i,1,Length[data]}];
list2=Table[CL,{i,1,Length[data]}];
list3=Table[UCL,{i,1,Length[data]}];

ListPlot[
{list3,list2,data,list1},
AxesOrigin->{1,0},
AxesLabel->{"N",""},
PlotStyle->{Directive[Red,Thick],Directive[Green,Thick],Directive[Blue,Thick], Directive[Red]},
PlotRange->{{0,Length[data]+1},{-1,Max[Max[data],UCL]+1}},PlotLegends->Placed[{"UCL","CL","(Subscript[t, N])","LCL"},Right],Joined->{True,True,True,True}]

numberobsoutofcontrol=Length[data]×Mean[Table[If[data[[i]]<LCL ||  data[[i]]>UCL,1,0],{i,1,Length[data]}]];
If[numberobsoutofcontrol>0,Print["There is (are) ",numberobsoutofcontrol," out of control observation(s), thus we need to update the data and control limits."],Print["There are no out of control observations."]]

newdata={};
Table[If[data[[i]]≥LCL &&  data[[i]]≤UCL,newdata=Append[newdata,data[[i]]]],{i,1,Length[data]}];
newdata;

(Subscript[λ, 0])=N[Mean[newdata],5];
LCL=Max[0,(Subscript[λ, 0])-3(Sqrt[(Subscript[λ, 0])])]
CL=(Subscript[λ, 0])
UCL=(Subscript[λ, 0])+3(Sqrt[(Subscript[λ, 0])])
list1=Table[LCL,{i,1,Length[newdata]}];
list2=Table[CL,{i,1,Length[newdata]}];
list3=Table[UCL,{i,1,Length[newdata]}];

ListPlot[
{list3,list2,newdata,list1},
AxesOrigin->{1,0},
AxesLabel->{"N",""},
PlotStyle->{Directive[Red,Thick],Directive[Green,Thick],Directive[Blue,Thick], Directive[Red]},
PlotRange->{{0,Length[newdata]+1}, {-1,Max[Max[data],UCL]+1}},PlotLegends->Placed[{"UCL","CL","(Subscript[t, N])","LCL"},Right],Joined->{True,True,True,True}]

Exercise 9.22

dist=NormalDistribution[0,1];
ξ[δ_]=1-(CDF[dist,3-δ]-CDF[dist,-3-δ]);
ARL[δ_]=(1/(ξ[δ]));
Plot[ARL[δ],{δ,-3,3}]

Exercise 9.24

n=4;
(Subscript[μ, 0])=200;
σ=6.;
LCL=191;
UCL=209; (* these are the 3-sigma control limits *)
dist=NormalDistribution[0,1];

(Subscript[μ, 1])=188;
probvalidsignal=1-(CDF[dist,((UCL-(Subscript[μ, 1]))/(σ/(Sqrt[n])))]-CDF[dist,((LCL-(Subscript[μ, 1]))/(σ/(Sqrt[n])))])

(Subscript[μ, 1])=212;
probvalidsignal=1-(CDF[dist,((UCL-(Subscript[μ, 1]))/(σ/(Sqrt[n])))]-CDF[dist,((LCL-(Subscript[μ, 1]))/(σ/(Sqrt[n])))])

Exercise 9.25

dist=NormalDistribution[0,1];
γ=Quantile[dist,1-(1/500)];
ξ[δ_]=1-CDF[dist,γ-δ];
ARLU[δ_]=(1/(ξ[δ]));
g1=Plot[Log[ARLU[δ]],{δ,-1,3}];

β=Quantile[dist,1-((1/500)/2)];
ζ[δ_]=1-(CDF[dist,β-δ]-CDF[dist,-β-δ]);
ARLS[δ_]=(1/(ζ[δ]));
g2=Plot[Log[ARLS[δ]],{δ,-1,3}];

Show[g1,g2]

Exercise 9.26

n=5;
σ=(Sqrt[49]);
data={34.5,34.2,31.6,31.5,35.0,34.1,32.6,33.8,34.8,33.6,31.9,38.6,35.4,34.0,37.1,34.9,33.5,31.7,34.0,35.1,33.7,32.8,33.5,34.2};
(Subscript[μ, 0])=N[Mean[data],5];
LCL=(Subscript[μ, 0])-3×(σ/(Sqrt[n]))
CL=(Subscript[μ, 0])
UCL=(Subscript[μ, 0])+3×(σ/(Sqrt[n]))
list1=Table[LCL,{i,1,Length[data]}];
list2=Table[CL,{i,1,Length[data]}];
list3=Table[UCL,{i,1,Length[data]}];

ListPlot[
{list3,list2,data,list1},
AxesOrigin->{1,0},
AxesLabel->{"N",""},
PlotStyle->{Directive[Red,Thick],Directive[Green,Thick],Directive[Blue,Thick], Directive[Red]},
PlotRange->{{0,Length[data]+1},{Min[Min[data],LCL]-1,Max[Max[data],UCL]+1}},PlotLegends->Placed[{"UCL","CL","(Subscript[(OverBar[x]), N])","LCL"},Right],Joined->{True,True,True,True}]

numberobsoutofcontrol=Length[data]×Mean[Table[If[data[[i]]<LCL ||  data[[i]]>UCL,1,0],{i,1,Length[data]}]];
If[numberobsoutofcontrol>0,Print["There is (are) ",numberobsoutofcontrol," out of control observation(s), thus we need to update the data and control limits."],Print["There are no out of control observations."]]

dist=NormalDistribution[0,1];
ξ[δ_]=1-(CDF[dist,3-δ]-CDF[dist,-3-δ]);
ξ[0.]

(Subscript[μ, 1])=45;
shift=(((Subscript[μ, 1])-(Subscript[μ, 0]))/(σ/(Sqrt[n])));
probvalidsignal=ξ[shift]
dist=GeometricDistribution[probvalidsignal]  ;(* geometric distribution for the number of trials before the first success, where the probability of success in a trial is p *)
PDF[dist,5-1]

dist=NormalDistribution[0,1];
fractiondefective=1-(CDF[dist,(((30+1)-(Subscript[μ, 0]))/(σ/(Sqrt[n])))]- CDF[dist,(((30-1)-(Subscript[μ, 0]))/(σ/(Sqrt[n])))])

Exercise 9.27

n=5;
(Subscript[σ, 0])=(Sqrt[4]);
incontrolARL=200;
probfalsealarm=(1/incontrolARL);
dist=ChiSquareDistribution[n-1];
γ=Quantile[dist,(probfalsealarm/2.)];
β=Quantile[dist,1-(probfalsealarm/2.)];
LCL=(((Subscript[σ, 0])^2)/(n-1))×γ
CL=((Subscript[σ, 0])^2)
UCL=(((Subscript[σ, 0])^2)/(n-1))×β
ξ[θ_]=1-(CDF[dist,(β/(θ^2))]-CDF[dist,(γ/(θ^2))]);
1/ξ[1]

(Subscript[σ, 1])=(Sqrt[6]);
shiftinsigma=((Subscript[σ, 1])/(Subscript[σ, 0]));
probvalidsignal=ξ[shift]
dist=GeometricDistribution[probvalidsignal]  ;(* geometric distribution for the number of trials before the first success, where the probability of success in a trial is p *)
PDF[dist,1-1]

Example 9.28

(Subscript[σ, 0])=1;
probfalsealarm=0.002;
θ={0.5,0.75,0.8,0.9,0.95,1,1.1,1.2};
n={4,5,7,10,15,100};
For[i=1,i≤Length[θ],i++,
For[j=1,j≤Length[n],j++,ξ[i,j]=1-(CDF[ChiSquareDistribution[n[[j]]-1],((Quantile[ChiSquareDistribution[n[[j]]-1],1-(probfalsealarm/2.)])/((θ[[i]])^2))]-CDF[ChiSquareDistribution[n[[j]]-1],((Quantile[ChiSquareDistribution[n[[j]]-1],(probfalsealarm/2.)])/((θ[[i]])^2))])]]
MatrixForm[Table[ξ[i,j],{i,1,Length[θ]},{j,1,Length[n]}]]

Clear[θ];
n=5;
(Subscript[σ, 0])=1;
probfalsealarm=0.002;
dist=ChiSquareDistribution[n-1];
γ=Quantile[dist,(probfalsealarm/2.)];
β=Quantile[dist,1-(probfalsealarm/2.)];
ξ[θ_]=1-(CDF[dist,(β/(θ^2))]-CDF[dist,(γ/(θ^2))]);
ARL[θ_]=(1/(ξ[θ]));
Plot[ARL[θ],{θ,0.01,2},AxesOrigin->{1,0}]
ARL[1]

Exercise 9.29

(Subscript[σ, 0])=1;
probfalsealarm=0.002;
θ={0.5,0.75,0.8,0.9,0.95,1,1.1,1.2};
n={4,5,7,10,15,100};
For[i=1,i≤Length[θ],i++,
For[j=1,j≤Length[n],j++,

r=FindRoot[{1-(CDF[ChiSquareDistribution[n[[j]]-1],b]-CDF[ChiSquareDistribution[n[[j]]-1],a])==probfalsealarm,a×PDF[ChiSquareDistribution[n[[j]]-1],a]-b×PDF[ChiSquareDistribution[n[[j]]-1],b]==0},{{a,Quantile[ChiSquareDistribution[n[[j]]-1],(probfalsealarm/2.)]},{b,Quantile[ChiSquareDistribution[n[[j]]-1],1-(probfalsealarm/2.)]}}];
NQ={a,b}/.Dispatch[Flatten[r]];(* Non balanced quantiles *)
γ[j]=NQ[[1]];
β[j]=NQ[[2]];
LCL[j]=((((Subscript[σ, 0])^2)×γ[j])/(n[[j]]-1));(* New lower control limits *)
UCL[j]=((((Subscript[σ, 0])^2)×β[j])/(n[[j]]-1)); (* New upper control limits *)
ξ[i,j]=1-(CDF[ChiSquareDistribution[n[[j]]-1],((β[j])/((θ[[i]])^2))]-CDF[ChiSquareDistribution[n[[j]]-1],((γ[j])/((θ[[i]])^2))])]]
MatrixForm[Table[LCL[j],{j,1,Length[n]}]] (* New lower control limits *)
MatrixForm[Table[UCL[j],{j,1,Length[n]}]] (* New upper control limits *)
MatrixForm[Table[ξ[i,j],{i,1,Length[θ]},{j,1,Length[n]}]] (* New prob. signal *)

MatrixForm[Table[(1/(ξ[i,j])),{i,1,Length[θ]},{j,1,Length[n]}]] (* ARL values: all ≤ incontrolARL=500 *)

MatrixForm[Table[{n[[j]],Plot[1/(1-(CDF[ChiSquareDistribution[n[[j]]-1],((β[j])/(δ^2))]-CDF[ChiSquareDistribution[n[[j]]-1],((γ[j])/(δ^2))])),{δ,0.9,1.1},AxesOrigin->{1,0},PlotRange->{0,(1/probfalsealarm)}]},{j,1,Length[n]}]]

Exercise 9.34

(* We should have replaced σ, in the expressions of the control limits of the standard (OverBar[X])-chart (check Table 9.12) and standard S-chart (check Exercise 9.31), by its unbiased estimate ((OverBar[s])/((E(S))/σ))= ((OverBar[s])/(((((2/(n-1))))^(1/2))×((Γ((n/2)))/(Γ(((n-1)/2)))))). Check (9.11). *)

n=6;
numberofsamples=50;
sumofsamplemeans=1000;
sumofsamplestandarddeviations=75;
estimateofmu=(sumofsamplemeans/numberofsamples);
unbiasedestimateofsigma=N[((sumofsamplestandarddeviations/numberofsamples)/(((((2/(n-1))))^(1/2))×((Gamma[(n/2)])/(Gamma[((n-1)/2)])))),5];

(* estimates of control limits for the (OverBar[X])-chart *)
LCL=estimateofmu-3×(unbiasedestimateofsigma/(Sqrt[n])) 
UCL=estimateofmu+3×(unbiasedestimateofsigma/(Sqrt[n]))
dist=NormalDistribution[0,1];
probfalsealarm=1-(CDF[dist,((UCL-estimateofmu)/(unbiasedestimateofsigma/(Sqrt[n])))]-CDF[dist,((LCL-estimateofmu)/(unbiasedestimateofsigma/(Sqrt[n])))])
(Subscript[μ, 1])=25;
(Subscript[σ, 1])=2;
probvalidsignal=1-(CDF[dist,((UCL-(Subscript[μ, 1]))/((Subscript[σ, 1])/(Sqrt[n])))]-CDF[dist,((LCL-(Subscript[μ, 1]))/((Subscript[σ, 1])/(Sqrt[n])))]);
dist=GeometricDistribution[probvalidsignal]  ;(* geometric distribution for the number of trials before the first success, where the probability of success in a trial is p *)
CDF[dist,5-1]

(* estimates of control limits for the S-chart *)LCL=unbiasedestimateofsigma×((((2/(n-1))))^(1/2))×((Gamma[(n/2)])/(Gamma[((n-1)/2)]))-3×unbiasedestimateofsigma ×(Sqrt[1-(((((((2/(n-1))))^(1/2))×((Gamma[(n/2)])/(Gamma[((n-1)/2)]))))^2)])
UCL=unbiasedestimateofsigma×((((2/(n-1))))^(1/2))×((Gamma[(n/2)])/(Gamma[((n-1)/2)]))+3×unbiasedestimateofsigma ×(Sqrt[1-(((((((2/(n-1))))^(1/2))×((Gamma[(n/2)])/(Gamma[((n-1)/2)]))))^2)])