There are a few steps to this:
1. Populate your vector of values;
2. Make a vector representing the buckets and determine the range of each bucket.
3. for each bucket, go through the list of values and check if it falls in that range, if so, increment a frequency.
To find the range for the buckets do the following:
1. Find the minimum and maximum of the vector of values.
2. for i is 1 to 10, the bucket ranges can be determined by:
minimum = i * (max-min)/numberOfBuckets
maximum = (i+1) * (max-min)/numberOfBuckets.
Here is an algorithm below. If you want to figure it out yourself
stop reading now.
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
|
struct bucket
{
double minimum;
double maximum;
double frequency;
};
vector<double> values;
vector<bucket> buckets;
//Populate values here...
//get min and max:
double min=999999, max=-999999;
for (vector<double>::iterator it = values.begin(); it < values.end(); it++)
{
if (*it < min) min = *it;
if (*it > max) max = *it;
}
//create buckets
for (int i = 1; i < 11; i++)
{
bucket temp = { i * (max-min)/10. , (i+1) * (max-min)/10. , 0.};
buckets.push_back(temp);
}
//populate the probability density function
double weight = 1./(double)values.size();
for (vector<bucket>::iterator it = buckets.begin(); it < buckets.end(); it++)
for (vector<double>::iterator jt = values.begin(); jt < values.end(); jt++)
if (*jt <= *it->maximum && *jt > *it->minimum) *it->frequency += weight;
|