Add statistics module, more functions to contribute.

This commit is contained in:
houyang
2008-05-05 21:52:21 +00:00
parent e2496bcaaf
commit ef2cda3d1c
3 changed files with 105 additions and 2 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
librule(
sources = ["discrete.cc", "geometry.cc"],
headers = ["discrete.h", "geometry.h", "kernel.h", "math.h"],
sources = ["discrete.cc", "geometry.cc", "statistics.cc"],
headers = ["discrete.h", "geometry.h", "statistics.h", "kernel.h", "math.h"],
deplibs = ["fastlib/base:base", "fastlib/col:col"]
)
+48
View File
@@ -0,0 +1,48 @@
// Copyright 2007 Georgia Institute of Technology. All rights reserved.
// ABSOLUTELY NOT FOR DISTRIBUTION
/**
* @file statistics.cc
*
* Implementation for statistics helpers.
*/
#include "statistics.h"
#include "math.h"
namespace math {
double Mean(Vector V) {
double c = 0.0;
index_t n = V.length();
for (index_t i=0; i<n; i++)
c = c + V[i];
return c / n;
}
double Var(Vector V) {
double c = 0.0, mean, va, ep;
index_t n = V.length();
for (index_t i=0; i<n; i++)
c = c + V[i];
mean = c / n;
ep = 0.0; va = 0.0;
for (index_t i=0; i<n; i++) {
c = V[i] - mean;
ep = ep + c;
va = va + c * c;
}
return (va - ep * ep / n) / (n - 1);
}
double Std(Vector V) {
return sqrt( Var(V) );
}
double Sigmoid(double x) {
return 1.0 / ( 1.0 + exp(-x) );
}
};
+55
View File
@@ -0,0 +1,55 @@
// Copyright 2007 Georgia Institute of Technology. All rights reserved.
// ABSOLUTELY NOT FOR DISTRIBUTION
/**
* @file statistics.h
*
* Statistics utilities.
*/
#ifndef MATH_STATISTICS_H
#define MATH_STATISTICS_H
#include "fastlib/base/base.h"
#include "fastlib/la/matrix.h"
#include <math.h>
namespace math {
/**
* Computes the mean value of a vector.
* Don't forget initializing V before using this function
*
* @param V the input vector
* @return the mean value
*/
double Mean(Vector V);
/**
* Computes the variance of a vector using "corrected two-pass algorithm".
* See "Numerical Recipes in C" for reference.
* Don't forget initializing V before using this function.
*
* @param V the input vector
* @return the variance
*/
double Var(Vector V);
/**
* Computes the standard deviation of a vector.
* Don't forget initializing V before using this function.
*
* @param V the input vector
* @return the standard deviation
*/
double Std(Vector V);
/**
* Computes the sigmoid function of a real number x
* Sigmoid(x) = 1/[1+exp(-x)]
*
* @param x the input real number
* @return the sigmoid function value
*/
double Sigmoid(double x);
};
#endif