Krishna iResearch Intelligent Cloud Platform - acadpdrafts
pgood.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 
75 int main()
76 {
77 /*
78 P(good) = (2n)!/(4^n) { 1/(n+1)!(n-1)! + 1/(n+2)!(n-2)! + ... + 1/(n+n)!(n-n)!}
79 */
80  long double n = 0.0, i=0.0;
81  long double prevsum = 0.0, sum = 0.0, prevsumdiff = 0.0, sumdiff = 0.0, term1 = 0.0;
82  for(n = 0.0; n <= 30000.0; n++)
83  {
84  term1 = factorial(2*n) / power_of_4(n);
85  for (i=1.0; i <= n ; i++)
86  sum = sum + (1.0 / (factorial(n+i) * factorial(n-i)));
87  sum = term1 * sum;
88  cout << "Probability of good choice for population of " << 2*n << "=" << sum*100.0 <<endl;
89  sumdiff = sum - prevsum;
90  cout << "prob - prevprob = " << sumdiff << endl;
91  cout << "Convergence test: (sum - prevsum)/prevsum = " << sumdiff/prevsum << endl;
92  prevsum = sum;
93  prevsumdiff = sumdiff;
94  sum =0.0;
95  }
96 
97 }
98 
99 
100 long double factorial(long double n)
101 {
102  if (n==0.0)
103  return 1.0;
104  else
105  return (long double) n*factorial(n-1);
106 }
107 
108 long double power_of_4(long double n)
109 {
110  long double power = 1.0 ;
111  long double i;
112  for (i=n;i > 0.0;i--)
113  power = power * 4.0;
114  return power;
115 }