library(Rstat)
# 20 Balls
ball <- 1:20
S2 <- urnsample2(ball, size=2); (N2 <- nrow(S2))
S5 <- urnsample2(ball, size=5); (N5 <- nrow(S5))
S10 <- urnsample2(ball, size=10); (N10 <- nrow(S10))
S15 <- urnsample2(ball, size=15); (N15 <- nrow(S15))
# Sample Mean and Probability Distribution
X2 <- apply(S2,1,mean); T2 <- table(X2); P2 <- T2/N2; v2 <- as.numeric(names(T2))
X5 <- apply(S5,1,mean); T5 <- table(X5); P5 <- T5/N5; v5 <- as.numeric(names(T5))
X10 <- apply(S10,1,mean); T10 <- table(X10); P10 <- T10/N10; v10 <- as.numeric(names(T10))
X15 <- apply(S15,1,mean); T15 <- table(X15); P15 <- T15/N15; v15 <- as.numeric(names(T15))
# Probability Distribution Plot
pmax <- max(c(P2,P5,P10,P15))
win.graph(9,6); par(mfrow=c(2,2)); par(mar=c(2,2,3,2))
plot(v2, P2, type="h", main="n=2", ylim=c(0,pmax), xlim=c(1,20), lwd=2, col=2)
plot(v5, P5, type="h", main="n=5", ylim=c(0,pmax), xlim=c(1,20), lwd=2, col=2)
plot(v10, P10, type="h", main="n=10", ylim=c(0,pmax), xlim=c(1,20), lwd=2, col=2)
plot(v15, P15, type="h", main="n=15", ylim=c(0,pmax), xlim=c(1,20), lwd=2, col=2)
# Expected Value and Variance
disc.exp(v2,T2); disc.exp(v5,T5); disc.exp(v10,T10); disc.exp(v15,T15)
# P(X>=15)
PX2 <- sum(P2[v2 >= 15]); PX5 <- sum(P5[v5 >= 15])
PX10 <- sum(P10[v10 >= 15]); PX15 <- sum(P15[v15 >= 15])
cat(PX2, PX5, PX10, PX15, "\n")