Krishna iResearch Intelligent Cloud Platform - acadpdrafts
pgood_batch_sum.cpp
1 /***************************************************************************************
2 ASFER - a ruleminer which gets rules specific to a query and executes them
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11  ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 ---------------------------------------------------------------------------------------------------
18 Copyright (C):
19 Srinivasan Kannan (alias) Ka.Shrinivaasan (alias) Shrinivas Kannan
20 Independent Open Source Developer, Researcher and Consultant
21 Ph: 9789346927, 9003082186, 9791165980
22 Open Source Products Profile(Krishna iResearch): http://sourceforge.net/users/ka_shrinivaasan
23 Personal website(research): https://sites.google.com/site/kuja27/
24 emails: ka.shrinivaasan@gmail.com, shrinivas.kannan@gmail.com, kashrinivaasan@live.com
25 ---------------------------------------------------------------------------------------------------
26 *****************************************************************************************/
27 
28 /*
29 #####################################################################################
30  Old CPP test code written in 2006 for deriving error probability of majority voting
31  and used 3 years ago in January 2010 during MSc thesis at IIT Chennai
32  for deriving error probability of majority voting
33 (Full report with results for Classification based on indegrees, TDT, Summarization, Citation graph Maxflow and
34 Interview Algorithm based on Recursive Gloss Overlap Definition Graph:
35 http://sourceforge.net/projects/acadpdrafts/files/MScThesis-Writeup-Complete.pdf/download)
36 
37 For publications:
38  1. http://arxiv.org/abs/1006.4458
39  2. http://www.nist.gov/tac/publications/2010/participant.papers/CMI_IIT.proceedings.pdf
40  and other publication drafts for majority voting BPNC circuits in https://sites.google.com/site/kuja27/
41 #####################################################################################
42 */
43 
44 /*
45 Updated draft for Majority Voting Error Probability based on hypergeometric functions has been uploaded at:
46 1.https://sites.google.com/site/kuja27/CircuitForComputingErrorProbabilityOfMajorityVoting_2014.pdf?attredirects=0&d=1
47 (and)
48 2.https://sites.google.com/site/kuja27/CircuitForComputingErrorProbabilityOfMajorityVoting_2014.tex?attredirects=0&d=1
49 
50 Special case of convergence of the series when p=0.5
51 ----------------------------------------------------
52 P(good) = (2n)!/(4^n) { 1/(n+1)!(n-1)! + 1/(n+2)!(n-2)! + ... + 1/(n+n)!(n-n)!}
53 has been derived and shown to be 0.5 in the handwritten notes uploaded at:
54 http://sourceforge.net/p/asfer/code/HEAD/tree/cpp-src/miscellaneous/MajorityVotingErrorProbabilityConvergence.JPG
55 But when the individual terms above differ in exponents of the probability terms (i.e there is no uniformity) ,the convergence has to be established only through hypergeometric functions.
56 
57 Special case of convergence of the series when p=1:
58 ---------------------------------------------------
59 1= 0 + 0 + 0 + ...+ (2n)C(cn) (1)^n (0)^(0) = 1
60 Thus with zero error both pseudorandom choice and majority vote yield P(good)= 100%.
61 */
62 
63 using namespace std;
64 #include <iostream>
65 #include <fstream>
66 
67 extern "C"
68 {
69 #include <stdio.h>
70 }
71 
72 long double factorial(long double);
73 long double power_of_4(long double);
74 long double compute_batch_sum(long double*);
75 
76 long double compute_factorial_fraction(long double n, long double i);
77 
78 int main()
79 {
80 /*
81 P(good) = (2n)!/(4^n) { 1/(n+1)!(n-1)! + 1/(n+2)!(n-2)! + ... + 1/(n+n)!(n-n)!}
82 */
83  long double n = 0.0, i=0.0;
84  int s=0;
85  int k=0;
86  long double prevcheckpoint=0.0;
87  long double prevsum = 0.0, sum = 0.0, prevsumdiff = 0.0, sumdiff = 0.0, term1 = 0.0;
88  long double sum_batched[3000];
89  for(k=0; k < 3000;k++)
90  {
91  sum_batched[k]=0.0;
92  }
93 
94  for(n = 8185.0; n <= 30000.0; n++)
95  {
96  term1 = factorial(2*n) / power_of_4(n);
97  do
98  {
99  for (i=prevcheckpoint+1.0; i <= prevcheckpoint+50.0; ++i)
100  {
101  if(i > n)
102  break;
103  //cout<<"n+i = "<<n+i<<endl;
104  //sum = sum + (1.0 / (factorial(n+i) * factorial(n-i)));
105  //sum = sum + ((factorial(2*n) / factorial(n+i)) / factorial(n-i));
106 
107  //sum = sum + ((factorial(2*n) / (factorial(n+i)) / factorial(n-i)));
108  sum = sum + compute_factorial_fraction(n,i);
109 
110  //sum = term1 * sum;
111  }
112  sum_batched[s++]=sum;
113  sum=0.0;
114  //cout<<"sum_batched["<<s<<"] = "<<sum_batched[s]<<endl;
115  prevcheckpoint=i;
116  //cout<<"n="<<n<<",i="<<i<<",prevcheckpoint="<<prevcheckpoint<<endl;
117  }
118  while(i <= n);
119  prevcheckpoint=0.0;
120  sum=compute_batch_sum(sum_batched);
121  cout << "sum = "<<sum<<endl;
122  cout << "Probability of good choice for population of " << 2*n << "=" << (sum / power_of_4(n))*100.0 <<endl;
123  sumdiff = sum - prevsum;
124  cout << "prob - prevprob = " << sumdiff << endl;
125  cout << "Convergence test: (sum - prevsum)/prevsum = " << sumdiff/prevsum << endl;
126  prevsum = sum;
127  prevsumdiff = sumdiff;
128  sum =0.0;
129  for(k=0; k < 3000;k++)
130  {
131  sum_batched[k]=0.0;
132  }
133  s=0.0;
134  }
135 
136 }
137 
138 long double compute_factorial_fraction(long double n, long double i)
139 {
140  // compute ((factorial(2*n) / (factorial(n+i)) / factorial(n-i)));
141  long double numdenom = 1.0;
142  int k=0,l=0;
143  for(k=i+1,l=i; k <= n,l < n; k++,l++)
144  {
145  numdenom *= (n+k)/(n-l);
146 
147  }
148  return numdenom;
149 }
150 
151 long double compute_batch_sum(long double* sum_batched)
152 {
153  long double sum=0.0;
154  for(int i=0; i<3000; i++)
155  {
156  if(sum_batched[i] > 0.0)
157  {
158  cout<<"compute_batch_sum(): sum_batched["<<i<<"]:"<<sum_batched[i]<<endl;
159  sum+=sum_batched[i];
160  }
161  }
162  return sum;
163 }
164 
165 long double factorial(long double n)
166 {
167  //cout<<"factorial("<<n<<")"<<endl;
168  if (n==0.0)
169  return 1.0;
170  else
171  return (long double) n*factorial(n-1);
172 }
173 
174 long double power_of_4(long double n)
175 {
176  long double power = 1.0 ;
177  long double i;
178  for (i=n;i > 0.0;i--)
179  power = power * 4.0;
180  return power;
181 }