(* porovnani dvou normalnich rozdeleni se stredni hodnotou 0 a varinaci 1 a 4 *)
Echo["Variance 1"];
h1=Plot[PDF[NormalDistribution[0,1],x]//Evaluate,{x,-13,+13},Filling->Axis,PlotRange->{0,0.5},PlotStyle->Blue]
Echo["Variance 4"];
h2=Plot[PDF[NormalDistribution[0,4],x]//Evaluate,{x,-13,+13},Filling->Axis,PlotRange->{0,0.5},PlotStyle->Green]
Show[h1,h2]

(* Metoda nejmensich ctvercu v pripade tri nezavislych mereni se stredni hodnotou 0 *)
A={{1},{1},{1}};
W1=DiagonalMatrix[{1,1,1}];
Cov1=Transpose[W1].W1;
InvCov1=Inverse[Cov1];
LSQ1[x_]:=Inverse[Transpose[A].InvCov1.A].Transpose[A].InvCov1.x;
Echo["Variance chyby odhadu v pripade chyb mereni se stejnou varianci 1"];
P1=Inverse[Transpose[A].InvCov1.A]
MatrixForm[P1];
MatrixForm[Inverse[Transpose[A].InvCov1.A].Transpose[A].InvCov1];
W2=DiagonalMatrix[{1,1,2}];
MatrixForm[W2];
Cov2=Transpose[W2].W2;
MatrixForm[Cov2];
InvCov2=Inverse[Cov2];
MatrixForm[InvCov2];
LSQ2[x_]:=Inverse[Transpose[A].InvCov2.A].Transpose[A].InvCov2.x;
MatrixForm[Inverse[Transpose[A].InvCov2.A].Transpose[A].InvCov2];
Echo["Variance chyby odhadu v pripade dvou chyb mereni s varianci 1 a tretiho mereni s varianci 4"];
P2=Inverse[Transpose[A].InvCov2.A]
MatrixForm[P2];

(* Priklad s odhadem veliciny t=23 merene nezavisle tremi merenimi s varianci 1 *)
(Subscript[t, true])=23;
(Subscript[b, true])={{23},{23},{23}};
MatrixForm[(Subscript[b, true])];
errors1={{RandomVariate[NormalDistribution[0,1]]},{RandomVariate[NormalDistribution[0,1]]},{RandomVariate[NormalDistribution[0,1]]}};
Echo["chyby mereni"];
MatrixForm[errors1]
b1=(Subscript[b, true])+errors1;
Echo["namerene hodnoty"];
MatrixForm[b1]
Echo["levy inverz metodou nejmensich ctvercu bez vah"];
L= Inverse[Transpose[A].InvCov1.A].Transpose[A].InvCov1;
MatrixForm[L]
Echo["odhad veliciny t=23 metodou nejmensich ctvercu (bez vah)"];
(Subscript[x1, est])=LSQ1[b1]
g1=Plot[PDF[NormalDistribution[First[First[(Subscript[x1, est])]],First[First[P1]]],x]//Evaluate,{x,First[First[(Subscript[x1, est])]]-4,First[First[(Subscript[x1, est])]]+4},Filling->Axis,PlotRange->{-0.1,1.4}];
g1a=ListPlot[{{23,0}},PlotStyle->{PointSize[Large],Red}];
g1b=ListPlot[{{First[First[(Subscript[x1, est])]],0}},PlotStyle->{PointSize[Large],Black}];
Show[g1,g1a,g1b]

(* srovnani metody nejmensich ctvercu a metody nejmensich ctvercu s vahami *)
errors2={{RandomVariate[NormalDistribution[0,1]]},{RandomVariate[NormalDistribution[0,1]]},{RandomVariate[NormalDistribution[0,4]]}};
Echo["chyby mereni, prvni dve s varianci 1, treti s varianci 4"];
MatrixForm[errors2]
(Subscript[t, true])=23;
(Subscript[b, true])={{23},{23},{23}};
b2=(Subscript[b, true])+errors2;
Echo["namerene hodnoty, prvni dve s varianci 1, treti s varianci 4"];
MatrixForm[b2]
Echo["levy inverz spocitany metodou nejmensich ctvercu s vahami"];
L0 = MatrixForm[Inverse[Transpose[A].InvCov2.A].Transpose[A].InvCov2]
Echo["odhad spocitany metodou nejmensich ctvercu s vahami"];
(Subscript[x2, est])=LSQ2[b2]
Echo["horsi odhad spocitaneho metodou nejmensich ctvercu bez vah, tj. s ignorovanim ruzne variance chyb pri mereni"];
(Subscript[BADx2, est])=LSQ1[b2]
BADP2 = L.Cov2.Transpose[L];
Echo["variance chyby horsiho odhadu v pripade ignorovani ruzne variance chyb pri mereni"];
MatrixForm[BADP2]
g2=Plot[PDF[NormalDistribution[First[First[(Subscript[x2, est])]],First[First[P2]]],x]//Evaluate,{x,First[First[(Subscript[x2, est])]]-4,First[First[(Subscript[x2, est])]]+4},Filling->Axis,PlotRange->{-0.1,1.4}];
g2a=ListPlot[{{23,0}},PlotStyle->{PointSize[Large],Red}];
g2b=ListPlot[{{First[First[(Subscript[x2, est])]],0}},PlotStyle->{PointSize[Large],Black}];
Echo["normalni rozdeleni chyb odhadu metodou nejmensich ctvercu s vahami, cervene je skutecna hodnota t = 23"];
Show[g2,g2a,g2b]
BADg2=Plot[PDF[NormalDistribution[First[First[(Subscript[BADx2, est])]],First[First[BADP2]]],x]//Evaluate,{x,First[First[(Subscript[BADx2, est])]]-4,First[First[(Subscript[BADx2, est])]]+4},Filling->Axis,PlotRange->{-0.1,1.4},PlotStyle->Green];
BADg2b=ListPlot[{{First[First[(Subscript[BADx2, est])]],0}},PlotStyle->{PointSize[Medium],Black}];
Echo["normalni rozdeleni chyb odhadu spocitany metodou nejmensich ctvercu bez vah, tj. s ignorovanim ruzne variance chyb pri mereni"];
Show[BADg2,g2a,BADg2b]
Echo["porovnani chyb obou odhadu"];
Show[g2,g2a,g2b,BADg2,g2a,BADg2b]

(Subscript[b, true])={{23},{23},{23}};
errors3={{RandomVariate[NormalDistribution[0,1]]},{RandomVariate[NormalDistribution[0,1]]}};
(Subscript[b3, true])={{23},{23}};
errors4={{RandomVariate[NormalDistribution[0,4]]}};
(Subscript[b4, true])={{23}};
errors=Join[errors3,errors4];
Echo["chyby mereni, prvni dve s varianci 1, treti s varianci 4"];
MatrixForm[errors]
b3=(Subscript[b3, true])+errors3;
b4=(Subscript[b4, true])+errors4;
b=(Subscript[b, true])+errors;
Echo["namerene hodnoty, prvni dve s varianci 1, treti s varianci 4"];
MatrixForm[b]
A3={{1},{1}};
W3=DiagonalMatrix[{1,1}];
Cov3=Transpose[W3].W3;
InvCov3=Inverse[Cov3];
LSQ3[x_]:=Inverse[Transpose[A3].InvCov3.A3].Transpose[A3].InvCov3.x;
Echo["Odhad ziskany metodou nejmensich ctvercu pouze z prvnich dvou mereni"];
(Subscript[x3, est])=LSQ3[b3]
P3=Inverse[Transpose[A3].InvCov3.A3];
Echo["Variance chyby odhadu ziskaneho z prvnich dvou mereni"];
MatrixForm[P3]
Echo["normalni rozdeleni chyb odhadu spocitaneho metodou nejmensich ctvercu bez vah pouze z prvnich dvou mereni"];
g3=Plot[PDF[NormalDistribution[First[First[(Subscript[x3, est])]],First[First[P3]]],x]//Evaluate,{x,19,27},Filling->Axis,PlotRange->{-0.1,1}];
g2a=ListPlot[{{23,0}},PlotStyle->{PointSize[Large],Red}];
g3b=ListPlot[{{First[First[(Subscript[x3, est])]],0}},PlotStyle->{PointSize[Medium],Black}];
Show[g3,g2a,g3b]
MatrixForm[Inverse[Transpose[A4].InvCov4.A4].Transpose[A4].InvCov4];
(Subscript[BADx2, est])=LSQ1[b2];
BADP2=Inverse[Transpose[A].InvCov1.A].Transpose[A].InvCov1.Cov2.Transpose[Inverse[Transpose[A].InvCov1.A].Transpose[A].InvCov1];
g2a=ListPlot[{{23,0}},PlotStyle->{PointSize[Large],Red}];
g4b=ListPlot[{{First[First[(Subscript[x4, est])]],0}},PlotStyle->{PointSize[Large],Black}];
g4=Plot[PDF[NormalDistribution[First[First[(Subscript[x4, est])]],First[First[P4]]],x]//Evaluate,{x,First[First[(Subscript[x4, est])]]-12,First[First[(Subscript[x4, est])]]+12},Filling->Axis,PlotRange->{-0.1,1}];
Show[g4,g2a,g4b];
MatrixForm[errors];
MatrixForm[b];
Echo["Odhad ziskany metodou nejmensich ctvercu s vahami ze vsech tri mereni"];
(Subscript[x, est])=LSQ2[b]
Echo["Variance chyby odhadu ziskaneho metodou nejmensich ctvercu s vahami ze vsech tri mereni"];
P2=Inverse[Transpose[A].InvCov2.A]
MatrixForm[P2];
g5=Plot[PDF[NormalDistribution[First[First[(Subscript[x, est])]],First[First[P2]]],x]//Evaluate,{x,19,27},Filling->Axis,PlotRange->{-0.1,1},PlotStyle->Green];
g5b=ListPlot[{{First[First[(Subscript[x, est])]],0}},PlotStyle->{PointSize[Large],Black}];
Show[g5,g2a,g5b]
Echo["Porovnani obou odhadu"];
Show[g3,g2a,g5,g3b,g5b]