contains rudimentary statistical functions
Revision 0:63c55a3cd73c, committed 2013-02-25
- Comitter:
- bclaus
- Date:
- Mon Feb 25 19:41:26 2013 +0000
- Commit message:
- initial commit of stats library;
Changed in this revision
normstat.cpp | Show annotated file Show diff for this revision Revisions of this file |
normstat.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/normstat.cpp Mon Feb 25 19:41:26 2013 +0000 @@ -0,0 +1,33 @@ + #include "normstat.h" + + + + + +double normpdf(double x, double m, double s) +{ + + return (1/(s*sqrt(2*pi)))*exp(-pow((x-m),2)/(2*pow(s,2))); + +} + +normrand::normrand(double m, double s){ + mu = m; + sigma = s; +} + +double normrand::draw() +{ + double r1 = (std::rand() + 1.0)/(RAND_MAX + 1.0); // gives equal distribution in (0, 1] + double r2 = (std::rand() + 1.0)/(RAND_MAX + 1.0); + return mu + sigma * std::sqrt(-2*std::log(r1))*std::cos(2*pi*r2); +} + +double normrand::draw(double m, double s) +{ + this->mu=m; + this->sigma=s; + double r1 = (std::rand() + 1.0)/(RAND_MAX + 1.0); // gives equal distribution in (0, 1] + double r2 = (std::rand() + 1.0)/(RAND_MAX + 1.0); + return mu + sigma * std::sqrt(-2*std::log(r1))*std::cos(2*pi*r2); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/normstat.h Mon Feb 25 19:41:26 2013 +0000 @@ -0,0 +1,33 @@ + + +#ifndef __NORMSTAT_H +#define __NORMSTAT_H +#include "mbed.h" +#include <cstdlib> // for rand +#include <cmath> // for atan, sqrt, log, cos +#include <algorithm> // for generate_n + +double const pi = 4*std::atan(1.0); + //double const pi = 3.141592653589793; + + +// simple function for normal distribution +double normpdf(double x, double m, double s); //returns f(x|m,s) + + +//Generates a normally distributed random number +//taken from http://rosettacode.org/wiki/Random_numbers#C.2B.2B +//on 21/02/2013 +//by Brian Claus +class normrand +{ +public: + normrand(double m, double s); + + double draw(); // returns a single normally distributed number + double draw(double m, double s); // returns a single normally distributed number + +private: + double mu, sigma; +}; +#endif \ No newline at end of file