openModeller  Version 1.4.0
rules_logit.cpp
Go to the documentation of this file.
00001 
00036 #include <openmodeller/Random.hh>
00037 #include <math.h>
00038 #include <string.h>
00039 
00040 #include <openmodeller/Log.hh>
00041 #include <openmodeller/Sample.hh>
00042 
00043 #include "rules_logit.hh"
00044 #include "regression.hh"
00045 
00046 const double coeficientThreshold = 0.05;
00047 
00048 // ==========================================================================
00049 //  LogitRule implelentation
00050 // ==========================================================================
00051 LogitRule::LogitRule() : 
00052   GarpRule() 
00053 { }
00054 
00055 LogitRule::LogitRule(int numGenes) : 
00056   GarpRule(numGenes) 
00057 { }
00058 
00059 LogitRule::LogitRule(Scalar prediction, int numGenes, 
00060                      const Sample& chrom1, const Sample& chrom2, 
00061                      const double * performances) : 
00062   GarpRule(prediction, numGenes, chrom1, chrom2, performances) 
00063 {}
00064 
00065 // ==========================================================================
00066 LogitRule::~LogitRule() {}
00067 
00068 // ==========================================================================
00069 void LogitRule::initialize(const Regression& reg)
00070 {
00071   int i, j;
00072   Random rnd;
00073   
00074   for (i = 0; i < _numGenes; i++)
00075     {
00076       j = rnd.get(_numGenes);
00077       
00078       // decide where the constant (a) will go;
00079       _chrom1[j] = reg.getB()[j]; 
00080       _chrom2[j] = reg.getC()[j];
00081     }  
00082 }
00083 
00084 // ==========================================================================
00085 bool LogitRule::applies(const Sample& sample) const
00086 {
00087   return (getStrength(sample) == 1); 
00088 }
00089 
00090 // ==========================================================================
00091 int LogitRule::getStrength(const Sample& sample) const
00092 { 
00093   Scalar sum = 0.0;
00094   Scalar prob = 0.0;
00095 
00096   //static SExprType< PLUS<  <TIMES<_2<Sample>, _1<Sample> ,TIMES< _3<Sample>, SQR<_1<Sample> > > > > > >::type expr;
00097   //sum = expr(sample)(_chrom1)(_chrom2);
00098   //sum = (sample * _chrom1) + (sample * sqr(_chrom2));
00099   
00100   Sample::const_iterator si = sample.begin();
00101   Sample::const_iterator end = sample.end();
00102   Sample::const_iterator c1i = _chrom1.begin();
00103   Sample::const_iterator c2i = _chrom2.begin();
00104   
00105   while (si != end)
00106     {
00107       if (!equalEps( (*c1i), -1.0 ) )
00108   {
00109     Scalar c2i2 = (*c2i); c2i2 *= c2i2;
00110     sum += ( (*si) * (*c1i) ) + ( (*si) * c2i2 );
00111   }
00112 
00113       ++si; ++c1i; ++c2i;
00114     }
00115   
00116   prob = 1.0 / (1.0 + (double) exp(-sum));
00117 
00118   return (prob >= 0.5);
00119 }
00120 
00121 // ==========================================================================
00122 bool LogitRule::similar(const GarpRule * rule) const
00123 {
00124   Scalar thisGene, otherGene;
00125   
00126   if (type() != rule->type())
00127     { return false; }
00128 
00129   // cast to LogitRule to gain access to private data members
00130   LogitRule * otherRule = (LogitRule *) rule;
00131   
00132   // check rule value (presence/absence)
00133   if (_prediction != otherRule->_prediction) 
00134     { return false; }
00135   
00136   // rules are similar if they share the same relevant coeficients, 
00137   // i.e., abs(gene) > 0.05.
00138   for (int k = 0; k < _numGenes; k++)
00139     { 
00140       thisGene  = fabs(_chrom1[k]);
00141       otherGene = fabs(otherRule->_chrom1[k]);
00142 
00143       if ( ( (thisGene < coeficientThreshold) && (otherGene > coeficientThreshold) ) ||
00144      ( (thisGene > coeficientThreshold) && (otherGene < coeficientThreshold) ) ) 
00145   {
00146         return false;
00147   }
00148     }
00149   
00150   return true;
00151 }
00152 
00153 // ==========================================================================
00154 void LogitRule::log()
00155 {
00156   Log::instance()->info( "Logit: " );
00157 
00158   for (int i = 0; i < _numGenes; ++i)
00159     {
00160       if (fabs(_chrom1[i]) + fabs(_chrom2[i]) <= coeficientThreshold)
00161   Log::instance()->info( "****** ****** ");
00162       else
00163         Log::instance()->info( "%+6.2f %+6.2f ", _chrom1[i], _chrom2[i] );
00164     }
00165 
00166   Log::instance()->info( "- (%.2f) : %f\n", _prediction, getPerformance(PerfSig));
00167 }
00168 
00169 // ==========================================================================