School Commit Init
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
#A=[1 2 3; 4 5 6; 7 8 9];
|
||||
#x=1:2:10;
|
||||
#A(1:2,2:3);
|
||||
#x+2;
|
||||
#3*x;
|
||||
#x.^2;
|
||||
#B=[5 4 3; 8 2 1; 1 5 5];
|
||||
#A*B;
|
||||
#B*A;
|
||||
|
||||
A=[1 0 -2; 2 1 3; 0 1 0];
|
||||
B=[2 1 1; 1 0 -1;1 1 0];
|
||||
C=A-B
|
||||
D=A*B
|
||||
E=A.*B
|
||||
|
||||
x=0:0.01:3;
|
||||
plot(x, x.^5/10,"k:",x,x.*sin(x),"r--",x,cos(x),"b")
|
||||
@@ -0,0 +1,27 @@
|
||||
## Copyright (C) 2023 danie
|
||||
##
|
||||
## This program is free software: you can redistribute it and/or modify
|
||||
## it under the terms of the GNU General Public License as published by
|
||||
## the Free Software Foundation, either version 3 of the License, or
|
||||
## (at your option) any later version.
|
||||
##
|
||||
## This program is distributed in the hope that it will be useful,
|
||||
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
## GNU General Public License for more details.
|
||||
##
|
||||
## You should have received a copy of the GNU General Public License
|
||||
## along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
## -*- texinfo -*-
|
||||
## @deftypefn {} {@var{retval} =} fun1 (@var{input1}, @var{input2})
|
||||
##
|
||||
## @seealso{}
|
||||
## @end deftypefn
|
||||
|
||||
## Author: danie <danie@DANIELCUJBA>
|
||||
## Created: 2023-10-09
|
||||
|
||||
function y = fun1 (x)
|
||||
y=x^2-x
|
||||
endfunction
|
||||
@@ -0,0 +1,14 @@
|
||||
# We are solving ex 2
|
||||
# We plot the pdf and cdf of the binomial
|
||||
|
||||
n = input("Give the number of trials n=");
|
||||
p = input("Give the probability of success p=");
|
||||
x=0:1:n;
|
||||
|
||||
px=binopdf(x,n,p);
|
||||
plot(x,px,'*r');
|
||||
hold on
|
||||
|
||||
xx=0:0.0001:n;
|
||||
cx=binocdf(xx,n,p);
|
||||
plot(xx,cx,'g');
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
|
||||
n = input("Give the degrees of freedom n=");
|
||||
|
||||
#a
|
||||
chi2cdf(0,n);
|
||||
|
||||
1-chi2cdf(0,n);
|
||||
|
||||
#b
|
||||
|
||||
chi2cdf(1,n)-chi2cdf(-1,n);
|
||||
|
||||
1-(chi2cdf(1,n)-chi2cdf(-1,n));
|
||||
|
||||
#c+d
|
||||
|
||||
a = input("Give a number between 0 and 1 a=");
|
||||
b = input("Give a number between 0 and 1 b=");
|
||||
|
||||
chi2inv(a,n)
|
||||
|
||||
chi2inv(1-b,n)
|
||||
@@ -0,0 +1,48 @@
|
||||
# X ~ Bino(3,0.5)
|
||||
|
||||
# We are solving Application from lab 2
|
||||
|
||||
n=3 # 3 coin flips
|
||||
p=0.5 # coin flips <=> 0.5 probability
|
||||
|
||||
x=0:1:n;
|
||||
px=binopdf(x,n,p);
|
||||
# ( 0 1 2 3 )
|
||||
# X ( 1/8 3/8 3/8 1/8)
|
||||
#
|
||||
plot(x,px,"+");
|
||||
hold on
|
||||
|
||||
# P(X=0) = binopdf(0,3,0.5) =1/8
|
||||
p1=binopdf(0,3,0.5);
|
||||
printf('P(X=0)=%1.6f\n',p1)
|
||||
|
||||
xx=0:0.01:n;
|
||||
|
||||
|
||||
# P(X!=0) = 1 - binopdf(1,3,0.5) =5/8
|
||||
p2=1-binopdf(1,3,0.5); # PROBABILITY OF THE COMPLAMENTARY EVENT
|
||||
printf('P(X!=0)=%1.6f\n',p2)
|
||||
|
||||
#P(X<=2) = binocdf(2,3,0.5) = 7/8
|
||||
p3=binocdf(2,3,0.5);
|
||||
printf('P(X<=2)=%1.6f\n',p3)
|
||||
|
||||
#P(X<2) = binocdf(1,3,0.5) = 1/2 P(X<2)=P(X<=1)
|
||||
p4=binocdf(1,3,0.5)
|
||||
printf('P(X<2)=%1.6f\n',p4)
|
||||
|
||||
# P(X>=1) = 1-P(X<1) = 1 - binocdf(0,3,0.5)
|
||||
p5=1 - binocdf(0,3,0.5); # PROBABILITY OF THE COMPLAMENTARY EVENT
|
||||
printf('P(X>=1)=%1.6f\n',p5)
|
||||
|
||||
# P(X>1) = 1 - P(X<=1) - binocdf(1,3,0.5)
|
||||
p6=1 - binocdf(1,3,0.5); # PROBABILITY OF THE COMPLAMENTARY EVENT
|
||||
printf('P(X>1)=%1.6f\n',p6)
|
||||
|
||||
N=input("Give the number of simulations N=");
|
||||
U=rand(3,N); # Generate N simulations of 3 coin tosses
|
||||
Y=(U>0.5); # Transforms decimal in True for Heads(>0.5) or False for Tails
|
||||
X=sum(Y); # Sums up the collums
|
||||
clf # Clear the figure
|
||||
hist(X); # Plots X
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
p = input("Give a probability between 0 and 1 p=");
|
||||
|
||||
for n = 1:3:100
|
||||
x=0:n;
|
||||
y=binopdf(x,n,p);
|
||||
plot(x,y)
|
||||
pause(0.5)
|
||||
|
||||
endfor
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
p = input("Give a probability between 0 and 0.05 p=");
|
||||
n = input("Give a number greater than 30 n=");
|
||||
|
||||
l=n*p;
|
||||
|
||||
for n = 1:3:100
|
||||
x=0:n;
|
||||
y=binopdf(x,n,p);
|
||||
plot(x,y)
|
||||
pause(0.5)
|
||||
|
||||
endfor
|
||||
@@ -0,0 +1,32 @@
|
||||
X=[7 7 4 5 9 9 4 12 8 1 8 7 3 13 2 1 17 7 12 5 6 2 1 13 14 10 2 4 9 11 3 5 12 6 10 7];
|
||||
|
||||
n=length(X);
|
||||
|
||||
#oneminusalpha = input("Confidence level: (between 0 and 1)");
|
||||
oneminusalpha = 0.95;
|
||||
alpha = 1-oneminusalpha;
|
||||
|
||||
sigma=5;
|
||||
|
||||
m1=mean(X)-(sigma/sqrt(n))*norminv(1-alpha/2, 0, 1);
|
||||
|
||||
m2= mean(X)-(sigma/sqrt(n))*norminv(alpha/2, 0, 1);
|
||||
|
||||
printf("Confidence interval for the theoretical mean when sigma is known is (%4.3f, %4.3f)\n", m1,m2);
|
||||
|
||||
m3=mean(X)-(std(X)/sqrt(n))*tinv(1-alpha/2,n-1);
|
||||
|
||||
m4= mean(X)-(std(X)/sqrt(n))*tinv(alpha/2,n-1);
|
||||
|
||||
printf("Confidence interval for the theoretical mean when sigma is unknown is (%4.3f, %4.3f)\n", m3,m4);
|
||||
|
||||
v1=((n-1)*var(X))/(chi2inv(1-alpha/2, n-1));
|
||||
|
||||
v2=((n-1)*var(X))/(chi2inv(alpha/2,n-1));
|
||||
|
||||
printf("Confidence interval for the variance is (%4.3f, %4.3f)\n", v1,v2);
|
||||
|
||||
s1=sqrt(v1);
|
||||
s2=sqrt(v2);
|
||||
|
||||
printf("Confidence interval for the theoretical standard variance is (%4.3f, %4.3f)\n", s1,s2);
|
||||
@@ -0,0 +1,17 @@
|
||||
x1=[22.4 21.7 24.5 23.4 21.6 23.3 22.4 21.6 24.8 20.0];
|
||||
|
||||
x2=[17.7 14.8 19.6 19.6 12.1 14.8 15.4 12.6 14.0 12.2];
|
||||
|
||||
n1=length(x1);
|
||||
n2=length(x2);
|
||||
|
||||
#oneminusalpha = input("Confidence level: (between 0 and 1)");
|
||||
oneminusalpha = 0.95;
|
||||
alpha = 1-oneminusalpha;
|
||||
|
||||
a1= (mean(x1) - mean(x2) - tinv(1-alpha/2,n1+n2-2)*sqrt(((n1-1)*var(x1)+(n2-2)*var(x2))/(n1+n2-2))*sqrt(1/n1+1/n2));
|
||||
|
||||
a2=(mean(x1) - mean(x2) + tinv(1-alpha/2,n1+n2-2)*sqrt(((n1-1)*var(x1)+(n2-2)*var(x2))/(n1+n2-2))*sqrt(1/n1+1/n2));
|
||||
|
||||
|
||||
printf("The Confidence interval between population means when sigma1=sigma2 is (%4.3f, %4.3f)\n", a1,a2);
|
||||
@@ -0,0 +1,42 @@
|
||||
X = [7 7 4 5 9 9 4 12 8 1 8 7 3 13 2 1 17 7 12 5 6 2 1 13 14 10 2 4 9 11 3 5 12 6 10 7];
|
||||
|
||||
n=length(X);
|
||||
|
||||
#oneminusalpha = input("Confidence level: (between 0 and 1)");
|
||||
oneminusalpha = 0.95;
|
||||
alpha = 1-oneminusalpha;
|
||||
|
||||
sigma=5;
|
||||
|
||||
#The null hypothesis is: H0: u=8.5 (it goes togheter with u>8.5, the standard is meet)
|
||||
#The alternative hypothesis is: H1: u<8.5 (the standard is not meet)
|
||||
#Left tail test for u when sigma is known
|
||||
|
||||
printf("This is a left tail test for the mean when sigma is known\n\n");
|
||||
|
||||
#n0 = input("Test value n0=");
|
||||
n0=8.5;
|
||||
|
||||
[H, PVAL, CI, ZVALUE] = ztest(X,n0,sigma,'alpha',alpha,'tail','left');
|
||||
|
||||
z_alpha = norminv(alpha, 0, 1);
|
||||
|
||||
RR = [-inf z_alpha];
|
||||
|
||||
printf("The value of h is: %d\n\n",H);
|
||||
|
||||
if H==1
|
||||
printf("The null hypothesis is rejected\n\n");
|
||||
printf("The data suggests that the standard is not meet\n\n");
|
||||
else
|
||||
printf("The null hypothesis is not rejected\n\n");
|
||||
printf("The data suggests that the standard is meet\n\n");
|
||||
endif
|
||||
|
||||
printf("The regection region is: (%4.3f, %4.3f)\n",RR);
|
||||
printf("The observed value of the test statistic is: %4.3f\n",ZVALUE );
|
||||
printf("The Pvalue of the test is: %4.3f\n",PVAL);
|
||||
|
||||
#b use ttest
|
||||
#2 a use vartest2
|
||||
#2 b use ttest2
|
||||
@@ -0,0 +1,47 @@
|
||||
clear
|
||||
X=[46 37 39 48 47 44 35 31 44 37];
|
||||
Y=[35 33 31 34 34 30 27 32 31 31];
|
||||
|
||||
n1=length(X);
|
||||
n2=length(Y);
|
||||
|
||||
alpha=0.05; # significance level
|
||||
|
||||
|
||||
#The null hypothesis is: H0: vx!=vy (the standard is meet) (the variances differ)
|
||||
#The alternative hypothesis is: H1: vx=vy (the standard is not meet) (the variances are the same)
|
||||
#Both tail test when sigma is unknown
|
||||
|
||||
|
||||
[H, PVAL, CI, STATS] = vartest2(X,Y,'alpha',alpha,'tail','both');
|
||||
|
||||
if H==1
|
||||
printf("The null hypothesis is rejected\n\n");
|
||||
printf("The data suggests that the standard is not meet\n\n");
|
||||
printf("The variances seem to be the same");
|
||||
else
|
||||
printf("The null hypothesis is not rejected\n\n");
|
||||
printf("The data suggests that the standard is meet\n\n");
|
||||
printf("The variances seem to differ\n\n");
|
||||
endif
|
||||
|
||||
z_alpha = norminv(alpha/2, 0, 1); # calculating tt of alpha/2
|
||||
|
||||
RR = [-inf z_alpha]; # first part of the regection region
|
||||
|
||||
z_alpha2 = norminv(1-alpha/2, 0, 1); # calculating tt of 1-alpha/2
|
||||
|
||||
RR2 = [z_alpha2 +inf]; # second part of the regection region
|
||||
|
||||
printf("The regection region is: (%4.3f, %4.3f) reunited with (%4.3f, %4.3f)\n",RR,RR2);
|
||||
printf("The observed value of the test statistic is: %4.3f\n", STATS.fstat );
|
||||
printf("The Pvalue of the test is: %4.3f\n\n",PVAL);
|
||||
|
||||
oneminualpha=0.95; # confidence level
|
||||
|
||||
a1= (mean(X) - mean(Y) - tinv(1-alpha/2,n1+n2-2)*sqrt(((n1-1)*var(X)+(n2-2)*var(Y))/(n1+n2-2))*sqrt(1/n1+1/n2));
|
||||
|
||||
a2=(mean(X) - mean(Y) + tinv(1-alpha/2,n1+n2-2)*sqrt(((n1-1)*var(X)+(n2-2)*var(Y))/(n1+n2-2))*sqrt(1/n1+1/n2));
|
||||
|
||||
|
||||
printf("The Confidence interval between population means when sigma1=sigma2, where sigma and sigma2 are unknown, is (%4.3f, %4.3f)\n", a1,a2);
|
||||
@@ -0,0 +1,40 @@
|
||||
X=[4.6 0.7 4.2 1.9 4.8 6.1 4.7 5.5 5.4];
|
||||
Y=[2.5 1.3 2.0 1.8 2.7 3.2 3.0 3.5 3.4];
|
||||
|
||||
nx=length(X);
|
||||
ny=length(Y);
|
||||
|
||||
vx=var(X);
|
||||
vy=var(Y);
|
||||
|
||||
#The null hypothesis is: H0: vx!=vy (the standard is meet)
|
||||
#The alternative hypothesis is: H1: vx=vy (the standard is not meet)
|
||||
#Both tail test for u when sigma is unknown
|
||||
|
||||
#oneminusalpha = input("Confidence level: (between 0 and 1)");
|
||||
oneminusalpha = 0.95;
|
||||
alpha = 1-oneminusalpha;
|
||||
|
||||
[H, PVAL, CI, ZVALUE] = vartest2(X,Y,'alpha',alpha,'tail','both');
|
||||
|
||||
if H==1
|
||||
printf("The null hypothesis is rejected\n\n");
|
||||
printf("The data suggests that the standard is not meet\n\n");
|
||||
else
|
||||
printf("The null hypothesis is not rejected\n\n");
|
||||
printf("The data suggests that the standard is meet\n\n");
|
||||
endif
|
||||
|
||||
#The null hypothesis is: H0: mx!=my (the standard is meet)
|
||||
#The alternative hypothesis is: H1: mx=my (the standard is not meet)
|
||||
#Both tail test for u when sigma is unknown
|
||||
|
||||
[H, PVAL, CI, ZVALUE] = ttest2(X,Y,'alpha',alpha,'tail','both');
|
||||
|
||||
if H==1
|
||||
printf("The null hypothesis is rejected\n\n");
|
||||
printf("The data suggests that the standard is not meet\n\n");
|
||||
else
|
||||
printf("The null hypothesis is not rejected\n\n");
|
||||
printf("The data suggests that the standard is meet\n\n");
|
||||
endif
|
||||
@@ -0,0 +1,37 @@
|
||||
X=[1001.7 975.0 978.3 988.3 978.7 988.9 1000.3 979.2 968.9 983.5 999.2 985.6];
|
||||
|
||||
n=length(X);
|
||||
|
||||
#oneminusalpha = input("Confidence level: (between 0 and 1)");
|
||||
oneminusalpha = 0.95;
|
||||
alpha = 1-oneminusalpha;
|
||||
|
||||
#The null hypothesis is: H0: u=995 (it goes togheter with u>995, the standard is meet)
|
||||
#The alternative hypothesis is: H1: u<995 (the standard is not meet)
|
||||
#Left tail test for u when sigma is unknown
|
||||
|
||||
printf("This is a left tail test for the mean when sigma is inknown\n\n");
|
||||
|
||||
n0=995;
|
||||
|
||||
[H, PVAL, CI, ZVALUE] = ttest(X,n0,'alpha',alpha,'tail','left');
|
||||
|
||||
if H==1
|
||||
printf("The null hypothesis is rejected\n\n");
|
||||
printf("The data suggests that the standard is not meet\n\n");
|
||||
else
|
||||
printf("The null hypothesis is not rejected\n\n");
|
||||
printf("The data suggests that the standard is meet\n\n");
|
||||
endif
|
||||
|
||||
oneminusalpha = 0.99;
|
||||
alpha = 1-oneminusalpha;
|
||||
|
||||
v1=((n-1)*var(X))/(chi2inv(1-alpha/2, n-1));
|
||||
|
||||
v2=((n-1)*var(X))/(chi2inv(alpha/2,n-1));
|
||||
|
||||
s1=sqrt(v1);
|
||||
s2=sqrt(v2);
|
||||
|
||||
printf("Confidence interval for the theoretical standard variance is (%4.3f, %4.3f)\n", s1,s2);
|
||||
Reference in New Issue
Block a user