+ replace float with double

+ improved comments
This commit is contained in:
Rob Tillaart 2013-09-29 19:32:20 +02:00
parent be3ad90e3f
commit a9e0b6775b
2 changed files with 52 additions and 25 deletions

View File

@ -7,13 +7,14 @@
// HISTORY:
// 0.1.0 - 2012-11-10 initial version
// 0.1.1 - 2012-11-10 added PMF() and CDF()
// 0.1.2 - 2012-12-23 changed float to double; some comments
//
// Released to the public domain
//
#include "histogram.h"
Histogram::Histogram(uint8_t len, float *bounds)
Histogram::Histogram(uint8_t len, double *bounds)
{
_bounds = bounds;
_len = len;
@ -23,7 +24,7 @@ Histogram::Histogram(uint8_t len, float *bounds)
Histogram::~Histogram()
{
free(_data);
free(_data); // free may still has a bug :(
}
// resets all counters
@ -37,14 +38,14 @@ void Histogram::clear()
}
// adds a new value to the histogram - increasing
void Histogram::add(float f)
void Histogram::add(double f)
{
_data[find(f)]++;
_cnt++;
}
// adds a new value to the histogram - decreasing
void Histogram::sub(float f)
void Histogram::sub(double f)
{
_data[find(f)]--;
_cnt++;;
@ -62,35 +63,60 @@ unsigned long Histogram::count()
return _cnt;
}
// returns the absolute count of a bucket
// returns the count of a bucket
long Histogram::bucket(uint8_t idx)
{
if (idx > _len) return 0;
if (idx > _len+1) return 0;
return _data[idx];
}
#define frequency(x) PMF(x)
// returns the relative frequency of a bucket
float Histogram::PMF(uint8_t idx)
double Histogram::frequency(uint8_t idx)
{
if (_cnt < 1) return NAN;
if (_cnt == 0) return NAN;
if (idx > _len+1) return 0;
return _data[idx]/ _cnt;
return (1.0 * _data[idx]) / _cnt;
}
// returns the relative frequency of a bucket
float Histogram::CDF(uint8_t idx)
// returns the probability of the bucket of a value
double Histogram::PMF(double val)
{
if (_cnt < 1) return NAN;
if (idx > _len+1) idx = _len+1;
if (_cnt == 0) return NAN;
uint8_t idx = find(val);
return (1.0 *_data[idx]) / _cnt;
}
// returns the cummulative probability of
// values <= value
double Histogram::CDF(double val)
{
if (_cnt == 0) return NAN;
uint8_t idx = find(val);
long sum = 0;
for (uint8_t i=0; i<= idx; i++) sum += _data[i];
return sum/ _cnt;
return (1.0 * sum) / _cnt;
}
// EXPERIMENTAL
// returns the value of the original array for
// which the CDF is at least prob.
double Histogram::VAL(double prob)
{
if (_cnt == 0) return NAN;
if (prob < 0.0) prob = 0.0;
if (prob > 1.0) prob = 1.0;
long sum = 0;
for (uint8_t i = 0; i <= _len; i++)
{
sum += _data[i];
if (sum >= (prob * _cnt)) return _bounds[i];
}
return INFINITY;
}
// returns the bucket number for value f
uint8_t Histogram::find(float f)
uint8_t Histogram::find(double f)
{
uint8_t i = 0;
while(i < _len && f > _bounds[i]) i++;

View File

@ -18,22 +18,23 @@
class Histogram
{
public:
Histogram(uint8_t len, float *bounds);
Histogram(uint8_t len, double *bounds);
~Histogram();
void clear();
void add(float val);
void sub(float val);
void add(double val);
void sub(double val);
uint8_t size();
unsigned long count();
long bucket(uint8_t idx);
float frequency(uint8_t idx);
float PMF(uint8_t idx);
float CDF(uint8_t idx);
uint8_t find(float f);
double frequency(uint8_t idx);
double PMF(double val);
double CDF(double val);
double VAL(double prob);
uint8_t find(double f);
// void strategy();
protected:
float * _bounds;
double * _bounds;
long * _data;
uint8_t _len;
unsigned long _cnt;