Вы находитесь на странице: 1из 16

Probability in matlab

Rand(3,3) Ceil(pi) Floor(pi)

Set operators. union - Set union. unique - Set unique. intersect - Set intersection. setdiff - Set difference. setxor - Set exclusive-or. ismember - True for set member.

Intersection and Union


A=[2 4 5 7] B=[3 4 7 8] The set operation can now be performed as C=intersect(A,B) Similarly union can be performed as D=union(A,B)

Combined operation on Sets A=[2 4 5 7]


B=[3 4 7 8] C=[5 6 7 8] D=intersect(A,union(B,C)) Demorgans laws U=[1:10] Using setdiff not set is produced Nota=setdiff(U,A) Setdiff(U,intersect(A,B)) Union(setdiff(U,A),setdiff(U,B))

We can simulate flipping a coin by generating random numbers from a uniform distribution in [0,1) and considering numbers < 0.5 to be tails and numbers >= 0.5 heads. Do the following in MATLAB: trials = 100; flip = rand(trials,1); heads = (flip >= 0.5); percentheads = sum(heads)/trials

Finding probability
A=[2 4 5 7] B=[3 4 7 8] C=[5 6 7 8] U=[1:8] an=intersect(A,intersect(setdiff(U,B),set diff(U,C))) P(ABC) Bn=intersect(A,intersect(B,C))

Binomial distribution
Syms p q D=p+q F=(D)^2 G=(p+q)^3 H=(p+q)^4 pretty(D) pretty(expand(G))

Combinatorial formula
Syms x k Kfac=sym(k!) N=0:4; X=(ones(size(N))) P=subs(Kfac,k,4)./subs(kfac,k,x.*n).*su bs(kfac,k,4,4-x.*n)

Mean of a vector
Funxtion xbar=mean1(x) N=length(x); xbar=(x(1); For I=2:n xbar=xbar+/n; X=rand(100,1); M=mean1(x)

Script for Random vector generation N=input(how many times to toss the coin)
P=input(probability of heads in a single); Nh=0;nt=0;x=[]; For I=1:n A=rand(1,1); if a<=p nh=nh+1;x=[x 1]; Else Nt=nt+1; X=[x 0] End; end;

Per=nh/n; Out=[nh nt]; Disp(no and percentage of heads is); Nh,per Pause Disp(no and percentage of tails is); Nt,1.-per,pause Hist(x,2) Title(histogram of h(1) and T(0)) ,pause

Function for statistical measures Function [xbar,s,sk,k]=statistics(X) [n.m]=size(X); xbar=sum(X)/n; X=X-ones(n,1)*xbar S=sqrt(sum(X.^2)/n) Sk=sum(X.^3)./(n*s.^3) K=sum(X.^4)./(n*s.^4); X=rand(100)

Graphic plots
Hist(x) Boxplot(x) Cdfplot(x) Normplot(X) Qqplot(x,y)

Statistical functions
Mean(x) Median(x) Std(x),var(x) Range(x) Max(x),min(x) Skewness(x) Kurtosis(x) Prctile(c,p) Cov(x,y) Corrcoef(x,y)

X=rand(1000,1) y=rand(1000,1) Qqplot(x,y) function y=binomialpdf(x,n,p)

Вам также может понравиться