From e99e41a88b0ab8dcb3a008cf0d601324a606fd9a Mon Sep 17 00:00:00 2001 From: Alexander Mozeika <106667936+AMozeika@users.noreply.github.com> Date: Tue, 29 Nov 2022 20:35:18 +0000 Subject: [PATCH 1/9] Create comm.py --- comm.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 comm.py diff --git a/comm.py b/comm.py new file mode 100644 index 0000000..c017f44 --- /dev/null +++ b/comm.py @@ -0,0 +1,25 @@ +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 + while Prob < delta: + K_1 = K + n_1 = n + r_1 = r + Prob_1 = Prob + K = K + 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; From d661f2e41488abd9644c568f98809817ae1ff4ab Mon Sep 17 00:00:00 2001 From: Alexander Mozeika <106667936+AMozeika@users.noreply.github.com> Date: Tue, 29 Nov 2022 20:55:19 +0000 Subject: [PATCH 2/9] Create comm.py --- amozeika/comm.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 amozeika/comm.py diff --git a/amozeika/comm.py b/amozeika/comm.py new file mode 100644 index 0000000..c017f44 --- /dev/null +++ b/amozeika/comm.py @@ -0,0 +1,25 @@ +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 + while Prob < delta: + K_1 = K + n_1 = n + r_1 = r + Prob_1 = Prob + K = K + 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; From 6cb27e9703d0696ed7a7843a787936c087b20af8 Mon Sep 17 00:00:00 2001 From: Alexander Mozeika <106667936+AMozeika@users.noreply.github.com> Date: Tue, 29 Nov 2022 20:56:33 +0000 Subject: [PATCH 3/9] Delete comm.py --- comm.py | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 comm.py diff --git a/comm.py b/comm.py deleted file mode 100644 index c017f44..0000000 --- a/comm.py +++ /dev/null @@ -1,25 +0,0 @@ -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 - while Prob < delta: - K_1 = K - n_1 = n - r_1 = r - Prob_1 = Prob - K = K + 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; From eb4262ee2ceea47a07c5db1c6824391d74f34a2c Mon Sep 17 00:00:00 2001 From: Alexander Mozeika <106667936+AMozeika@users.noreply.github.com> Date: Tue, 29 Nov 2022 21:19:31 +0000 Subject: [PATCH 4/9] Create README.md --- amozeika/comm/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 amozeika/comm/README.md diff --git a/amozeika/comm/README.md b/amozeika/comm/README.md new file mode 100644 index 0000000..165fd8c --- /dev/null +++ b/amozeika/comm/README.md @@ -0,0 +1 @@ +This repository.... From edab58c068fd2a03644dc27c0719860a4bec359b Mon Sep 17 00:00:00 2001 From: Alexander Mozeika <106667936+AMozeika@users.noreply.github.com> Date: Tue, 29 Nov 2022 21:20:10 +0000 Subject: [PATCH 5/9] Add files via upload --- amozeika/comm/comm.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 amozeika/comm/comm.py diff --git a/amozeika/comm/comm.py b/amozeika/comm/comm.py new file mode 100644 index 0000000..c017f44 --- /dev/null +++ b/amozeika/comm/comm.py @@ -0,0 +1,25 @@ +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 + while Prob < delta: + K_1 = K + n_1 = n + r_1 = r + Prob_1 = Prob + K = K + 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; From b652420947323809c69245ba2bca4cc782ac78e6 Mon Sep 17 00:00:00 2001 From: Alexander Mozeika <106667936+AMozeika@users.noreply.github.com> Date: Tue, 29 Nov 2022 22:36:55 +0000 Subject: [PATCH 6/9] Update README.md --- amozeika/comm/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amozeika/comm/README.md b/amozeika/comm/README.md index 165fd8c..fb93078 100644 --- a/amozeika/comm/README.md +++ b/amozeika/comm/README.md @@ -1 +1 @@ -This repository.... +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. From edc5ad54e7128445b5945ce6a62204f9b7b1d9f3 Mon Sep 17 00:00:00 2001 From: Alexander Mozeika <106667936+AMozeika@users.noreply.github.com> Date: Tue, 29 Nov 2022 22:37:47 +0000 Subject: [PATCH 7/9] Add files via upload --- amozeika/comm/rand_part_3_v2.r | 69 +++++++++++++++++++++++++++++++ amozeika/comm/rand_part_4_v2.r | 75 ++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 amozeika/comm/rand_part_3_v2.r create mode 100644 amozeika/comm/rand_part_4_v2.r diff --git a/amozeika/comm/rand_part_3_v2.r b/amozeika/comm/rand_part_3_v2.r new file mode 100644 index 0000000..6f5c4d1 --- /dev/null +++ b/amozeika/comm/rand_part_3_v2.r @@ -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); diff --git a/amozeika/comm/rand_part_4_v2.r b/amozeika/comm/rand_part_4_v2.r new file mode 100644 index 0000000..b5be25f --- /dev/null +++ b/amozeika/comm/rand_part_4_v2.r @@ -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); From 7f7a85ff94fd66fed084de74b1910dcaea9d0680 Mon Sep 17 00:00:00 2001 From: Alexander Mozeika <106667936+AMozeika@users.noreply.github.com> Date: Tue, 29 Nov 2022 22:42:12 +0000 Subject: [PATCH 8/9] Delete comm.py --- amozeika/comm.py | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 amozeika/comm.py diff --git a/amozeika/comm.py b/amozeika/comm.py deleted file mode 100644 index c017f44..0000000 --- a/amozeika/comm.py +++ /dev/null @@ -1,25 +0,0 @@ -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 - while Prob < delta: - K_1 = K - n_1 = n - r_1 = r - Prob_1 = Prob - K = K + 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; From b5b008bc6aba9b47962801146db2cec5a9c3d22f Mon Sep 17 00:00:00 2001 From: Alexander Mozeika <106667936+AMozeika@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:31:59 +0000 Subject: [PATCH 9/9] Add files via upload This is modified version which returns only odd number of comm. K --- amozeika/comm/comm.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/amozeika/comm/comm.py b/amozeika/comm/comm.py index c017f44..088a94c 100644 --- a/amozeika/comm/comm.py +++ b/amozeika/comm/comm.py @@ -6,12 +6,14 @@ def comm(N,delta,A,P): n = N r = 0 Prob = 0.0 + m = 0 while Prob < delta: K_1 = K n_1 = n r_1 = r Prob_1 = Prob - K = K + 1 + m = m +1 + K = 2*m + 1 n = N // K r = N % K if 0 < r: