psycho-skin-accuracyEstimator.C
00001 #include "psycho-skin-accuracyEstimator.h"
00002 #include <vector>
00003 #include <iostream>
00004 #include <string>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <sstream>
00008 #include <time.h>
00009 #include <math.h>
00010
00011 using namespace std ;
00012
00013 IntString::IntString( int* m , int s)
00014 {
00015 mems = new int[s] ;
00016 for(int i = 0 ; i < s ; i++ ) mems[i] = *(m + i) ;
00017 theSize = s ;
00018 degree = -1 ;
00019 }
00020
00021
00022 IntString::~IntString()
00023
00024 {
00025 delete[] mems;
00026 }
00027
00028
00029
00030 inline int IntString::getDegree(){
00031 if (degree == -1 ){
00032 degree = 0 ;
00033 for(int i = 0 ; i < theSize ; i++)
00034 degree += *(mems + i) ;
00035 }
00036 return degree ;
00037 }
00038
00039 inline int IntString::size(){
00040 return theSize ;
00041 }
00042
00043 inline int IntString::getElementAt(int i) {
00044
00045 return *(mems + i) ;
00046 }
00047
00048
00049 inline void IntString::set(int pos , int val){
00050 *(mems + pos) = val ;
00051 }
00052
00053 std::string IntString::toString(){
00054 string s("") ;
00055 for(int i = 0 ; i < theSize ; i++) {
00056 int m = *(mems + i) ;
00057 s += stringify(m) ;
00058 }
00059 return s ;
00060 }
00061
00062 string IntString::stringify(int i)
00063 {
00064 ostringstream o ;
00065 o << i ;
00066 return o.str();
00067 }
00068
00069
00070 IntString* getTPS(IntString& testIntStr , IntString& repIntStr){
00071 int s =testIntStr.size() ;
00072 int* a = new int[s] ;
00073 IntString* pps = new IntString(a , s);
00074 for(int i = 0 ; i < s ; i++){
00075 if(testIntStr.getElementAt(i)==1 && repIntStr.getElementAt(i) == 1)
00076 {
00077 pps->set(i,1);
00078 }else{
00079 pps->set(i,0) ;
00080 }
00081 }
00082 return pps ;
00083 }
00084
00085 inline float getAccuracyOfPPS(int d , int p , int n) {
00086
00087 return ((float)(n+d))/((float)(n+p)) ;
00088 }
00089
00090 map<float,float>* getProbabilityDistribution(IntString& tstIntStr , IntString& repIntStr){
00091 map<float , float>* m = new map<float , float>();
00092 int p = tstIntStr.getDegree();
00093 int n = tstIntStr.size() - p ;
00094 IntString* tps = getTPS(tstIntStr,repIntStr) ;
00095 int d = tps->getDegree();
00096 int p_repStr = repIntStr.getDegree() ;
00097 int n_repStr = repIntStr.size() - p_repStr ;
00098
00099 for(int dp = 0 ; dp <= d ; dp++){
00100 int powerOfPositiveProbability = p_repStr - dp ;
00101 float prbOfPositive = ((float)(p-dp))/((float)(p+n-dp)) ;
00102 float prbOfNegative = ((float)(n))/((float)(p+n-dp)) ;
00103
00104 float acc = ((float)(dp))/((float)(p)) ;
00105 int numOfPossibledps = getBinomialCoef(d,dp) ;
00106 float prb = 1.0f ;
00107 if(prbOfPositive != 0) {
00108 prb = numOfPossibledps * pow(prbOfPositive,powerOfPositiveProbability)*pow(prbOfNegative,n_repStr) ;
00109 }else{
00110 if(powerOfPositiveProbability!=0){prb = 0.0f;}
00111 }
00112 (*m)[acc] = prb;
00113 }
00114 normalizeThePDF(*m) ;
00115 return m ;
00116 }
00117
00118 map<float,float>* getCummulativeProbabilityDistribution(IntString& tstIntStr , IntString& repIntStr){
00119 map<float , float>* pdf = getProbabilityDistribution(tstIntStr,repIntStr) ;
00120 map<float , float>* cdf = new map<float , float>();
00121
00122 for (map<float,float>::iterator fit = pdf->begin(); fit != pdf->end(); ++fit) {
00123 float cp = 0.0f ;
00124 for (map<float,float>::iterator sit = pdf->begin(); sit != pdf->end(); ++sit) {
00125 if ((sit->first) <= (fit->first)) cp+= sit->second ;
00126 }
00127 (*cdf)[fit->first] = cp ;
00128 }
00129 pdf->map::~map() ;
00130 return cdf ;
00131 }
00132
00133 void normalizeThePDF(map<float,float>& pdf){
00134
00135 float sum = 0 ;
00136 for (map<float,float>::iterator it = pdf.begin(); it != pdf.end(); ++it) {
00137 sum += pdf[it->first];
00138 }
00139
00140 for (map<float,float>::iterator it = pdf.begin(); it != pdf.end(); ++it) {
00141 pdf[it->first] = pdf[it->first]/sum;
00142 }
00143 }
00144
00145 inline int getBinomialCoef(int n , int r) {
00146 return factorial(n)/(factorial(r)*factorial(n-r));
00147 }
00148
00149 inline int factorial(int n) {
00150 if(n==0 || n==1){return 1 ;}else{return n*factorial(n-1);}
00151 }
00152
00153 float getExpectedValue(map<float , float>& dist){
00154 float avrg = 0 ;
00155 for(map<float , float>::iterator it = dist.begin() ; it != dist.end() ; ++it){
00156 avrg += (it->first) * (it->second);
00157 }
00158 return avrg ;
00159 }
00160
00161 float getSTD(std::map<float , float>& dist){
00162 float avrg = getExpectedValue(dist) ;
00163 float std = 0 ;
00164 for(map<float , float>::iterator it = dist.begin() ; it != dist.end() ; ++it){
00165 std += pow(it->first-avrg,2.0f) * (it->second);
00166 }
00167
00168 return sqrt(std) ;
00169 }
00170
00171 extern "C" int main( int argc, char* args[] ){
00172 cout<<"Hi it\'s working!"<<endl ;
00173 int* a = new int [24] ;
00174 int* b = new int[24] ;
00175 for(int i = 0 ; i < 24 ; i++){
00176 if (i<7){a[i]=1;}else{a[i]=0;}
00177 if (i<6){b[i]=1;}else{b[i]=0;}
00178 }
00179 b[11]= 1 ;
00180 b[12] = 1 ;
00181 b[13] = 1;
00182 b[14] = 1 ;
00183 b[15]= 0 ;
00184 IntString* ts = new IntString(a,24) ;
00185 IntString* is = new IntString(b,24) ;
00186 IntString* tps = getTPS(*ts,*is) ;
00187
00188 cout<<tps->toString()<< " size: "<< tps->size()<<" degree:"<< tps->getDegree()<<endl ;
00189
00190 map<float,float>* pdf = getProbabilityDistribution(*ts,*is);
00191 for(map<float,float>::iterator it= pdf->begin() ; it!= pdf->end(); ++it){
00192 cout<<"a: " <<it->first << " p = " << it->second <<endl;
00193 }
00194
00195 map<float,float>* cdf = getCummulativeProbabilityDistribution(*ts,*is) ;
00196 for(map<float,float>::iterator it= cdf->begin() ; it!= cdf->end(); ++it){
00197 cout<<"a: " <<it->first << " p = " << it->second <<endl;
00198 }
00199 cout << "expected accuracy = "<<getExpectedValue(*pdf)<< " with std = "<< getSTD(*pdf)<<endl;
00200 return 0 ;
00201 }
00202