72 return( Value * Value );
92 return( floor(0.5 + Value) );
95 double d = pow(10., Decimals);
98 if( fabs(v - floor(v)) > 0. )
100 return( floor(0.5 + v) / d );
109 if( Decimals <= 0 || Value == 0. )
111 return( (
int)(0.5 + Value) );
114 Decimals = (int)(-(ceil(log10(fabs(Value))) - Decimals));
118 double d = pow(10., Decimals);
121 ? -((
int)(0.5 - Value * d)) / d
122 : ((
int)(0.5 + Value * d)) / d
127 double d = pow(10., -Decimals);
130 ? -((
int)(0.5 - Value / d)) * d
131 : ((
int)(0.5 + Value / d)) * d
146 Number = abs(Number);
148 return( Number < 10 ? 1 : 1 + (
int)log10((
double)Number) );
163 if( Width > 0 && Precision >= 0 )
return(
CSG_String::Format(
"%*.*e", Width, Precision, Number) );
171 if( Width > 0 && Precision >= 0 )
return(
CSG_String::Format(
"%*.*f", Width, Precision, Number) );
189 if( *((
int *)a) < *((
int *)b) )
192 if( *((
int *)a) > *((
int *)b) )
201 if( *((
double *)a) < *((
double *)b) )
204 if( *((
double *)a) > *((
double *)b) )
213 return( strcmp((
const char *)a, (
const char *)b) );
227 ? (Deg + Min / 60. + Sec / 3600.)
228 : (Deg - Min / 60. - Sec / 3600.)
235 Sec = fmod(Value < 0. ? -Value : Value, 360.);
237 Deg = (int)Sec; Sec = 60. * (Sec - Deg);
238 Min = (int)Sec; Sec = 60. * (Sec - Min);
276 return( 1. * rand() / (
double)RAND_MAX );
284 return( min + (max - min) * rand() / (
double)RAND_MAX );
307 w = x1 * x1 + x2 * x2;
311 w = sqrt((-2. * log(w)) / w);
313 return( mean + stddev * x1 * w );
341 Create(Mean, StdDev, Count);
346 Create(Values, bHoldValues);
354 m_Values.
Create(bHoldValues ?
sizeof(
double) : 0, 0, TSG_Array_Growth::SG_ARRAY_GROWTH_1);
431 double Scale = nValues / (double)
m_nValues;
545 m_Sum += Weight * Value;
546 m_Sum2 += Weight * Value*Value;
726 return( (
sLong)Index );
750 return( (
sLong)Index );
879 if( Value == m_Value[i] )
906 if( Value == m_Value[i] )
934 if( Value.
Cmp(m_Value[i]) == 0 )
961 if( Value.
Cmp(m_Value[i]) == 0 )
1022 return( pRecord ? (
int)pRecord->
Get_Index() : -1);
1030 return( pRecord ? (
int)pRecord->
Get_Index() : -1);
1038 return( pRecord ? (
int)pRecord->
Get_Index() : -1);
1057 (pRecord = m_pTable->
Add_Record())->Set_Value(0, Value);
1076 (pRecord = m_pTable->
Add_Record())->Set_Value(0, Value);
1095 (pRecord = m_pTable->
Add_Record())->Set_Value(0, Value);
1128 return( pRecord ? pRecord->
asInt(1) : 0 );
1136 return( pRecord ? pRecord->
asInt(0) : 0 );
1144 return( pRecord ? pRecord->
asDouble(0) : 0 );
1167 for(
int i=1; i<m_pTable->
Get_Count(); i++)
1169 if( Count < m_pTable->Get_Record_byIndex(i)->
asInt(1) )
1189 for(
int i=1; i<m_pTable->
Get_Count(); i++)
1230 Create(nClasses, Minimum, Maximum);
1238 Create(nClasses, Minimum, Maximum, Values, maxSamples);
1246 Create(nClasses, Minimum, Maximum, pTable, Field, maxSamples);
1254 Create(nClasses, Minimum, Maximum, pGrid, maxSamples);
1262 Create(nClasses, Minimum, Maximum, pGrids, maxSamples);
1290 void CSG_Histogram::_On_Construction(
void)
1294 m_Cumulative = NULL;
1301 bool CSG_Histogram::_Create(
size_t nClasses,
double Minimum,
double Maximum)
1303 if( nClasses > 0 && Minimum < Maximum )
1307 m_Elements = (
size_t *)
SG_Calloc(nClasses,
sizeof(
size_t));
1308 m_Cumulative = (
size_t *)
SG_Calloc(nClasses,
sizeof(
size_t));
1310 if( m_Elements && m_Cumulative )
1312 m_nClasses = nClasses;
1313 m_Minimum = Minimum;
1314 m_Maximum = Maximum;
1315 m_ClassWidth = (Maximum - Minimum) / (
double)m_nClasses;
1329 m_Statistics += Value;
1331 if( m_nClasses > 0 && m_Minimum <= Value && Value <= m_Maximum )
1333 size_t Class = (size_t)((Value - m_Minimum) / m_ClassWidth);
1335 if( Class >= m_nClasses )
1337 Class = m_nClasses - 1;
1340 m_Elements[Class]++;
1347 if( m_nClasses > 0 && Scale > 0. )
1351 for(
size_t i=0; i<m_nClasses; i++)
1353 m_Elements[i] = (size_t)(Scale * m_Elements[i]);
1365 if( m_nClasses > 0 )
1369 m_nMaximum = m_Cumulative[0] = m_Elements[0];
1371 for(
size_t i=1; i<m_nClasses; i++)
1373 m_Cumulative[i] = m_Cumulative[i - 1] + m_Elements[i];
1375 if( m_nMaximum < m_Elements[i] )
1377 m_nMaximum = m_Elements[i];
1388 bool CSG_Histogram::_Update(
sLong nElements)
1390 if( nElements > 0 && m_Statistics.
Get_Count() > 0 )
1392 double Scale = (double)nElements / (
double)m_Statistics.
Get_Count();
1396 for(
size_t i=1; i<m_nClasses; i++)
1398 m_Elements[i] = (size_t)(0.5 + Scale * m_Elements[i]);
1411 if( m_nClasses < 2 ) {
return( 0. ); }
1413 if( Quantile <= 0. ) {
return( m_Minimum ); }
1414 if( Quantile >= 1. ) {
return( m_Maximum ); }
1418 for(
size_t i=0, n0=0; i<m_nClasses; n0=m_Cumulative[i++])
1420 if( n < m_Cumulative[i] )
1422 if( m_Cumulative[i] >= n0 )
1427 double d = (n - n0) / (
double)(m_Cumulative[i] - n0);
1429 return(
Get_Break(i) + d * m_ClassWidth );
1431 else if( n == m_Cumulative[i] )
1437 return( m_Maximum );
1455 if( m_nClasses < 2 ) {
return( 0. ); }
1457 if( Value <= m_Minimum ) {
return( 0. ); }
1458 if( Value >= m_Maximum ) {
return( 1. ); }
1460 size_t Class = (size_t)(m_nClasses * (Value - m_Minimum) / (m_Maximum - m_Minimum));
1462 if( Class >= m_nClasses )
1471 return( dq * (Value - m_Minimum) / m_ClassWidth );
1477 return( q0 + dq * (Value -
Get_Break(Class)) / m_ClassWidth );
1497 if( !_Create(Histogram.m_nClasses, Histogram.m_Minimum, Histogram.m_Maximum) )
1502 m_Statistics = Histogram.m_Statistics;
1503 m_ClassWidth = Histogram.m_ClassWidth;
1504 m_nMaximum = Histogram.m_nMaximum ;
1506 for(
size_t i=0; i<m_nClasses; i++)
1508 m_Cumulative[i] = Histogram.m_Cumulative[i];
1509 m_Elements [i] = Histogram.m_Elements [i];
1518 return( _Create(nClasses, Minimum, Maximum) );
1524 if( Minimum >= Maximum )
1532 if( !_Create(nClasses, Minimum, Maximum) )
1538 if( maxSamples > 0 && maxSamples < (
size_t)Values.
Get_N() )
1540 double d = (double)Values.
Get_N() / (double)maxSamples;
1542 for(
double i=0; i<(double)Values.
Get_N(); i+=d)
1547 d = (double)m_Statistics.
Get_Count() / (double)maxSamples;
1549 return( _Update(d < 1. ? (
int)(d * (double)Values.
Get_N()) : Values.
Get_N()) );
1553 for(
int i=0; i<Values.
Get_N(); i++)
1564 if( !pTable || Field < 0 || Field >= pTable->
Get_Field_Count() || !_Create(nClasses,
1565 Minimum < Maximum ? Minimum : pTable->Get_Minimum(Field),
1566 Minimum < Maximum ? Maximum : pTable->Get_Maximum(Field)) )
1572 if( maxSamples > 0 && maxSamples < (
size_t)pTable->
Get_Count() )
1574 double d = (double)pTable->
Get_Count() / (double)maxSamples;
1576 for(
double i=0; i<(double)pTable->
Get_Count(); i+=d)
1578 double Value = pTable->
Get_Record((
int)i)->asDouble(Field);
1586 d = (double)m_Statistics.
Get_Count() / (double)maxSamples;
1608 if( !pGrid || !_Create(nClasses,
1609 Minimum < Maximum ? Minimum : pGrid->Get_Min(),
1610 Minimum < Maximum ? Maximum : pGrid->Get_Max()) )
1616 if( maxSamples > 0 && (
sLong)maxSamples < pGrid->Get_NCells() )
1618 double d = (double)pGrid->
Get_NCells() / (double)maxSamples;
1620 for(
double i=0; i<(double)pGrid->
Get_NCells(); i+=d)
1630 d = (double)m_Statistics.
Get_Count() / (double)maxSamples;
1650 if( !pGrids || !_Create(nClasses,
1651 Minimum < Maximum ? Minimum : pGrids->Get_Min(),
1652 Minimum < Maximum ? Maximum : pGrids->Get_Max()) )
1658 if( maxSamples > 0 && (
sLong)maxSamples < pGrids->Get_NCells() )
1660 double d = (double)pGrids->
Get_NCells() / (double)maxSamples;
1662 for(
double i=0; i<(double)pGrids->
Get_NCells(); i+=d)
1672 d = (double)m_Statistics.
Get_Count() / (double)maxSamples;
1720 Create(pTable, Field, nClasses, Histogram);
1726 Create(pGrid, nClasses, Histogram);
1732 Create(pGrids, nClasses, Histogram);
1738 Create(Values, nClasses, Histogram);
1749 bool bResult =
false;
1753 bResult = m_Histogram.
Create(Histogram, 0, 0, pTable, Field) && _Histogram(nClasses);
1755 else if( Field >= 0 && Field < pTable->Get_Field_Count() )
1767 bResult = m_Values.
Sort() && _Calculate(nClasses);
1778 bool bResult =
false;
1782 bResult = m_Histogram.
Create(Histogram, 0, 0, pGrid) && _Histogram(nClasses);
1794 bResult = m_Values.
Sort() && _Calculate(nClasses);
1805 bool bResult =
false;
1809 bResult = m_Histogram.
Create(Histogram, 0, 0, pGrids) && _Histogram(nClasses);
1821 bResult = m_Values.
Sort() && _Calculate(nClasses);
1832 bool bResult =
false;
1836 bResult = m_Histogram.
Create(Histogram, 0, 0, Values) && _Histogram(nClasses);
1840 bResult = m_Values.
Create(Values) && m_Values.
Sort() && _Calculate(nClasses);
1854 bool CSG_Natural_Breaks::_Histogram(
int nClasses)
1856 if( _Calculate(nClasses) )
1864 m_Breaks[i] = m_Histogram.
Get_Value(m_Breaks[i] * d);
1880 inline double CSG_Natural_Breaks::_Get_Value(
int i)
1887 return( m_Values[i] );
1891 bool CSG_Natural_Breaks::_Calculate(
int nClasses)
1900 CSG_Matrix mv(nClasses, nValues); mv.Assign(FLT_MAX);
1902 int i, **mc = (
int **)
SG_Malloc(nValues *
sizeof(
int *));
1904 mc[0] = (
int *)
SG_Calloc(nClasses * nValues,
sizeof(
int));
1906 for(i=0; i<nValues; i++)
1908 mc[i] = mc[0] + i * nClasses;
1912 for(i=1; i<nValues; i++)
1914 double v = 0., s1 = 0., s2 = 0., w = 0.;
1916 for(
int m=0, n=i+1; m<=i; m++, n--)
1922 v = s2 - (s1 * s1) / w;
1926 for(
int j=1; j<nClasses; j++)
1928 if( mv[i][j] >= (v + mv[n - 1][j - 1]) )
1931 mv[i][j] = v + mv[n - 1][j - 1];
1944 for(i=0; i<nClasses; i++)
1949 int j = Class[nClasses - 1] = nValues - 1;
1951 for(i=nClasses-1; i>0; i--)
1953 Class[i - 1] = j = mc[j - 1][i];
1957 m_Breaks.
Create(nClasses + 1);
1959 m_Breaks[0] = _Get_Value(0);
1961 for(i=1; i<nClasses; i++)
1963 m_Breaks[i] = _Get_Value(Class[i - 1]);
1966 m_Breaks[nClasses] = _Get_Value(nValues - 1);
2014 m_nFeatures = nFeatures;
2016 m_Features.
Create(m_nFeatures *
sizeof(
double), 0, TSG_Array_Growth::SG_ARRAY_GROWTH_3);
2027 return( m_nFeatures > 0 && m_Features.
Inc_Array() );
2033 if( iElement >= 0 && iElement <
Get_nElements() && iFeature >= 0 && iFeature < m_nFeatures )
2035 ((
double *)m_Features.
Get_Entry(iElement))[iFeature] = Value;
2062 m_nMembers.
Create(nClusters);
2063 m_Variance.
Create(nClusters);
2064 m_Centroid.
Create(m_nFeatures, nClusters);
2071 switch( Initialization )
2076 m_Clusters[iElement] = nClusters - 1;
2082 m_Clusters[iElement] = iElement % nClusters;
2087 if( 0 > m_Clusters[iElement] || m_Clusters[iElement] >= nClusters )
2089 m_Clusters[iElement] = iElement % nClusters;
2102 default: bResult = _Minimum_Distance(
true , nMaxIterations);
break;
2103 case 1: bResult = _Hill_Climbing (
true , nMaxIterations);
break;
2104 case 2: bResult = _Minimum_Distance(
true , nMaxIterations)
2105 && _Hill_Climbing (
false, nMaxIterations);
break;
2111 for(
int iCluster=0; iCluster<nClusters; iCluster++)
2113 m_Variance[iCluster] = m_nMembers[iCluster] <= 0 ? 0. : m_Variance[iCluster] / m_nMembers[iCluster];
2121 bool CSG_Cluster_Analysis::_Minimum_Distance(
bool bInitialize,
int nMaxIterations)
2123 int iElement, iCluster, nClusters = m_Variance.
Get_N();
2125 double SP_Last = -1.;
2137 m_nMembers[iCluster = m_Clusters[iElement]]++;
2139 double *Feature = (
double *)m_Features.
Get_Entry(iElement);
2141 for(
int iFeature=0; iFeature<m_nFeatures; iFeature++)
2143 m_Centroid[iCluster][iFeature] += Feature[iFeature];
2148 for(iCluster=0; iCluster<nClusters; iCluster++)
2150 double d = m_nMembers[iCluster] > 0 ? 1. / m_nMembers[iCluster] : 0.;
2152 for(
int iFeature=0; iFeature<m_nFeatures; iFeature++)
2154 m_Centroid[iCluster][iFeature] *= d;
2165 double *Feature = (
double *)m_Features.
Get_Entry(iElement);
2167 double minVariance = -1.;
2168 int minCluster = -1;
2170 for(iCluster=0; iCluster<nClusters; iCluster++)
2172 double Variance = 0.;
2174 for(
int iFeature=0; iFeature<m_nFeatures; iFeature++)
2176 Variance +=
SG_Get_Square(m_Centroid[iCluster][iFeature] - Feature[iFeature]);
2179 if( minVariance < 0. || Variance < minVariance )
2181 minVariance = Variance;
2182 minCluster = iCluster;
2186 if( m_Clusters[iElement] != minCluster )
2188 m_Clusters[iElement] = minCluster;
2193 m_SP += minVariance;
2194 m_Variance[minCluster] += minVariance;
2201 _TL(
"pass" ), m_Iteration,
2202 _TL(
"change"), m_Iteration < 2 ? m_SP : SP_Last - m_SP
2207 if( nShifts == 0 || (nMaxIterations > 0 && nMaxIterations <= m_Iteration) )
2217 bool CSG_Cluster_Analysis::_Hill_Climbing(
bool bInitialize,
int nMaxIterations)
2219 int iElement, iCluster, nClusters = m_Variance.
Get_N();
2228 m_nMembers[iCluster = m_Clusters[iElement]]++;
2230 double *Feature = (
double *)m_Features.
Get_Entry(iElement);
2232 for(
int iFeature=0; iFeature<m_nFeatures; iFeature++)
2234 double d = Feature[iFeature];
2236 m_Centroid[iCluster][iFeature] += d;
2237 m_Variance[iCluster] += d*d;
2242 for(iCluster=0; iCluster<nClusters; iCluster++)
2244 double v = 0., d = m_nMembers[iCluster] <= 0 ? 0. : 1. / (double)m_nMembers[iCluster];
2246 for(
int iFeature=0; iFeature<m_nFeatures; iFeature++)
2248 m_Centroid[iCluster][iFeature] *= d;
2252 m_Variance[iCluster] -= v * m_nMembers[iCluster];
2256 double SP_Last = -1.;
int noShift = 0;
2262 iCluster = m_Clusters[iElement];
2264 if( noShift++ <
Get_nElements() && m_nMembers[iCluster] > 1 )
2266 int iFeature;
double *Feature = (
double *)m_Features.
Get_Entry(iElement);
2268 double V1, V2, Variance = 0.;
2270 for(iFeature=0; iFeature<m_nFeatures; iFeature++)
2272 Variance +=
SG_Get_Square(m_Centroid[iCluster][iFeature] - Feature[iFeature]);
2275 V1 = Variance * m_nMembers[iCluster] / (m_nMembers[iCluster] - 1.);
2281 for(
int jCluster=0; jCluster<nClusters; jCluster++)
2283 if( jCluster != iCluster )
2287 for(iFeature=0; iFeature<m_nFeatures; iFeature++)
2289 Variance +=
SG_Get_Square(m_Centroid[jCluster][iFeature] - Feature[iFeature]);
2292 V2 = Variance * m_nMembers[jCluster] / (m_nMembers[jCluster] + 1.);
2294 if( VMin < 0. || V2 < VMin )
2297 kCluster = jCluster;
2303 if( VMin >= 0 && VMin < V1 )
2306 m_Variance[iCluster] -= V1;
2307 m_Variance[kCluster] += VMin;
2308 V1 = 1. / (m_nMembers[iCluster] - 1.);
2309 V2 = 1. / (m_nMembers[kCluster] + 1.);
2311 for(iFeature=0; iFeature<m_nFeatures; iFeature++)
2313 double d = Feature[iFeature];
2315 m_Centroid[iCluster][iFeature] = (m_nMembers[iCluster] * m_Centroid[iCluster][iFeature] - d) * V1;
2316 m_Centroid[kCluster][iFeature] = (m_nMembers[kCluster] * m_Centroid[kCluster][iFeature] + d) * V2;
2319 m_Clusters[iElement] = kCluster;
2321 m_nMembers[iCluster]--;
2322 m_nMembers[kCluster]++;
2328 for(iCluster=0, m_SP=0.; iCluster<nClusters; iCluster++)
2330 m_SP += m_Variance[iCluster];
2336 _TL(
"pass" ), m_Iteration,
2337 _TL(
"change"), m_Iteration <= 1 ? m_SP : SP_Last - m_SP
2342 if( noShift >=
Get_nElements() || (nMaxIterations > 0 && nMaxIterations <= m_Iteration) )
2366 m_Threshold_Distance = 0.;
2367 m_Threshold_Angle = 0.;
2368 m_Threshold_Probability = 0.;
2369 m_Probability_Relative =
false;
2393 m_nFeatures = nFeatures;
2400 if( m_nClasses > 0 )
2402 for(
int i=0; i<m_nClasses; i++)
2404 delete(m_pClasses[i]);
2441 m_bWTA[Method] = bOn;
2461 int nFeatures = m_nFeatures;
Destroy(); m_nFeatures = nFeatures;
2471 if( !Data(
"classes") || !Data(
"features") || !Data[
"features"](
"count") || Data[
"features"][
"count"].Get_Content().asInt() != m_nFeatures || m_nFeatures == 0 )
2476 if( Data[
"features"](
"info") )
2486 if( Classes[i].Cmp_Name(
"class") && Classes[i].Get_Child(
"id") )
2490 CClass *pClass =
new CClass(Classes[i][
"id"].Get_Content());
2492 if( !pClass->m_Cov .from_String(Classes[i][
"cov" ].
Get_Content()) || pClass->m_Cov .Get_NX() != m_nFeatures || !pClass->m_Cov.is_Square() ) { bAdd =
false; }
2493 if( !pClass->m_Mean.from_String(Classes[i][
"mean"].
Get_Content()) || pClass->m_Mean.Get_N () != m_nFeatures ) { bAdd =
false; }
2494 if( !pClass->m_Min .from_String(Classes[i][
"min" ].
Get_Content()) || pClass->m_Min .Get_N () != m_nFeatures ) { bAdd =
false; }
2495 if( !pClass->m_Max .from_String(Classes[i][
"max" ].
Get_Content()) || pClass->m_Max .Get_N () != m_nFeatures ) { bAdd =
false; }
2504 m_pClasses = (CClass **)
SG_Realloc(m_pClasses, (m_nClasses + 1) *
sizeof(CClass *));
2505 m_pClasses[m_nClasses++] = pClass;
2507 pClass->m_Cov_Det = pClass->m_Cov.Get_Determinant();
2508 pClass->m_Cov_Inv = pClass->m_Cov.Get_Inverse();
2515 return( m_nClasses > 0 );
2521 if( m_nFeatures < 1 || m_nClasses < 1 || File.
is_Empty() )
2528 Data.
Set_Name (
"supervised_classifier");
2533 Features.
Add_Child(
"count", m_nFeatures);
2535 if( Feature_Info && *Feature_Info )
2537 Features.
Add_Child(
"info", Feature_Info);
2544 for(
int i=0; i<m_nClasses; i++)
2548 CClass *pClass = m_pClasses[i];
2551 Class.
Add_Child(
"mean", pClass->m_Mean.to_String());
2552 Class.
Add_Child(
"min" , pClass->m_Min .to_String());
2553 Class.
Add_Child(
"max" , pClass->m_Max .to_String());
2554 Class.
Add_Child(
"cov" , pClass->m_Cov .to_String());
2557 return( Data.
Save(File) );
2570 if( m_nFeatures > 0 && m_nClasses > 0 )
2574 for(
int iClass=0; iClass<m_nClasses; iClass++)
2576 CClass *pClass = m_pClasses[iClass];
2578 s +=
"\n____\n" + pClass->m_ID +
"\nFeature\tMean\tMin\tMax\tStdDev";
2580 for(
int i=0; i<m_nFeatures; i++)
2604 if( m_nFeatures < 1 || Mean.
Get_N() != m_nFeatures || Min.
Get_N() != m_nFeatures || Max.
Get_N() != m_nFeatures || Cov.
Get_NCols() != m_nFeatures || Cov.
Get_NRows() != m_nFeatures )
2609 CClass *pClass, **pClasses = (CClass **)
SG_Realloc(m_pClasses, (m_nClasses + 1) *
sizeof(CClass *));
2613 m_pClasses = pClasses;
2615 m_pClasses[m_nClasses++] = pClass =
new CClass(Class_ID);
2617 pClass->m_ID = Class_ID;
2619 pClass->m_Mean = Mean;
2620 pClass->m_Min = Min;
2621 pClass->m_Max = Max;
2622 pClass->m_Cov = Cov;
2643 for(
int i=0; i<m_nClasses; i++)
2645 m_pClasses[i]->m_Samples.Destroy();
2654 if( m_nFeatures > 0 && m_nFeatures == Features.
Get_N() )
2660 CClass **pClasses = (CClass **)
SG_Realloc(m_pClasses, (m_nClasses + 1) *
sizeof(CClass *));
2664 m_pClasses = pClasses;
2666 m_pClasses[iClass = m_nClasses++] =
new CClass(Class_ID);
2672 return( m_pClasses[iClass]->m_Samples.Add_Row(Features) );
2682 if( m_nFeatures < 1 || m_nClasses < 1 )
2687 for(
int iClass=0; iClass<m_nClasses; iClass++)
2689 if( !m_pClasses[iClass]->
Train() )
2695 if( bClear_Samples )
2709 bool CSG_Classifier_Supervised::CClass::Train(
void)
2711 if( m_Samples.Get_NCols() < 1 || m_Samples.Get_NRows() < 1 )
2719 m_Mean.Create(m_Samples.Get_NCols());
2720 m_Min .Create(m_Samples.Get_NCols());
2721 m_Max .Create(m_Samples.Get_NCols());
2723 for(iFeature=0; iFeature<m_Samples.Get_NCols(); iFeature++)
2727 for(
int iSample=0; iSample<m_Samples.Get_NRows(); iSample++)
2729 s += m_Samples[iSample][iFeature];
2738 m_Cov.Create(m_Samples.Get_NCols(), m_Samples.Get_NCols());
2740 for(iFeature=0; iFeature<m_Samples.Get_NCols(); iFeature++)
2742 for(
int jFeature=iFeature; jFeature<m_Samples.Get_NCols(); jFeature++)
2746 for(
int iSample=0; iSample<m_Samples.Get_NRows(); iSample++)
2748 cov += (m_Samples[iSample][iFeature] - m_Mean[iFeature]) * (m_Samples[iSample][jFeature] - m_Mean[jFeature]);
2751 if( m_Samples.Get_NRows() > 1 )
2753 cov /= m_Samples.Get_NRows() - 1;
2756 m_Cov[iFeature][jFeature] = m_Cov[jFeature][iFeature] = cov;
2760 m_Cov_Inv = m_Cov.Get_Inverse ();
2761 m_Cov_Det = m_Cov.Get_Determinant();
2777 if( m_nFeatures > 0 )
2811 return( Class >= 0 );
2870 void CSG_Classifier_Supervised::_Get_Binary_Encoding(
const CSG_Vector &Features,
int &Class,
double &Quality)
2874 CClass *pClass = m_pClasses[iClass];
2882 d += (Features(iFeature) < Mean_Spectral) == (pClass->m_Mean[iFeature] < pClass->m_Mean_Spectral) ? 0 : 1;
2886 d += (Features[iFeature ] < Features[iFeature + 1]) == (pClass->m_Mean[iFeature ] < pClass->m_Mean[iFeature + 1]) ? 0 : 1;
2890 d += (Features[iFeature - 1] < Features[iFeature ]) == (pClass->m_Mean[iFeature - 1] < pClass->m_Mean[iFeature ]) ? 0 : 1;
2894 d += (Features[iFeature - 1] < Features[iFeature + 1]) == (pClass->m_Mean[iFeature - 1] < pClass->m_Mean[iFeature + 1]) ? 0 : 1;
2898 if( Class < 0 || Quality > d )
2907 void CSG_Classifier_Supervised::_Get_Parallel_Epiped(
const CSG_Vector &Features,
int &Class,
double &Quality)
2911 CClass *pClass = m_pClasses[iClass];
2913 bool bMember =
true;
2917 bMember = pClass->m_Min[iFeature] <= Features[iFeature] && Features[iFeature] <= pClass->m_Max[iFeature];
2929 void CSG_Classifier_Supervised::_Get_Minimum_Distance(
const CSG_Vector &Features,
int &Class,
double &Quality)
2933 CClass *pClass = m_pClasses[iClass];
2935 double Distance = (Features - pClass->m_Mean).Get_Length();
2937 if( Class < 0 || Quality > Distance )
2944 if( m_Threshold_Distance > 0. && Quality > m_Threshold_Distance )
2951 void CSG_Classifier_Supervised::_Get_Mahalanobis_Distance(
const CSG_Vector &Features,
int &Class,
double &Quality)
2955 CClass *pClass = m_pClasses[iClass];
2959 double Distance = D * (pClass->m_Cov_Inv * D);
2961 if( Class < 0 || Quality > Distance )
2968 if( m_Threshold_Distance > 0. && Quality > m_Threshold_Distance )
2975 void CSG_Classifier_Supervised::_Get_Maximum_Likelihood(
const CSG_Vector &Features,
int &Class,
double &Quality)
2981 CClass *pClass = m_pClasses[iClass];
2985 double Distance = D * (pClass->m_Cov_Inv * D);
2987 double Probability = pow(2. *
M_PI, -0.5 * m_nFeatures) * pow(pClass->m_Cov_Det, -0.5) * exp(-0.5 * Distance);
2990 dSum += Probability;
2992 if( Class < 0 || Quality < Probability )
2994 Quality = Probability;
3001 if( m_Probability_Relative )
3003 Quality = 100. * Quality / dSum;
3006 if( m_Threshold_Probability > 0. && Quality < m_Threshold_Probability )
3014 void CSG_Classifier_Supervised::_Get_Spectral_Angle_Mapping(
const CSG_Vector &Features,
int &Class,
double &Quality)
3018 CClass *pClass = m_pClasses[iClass];
3020 double Angle = Features.
Get_Angle(pClass->m_Mean);
3022 if( Class < 0 || Quality > Angle )
3031 if( m_Threshold_Angle > 0. && Quality > m_Threshold_Angle )
3038 void CSG_Classifier_Supervised::_Get_Spectral_Divergence(
const CSG_Vector &Features,
int &Class,
double &Quality)
3043 void CSG_Classifier_Supervised::_Get_Winner_Takes_All(
const CSG_Vector &Features,
int &Class,
double &Quality)
3052 if( m_bWTA[iMethod] &&
Get_Class(Features, iClass, iQuality, iMethod) && ++Votes[iClass] > Quality )
3054 Quality = Votes[iClass];
3078 if( !T || !df || df < 1. )
3091 if( p <= 0. || p >= 1. || df < 1 )
3097 double t, p0, p1, diff;
3102 while( fabs(diff) > 0.0001 )
3104 t = Get_T_Inv(p1, df);
3105 diff = Get_T_P(t, df) - p0;
3109 return( bNegative ? -t : t );
3142 const double a1 = 0.0000053830, a2 = 0.0000488906, a3 = 0.0000380036,
3143 a4 = 0.0032776263, a5 = 0.0211410061, a6 = 0.0498673470;
3149 p = (((((a1 * z + a2) * z + a3) * z + a4) * z + a5) * z + a6) * z + 1.;
3151 return( pow(p, -16) );
3157 const double a0 = 2.5066282, a1 = -18.6150006, a2 = 41.3911977, a3 = -25.4410605,
3158 b1 = -8.4735109, b2 = 23.0833674, b3 = -21.0622410, b4 = 3.1308291,
3159 c0 = -2.7871893, c1 = -2.2979648, c2 = 4.8501413, c3 = 2.3212128,
3160 d1 = 3.5438892, d2 = 1.6370678;
3166 r = sqrt(-log(0.5 - p));
3167 z = (((c3 * r + c2) * r + c1) * r + c0) / ((d2 * r + d1) * r + 1.);
3172 z = p * (((a3 * r + a2) * r + a1) * r + a0) / ((((b4 * r + b3) * r + b2) * r + b1) * r + 1.);
3179 double CSG_Test_Distribution::Get_T_P(
double T,
int df)
3181 return( df == 1 ? 1. - 2. * atan(fabs(T)) /
M_PI
3182 : df == 2 ? 1. - fabs(T) / sqrt(T*T + 2.)
3183 : df == 3 ? 1. - 2. * (atan(fabs(T) / sqrt(3.)) + fabs(T) * sqrt(3.) / (T*T + 3.)) /
M_PI
3184 : df == 4 ? 1. - fabs(T) * (1. + 2. / (T*T + 4.)) / sqrt(T*T + 4.)
3190 double CSG_Test_Distribution::Get_T_Z(
double T,
int df)
3194 double A9, B9, T9, Z8, P7, B7, z;
3201 : A9 * (((1. - T9 * 0.75) * T9 / 3. - 0.5) * T9 + 1.) * T9;
3202 P7 = ((0.4 * Z8 + 3.3) * Z8 + 24.) * Z8 + 85.5;
3203 B7 = 0.8 * pow(Z8, 2.) + 100. + B9;
3204 z = (1. + (-P7 / B7 + Z8 + 3.) / B9) * sqrt(Z8);
3210 double CSG_Test_Distribution::Get_T_Inv(
double p,
int df)
3216 return( cos(p *
M_PI / 2.) / sin(p *
M_PI / 2.) );
3221 return( sqrt(2. / (p * (2. - p)) - 2.) );
3224 double a, b, c, d, x, y;
3226 a = 1. / (df - 0.5);
3228 c = ((20700. * a / b - 98.) * a - 16.) * a + 96.36;
3229 d = ((94.5 / (b + c) - 3.) / b + 1.) * sqrt(a *
M_PI / 2.) * df;
3231 y = pow(x, 2. / df);
3240 c = c + 0.3 * (df - 4.5) * (x + 0.6);
3243 c = (((0.05 * d * x - 5) * x - 7.) * x - 2.) * x + b + c;
3244 y = (((((0.4 * y + 6.3) * y + 36.) * y + 94.5) / c - y - 3.) / b + 1.) * x;
3258 y = ((1. / (((df + 6.) / (df * y) - 0.089 * d - 0.822) * (df + 2.) * 3.)
3259 + 0.5 / (df + 4.)) * y - 1.) * (df + 1.) / (df + 2.) + 1. / y;
3262 return( sqrt(df * y) );
3273 double F = (nSamples - nPredictors - 1) * (R2 / nPredictors) / (1. - R2);
3285 if( F >= 0.00001 && dfn > 0 && dfd > 0 )
3287 if( F * dfn >= dfd || F > 1. + 20. / dfn + 10. / sqrt((
double)dfn) )
3289 p = Get_Gamma(F, dfn, dfd);
3293 p = 1. - Get_Gamma(1. / F, dfd, dfn);
3297 if( p <= 0. || p >= 1. )
3299 p = F > 1. ? 0. : F < 1. ? 1. : 0.5;
3308 if( alpha < 0. || alpha > 1. || dfd < 0 || dfn < 0 )
3318 const int ITERMAX = 100;
3319 const double EPSILON = 0.0001;
3322 double lo, hi, mid, p;
3329 for(i=0; i<ITERMAX; i++)
3354 for(i=0; i<ITERMAX; i++)
3375 mid = (hi + lo) / 2.;
3377 for(i=0; i<ITERMAX && (hi-lo)>
EPSILON*mid; i++)
3379 mid = (hi + lo) / 2.;
3384 else if( p > alpha )
3394 double CSG_Test_Distribution::Get_Gamma(
double F,
double dfn,
double dfd)
3398 const double EXPMIN = -30.;
3399 const double SMALL = 0.00000000001;
3401 double x, c, er, s, n, t1, t;
3406 x = dfd / (dfd + dfn * F);
3407 c = Get_Log_Gamma(dfn + dfd) - Get_Log_Gamma(dfn) - Get_Log_Gamma(dfd + 1.) + dfd * log(x) + dfn * log(1. - x);
3423 while( t > er || t > t1 )
3427 t *= ((dfn + n) * x / (dfd + n));
3435 double CSG_Test_Distribution::Get_Log_Gamma(
double a)
3439 const int ARGMIN = 6;
3441 const double HL2PI = 0.91893853320467275;
3443 int n = (int)floor(ARGMIN - a + 0.0001);
3453 g = (1. - g * (1. / 30. - g * (1. / 105. - g * (1. / 140. - g / 99.)))) / (12. * a);
3454 g = g + ((a - 0.5) * log(a) - a + HL2PI);
3456 for(
int i=0; i<n; i++)
3475 int nVariables = Values.
Get_NX();
3476 int nSamples = Values.
Get_NY();
3483 C.Create(nVariables, nVariables);
3488 for(j=0; j<nVariables; j++)
3490 for(i=0; i<nSamples; i++)
3492 S[j] += Values[i][j];
3497 for(k=0; k<nVariables; k++)
3499 for(j=k; j<nVariables; j++)
3503 for(i=0; i<nSamples; i++)
3505 cov += (Values[i][j] - S[j].
Get_Mean()) * (Values[i][k] - S[k].Get_Mean());
3515 C[j][k] =
C[k][j] = cov;