Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create comm.py #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions amozeika/comm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This repository contains the python code which computes the number of committees, K, given the total number of nodes, N, (a detailed description of the algorithm is provided in https://www.overleaf.com/read/yqvczvrmsgts) and the simulation code which uses this K to compute the prob. that in at least one of these committees the number of adversarial nodes exceeds a fraction of the committee size.
27 changes: 27 additions & 0 deletions amozeika/comm/comm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import math
from scipy.stats import binom
def comm(N,delta,A,P):
#N is the number of nodes, delta is the failure prob. which can be tolerated, A is the fraction of a committee (typical value is 1/3) and P is the fraction of adversarial nodes (typical value is 1/4).
K = 1
n = N
r = 0
Prob = 0.0
m = 0
while Prob < delta:
K_1 = K
n_1 = n
r_1 = r
Prob_1 = Prob
m = m +1
K = 2*m + 1
n = N // K
r = N % K
if 0 < r:
Prob_n = binom.cdf(math.floor(A * n), n, P)
Prob_n1 = binom.cdf(math.floor(A * (n+1)), n+1, P)
Prob = 1 - Prob_n ** (K - r) * Prob_n1 ** r
else:
Prob_n = binom.cdf(math.floor(A * n), n, P)
Prob = 1 - Prob_n ** K
#return number of committees, K_1, committee size, n_1, number of committees with size n_1+1, r_1 and prob. of failure, Prob_1.
return K_1, n_1, r_1, Prob_1;
69 changes: 69 additions & 0 deletions amozeika/comm/rand_part_3_v2.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#This code simulates the assignment of N nodes into the K committees.
#A node is adversarial with the probability p1, i.e. the number of adversarial nodes is N x p1 on average.
#The code computes the probability that in at least one of the committees the number of adversarial nodes exceeds the A x the number of nodes in the committee, where A is the number between 0 and 1.
#For details see https://www.overleaf.com/read/yqvczvrmsgts
#To run code launch R from the terminal and type source('rand_part_3_v2.r'); The output is in rand_num* files.
code_name<-'rand_part_3_v2.r';
rand_num=9038;
set.seed(rand_num);
print(rand_num);
##################parameters##############################################
M=10^6; #number of samples
N=773; #total number of nodes
K=3; #number of committees
p1=1/4; #fraction of advrsarial nodes
A=1/3; #fraction of nodes in a reservoir
n=N%/%K; #compute quotient
r=N%%K; #compute remainder
###################generate array of committee sizes such that K-r committees are of size n and r committee are of size n+1########################
comm=replicate(K,n);
for (k in 1:r) comm[k]=comm[k]+1;
###########################################################################
in_file<-paste(rand_num,"_param.txt",sep="");
out_file<-paste(rand_num,"_stats.csv", sep="");
###########################################################################
write(paste("r-code=", code_name,", rand_num=",rand_num, ", M=",M,", N=",N, ", K=", K, ", n=", n, ", r=", r, ", p1=",p1,", A=", A, sep=""), in_file, ncolumns =1, append = FALSE, sep = "\n");
write(paste("K", "Prob", sep = "\t"), out_file, ncolumns =2, append = FALSE, sep = "\t");
start_time <- Sys.time();
Prob=0;
for (j in 1:M)
{
s<-rbinom(N,1, p1); #generate 0/1
N0<-replicate(K,0);
N1<-replicate(K,0);
mu=1;
cntr=0;
for (i in 1:N)
{
if (s[i]>0)
{
N1[mu]=N1[mu]+1;
} else
{
N0[mu]=N0[mu]+1;
}
cntr=cntr+1;
if (cntr==comm[mu])
{
cntr=0;
mu=mu+1;
}

}
SUM=0;
for (mu in 1:K)
{
if (N1[mu]>=(floor((A*(N1[mu]+N0[mu])))+1))
{
SUM=SUM+1;
}
}
if (SUM>0)
{
Prob=Prob+1;
}
}
Prob=Prob/M;
write(paste(K,Prob,sep = "\t"), out_file, ncolumns =1, append =TRUE, sep = "\t");
end_time <- Sys.time();
print(end_time - start_time);
75 changes: 75 additions & 0 deletions amozeika/comm/rand_part_4_v2.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#This code simulates random assignment of N nodes into the K committees.
#The number of adversarial nodes is N_r (N_r < N).
#The code computes the probability that in at least one of the committees the number of adversarial nodes exceeds the A x the number of nodes in the committee, where A is the number between 0 and 1.
#For details see https://www.overleaf.com/read/yqvczvrmsgts
#To run code launch R from the terminal and type source('rand_part_4_v2.r'); The output is in rand_num* files.
code_name<-'rand_part_4_v2.r';
rand_num=7800;
set.seed(rand_num);
print(rand_num);
##################parameters##############################################
M=10^6; #number of samples
N=773; #total number of nodes
K=3; #number of committees
N_r=floor(N/4); #number of adversarial nodes
A=1/3; #fraction of nodes in a committee
n=N%/%K; #compute quotient
r=N%%K; #compute remainder
###################generate array of committee sizes such that K-r committees are of size n and r committees are of size n+1########################
comm=replicate(K,n);
for (k in 1:r) comm[k]=comm[k]+1;
###########################################################################
in_file<-paste(rand_num,"_param.txt",sep="");
out_file<-paste(rand_num,"_stats.csv", sep="");
###########################################################################
write(paste("r-code=", code_name,", rand_num=",rand_num, ", M=",M,", N=",N, ", K=",K,", n=",n, ", r=",r, ", N/N_r=",(N/N_r),", A=", A, sep=""), in_file, ncolumns =1, append = FALSE, sep = "\n");
write(paste("K", "Prob", sep = "\t"), out_file, ncolumns =2, append = FALSE, sep = "\t");
start_time <- Sys.time();
#generate binary sequence with exactly N_r of 1's
s0<-replicate(N,0);
for (i in 1:N_r)
{
s0[i]=1;
};
Prob=0;
for (j in 1:M)
{
s<-sample(s0);
N0<-replicate(K,0);
N1<-replicate(K,0);
mu=1;
cntr=0;
for (i in 1:N)
{
if (s[i]>0)
{
N1[mu]=N1[mu]+1;
} else
{
N0[mu]=N0[mu]+1;
}
cntr=cntr+1;
if (cntr==comm[mu])
{
cntr=0;
mu=mu+1;
}

}
SUM=0;
for (mu in 1:K)
{
if (N1[mu]>=(floor((A*(N1[mu]+N0[mu])))+1))
{
SUM=SUM+1;
}
}
if (SUM>0)
{
Prob=Prob+1;
}
}
Prob=Prob/M;
write(paste(K,Prob,sep = "\t"), out_file, ncolumns =1, append =TRUE, sep = "\t");
end_time <- Sys.time();
print(end_time - start_time);