29 using namespace Eigen;
32 using namespace shogun;
43 return sum/values.
vlen;
74 result=values[median];
80 if (values[low]>values[high])
81 CMath::CMath::swap(values[low], values[high]);
82 result=values[median];
87 if (values[middle]>values[high])
88 CMath::swap(values[middle], values[high]);
89 if (values[low]>values[high])
90 CMath::swap(values[low], values[high]);
91 if (values[middle]>values[low])
92 CMath::swap(values[middle], values[low]);
94 CMath::swap(values[middle], values[low+1]);
102 while (values[low]>values[l]);
105 while (values[h]>values[low]);
108 CMath::swap(values[l], values[h]);
111 CMath::swap(values[low], values[h]);
139 for (i=1; i<values.
vlen; i++)
154 for (i=0; i<values.
vlen; i++)
159 if (values[i]>maxltguess)
160 maxltguess=values[i];
162 else if (values[i]>guess)
165 if (values[i]<mingtguess)
166 mingtguess=values[i];
171 if (less<=(values.
vlen+1)/2&&greater<=(values.
vlen+1)/2)
173 else if (less>greater)
179 if (less>=(values.
vlen+1)/2)
181 else if (less+equal>=(values.
vlen+1)/2)
191 result=median(copy,
true);
199 bool modify,
bool in_place)
206 return median(as_vector, modify, in_place);
215 float64_t mean=CStatistics::mean(values);
219 sum_squared_diff+=CMath::pow(values.
vector[i]-mean, 2);
221 return sum_squared_diff/(values.
vlen-1);
240 result[j]+=values(i,j);
252 result[j]+=values(j,i);
280 result[j]+=CMath::pow(values(i,j)-mean[j], 2);
292 result[j]+=CMath::pow(values(j,i)-mean[j], 2);
303 return CMath::sqrt(variance(values));
311 var[i]=CMath::sqrt(var[i]);
335 centered,
true,
false, 1.0/(observations.
num_rows-1));
351 int32_t deg=values.
vlen-1;
354 float64_t t=CMath::abs(inverse_student_t(deg, alpha));
357 float64_t std_dev=CStatistics::std_deviation(values);
358 float64_t mean=CStatistics::mean(values);
362 conf_int_low=mean-interval;
363 conf_int_up=mean+interval;
376 if (!(k>0 && greater(p, 0)) && less(p, 1))
378 SG_SERROR(
"CStatistics::inverse_student_t_distribution(): "
382 if (greater(p, 0.25) && less(p, 0.75))
390 z=inverse_incomplete_beta(0.5, 0.5*rk, CMath::abs(z));
391 t=CMath::sqrt(rk*z/(1.0-z));
400 if (greater_equal(p, 0.5))
405 z=inverse_incomplete_beta(0.5*rk, 0.5, 2.0*p);
406 if (less(CMath::MAX_REAL_NUMBER*z, rk))
408 result=rflg*CMath::MAX_REAL_NUMBER;
411 t=CMath::sqrt(rk/z-rk);
443 int32_t breaknewtcycle;
444 int32_t breakihalvecycle;
448 if (!(greater_equal(y, 0) && less_equal(y, 1)))
450 SG_SERROR(
"CStatistics::inverse_incomplete_beta(): "
510 if (less_equal(a, 1.0) || less_equal(b, 1.0))
518 yyy=incomplete_beta(aaa, bbb, x);
526 yp=-inverse_normal_cdf(y);
543 x=2.0/(1.0/(2.0*aaa-1.0)+1.0/(2.0*bbb-1.0));
544 d=yp*CMath::sqrt(x+lgm)/x
545 -(1.0/(2.0*bbb-1.0)-1.0/(2.0*aaa-1.0))
546 *(lgm+5.0/6.0-2.0/(3.0*x));
548 if (less(d, CMath::log(CMath::MIN_REAL_NUMBER)))
553 x=aaa/(aaa+bbb*CMath::exp(d));
554 yyy=incomplete_beta(aaa, bbb, x);
556 if (less(CMath::abs(yp), 0.2))
568 if (mainlooppos==ihalve)
573 mainlooppos=ihalvecycle;
580 if (mainlooppos==ihalvecycle)
589 x=1.0-CMath::MACHINE_EPSILON;
600 yyy=incomplete_beta(aaa, bbb, x);
602 if (less(CMath::abs(yp), dithresh))
608 if (less(CMath::abs(yp), dithresh))
627 di=1.0-(1.0-di)*(1.0-di);
642 if (greater(x0, 0.75))
659 yyy=incomplete_beta(aaa, bbb, x);
671 if (rflg==1 && less(x1, CMath::MACHINE_EPSILON))
703 mainlooppos=ihalvecycle;
708 mainlooppos=breakihalvecycle;
716 if (mainlooppos==breakihalvecycle)
718 if (greater_equal(x0, 1.0))
720 x=1.0-CMath::MACHINE_EPSILON;
723 if (less_equal(x, 0.0))
735 if (mainlooppos==newt)
742 lgm=lgamma(aaa+bbb)-lgamma(aaa)-lgamma(bbb);
744 mainlooppos=newtcycle;
751 if (mainlooppos==newtcycle)
757 yyy=incomplete_beta(aaa, bbb, x);
766 if (greater(yyy, yh))
785 if (equal(x, 1.0) || equal(x, 0.0))
787 mainlooppos=breaknewtcycle;
790 d=(aaa-1.0)*CMath::log(x)+(bbb-1.0)*CMath::log(1.0-x)+lgm;
791 if (less(d, CMath::log(CMath::MIN_REAL_NUMBER)))
795 if (greater(d, CMath::log(CMath::MAX_REAL_NUMBER)))
797 mainlooppos=breaknewtcycle;
803 if (less_equal(xt, x0))
806 xt=x0+0.5*yyy*(x-x0);
807 if (less_equal(xt, 0.0))
809 mainlooppos=breaknewtcycle;
813 if (greater_equal(xt, x1))
816 xt=x1-0.5*yyy*(x1-x);
817 if (greater_equal(xt, 1.0))
819 mainlooppos=breaknewtcycle;
824 if (less(CMath::abs(d/x), 128.0*CMath::MACHINE_EPSILON))
829 mainlooppos=newtcycle;
834 mainlooppos=breaknewtcycle;
842 if (mainlooppos==breaknewtcycle)
844 dithresh=256.0*CMath::MACHINE_EPSILON;
855 if (less_equal(x, CMath::MACHINE_EPSILON))
857 x=1.0-CMath::MACHINE_EPSILON;
882 big=4.503599627370496e15;
883 biginv=2.22044604925031308085e-16;
884 maxgam=171.624376956302725;
885 minlog=CMath::log(CMath::MIN_REAL_NUMBER);
886 maxlog=CMath::log(CMath::MAX_REAL_NUMBER);
888 if (!(greater(a, 0) && greater(b, 0)))
890 SG_SERROR(
"CStatistics::incomplete_beta(): "
894 if (!(greater_equal(x, 0) && less_equal(x, 1)))
896 SG_SERROR(
"CStatistics::incomplete_beta(): "
911 if (less_equal(b*x, 1.0) && less_equal(x, 0.95))
913 result=ibetaf_incompletebetaps(a, b, x, maxgam);
917 if (greater(x, a/(a+b)))
930 if ((flag==1 && less_equal(b*x, 1.0)) && less_equal(x, 0.95))
932 t=ibetaf_incompletebetaps(a, b, x, maxgam);
933 if (less_equal(t, CMath::MACHINE_EPSILON))
935 result=1.0-CMath::MACHINE_EPSILON;
943 y=x*(a+b-2.0)-(a-1.0);
946 w=ibetaf_incompletebetafe(a, b, x, big, biginv);
950 w=ibetaf_incompletebetafe2(a, b, x, big, biginv)/xc;
954 if ((less(a+b, maxgam) && less(CMath::abs(y), maxlog))
955 && less(CMath::abs(t), maxlog))
958 t=t*CMath::pow(x, a);
961 t=t*(tgamma(a+b)/(tgamma(a)*tgamma(b)));
964 if (less_equal(t, CMath::MACHINE_EPSILON))
966 result=1.0-CMath::MACHINE_EPSILON;
979 y=y+t+lgamma(a+b)-lgamma(a)-lgamma(b);
991 if (less_equal(t, CMath::MACHINE_EPSILON))
993 t=1.0-CMath::MACHINE_EPSILON;
1007 return inverse_normal_cdf(y0)*std_dev+mean;
1029 expm2=0.13533528323661269189;
1030 s2pi=2.50662827463100050242;
1031 if (less_equal(y0, 0))
1033 result=-CMath::MAX_REAL_NUMBER;
1036 if (greater_equal(y0, 1))
1038 result=CMath::MAX_REAL_NUMBER;
1043 if (greater(y, 1.0-expm2))
1048 if (greater(y, expm2))
1052 p0=-59.9633501014107895267;
1053 p0=98.0010754185999661536+y2*p0;
1054 p0=-56.6762857469070293439+y2*p0;
1055 p0=13.9312609387279679503+y2*p0;
1056 p0=-1.23916583867381258016+y2*p0;
1058 q0=1.95448858338141759834+y2*q0;
1059 q0=4.67627912898881538453+y2*q0;
1060 q0=86.3602421390890590575+y2*q0;
1061 q0=-225.462687854119370527+y2*q0;
1062 q0=200.260212380060660359+y2*q0;
1063 q0=-82.0372256168333339912+y2*q0;
1064 q0=15.9056225126211695515+y2*q0;
1065 q0=-1.18331621121330003142+y2*q0;
1071 x=CMath::sqrt(-2.0*CMath::log(y));
1072 x0=x-CMath::log(x)/x;
1076 p1=4.05544892305962419923;
1077 p1=31.5251094599893866154+z*p1;
1078 p1=57.1628192246421288162+z*p1;
1079 p1=44.0805073893200834700+z*p1;
1080 p1=14.6849561928858024014+z*p1;
1081 p1=2.18663306850790267539+z*p1;
1082 p1=-1.40256079171354495875*0.1+z*p1;
1083 p1=-3.50424626827848203418*0.01+z*p1;
1084 p1=-8.57456785154685413611*0.0001+z*p1;
1086 q1=15.7799883256466749731+z*q1;
1087 q1=45.3907635128879210584+z*q1;
1088 q1=41.3172038254672030440+z*q1;
1089 q1=15.0425385692907503408+z*q1;
1090 q1=2.50464946208309415979+z*q1;
1091 q1=-1.42182922854787788574*0.1+z*q1;
1092 q1=-3.80806407691578277194*0.01+z*q1;
1093 q1=-9.33259480895457427372*0.0001+z*q1;
1098 p2=3.23774891776946035970;
1099 p2=6.91522889068984211695+z*p2;
1100 p2=3.93881025292474443415+z*p2;
1101 p2=1.33303460815807542389+z*p2;
1102 p2=2.01485389549179081538*0.1+z*p2;
1103 p2=1.23716634817820021358*0.01+z*p2;
1104 p2=3.01581553508235416007*0.0001+z*p2;
1105 p2=2.65806974686737550832*0.000001+z*p2;
1106 p2=6.23974539184983293730*0.000000001+z*p2;
1108 q2=6.02427039364742014255+z*q2;
1109 q2=3.67983563856160859403+z*q2;
1110 q2=1.37702099489081330271+z*q2;
1111 q2=2.16236993594496635890*0.1+z*q2;
1112 q2=1.34204006088543189037*0.01+z*q2;
1113 q2=3.28014464682127739104*0.0001+z*q2;
1114 q2=2.89247864745380683936*0.000001+z*q2;
1115 q2=6.79019408009981274425*0.000000001+z*q2;
1147 z=CMath::MACHINE_EPSILON*ai;
1148 while (greater(CMath::abs(v), z))
1159 if (less(a+b, maxgam)
1160 && less(CMath::abs(u), CMath::log(CMath::MAX_REAL_NUMBER)))
1162 t=tgamma(a+b)/(tgamma(a)*tgamma(b));
1163 s=s*t*CMath::pow(x, a);
1167 t=lgamma(a+b)-lgamma(a)-lgamma(b)+u+CMath::log(s);
1168 if (less(t, CMath::log(CMath::MIN_REAL_NUMBER)))
1221 thresh=3.0*CMath::MACHINE_EPSILON;
1224 xk=-x*k1*k2/(k3*k4);
1238 if (not_equal(qk, 0))
1242 if (not_equal(r, 0))
1244 t=CMath::abs((ans-r)/r);
1251 if (less(t, thresh))
1263 if (greater(CMath::abs(qk)+CMath::abs(pk), big))
1270 if (less(CMath::abs(qk), biginv) || less(CMath::abs(pk), biginv))
1326 thresh=3.0*CMath::MACHINE_EPSILON;
1329 xk=-z*k1*k2/(k3*k4);
1343 if (not_equal(qk, 0))
1347 if (not_equal(r, 0))
1349 t=CMath::abs((ans-r)/r);
1356 if (less(t, thresh))
1368 if (greater(CMath::abs(qk)+CMath::abs(pk), big))
1375 if (less(CMath::abs(qk), biginv) || less(CMath::abs(pk), biginv))
1398 igammaepsilon=0.000000000000001;
1399 if (less_equal(x, 0) || less_equal(a, 0))
1404 if (greater(x, 1) && greater(x, a))
1406 result=1-incomplete_gamma_completed(a, x);
1409 ax=a*CMath::log(x)-x-lgamma(a);
1410 if (less(ax, -709.78271289338399))
1425 while (greater(c/ans, igammaepsilon));
1451 igammaepsilon=0.000000000000001;
1452 igammabignumber=4503599627370496.0;
1453 igammabignumberinv=2.22044604925031308085*0.0000000000000001;
1454 if (less_equal(x, 0) || less_equal(a, 0))
1459 if (less(x, 1) || less(x, a))
1461 result=1-incomplete_gamma(a, x);
1464 ax=a*CMath::log(x)-x-lgamma(a);
1465 if (less(ax, -709.78271289338399))
1487 if (not_equal(qk, 0))
1490 t=CMath::abs((ans-r)/r);
1501 if (greater(CMath::abs(pk), igammabignumber))
1503 pkm2=pkm2*igammabignumberinv;
1504 pkm1=pkm1*igammabignumberinv;
1505 qkm2=qkm2*igammabignumberinv;
1506 qkm1=qkm1*igammabignumberinv;
1509 while (greater(t, igammaepsilon));
1517 return incomplete_gamma(a, x/b);
1525 return inverse_incomplete_gamma_completed(a, 1-p)*b;
1546 igammaepsilon=0.000000000000001;
1547 iinvgammabignumber=4503599627370496.0;
1548 x0=iinvgammabignumber;
1552 dithresh=5*igammaepsilon;
1554 y=1-d-inverse_normal_cdf(y0)*CMath::sqrt(d);
1560 if (greater(x, x0) || less(x, x1))
1565 y=incomplete_gamma_completed(a, x);
1566 if (less(y, yl) || greater(y, yh))
1581 d=(a-1)*CMath::log(x)-x-lgm;
1582 if (less(d, -709.78271289338399))
1589 if (less(CMath::abs(d/x), igammaepsilon))
1597 if (equal(x0, iinvgammabignumber))
1599 if (less_equal(x, 0))
1603 while (equal(x0, iinvgammabignumber))
1606 y=incomplete_gamma_completed(a, x);
1622 y=incomplete_gamma_completed(a, x);
1623 lgm=(x0-x1)/(x1+x0);
1624 if (less(CMath::abs(lgm), dithresh))
1629 if (less(CMath::abs(lgm), dithresh))
1633 if (less_equal(x, 0.0))
1637 if (greater_equal(y, y0))
1689 return 0.5*(error_function(x/std_dev/1.41421356237309504880)+1);
1699 float64_t s=1.0-1.0/x2*(1.0-3.0/x2*(1.0-5.0/x2*(1.0-7.0/x2)));
1700 result=-0.5*CMath::log(2*CMath::PI)-x2*0.5-CMath::log(-x)+CMath::log(s);
1703 result=CMath::log(normal_cdf(x));
1721 p=0.007547728033418631287834;
1722 p=0.288805137207594084924010+xsq*p;
1723 p=14.3383842191748205576712+xsq*p;
1724 p=38.0140318123903008244444+xsq*p;
1725 p=3017.82788536507577809226+xsq*p;
1726 p=7404.07142710151470082064+xsq*p;
1727 p=80437.3630960840172832162+xsq*p;
1729 q=1.00000000000000000000000+xsq*q;
1730 q=38.0190713951939403753468+xsq*q;
1731 q=658.070155459240506326937+xsq*q;
1732 q=6379.60017324428279487120+xsq*q;
1733 q=34216.5257924628539769006+xsq*q;
1734 q=80437.3630960840172826266+xsq*q;
1735 result=s*1.1283791670955125738961589031*x*p/q;
1738 if (greater_equal(x, 10))
1743 result=s*(1-error_function_complement(x));
1755 result=2-error_function_complement(-x);
1760 result=1.0-error_function(x);
1763 if (greater_equal(x, 10))
1769 p=0.5641877825507397413087057563+x*p;
1770 p=9.675807882987265400604202961+x*p;
1771 p=77.08161730368428609781633646+x*p;
1772 p=368.5196154710010637133875746+x*p;
1773 p=1143.262070703886173606073338+x*p;
1774 p=2320.439590251635247384768711+x*p;
1775 p=2898.0293292167655611275846+x*p;
1776 p=1826.3348842295112592168999+x*p;
1778 q=17.14980943627607849376131193+x*q;
1779 q=137.1255960500622202878443578+x*q;
1780 q=661.7361207107653469211984771+x*q;
1781 q=2094.384367789539593790281779+x*q;
1782 q=4429.612803883682726711528526+x*q;
1783 q=6089.5424232724435504633068+x*q;
1784 q=4958.82756472114071495438422+x*q;
1785 q=1826.3348842295112595576438+x*q;
1786 result=CMath::exp(-x*x)*p/q;
1797 for (int32_t i=0; i<len; i++)
1800 v.
vector[i]=fishers_exact_test_for_2x3_table(table);
1825 for (int32_t i=0; i<3+2; i++)
1826 log_nom+=lgamma(m[i]+1);
1827 log_nom-=lgamma(n+1.0);
1832 for (int32_t i=0; i<3*2; i++)
1840 int32_t dim1=CMath::min(m[0], m[2]);
1844 for (int32_t k=0; k<=dim1; k++)
1846 for (int32_t l=CMath::max(0.0, m[0]-m[4]-k);
1847 l<=CMath::min(m[0]-k, m[3]); l++)
1849 x[0+0*2+counter*2*3]=k;
1850 x[0+1*2+counter*2*3]=l;
1851 x[0+2*2+counter*2*3]=m[0]-x[0+0*2+counter*2*3]-x[0+1*2+counter*2*3];
1852 x[1+0*2+counter*2*3]=m[2]-x[0+0*2+counter*2*3];
1853 x[1+1*2+counter*2*3]=m[3]-x[0+1*2+counter*2*3];
1854 x[1+2*2+counter*2*3]=m[4]-x[0+2*2+counter*2*3];
1861 #ifdef DEBUG_FISHER_TABLE
1864 SG_SPRINT(
"l=%g...%g\n", CMath::max(0.0,m[0]-m[4]-0), CMath::min(m[0]-0, m[3]))
1866 SG_SPRINT(
"prob_table_log=%.18Lg\n", prob_table_log)
1867 SG_SPRINT(
"log_denomf=%.18g\n", log_denomf)
1868 SG_SPRINT(
"log_denom=%.18Lg\n", log_denom)
1870 display_vector(m, m_len,
"marginals");
1871 display_vector(x, 2*3*counter,
"x");
1872 #endif // DEBUG_FISHER_TABLE
1877 for (int32_t k=0; k<counter; k++)
1879 for (int32_t j=0; j<3; j++)
1881 for (int32_t i=0; i<2; i++)
1882 log_denom_vec[k]+=lgammal(x[i+j*2+k*2*3]+1.0);
1886 for (int32_t i=0; i<counter; i++)
1887 log_denom_vec[i]=log_nom-log_denom_vec[i];
1889 #ifdef DEBUG_FISHER_TABLE
1890 display_vector(log_denom_vec, counter,
"log_denom_vec");
1891 #endif // DEBUG_FISHER_TABLE
1894 for (int32_t i=0; i<counter; i++)
1896 if (log_denom_vec[i]<=prob_table_log)
1897 nonrand_p=CMath::logarithmic_sum(nonrand_p, log_denom_vec[i]);
1900 #ifdef DEBUG_FISHER_TABLE
1901 SG_SPRINT(
"nonrand_p=%.18g\n", nonrand_p)
1902 SG_SPRINT(
"exp_nonrand_p=%.18g\n", CMath::exp(nonrand_p))
1903 #endif // DEBUG_FISHER_TABLE
1904 nonrand_p=CMath::exp(nonrand_p);
1906 SG_FREE(log_denom_vec);
1917 for (int32_t i=0; i<len; i++)
1918 for (int32_t j=0; j<len; j++)
1919 e+=exp(p2[j*len+i])*(p2[j*len+i]-p1[i]-p1[j]);
1928 for (int32_t i=0; i<len; i++)
1929 e+=exp(p[i])*(p[i]-q[i]);
1938 for (int32_t i=0; i<len; i++)
1947 "sample size should be less than number of indices\n");
1948 int32_t* idxs=SG_MALLOC(int32_t,N);
1950 int32_t* permuted_idxs=SG_MALLOC(int32_t,sample_size);
1955 for (i=0; i<sample_size; i++)
1956 permuted_idxs[i]=idxs[i];
1957 for (i=sample_size; i<N; i++)
1959 rnd=CMath::random(1, i);
1960 if (rnd<sample_size)
1961 permuted_idxs[rnd]=idxs[i];
1978 result=CMath::PI/CMath::tan(CMath::PI*x);
1994 0.04166666666666666667,
1995 -0.00729166666666666667,
1996 0.00384424603174603175,
1997 -0.00413411458333333333,
1998 0.00756096117424242424,
1999 -0.02108249687595390720,
2000 0.08332316080729166666,
2001 -0.44324627670587277880,
2002 3.05393103044765369366,
2003 -26.45616165999210241989};
2012 result+=coeff[i]*power;
2029 MatrixXd l = llt.matrixL();
2032 VectorXd diag = l.diagonal();
2034 for( int32_t i = 0; i < diag.rows(); ++i ) {
2035 retval += log(diag(i));
2044 typedef SparseMatrix<float64_t> MatrixType;
2047 SimplicialLLT<MatrixType> llt;
2051 MatrixType L=llt.matrixL();
2055 for(
index_t i=0; i<M.rows(); ++i )
2056 retval+=log(L.coeff(i,i));
2066 "CStatistics::sample_from_gaussian(): \
2067 Number of covariance rows must be positive!\n");
2069 "CStatistics::sample_from_gaussian(): \
2070 Number of covariance cols must be positive!\n");
2072 "CStatistics::sample_from_gaussian(): \
2073 Covariance is not initialized!\n");
2075 "CStatistics::sample_from_gaussian(): \
2076 Covariance should be square matrix!\n");
2078 "CStatistics::sample_from_gaussian(): \
2079 Mean and covariance dimension mismatch!\n");
2081 int32_t dim=mean.
vlen;
2087 for( int32_t j=0; j<N; ++j )
2088 for( int32_t i=0; i<dim; ++i )
2089 S(i,j)=CMath::randn_double();
2092 MatrixXd U=c.llt().matrixU();
2096 if( precision_matrix )
2100 LDLT<MatrixXd> ldlt;
2107 if( !precision_matrix )
2116 for( int32_t i=0; i<N; ++i )
2126 "CStatistics::sample_from_gaussian(): \
2127 Number of covariance rows must be positive!\n");
2129 "CStatistics::sample_from_gaussian(): \
2130 Number of covariance cols must be positive!\n");
2132 "CStatistics::sample_from_gaussian(): \
2133 Covariance is not initialized!\n");
2135 "CStatistics::sample_from_gaussian(): \
2136 Covariance should be square matrix!\n");
2138 "CStatistics::sample_from_gaussian(): \
2139 Mean and covariance dimension mismatch!\n");
2141 int32_t dim=mean.
vlen;
2144 typedef SparseMatrix<float64_t> MatrixType;
2147 SimplicialLLT<MatrixType> llt;
2151 for( int32_t j=0; j<N; ++j )
2152 for( int32_t i=0; i<dim; ++i )
2153 S(i,j)=CMath::randn_double();
2159 MatrixType LP=llt.matrixL();
2160 MatrixType UP=llt.matrixU();
2164 if( precision_matrix )
2167 SimplicialLLT<MatrixType> lltUP;
2178 s=llt.permutationPinv()*s;
2183 for( int32_t i=0; i<N; ++i )
2189 #endif //HAVE_EIGEN3
2193 SG_SDEBUG(
"entering CStatistics::fit_sigmoid()\n")
2195 REQUIRE(scores.
vector,
"CStatistics::fit_sigmoid() requires "
2196 "scores vector!\n");
2201 SG_SDEBUG(
"counting number of positive and negative labels\n")
2211 SG_SDEBUG(
"%d pos; %d neg\n", prior1, prior0)
2225 float64_t hiTarget=(prior1+1.0)/(prior1+2.0);
2230 for (
index_t i=0; i<length; ++i)
2241 float64_t b=CMath::log((prior0+1.0)/(prior1+1.0));
2244 for (
index_t i=0; i<length; ++i)
2248 fval+=t[i]*fApB+CMath::log(1+CMath::exp(-fApB));
2250 fval+=(t[i]-1)*fApB+CMath::log(1+CMath::exp(fApB));
2256 for (it=0; it<maxiter; ++it)
2258 SG_SDEBUG(
"Iteration %d, a=%f, b=%f, fval=%f\n", it, a, b, fval)
2267 for (
index_t i=0; i<length; ++i)
2274 p=CMath::exp(-fApB)/(1.0+CMath::exp(-fApB));
2275 q=1.0/(1.0+CMath::exp(-fApB));
2279 p=1.0/(1.0+CMath::exp(fApB));
2280 q=CMath::exp(fApB)/(1.0+CMath::exp(fApB));
2284 h11+=scores[i]*scores[i]*d2;
2293 if (CMath::abs(g1)<eps && CMath::abs(g2)<eps)
2305 while (stepsize>=minstep)
2312 for (
index_t i=0; i<length; ++i)
2316 newf+=t[i]*fApB+CMath::log(1+CMath::exp(-fApB));
2318 newf+=(t[i]-1)*fApB+CMath::log(1+CMath::exp(fApB));
2322 if (newf<fval+0.0001*stepsize*gd)
2330 stepsize=stepsize/2.0;
2333 if (stepsize<minstep)
2335 SG_SWARNING(
"CStatistics::fit_sigmoid(): line search fails, A=%f, "
2336 "B=%f, g1=%f, g2=%f, dA=%f, dB=%f, gd=%f\n",
2337 a, b, g1, g2, dA, dB, gd);
2343 SG_SWARNING(
"CStatistics::fit_sigmoid(): reaching maximal iterations,"
2344 " g1=%f, g2=%f\n", g1, g2);
2347 SG_SDEBUG(
"fitted sigmoid: a=%f, b=%f\n", a, b)
2353 SG_SDEBUG(
"leaving CStatistics::fit_sigmoid()\n")