Add statistics module, more functions to contribute.
This commit is contained in:
@@ -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"]
|
||||
)
|
||||
|
||||
|
||||
@@ -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) );
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user