Commit 9b863dc9 authored by Davis King's avatar Davis King

Added the following functions for computing statistics on vectors:

mean_sign_agreement(), correlation(), covariance(), r_squared(),
and mean_squared_error()
parent 14b6eed5
......@@ -383,6 +383,137 @@ namespace dlib
T n;
};
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double mean_sign_agreement (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
)
{
// make sure requires clause is not broken
DLIB_ASSERT(a.size() == b.size(),
"\t double mean_sign_agreement(a,b)"
<< "\n\t a and b must be the same length."
<< "\n\t a.size(): " << a.size()
<< "\n\t b.size(): " << b.size()
);
double temp = 0;
for (unsigned long i = 0; i < a.size(); ++i)
{
if (a[i] >= 0 && b[i] >= 0 ||
a[i] < 0 && b[i] < 0)
{
temp += 1;
}
}
return temp/a.size();
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double correlation (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
)
{
// make sure requires clause is not broken
DLIB_ASSERT(a.size() == b.size() && a.size() > 1,
"\t double correlation(a,b)"
<< "\n\t a and b must be the same length and have more than one element."
<< "\n\t a.size(): " << a.size()
<< "\n\t b.size(): " << b.size()
);
running_scalar_covariance<double> rs;
for (unsigned long i = 0; i < a.size(); ++i)
{
rs.add(a[i], b[i]);
}
return rs.correlation();
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double covariance (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
)
{
// make sure requires clause is not broken
DLIB_ASSERT(a.size() == b.size() && a.size() > 1,
"\t double covariance(a,b)"
<< "\n\t a and b must be the same length and have more than one element."
<< "\n\t a.size(): " << a.size()
<< "\n\t b.size(): " << b.size()
);
running_scalar_covariance<double> rs;
for (unsigned long i = 0; i < a.size(); ++i)
{
rs.add(a[i], b[i]);
}
return rs.covariance();
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double r_squared (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
)
{
// make sure requires clause is not broken
DLIB_ASSERT(a.size() == b.size() && a.size() > 1,
"\t double r_squared(a,b)"
<< "\n\t a and b must be the same length and have more than one element."
<< "\n\t a.size(): " << a.size()
<< "\n\t b.size(): " << b.size()
);
return std::pow(correlation(a,b),2.0);
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double mean_squared_error (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
)
{
// make sure requires clause is not broken
DLIB_ASSERT(a.size() == b.size(),
"\t double mean_squared_error(a,b)"
<< "\n\t a and b must be the same length."
<< "\n\t a.size(): " << a.size()
<< "\n\t b.size(): " << b.size()
);
return mean(squared(matrix_cast<double>(vector_to_matrix(a))-matrix_cast<double>(vector_to_matrix(b))));
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -308,6 +308,100 @@ namespace dlib
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double mean_sign_agreement (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
);
/*!
requires
- a.size() == b.size()
ensures
- returns the number of times a[i] has the same sign as b[i] divided by
a.size(). So we return the probability that elements of a and b have
the same sign.
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double correlation (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
);
/*!
requires
- a.size() == b.size()
- a.size() > 1
ensures
- returns the correlation coefficient between all the elements of a and b.
(i.e. how correlated is a(i) with b(i))
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double covariance (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
);
/*!
requires
- a.size() == b.size()
- a.size() > 1
ensures
- returns the covariance between all the elements of a and b.
(i.e. how does a(i) vary with b(i))
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double r_squared (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
);
/*!
requires
- a.size() == b.size()
- a.size() > 1
ensures
- returns the R^2 coefficient of determination between all the elements of a and b.
This value is just the square of correlation(a,b).
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename alloc
>
double mean_squared_error (
const std::vector<T,alloc>& a,
const std::vector<T,alloc>& b
);
/*!
requires
- a.size() == b.size()
ensures
- returns the mean squared error between all the elements of a and b.
(i.e. mean(squared(vector_to_matrix(a)-vector_to_matrix(b))))
!*/
// ----------------------------------------------------------------------------------------
template <
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment