diff --git a/test/bin/InformationMeasure.class b/test/bin/InformationMeasure.class new file mode 100644 index 0000000..cdad8c9 Binary files /dev/null and b/test/bin/InformationMeasure.class differ diff --git a/test/bin/People.class b/test/bin/People.class new file mode 100644 index 0000000..22edb64 Binary files /dev/null and b/test/bin/People.class differ diff --git a/test/bin/database_Main.class b/test/bin/database_Main.class new file mode 100644 index 0000000..bd35aa2 Binary files /dev/null and b/test/bin/database_Main.class differ diff --git a/test/info.txt b/test/info.txt new file mode 100644 index 0000000..5217de6 --- /dev/null +++ b/test/info.txt @@ -0,0 +1 @@ +567 571 741 588 592 567 571 741 588 593 567 571 741 588 595 567 571 741 589 592 567 571 741 589 593 567 571 741 589 594 567 571 741 590 594 567 571 741 591 593 567 571 742 588 592 567 571 742 588 593 567 571 742 588 594 567 571 742 589 593 567 571 742 589 594 567 571 742 589 595 567 571 742 590 593 567 571 742 590 594 567 571 742 590 595 567 571 742 591 593 567 571 742 591 594 567 571 743 589 592 567 571 743 590 593 567 571 743 590 594 567 571 744 588 593 567 571 744 588 594 567 571 744 589 593 567 571 744 589 594 567 571 744 589 595 567 571 744 590 594 567 571 744 590 595 567 571 745 588 594 567 571 745 590 594 567 571 745 591 593 567 571 746 588 594 567 571 746 589 593 567 571 746 590 593 567 571 746 590 594 567 571 746 590 595 567 572 741 591 594 567 572 742 588 593 567 572 742 588 595 567 572 742 589 593 567 572 742 590 594 567 572 742 591 595 567 572 743 589 594 567 572 744 588 592 567 572 744 588 594 567 572 744 589 594 567 572 744 590 593 567 572 744 590 594 567 572 744 590 596 567 572 744 591 592 567 572 744 591 594 567 572 744 591 595 567 572 745 591 593 567 572 746 589 592 567 572 746 589 594 567 572 746 590 594 567 572 746 590 595 567 573 741 590 594 567 573 742 588 594 567 573 742 589 593 567 573 742 589 594 567 573 742 590 592 567 573 742 590 593 567 573 742 590 595 567 573 742 591 593 567 573 743 589 593 567 573 743 589 594 567 573 743 590 592 567 573 744 588 594 567 573 744 589 592 567 573 744 589 593 567 573 744 589 594 567 573 744 590 593 567 573 744 590 594 567 573 745 589 595 567 573 745 590 593 567 573 746 588 593 567 573 746 588 594 567 573 746 588 595 567 573 746 589 593 567 573 746 589 594 567 573 746 589 595 567 573 746 590 592 567 573 746 590 593 567 573 746 590 594 567 573 746 591 592 567 574 741 588 593 567 574 741 589 594 567 574 742 588 593 567 574 742 588 594 567 574 742 589 593 567 574 742 589 594 567 574 742 590 592 567 574 742 590 594 567 574 742 591 592 567 574 742 591 593 567 574 742 591 594 567 574 744 589 593 567 574 744 589 594 567 574 744 590 593 567 574 744 590 595 567 574 744 591 593 567 574 745 588 593 567 574 745 590 593 567 574 746 589 594 567 574 746 590 593 567 574 746 590 594 567 574 746 591 593 567 575 741 589 594 567 575 741 591 593 567 575 743 590 594 567 575 744 589 594 567 575 744 590 592 567 575 744 590 593 567 575 744 591 593 567 575 744 591 594 567 575 746 590 594 567 575 746 591 593 567 740 741 588 592 567 740 742 588 593 567 740 742 589 593 567 740 742 589 594 567 740 744 591 593 568 571 741 588 592 568 571 741 588 593 568 571 741 588 594 568 571 741 588 595 568 571 741 589 592 568 571 741 589 593 568 571 741 589 594 568 571 741 590 593 568 571 741 590 595 568 571 742 588 592 568 571 742 588 593 568 571 742 588 594 568 571 742 588 595 568 571 742 589 592 568 571 742 589 593 568 571 742 589 594 568 571 742 589 595 568 571 742 590 592 568 571 742 590 593 568 571 742 590 595 568 571 742 590 596 568 571 742 591 592 568 571 742 591 594 568 571 743 588 592 568 571 743 588 593 568 571 743 588 594 568 571 743 588 595 568 571 743 589 593 568 571 743 589 594 568 571 743 590 592 568 571 743 590 593 568 571 743 590 595 568 571 744 588 593 568 571 744 588 594 568 571 744 588 595 568 571 744 589 593 568 571 744 589 594 568 571 744 589 595 568 571 744 590 593 568 571 744 590 594 568 571 744 591 593 568 571 744 591 594 568 571 746 589 593 568 571 746 590 592 568 571 746 590 594 568 572 741 588 593 568 572 741 588 595 568 572 741 589 594 568 572 741 590 593 568 572 741 590 594 568 572 741 591 593 568 572 741 591 594 568 572 742 588 593 568 572 742 588 594 568 572 742 589 594 568 572 742 589 595 568 572 742 590 593 568 572 742 590 594 568 572 742 590 595 568 572 742 591 592 568 572 742 591 594 568 572 743 588 593 568 572 743 588 594 568 572 743 589 593 568 572 743 590 594 568 572 744 588 596 568 572 744 589 593 568 572 744 589 594 568 572 744 589 595 568 572 744 590 595 568 572 744 591 593 568 572 745 589 594 568 572 745 590 592 568 572 745 590 593 568 572 745 590 594 568 572 745 591 594 568 572 746 588 593 568 572 746 589 594 568 572 746 590 593 568 572 746 590 594 568 572 746 591 593 568 572 746 591 595 568 573 741 588 593 568 573 741 588 594 568 573 741 588 595 568 573 741 589 594 568 573 741 589 595 568 573 741 590 593 568 573 741 591 594 568 573 741 591 595 568 573 742 588 593 568 573 742 588 594 568 573 742 588 595 568 573 742 589 593 568 573 742 589 594 568 573 742 589 595 568 573 742 590 592 568 573 742 590 593 568 573 742 590 594 568 573 742 591 592 568 573 743 588 595 568 573 743 589 593 568 573 743 589 594 568 573 743 589 596 568 573 743 590 593 568 573 743 590 594 568 573 744 588 592 568 573 744 588 595 568 573 744 589 592 568 573 744 589 593 568 573 744 589 594 568 573 744 590 592 568 573 744 590 593 568 573 744 590 594 568 573 744 590 595 568 573 744 591 592 568 573 744 591 594 568 573 745 590 593 568 573 745 591 593 568 573 746 589 593 568 573 746 590 593 568 573 746 590 594 568 573 746 590 595 568 573 746 591 592 568 573 746 591 593 568 573 746 591 594 568 574 741 588 593 568 574 741 589 594 568 574 741 591 592 568 574 742 589 593 568 574 742 589 594 568 574 742 590 593 568 574 742 591 593 568 574 744 590 592 568 574 744 590 593 568 574 744 590 594 568 574 744 591 594 568 574 744 591 595 568 574 745 590 593 568 574 746 590 592 568 574 746 590 593 568 574 746 590 594 568 574 746 591 594 568 575 741 591 594 568 575 742 588 592 568 575 742 590 594 568 575 742 590 595 568 575 744 590 592 568 575 744 590 593 568 575 746 590 592 568 575 746 591 593 568 740 741 590 592 568 740 742 588 593 568 740 742 589 594 568 740 742 591 592 568 740 744 588 593 568 740 744 590 592 568 740 744 590 593 568 740 744 591 593 568 740 746 590 592 568 740 746 590 594 568 740 746 591 592 569 571 743 590 594 569 572 742 588 593 569 573 742 589 594 569 573 745 591 593 570 571 741 589 593 \ No newline at end of file diff --git a/test/src/InformationMeasure.java b/test/src/InformationMeasure.java new file mode 100644 index 0000000..1f7263b --- /dev/null +++ b/test/src/InformationMeasure.java @@ -0,0 +1,40 @@ + +public class InformationMeasure +{ + private int sum; + private int freq; + private int total; + + public InformationMeasure() + { } + public InformationMeasure (int freq, int total) { + // setSum(sum); + setFrequency(freq); + setTotal(total); + } + //setter methods + //public void setSum(int s) { this.sum = s; } + public void setFrequency(int f) { this.freq = f; } + public void setTotal(int t) { this.total = t; } + + //getters Methods + //public int getSum() { return sum; } + public int getFrequency() { return freq; } + public int getTotal() { return total; } + + public double I () { + double p = probability(freq); + return -(p*log2(p)); + } + + + public double log2(double x){ return Math.log(x)/Math.log(2.0); } + + //Pr(Income| Q = X(?)) + public double probability(int frequency) + { + double f = frequency; + + return f/total; + } +} diff --git a/test/src/People.java b/test/src/People.java new file mode 100644 index 0000000..a1648a7 --- /dev/null +++ b/test/src/People.java @@ -0,0 +1,44 @@ + +public class People { + + int income,hoursStudy,gender,education,overallHealth; + int[] people = new int[291]; + + People(int gender,int income,int education,int hoursStudy,int overallHealth){ + this.income = income; + this.hoursStudy = hoursStudy; + this.gender = gender; + this.education = education; + this.overallHealth = overallHealth; + + } + + public int getGender(){ + return gender; + } + + public int getEducation(){ + return education; + } + + public int getOverallHealth(){ + return overallHealth; + } + + public int getIncome(){ + return income; + } + + public int gethoursStudy(){ + return hoursStudy; + } + + + + + + + + + +} diff --git a/test/src/database_Main.java b/test/src/database_Main.java new file mode 100644 index 0000000..cb3bee6 --- /dev/null +++ b/test/src/database_Main.java @@ -0,0 +1,247 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Scanner; + + +public class database_Main { + //total participants + static int total; + + //frequencies + static int[] count_Edu = new int[6]; + static int[] count_Gen = new int[4]; + static int[] count_Hel = new int[5]; + static int[] count_Sty = new int[4]; + static int[] count_Inc = new int[6]; + static int[] count_Min_Gen = new int[16]; + static int[] count_Min_Sty = new int[23]; + static int[] count_Min_Hel = new int[27]; + + static InformationMeasure InfoM = new InformationMeasure(); + + static double EntropyOfGen; + static double EntropyOfEdu; + static double EntropyOfHel; + static double EntropyOfSty; + + public static void main(String args[]) throws FileNotFoundException + { + File input = new File("info.txt"); + People[] newPerson = new People[291]; + Scanner scn = new Scanner(input); + + int count = 0; + + while(scn.hasNext()) + newPerson[count++] = new People(scn.nextInt(), scn.nextInt(), scn.nextInt(), scn.nextInt(), scn.nextInt()); + + scn.close(); + + ArrayList edu = new ArrayList(); + ArrayList gen = new ArrayList(); + ArrayList hel = new ArrayList(); + ArrayList sty = new ArrayList(); + ArrayList inc = new ArrayList(); + + int count_Edu_Count = 0, count_Inc_Count = 0, count_Gen_Count = 0, count_Hel_Count = 0, count_Sty_Count = 0; + + for (int counter = 0; counter < 291; counter++) + { + //initialize varibles gets the first element and so on + String n = Integer.toString(newPerson[counter].getEducation()); + String a = Integer.toString(newPerson[counter].getGender()); + String b = Integer.toString(newPerson[counter].getOverallHealth()); + String c = Integer.toString(newPerson[counter].gethoursStudy()); + String in = Integer.toString(newPerson[counter].getIncome()); + + //if the element is not in the arylist + if(!edu.contains(n)) + { + count = 0; + //traverse through the list + for (int i = 0; i < 291; i++) + if (Integer.toString(newPerson[i].getEducation()).equals(n)) + count++; + + count_Edu[count_Edu_Count++] = count; + edu.add(n); + //System.out.println(n + " - " + count); + }//if edu + + if(!inc.contains(in)) + { + count = 0; + //traverse through the list + for (int i = 0; i < 291; i++) + if (Integer.toString(newPerson[i].getIncome()).equals(in)) + count++; + + count_Inc[count_Inc_Count++] = count; + inc.add(in); + //System.out.println(in + " - " + count); + }//if inc + + if(!gen.contains(a)) + { + count = 0; + for(int i = 0;i<291;i++) + if(Integer.toString(newPerson[i].getGender()).equals(a)) + count++; + + count_Gen[count_Gen_Count++] = count; + gen.add(a); + // System.out.println(a + " - " + count); + }//if gen + + if(!hel.contains(b)){ + count = 0; + for(int i = 0;i<291;i++) + if(Integer.toString(newPerson[i].getOverallHealth()).equals(b)) + count++; + + count_Hel[count_Hel_Count++] = count; + hel.add(b); + // System.out.println(b + " - " + count); + + }//if hel + + if(!sty.contains(c)){ + count = 0; + for(int i = 0;i<291;i++) + if(Integer.toString(newPerson[i].gethoursStudy()).equals(c)) + count++; + + count_Sty[count_Sty_Count++] = count; + sty.add(c); + //System.out.println(c + " - " + count); + + }//if sty + + }//end for loop + for(int i = 0; i < count_Edu.length; i++) + total += count_Edu[i]; + + //Entropy for gender + for(int i = 0; i < count_Gen.length; i++) + EntropyOfGen += (InfoMeasure( count_Gen[i], total) * probability(count_Gen[i], total)); + System.out.println("Conditional Entropy for gender: "+ EntropyOfGen + "\n--------------------"); + + //Entropy for Education + for(int i = 0; i < count_Edu.length; i++) + EntropyOfEdu += (InfoMeasure( count_Edu[i], total) * probability(count_Edu[i], total)); + System.out.println("Conditional Entropy for education: "+ EntropyOfEdu + "\n--------------------"); + + //Entropy for Overall health + for(int i = 0; i < count_Hel.length; i++) + EntropyOfHel += (InfoMeasure( count_Hel[i], total) * probability(count_Hel[i], total)); + System.out.println("Conditional Entropy for overall health: "+ EntropyOfHel + "\n--------------------"); + + //Entropy for Work/Study hours + for(int i = 0; i < count_Sty.length; i++) + EntropyOfSty += (InfoMeasure( count_Sty[i], total) * probability(count_Sty[i], total)); + System.out.println("Conditional Entropy for work/study hours: "+ EntropyOfSty + "\n" + "------------------" ); + + System.out.println("the min is:"+findMin(EntropyOfSty,EntropyOfEdu,EntropyOfHel,EntropyOfGen)); + System.out.println("----------------"); + + //calc row 2 of I + ArrayList min_Gen = new ArrayList<>(); + ArrayList min_Sty = new ArrayList<>(); + ArrayList min_Hel = new ArrayList<>(); + + int count_min_Gen = 0,count_min_Hel=0,count_min_Sty = 0; + + for (int counter = 0; counter < 291; counter++){ + String n = newPerson[counter].getEducation() + " " + newPerson[counter].getGender(); + String a = newPerson[counter].getEducation() + " " + newPerson[counter].gethoursStudy(); + String b = newPerson[counter].getEducation() + " " + newPerson[counter].getOverallHealth(); + + if(!min_Gen.contains(n)){ + int count1 = 0; + for (int i = 0; i < 291; i++){ + String z = newPerson[counter].getEducation() + " " + newPerson[counter].getGender(); + if (z.equals(n)) + count1++; + + } + count_Min_Gen[count_min_Gen++] = count1; + min_Gen.add(n); + //System.out.println(n + " - " + count1); + + } + + + if(!min_Hel.contains(b)){ + int count1 = 0; + for (int i = 0; i < 291; i++){ + String z = newPerson[counter].getEducation() + " " + newPerson[counter].getOverallHealth(); + if (z.equals(b)) + count1++; + } + count_Min_Hel[count_min_Hel++] = count1; + min_Hel.add(b); + // System.out.println(b + " - " + count1); + + } + + if(!min_Sty.contains(a)){ + int count1 = 0; + for (int i = 0; i < 291; i++){ + String z = newPerson[counter].getEducation() + " " + newPerson[counter].gethoursStudy(); + if (z.equals(a)) + count1++; + + } + count_Min_Sty[count_min_Sty++] = count1; + min_Sty.add(a); + //System.out.println(a + " - " + count1); + + } + }//for + //System.out.println(min_Sty.size()); + + + + + /** + double EntropyOfMinGen = 0 ; + for(int i = 0; i < count_Gen.length;i++){ + for(int j = 0; i < count_Min_Edu.length; i++) + EntropyOfMinGen += (InfoMeasure(count_Min_Edu[i], count_Gen[j]) * probability(count_Min_Edu[i], total)); + System.out.println("Conditional Entropy for Gen_Edu: "+ EntropyOfMinGen + "\n--------------------"); + + } + **/ + } + + //Pr(Q) + public static double probability(int frequency, int tot) + { + double f = frequency; + double t = tot; + + return f/t; + } + + public static double InfoMeasure( int f, int t) + { + + InfoM.setFrequency(f); + InfoM.setTotal(t); + + System.out.println(f + "\t--\t" + InfoM.I()); + + return InfoM.I(); + } + + //finds minimum returns value + public static double findMin(double... vals) { + double min = Double.POSITIVE_INFINITY; + + for (double d : vals) + if (d < min) min = d; + + return min; + } +}