-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHistogram.cpp
More file actions
92 lines (71 loc) · 1.77 KB
/
Histogram.cpp
File metadata and controls
92 lines (71 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "Histogram.h"
#include "minval.h"
#include "maxval.h"
using namespace quantserver;
Histogram::Histogram(TimeSeries& timeSeries , int numberOfSegments)
{
histogramItems = HistogramItemVectorPtr(new std::vector<HistogramItemPtr>());
minval minv; maxval maxv;
BIGDECIMAL min = minv(timeSeries);
BIGDECIMAL max = maxv(timeSeries);
BIGDECIMAL interval = (max - min)/numberOfSegments ;
BIGDECIMAL curr = min;
int i = 0 ;
//Initiate the buckets
for(i = 0 ; i < numberOfSegments ; i++ )
{
HistogramItemPtr itemToAdd(new RangedHistogramItem(curr,curr + interval));
curr += interval;
histogramItems->push_back(itemToAdd);
}
//Register the items of the time series
int seriesSize = timeSeries.size();
int halfPoint = seriesSize/2;
for(i = 0 ; i < seriesSize ; i++)
{
PriceAtDate& currRef = timeSeries[i] ;
for(int j = 0 ; j < numberOfSegments ; j++ )
{
HistogramItemPtr item = histogramItems->at(j);
if(item->accept(currRef))
{
break;
}
}
}
//Find the median. The histogram currently has all its members sorted
unsigned int accumulateItems = 0;
for(i = 0 ; i < numberOfSegments ; i++ )
{
HistogramItemPtr item = histogramItems->at(i);
if(accumulateItems <= halfPoint && (accumulateItems + item->getCount()) >= halfPoint )
{
medianIndex = i;
break;
}
accumulateItems += item->getCount();
}
}
Histogram::~Histogram(void)
{
histogramItems->clear();
}
int Histogram::size()
{
return histogramItems->size();
}
unsigned int Histogram::getMedianIndex()
{
return medianIndex;
}
BIGDECIMAL Histogram::median()
{
return (*this)[medianIndex].val();
}
HistogramItem& Histogram::operator[](unsigned int i)
{
unsigned int arraySz = size();
if( i < 0 ) {i = 0;};
if( i >= arraySz ) {i = arraySz -1;};
return *(histogramItems->at(i)) ;
}