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