35 using namespace Eigen;
68 result=values[median];
74 if (values[low]>values[high])
75 CMath::CMath::swap(values[low], values[high]);
76 result=values[median];
81 if (values[middle]>values[high])
82 CMath::swap(values[middle], values[high]);
83 if (values[low]>values[high])
84 CMath::swap(values[low], values[high]);
85 if (values[middle]>values[low])
86 CMath::swap(values[middle], values[low]);
88 CMath::swap(values[middle], values[low+1]);
96 while (values[low]>values[l]);
99 while (values[h]>values[low]);
102 CMath::swap(values[l], values[h]);
105 CMath::swap(values[low], values[h]);
133 for (i=1; i<values.
vlen; i++)
148 for (i=0; i<values.
vlen; i++)
153 if (values[i]>maxltguess)
154 maxltguess=values[i];
156 else if (values[i]>guess)
159 if (values[i]<mingtguess)
160 mingtguess=values[i];
165 if (less<=(values.
vlen+1)/2&&greater<=(values.
vlen+1)/2)
167 else if (less>greater)
173 if (less>=(values.
vlen+1)/2)
175 else if (less+equal>=(values.
vlen+1)/2)
185 result=median(copy,
true);
193 bool modify,
bool in_place)
200 return median(as_vector, modify, in_place);
209 float64_t mean=CStatistics::mean(values);
213 sum_squared_diff+=CMath::pow(values.
vector[i]-mean, 2);
215 return sum_squared_diff/(values.
vlen-1);
234 result[j]+=values(i,j);
246 result[j]+=values(j,i);
274 result[j]+=CMath::pow(values(i,j)-mean[j], 2);
286 result[j]+=CMath::pow(values(j,i)-mean[j], 2);
297 return CMath::sqrt(variance(values));
305 var[i]=CMath::sqrt(var[i]);
329 centered,
true,
false, 1.0/(observations.
num_rows-1));
345 int32_t deg=values.
vlen-1;
348 float64_t t=CMath::abs(inverse_student_t(deg, alpha));
351 float64_t std_dev=CStatistics::std_deviation(values);
352 float64_t mean=CStatistics::mean(values);
356 conf_int_low=mean-interval;
357 conf_int_up=mean+interval;
370 if (!(k>0 && greater(p, 0)) && less(p, 1))
372 SG_SERROR(
"CStatistics::inverse_student_t_distribution(): "
376 if (greater(p, 0.25) && less(p, 0.75))
384 z=inverse_incomplete_beta(0.5, 0.5*rk, CMath::abs(z));
385 t=CMath::sqrt(rk*z/(1.0-z));
394 if (greater_equal(p, 0.5))
399 z=inverse_incomplete_beta(0.5*rk, 0.5, 2.0*p);
400 if (less(CMath::MAX_REAL_NUMBER*z, rk))
402 result=rflg*CMath::MAX_REAL_NUMBER;
405 t=CMath::sqrt(rk/z-rk);
437 int32_t breaknewtcycle;
438 int32_t breakihalvecycle;
442 if (!(greater_equal(y, 0) && less_equal(y, 1)))
444 SG_SERROR(
"CStatistics::inverse_incomplete_beta(): "
504 if (less_equal(a, 1.0) || less_equal(b, 1.0))
512 yyy=incomplete_beta(aaa, bbb, x);
520 yp=-inverse_normal_cdf(y);
537 x=2.0/(1.0/(2.0*aaa-1.0)+1.0/(2.0*bbb-1.0));
538 d=yp*CMath::sqrt(x+lgm)/x
539 -(1.0/(2.0*bbb-1.0)-1.0/(2.0*aaa-1.0))
540 *(lgm+5.0/6.0-2.0/(3.0*x));
542 if (less(d, CMath::log(CMath::MIN_REAL_NUMBER)))
547 x=aaa/(aaa+bbb*CMath::exp(d));
548 yyy=incomplete_beta(aaa, bbb, x);
550 if (less(CMath::abs(yp), 0.2))
562 if (mainlooppos==ihalve)
567 mainlooppos=ihalvecycle;
574 if (mainlooppos==ihalvecycle)
583 x=1.0-CMath::MACHINE_EPSILON;
594 yyy=incomplete_beta(aaa, bbb, x);
596 if (less(CMath::abs(yp), dithresh))
602 if (less(CMath::abs(yp), dithresh))
621 di=1.0-(1.0-di)*(1.0-di);
636 if (greater(x0, 0.75))
653 yyy=incomplete_beta(aaa, bbb, x);
665 if (rflg==1 && less(x1, CMath::MACHINE_EPSILON))
697 mainlooppos=ihalvecycle;
702 mainlooppos=breakihalvecycle;
710 if (mainlooppos==breakihalvecycle)
712 if (greater_equal(x0, 1.0))
714 x=1.0-CMath::MACHINE_EPSILON;
717 if (less_equal(x, 0.0))
729 if (mainlooppos==newt)
736 lgm=lgamma(aaa+bbb)-lgamma(aaa)-lgamma(bbb);
738 mainlooppos=newtcycle;
745 if (mainlooppos==newtcycle)
751 yyy=incomplete_beta(aaa, bbb, x);
760 if (greater(yyy, yh))
779 if (equal(x, 1.0) || equal(x, 0.0))
781 mainlooppos=breaknewtcycle;
784 d=(aaa-1.0)*CMath::log(x)+(bbb-1.0)*CMath::log(1.0-x)+lgm;
785 if (less(d, CMath::log(CMath::MIN_REAL_NUMBER)))
789 if (greater(d, CMath::log(CMath::MAX_REAL_NUMBER)))
791 mainlooppos=breaknewtcycle;
797 if (less_equal(xt, x0))
800 xt=x0+0.5*yyy*(x-x0);
801 if (less_equal(xt, 0.0))
803 mainlooppos=breaknewtcycle;
807 if (greater_equal(xt, x1))
810 xt=x1-0.5*yyy*(x1-x);
811 if (greater_equal(xt, 1.0))
813 mainlooppos=breaknewtcycle;
818 if (less(CMath::abs(d/x), 128.0*CMath::MACHINE_EPSILON))
823 mainlooppos=newtcycle;
828 mainlooppos=breaknewtcycle;
836 if (mainlooppos==breaknewtcycle)
838 dithresh=256.0*CMath::MACHINE_EPSILON;
849 if (less_equal(x, CMath::MACHINE_EPSILON))
851 x=1.0-CMath::MACHINE_EPSILON;
876 big=4.503599627370496e15;
877 biginv=2.22044604925031308085e-16;
878 maxgam=171.624376956302725;
879 minlog=CMath::log(CMath::MIN_REAL_NUMBER);
880 maxlog=CMath::log(CMath::MAX_REAL_NUMBER);
882 if (!(greater(a, 0) && greater(b, 0)))
884 SG_SERROR(
"CStatistics::incomplete_beta(): "
888 if (!(greater_equal(x, 0) && less_equal(x, 1)))
890 SG_SERROR(
"CStatistics::incomplete_beta(): "
905 if (less_equal(b*x, 1.0) && less_equal(x, 0.95))
907 result=ibetaf_incompletebetaps(a, b, x, maxgam);
911 if (greater(x, a/(a+b)))
924 if ((flag==1 && less_equal(b*x, 1.0)) && less_equal(x, 0.95))
926 t=ibetaf_incompletebetaps(a, b, x, maxgam);
927 if (less_equal(t, CMath::MACHINE_EPSILON))
929 result=1.0-CMath::MACHINE_EPSILON;
937 y=x*(a+b-2.0)-(a-1.0);
940 w=ibetaf_incompletebetafe(a, b, x, big, biginv);
944 w=ibetaf_incompletebetafe2(a, b, x, big, biginv)/xc;
948 if ((less(a+b, maxgam) && less(CMath::abs(y), maxlog))
949 && less(CMath::abs(t), maxlog))
952 t=t*CMath::pow(x, a);
955 t=t*(tgamma(a+b)/(tgamma(a)*tgamma(b)));
958 if (less_equal(t, CMath::MACHINE_EPSILON))
960 result=1.0-CMath::MACHINE_EPSILON;
973 y=y+t+lgamma(a+b)-lgamma(a)-lgamma(b);
985 if (less_equal(t, CMath::MACHINE_EPSILON))
987 t=1.0-CMath::MACHINE_EPSILON;
1001 return inverse_normal_cdf(y0)*std_dev+mean;
1023 expm2=0.13533528323661269189;
1024 s2pi=2.50662827463100050242;
1025 if (less_equal(y0, 0))
1027 result=-CMath::MAX_REAL_NUMBER;
1030 if (greater_equal(y0, 1))
1032 result=CMath::MAX_REAL_NUMBER;
1037 if (greater(y, 1.0-expm2))
1042 if (greater(y, expm2))
1046 p0=-59.9633501014107895267;
1047 p0=98.0010754185999661536+y2*p0;
1048 p0=-56.6762857469070293439+y2*p0;
1049 p0=13.9312609387279679503+y2*p0;
1050 p0=-1.23916583867381258016+y2*p0;
1052 q0=1.95448858338141759834+y2*q0;
1053 q0=4.67627912898881538453+y2*q0;
1054 q0=86.3602421390890590575+y2*q0;
1055 q0=-225.462687854119370527+y2*q0;
1056 q0=200.260212380060660359+y2*q0;
1057 q0=-82.0372256168333339912+y2*q0;
1058 q0=15.9056225126211695515+y2*q0;
1059 q0=-1.18331621121330003142+y2*q0;
1065 x=CMath::sqrt(-2.0*CMath::log(y));
1066 x0=x-CMath::log(x)/x;
1070 p1=4.05544892305962419923;
1071 p1=31.5251094599893866154+z*p1;
1072 p1=57.1628192246421288162+z*p1;
1073 p1=44.0805073893200834700+z*p1;
1074 p1=14.6849561928858024014+z*p1;
1075 p1=2.18663306850790267539+z*p1;
1076 p1=-1.40256079171354495875*0.1+z*p1;
1077 p1=-3.50424626827848203418*0.01+z*p1;
1078 p1=-8.57456785154685413611*0.0001+z*p1;
1080 q1=15.7799883256466749731+z*q1;
1081 q1=45.3907635128879210584+z*q1;
1082 q1=41.3172038254672030440+z*q1;
1083 q1=15.0425385692907503408+z*q1;
1084 q1=2.50464946208309415979+z*q1;
1085 q1=-1.42182922854787788574*0.1+z*q1;
1086 q1=-3.80806407691578277194*0.01+z*q1;
1087 q1=-9.33259480895457427372*0.0001+z*q1;
1092 p2=3.23774891776946035970;
1093 p2=6.91522889068984211695+z*p2;
1094 p2=3.93881025292474443415+z*p2;
1095 p2=1.33303460815807542389+z*p2;
1096 p2=2.01485389549179081538*0.1+z*p2;
1097 p2=1.23716634817820021358*0.01+z*p2;
1098 p2=3.01581553508235416007*0.0001+z*p2;
1099 p2=2.65806974686737550832*0.000001+z*p2;
1100 p2=6.23974539184983293730*0.000000001+z*p2;
1102 q2=6.02427039364742014255+z*q2;
1103 q2=3.67983563856160859403+z*q2;
1104 q2=1.37702099489081330271+z*q2;
1105 q2=2.16236993594496635890*0.1+z*q2;
1106 q2=1.34204006088543189037*0.01+z*q2;
1107 q2=3.28014464682127739104*0.0001+z*q2;
1108 q2=2.89247864745380683936*0.000001+z*q2;
1109 q2=6.79019408009981274425*0.000000001+z*q2;
1141 z=CMath::MACHINE_EPSILON*ai;
1142 while (greater(CMath::abs(v), z))
1153 if (less(a+b, maxgam)
1154 && less(CMath::abs(u), CMath::log(CMath::MAX_REAL_NUMBER)))
1156 t=tgamma(a+b)/(tgamma(a)*tgamma(b));
1157 s=s*t*CMath::pow(x, a);
1161 t=lgamma(a+b)-lgamma(a)-lgamma(b)+u+CMath::log(s);
1162 if (less(t, CMath::log(CMath::MIN_REAL_NUMBER)))
1215 thresh=3.0*CMath::MACHINE_EPSILON;
1218 xk=-x*k1*k2/(k3*k4);
1232 if (not_equal(qk, 0))
1236 if (not_equal(r, 0))
1238 t=CMath::abs((ans-r)/r);
1245 if (less(t, thresh))
1257 if (greater(CMath::abs(qk)+CMath::abs(pk), big))
1264 if (less(CMath::abs(qk), biginv) || less(CMath::abs(pk), biginv))
1320 thresh=3.0*CMath::MACHINE_EPSILON;
1323 xk=-z*k1*k2/(k3*k4);
1337 if (not_equal(qk, 0))
1341 if (not_equal(r, 0))
1343 t=CMath::abs((ans-r)/r);
1350 if (less(t, thresh))
1362 if (greater(CMath::abs(qk)+CMath::abs(pk), big))
1369 if (less(CMath::abs(qk), biginv) || less(CMath::abs(pk), biginv))
1392 igammaepsilon=0.000000000000001;
1393 if (less_equal(x, 0) || less_equal(a, 0))
1398 if (greater(x, 1) && greater(x, a))
1400 result=1-incomplete_gamma_completed(a, x);
1403 ax=a*CMath::log(x)-x-lgamma(a);
1404 if (less(ax, -709.78271289338399))
1419 while (greater(c/ans, igammaepsilon));
1445 igammaepsilon=0.000000000000001;
1446 igammabignumber=4503599627370496.0;
1447 igammabignumberinv=2.22044604925031308085*0.0000000000000001;
1448 if (less_equal(x, 0) || less_equal(a, 0))
1453 if (less(x, 1) || less(x, a))
1455 result=1-incomplete_gamma(a, x);
1458 ax=a*CMath::log(x)-x-lgamma(a);
1459 if (less(ax, -709.78271289338399))
1481 if (not_equal(qk, 0))
1484 t=CMath::abs((ans-r)/r);
1495 if (greater(CMath::abs(pk), igammabignumber))
1497 pkm2=pkm2*igammabignumberinv;
1498 pkm1=pkm1*igammabignumberinv;
1499 qkm2=qkm2*igammabignumberinv;
1500 qkm1=qkm1*igammabignumberinv;
1503 while (greater(t, igammaepsilon));
1511 return incomplete_gamma(a, x/b);
1519 return inverse_incomplete_gamma_completed(a, 1-p)*b;
1540 igammaepsilon=0.000000000000001;
1541 iinvgammabignumber=4503599627370496.0;
1542 x0=iinvgammabignumber;
1546 dithresh=5*igammaepsilon;
1548 y=1-d-inverse_normal_cdf(y0)*CMath::sqrt(d);
1554 if (greater(x, x0) || less(x, x1))
1559 y=incomplete_gamma_completed(a, x);
1560 if (less(y, yl) || greater(y, yh))
1575 d=(a-1)*CMath::log(x)-x-lgm;
1576 if (less(d, -709.78271289338399))
1583 if (less(CMath::abs(d/x), igammaepsilon))
1591 if (equal(x0, iinvgammabignumber))
1593 if (less_equal(x, 0))
1597 while (equal(x0, iinvgammabignumber))
1600 y=incomplete_gamma_completed(a, x);
1616 y=incomplete_gamma_completed(a, x);
1617 lgm=(x0-x1)/(x1+x0);
1618 if (less(CMath::abs(lgm), dithresh))
1623 if (less(CMath::abs(lgm), dithresh))
1627 if (less_equal(x, 0.0))
1631 if (greater_equal(y, y0))
1683 return 0.5*(error_function_complement(-x/std_dev/1.41421356237309514547));
1687 const float64_t CStatistics::ERFC_CASE1=0.0492;
1689 const float64_t CStatistics::ERFC_CASE2=-11.3137;
1693 const float64_t sqrt_of_2=1.41421356237309514547;
1694 const float64_t log_of_2=0.69314718055994528623;
1695 const float64_t sqrt_of_pi=1.77245385090551588192;
1706 0.00125993961762116,
1707 -0.01621575378835404,
1708 0.02629651521057465,
1709 -0.001829764677455021,
1710 2.0*(1.0-CMath::PI/3.0),
1711 (4.0-CMath::PI)/3.0,
1726 float64_t lp0 = -x/(sqrt_of_2*sqrt_of_pi);
1727 for (
index_t i=0; i<c_len; i++)
1728 f=lp0*(c_array[i]+f);
1729 return -2.0*f-log_of_2;
1731 else if (x<ERFC_CASE2)
1744 return CMath::log(erfc8_weighted_sum(x))-log_of_2-x*x*0.5;
1748 return CMath::log(normal_cdf(x));
1754 return incomplete_gamma(k/2.0,x/2.0);
1760 return incomplete_beta(d1/2.0,d2/2.0,d1*x/(d1*x+d2));
1767 const float64_t sqrt_of_2=1.41421356237309514547;
1771 0.5641895835477550741253201704,
1772 1.275366644729965952479585264,
1773 5.019049726784267463450058,
1774 6.1602098531096305440906,
1775 7.409740605964741794425,
1776 2.97886562639399288862
1782 2.260528520767326969591866945,
1783 9.396034016235054150430579648,
1784 12.0489519278551290360340491,
1785 17.08144074746600431571095,
1786 9.608965327192787870698,
1787 3.3690752069827527677
1795 num=-x*num/sqrt_of_2+P[i];
1801 den=-x*den/sqrt_of_2+Q[i];
1822 p=0.007547728033418631287834;
1823 p=0.288805137207594084924010+xsq*p;
1824 p=14.3383842191748205576712+xsq*p;
1825 p=38.0140318123903008244444+xsq*p;
1826 p=3017.82788536507577809226+xsq*p;
1827 p=7404.07142710151470082064+xsq*p;
1828 p=80437.3630960840172832162+xsq*p;
1830 q=1.00000000000000000000000+xsq*q;
1831 q=38.0190713951939403753468+xsq*q;
1832 q=658.070155459240506326937+xsq*q;
1833 q=6379.60017324428279487120+xsq*q;
1834 q=34216.5257924628539769006+xsq*q;
1835 q=80437.3630960840172826266+xsq*q;
1836 result=s*1.1283791670955125738961589031*x*p/q;
1839 if (greater_equal(x, 10))
1844 result=s*(1-error_function_complement(x));
1856 result=2-error_function_complement(-x);
1861 result=1.0-error_function(x);
1864 if (greater_equal(x, 10))
1870 p=0.5641877825507397413087057563+x*p;
1871 p=9.675807882987265400604202961+x*p;
1872 p=77.08161730368428609781633646+x*p;
1873 p=368.5196154710010637133875746+x*p;
1874 p=1143.262070703886173606073338+x*p;
1875 p=2320.439590251635247384768711+x*p;
1876 p=2898.0293292167655611275846+x*p;
1877 p=1826.3348842295112592168999+x*p;
1879 q=17.14980943627607849376131193+x*q;
1880 q=137.1255960500622202878443578+x*q;
1881 q=661.7361207107653469211984771+x*q;
1882 q=2094.384367789539593790281779+x*q;
1883 q=4429.612803883682726711528526+x*q;
1884 q=6089.5424232724435504633068+x*q;
1885 q=4958.82756472114071495438422+x*q;
1886 q=1826.3348842295112595576438+x*q;
1887 result=CMath::exp(-x*x)*p/q;
1898 for (int32_t i=0; i<len; i++)
1901 v.
vector[i]=fishers_exact_test_for_2x3_table(table);
1921 int32_t x_len=2*3*CMath::sq(
CMath::max(m, m_len));
1926 for (int32_t i=0; i<3+2; i++)
1927 log_nom+=lgamma(m[i]+1);
1928 log_nom-=lgamma(n+1.0);
1933 for (int32_t i=0; i<3*2; i++)
1941 int32_t dim1=CMath::min(m[0], m[2]);
1945 for (int32_t k=0; k<=dim1; k++)
1948 l<=CMath::min(m[0]-k, m[3]); l++)
1950 x[0+0*2+counter*2*3]=k;
1951 x[0+1*2+counter*2*3]=l;
1952 x[0+2*2+counter*2*3]=m[0]-x[0+0*2+counter*2*3]-x[0+1*2+counter*2*3];
1953 x[1+0*2+counter*2*3]=m[2]-x[0+0*2+counter*2*3];
1954 x[1+1*2+counter*2*3]=m[3]-x[0+1*2+counter*2*3];
1955 x[1+2*2+counter*2*3]=m[4]-x[0+2*2+counter*2*3];
1962 #ifdef DEBUG_FISHER_TABLE
1967 SG_SPRINT(
"prob_table_log=%.18Lg\n", prob_table_log)
1968 SG_SPRINT(
"log_denomf=%.18g\n", log_denomf)
1969 SG_SPRINT(
"log_denom=%.18Lg\n", log_denom)
1971 display_vector(m, m_len,
"marginals");
1972 display_vector(x, 2*3*counter,
"x");
1973 #endif // DEBUG_FISHER_TABLE
1978 for (int32_t k=0; k<counter; k++)
1980 for (int32_t j=0; j<3; j++)
1982 for (int32_t i=0; i<2; i++)
1983 log_denom_vec[k]+=lgammal(x[i+j*2+k*2*3]+1.0);
1987 for (int32_t i=0; i<counter; i++)
1988 log_denom_vec[i]=log_nom-log_denom_vec[i];
1990 #ifdef DEBUG_FISHER_TABLE
1991 display_vector(log_denom_vec, counter,
"log_denom_vec");
1992 #endif // DEBUG_FISHER_TABLE
1995 for (int32_t i=0; i<counter; i++)
1997 if (log_denom_vec[i]<=prob_table_log)
1998 nonrand_p=CMath::logarithmic_sum(nonrand_p, log_denom_vec[i]);
2001 #ifdef DEBUG_FISHER_TABLE
2002 SG_SPRINT(
"nonrand_p=%.18g\n", nonrand_p)
2003 SG_SPRINT(
"exp_nonrand_p=%.18g\n", CMath::exp(nonrand_p))
2004 #endif // DEBUG_FISHER_TABLE
2005 nonrand_p=CMath::exp(nonrand_p);
2007 SG_FREE(log_denom_vec);
2018 for (int32_t i=0; i<len; i++)
2019 for (int32_t j=0; j<len; j++)
2020 e+=exp(p2[j*len+i])*(p2[j*len+i]-p1[i]-p1[j]);
2029 for (int32_t i=0; i<len; i++)
2030 e+=exp(p[i])*(p[i]-q[i]);
2039 for (int32_t i=0; i<len; i++)
2048 "sample size should be less than number of indices\n");
2049 int32_t* idxs=SG_MALLOC(int32_t,N);
2051 int32_t* permuted_idxs=SG_MALLOC(int32_t,sample_size);
2056 for (i=0; i<sample_size; i++)
2057 permuted_idxs[i]=idxs[i];
2058 for (i=sample_size; i<N; i++)
2060 rnd=CMath::random(1, i);
2061 if (rnd<sample_size)
2062 permuted_idxs[rnd]=idxs[i];
2067 CMath::qsort(result);
2079 result=CMath::PI/CMath::tan(CMath::PI*x);
2095 0.04166666666666666667,
2096 -0.00729166666666666667,
2097 0.00384424603174603175,
2098 -0.00413411458333333333,
2099 0.00756096117424242424,
2100 -0.02108249687595390720,
2101 0.08332316080729166666,
2102 -0.44324627670587277880,
2103 3.05393103044765369366,
2104 -26.45616165999210241989};
2113 result+=coeff[i]*power;
2123 REQUIRE(eigen_A.rows()==eigen_A.cols(),
2124 "Input matrix should be a sqaure matrix row(%d) col(%d)\n",
2125 eigen_A.rows(), eigen_A.cols());
2127 PartialPivLU<MatrixXd> lu(eigen_A);
2128 VectorXd tmp(eigen_A.rows());
2130 for (
index_t idx=0; idx<tmp.rows(); idx++)
2133 VectorXd p=lu.permutationP()*tmp;
2136 for (
index_t idx=0; idx<p.rows(); idx++)
2152 VectorXd u=lu.matrixLU().diagonal();
2155 for (
int idx=0; idx<u.rows(); idx++)
2169 result=u.array().abs().log().sum();
2187 VectorXd diag = l.diagonal();
2189 for( int32_t i = 0; i < diag.rows(); ++i ) {
2190 retval += log(diag(i));
2199 typedef SparseMatrix<float64_t> MatrixType;
2202 SimplicialLLT<MatrixType> llt;
2206 MatrixType L=llt.matrixL();
2210 for(
index_t i=0; i<M.rows(); ++i )
2211 retval+=log(L.coeff(i,i));
2220 REQUIRE(cov.
num_rows>0,
"Number of covariance rows must be positive!\n");
2221 REQUIRE(cov.
num_cols>0,
"Number of covariance cols must be positive!\n");
2226 int32_t dim=mean.
vlen;
2232 for( int32_t j=0; j<N; ++j )
2233 for( int32_t i=0; i<dim; ++i )
2234 S(i,j)=CMath::randn_double();
2241 if( precision_matrix )
2252 if( !precision_matrix )
2261 for( int32_t i=0; i<N; ++i )
2271 "CStatistics::sample_from_gaussian(): \
2272 Number of covariance rows must be positive!\n");
2274 "CStatistics::sample_from_gaussian(): \
2275 Number of covariance cols must be positive!\n");
2277 "CStatistics::sample_from_gaussian(): \
2278 Covariance is not initialized!\n");
2280 "CStatistics::sample_from_gaussian(): \
2281 Covariance should be square matrix!\n");
2283 "CStatistics::sample_from_gaussian(): \
2284 Mean and covariance dimension mismatch!\n");
2286 int32_t dim=mean.
vlen;
2289 typedef SparseMatrix<float64_t> MatrixType;
2292 SimplicialLLT<MatrixType> llt;
2296 for( int32_t j=0; j<N; ++j )
2297 for( int32_t i=0; i<dim; ++i )
2298 S(i,j)=CMath::randn_double();
2304 MatrixType LP=llt.matrixL();
2305 MatrixType UP=llt.matrixU();
2309 if( precision_matrix )
2312 SimplicialLLT<MatrixType> lltUP;
2323 s=llt.permutationPinv()*s;
2328 for( int32_t i=0; i<N; ++i )
2334 #endif //HAVE_EIGEN3
2338 SG_SDEBUG(
"entering CStatistics::fit_sigmoid()\n")
2340 REQUIRE(scores.
vector,
"CStatistics::fit_sigmoid() requires "
2341 "scores vector!\n");
2346 SG_SDEBUG(
"counting number of positive and negative labels\n")
2356 SG_SDEBUG(
"%d pos; %d neg\n", prior1, prior0)
2370 float64_t hiTarget=(prior1+1.0)/(prior1+2.0);
2375 for (
index_t i=0; i<length; ++i)
2386 float64_t b=CMath::log((prior0+1.0)/(prior1+1.0));
2389 for (
index_t i=0; i<length; ++i)
2393 fval+=t[i]*fApB+CMath::log(1+CMath::exp(-fApB));
2395 fval+=(t[i]-1)*fApB+CMath::log(1+CMath::exp(fApB));
2401 for (it=0; it<maxiter; ++it)
2403 SG_SDEBUG(
"Iteration %d, a=%f, b=%f, fval=%f\n", it, a, b, fval)
2412 for (
index_t i=0; i<length; ++i)
2419 p=CMath::exp(-fApB)/(1.0+CMath::exp(-fApB));
2420 q=1.0/(1.0+CMath::exp(-fApB));
2424 p=1.0/(1.0+CMath::exp(fApB));
2425 q=CMath::exp(fApB)/(1.0+CMath::exp(fApB));
2429 h11+=scores[i]*scores[i]*d2;
2438 if (CMath::abs(g1)<eps && CMath::abs(g2)<eps)
2450 while (stepsize>=minstep)
2457 for (
index_t i=0; i<length; ++i)
2461 newf+=t[i]*fApB+CMath::log(1+CMath::exp(-fApB));
2463 newf+=(t[i]-1)*fApB+CMath::log(1+CMath::exp(fApB));
2467 if (newf<fval+0.0001*stepsize*gd)
2475 stepsize=stepsize/2.0;
2478 if (stepsize<minstep)
2480 SG_SWARNING(
"CStatistics::fit_sigmoid(): line search fails, A=%f, "
2481 "B=%f, g1=%f, g2=%f, dA=%f, dB=%f, gd=%f\n",
2482 a, b, g1, g2, dA, dB, gd);
2488 SG_SWARNING(
"CStatistics::fit_sigmoid(): reaching maximal iterations,"
2489 " g1=%f, g2=%f\n", g1, g2);
2492 SG_SDEBUG(
"fitted sigmoid: a=%f, b=%f\n", a, b)
2498 SG_SDEBUG(
"leaving CStatistics::fit_sigmoid()\n")
index_t num_features
total number of features
void remove_column_mean()
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
This class contains some utilities for Eigen3 Sparse Matrix integration with shogun. Currently it provides a method for converting SGSparseMatrix to Eigen3 SparseMatrix.
all of classes and functions are contained in the shogun namespace
Matrix::Scalar max(Matrix m)
index_t num_vectors
total number of vectors