# 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
