#
# 2fail.R, 21 Jun 26
#
# Data from:
# An Experimental Evaluation of the Assumption of Independence in Multiversion Programming
# JOHN C. KNIGHT AND NANCY G. LEVESON
#
# N-Version Programming with Coding Agents
# Javier Ron and Benoit Baudry and and Martin Monperrus


fail_rate=function(df, K)
{
P0=prod((1-df$q))        # Fraction of zero failures
P1=sum(P0*df$q/(1-df$q)) # Fraction of one failure
P_more=1-P0-P1           # Fraction of two or more failures

z=(K-n*P_more)/sqrt(n*P_more*(1-P_more)) # Knight & Leveson used this
# # R has a built-in function for the exact Clopper and Pearson method!
# binom.test(x=K, n, p=P_more, alternative="two.sided")

return(data.frame(P0, P1, P_more, z))
}


n=1e6

KL=read.csv("KL.csv")
KL$q=KL$Failures/n # fraction of failures

K_kl=1255 # actual number of two or more failures

RBM=read.csv("RBM.csv")
RBM$q=(n-RBM$passes)/n

K_rbm_pas=426
K_rbm_py=424
K_rbm_rus=424

KL_rate=fail_rate(KL, K_kl)
RBM_rate_pas=fail_rate(subset(RBM, language == "pascal"), K_rbm_pas)
RBM_rate_py=fail_rate(subset(RBM, language == "python"), K_rbm_py)
RBM_rate_rus=fail_rate(subset(RBM, language == "rust"), K_rbm_rus)

KL_rate
RBM_rate_pas
RBM_rate_py
RBM_rate_rus


p=0.05
p=0.01

# Confidence intervals
pL=n*qbeta(p/2, K, n-K+1) 
pU=n*qbeta(1-p/2, K+1, n-K) 



