|
00001 /*!@addtogroup other 00002 * @{ 00003 * @defgroup stats Statistics Library 00004 * Statistics Library 00005 * @{ 00006 */ 00007 00008 /* 00009 * $Id: stats.h 133 2013-03-10 15:15:38Z xander $ 00010 */ 00011 00012 #ifndef __STATS_H__ 00013 #define __STATS_H__ 00014 /** \file stats.h 00015 * \brief Statistics functions for ROBOTC. 00016 * 00017 * stats.h provides some statiscal functions for ROBOTC. 00018 * 00019 * Taken from http://www.cs.princeton.edu/introcs/21function/MyMath.java.html 00020 * and modified to compile under ROBOTC. 00021 * 00022 * License: You may use this code as you wish, provided you give credite where its due. 00023 * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 00024 00025 * 00026 * Changelog: 00027 * - 0.1: Initial release 00028 * 00029 * \author Xander Soldaat (xander_at_botbench.com) 00030 * \date 10 October 2010 00031 * \version 0.1 00032 * \example stats-test1.c 00033 */ 00034 00035 00036 /** 00037 * Error Function, subject to catastrophic cancellation when z 00038 * is very close to 0 00039 * @param z the number to be, ehm, error checked. 00040 * @return the value 00041 */ 00042 float erf(float z) { 00043 float t = 1.0 / (1.0 + 0.5 * abs(z)); 00044 00045 // use Horner's method 00046 float ans = 1 - t * exp( -z*z - 1.26551223 + 00047 t * ( 1.00002368 + 00048 t * ( 0.37409196 + 00049 t * ( 0.09678418 + 00050 t * (-0.18628806 + 00051 t * ( 0.27886807 + 00052 t * (-1.13520398 + 00053 t * ( 1.48851587 + 00054 t * (-0.82215223 + 00055 t * ( 0.17087277)))))))))); 00056 if (z >= 0) return ans; 00057 else return -ans; 00058 } 00059 00060 /** 00061 * Cumulative normal distribution 00062 * @param z the number to check 00063 * @return the probability 00064 */ 00065 float Phi(float z) { 00066 return 0.5 * (1.0 + erf(z / (sqrt(2.0)))); 00067 } 00068 00069 /** 00070 * Cumulative normal distribution with mean mu and std deviation sigma 00071 * @param z the number to check 00072 * @param mu mean value for x 00073 * @param sigma std dev for x 00074 * @return the probability 00075 */ 00076 float Phi(float z, float mu, float sigma) { 00077 return Phi((z - mu) / sigma); 00078 } 00079 00080 00081 /** 00082 * Random number with standard Gaussian distribution 00083 * @return random number with standard Gaussian distribution 00084 */ 00085 float gaussian() { 00086 float U = random[32767]/32767; // should be a number between 0 and 1.0 00087 float V = random[32767]/32767; // should be a number between 0 and 1.0 00088 return sin(2 * PI * V) * sqrt((-2 * log(1 - U))); // sin() returns radians, should this be degrees? 00089 } 00090 00091 // random number with Gaussian distribution of mean mu and stddev sigma 00092 float gaussian(float mu, float sigma) { 00093 return mu + sigma * gaussian(); 00094 } 00095 00096 00097 #endif // __STATS_H__ 00098 00099 /* 00100 * $Id: stats.h 133 2013-03-10 15:15:38Z xander $ 00101 */ 00102 /* @} */ 00103 /* @} */