Question

I am working on two functions to calculate the mean absolute deviation and median absolute deviation of a vector data set. I am using an overloaded calcAverage function inside of them. The problem is, I am returning the same incorrect value for both function calls.

This is the output. Is it giving me scientific notation or something?

Mean absolute deviation = 4.09929e-016
Median absolute deviation = 4.09929e-016

This is the mean absolute distribution function:

double calcMeanAD(vector<int> data_set){

vector<double> lessMean;
double mean = calcAverage(data_set);

for (auto it = data_set.begin(); it != data_set.end(); ++it){
    lessMean.push_back(*it);
}

for (auto it = lessMean.begin(); it != lessMean.end(); ++it){
    *it -= mean;
}

return calcAverage(lessMean);

}

This is the median absolute distribution function:

double calcMedAD(vector<int> data_set){

vector<double> lessMed;
double median = calcAverage(data_set);

for (auto it = data_set.begin(); it != data_set.end(); ++it){
    lessMed.push_back(static_cast<double>(*it));
}

for (auto it = lessMed.begin(); it != lessMed.end(); ++it){
    *it -= median;
}

return calcAverage(lessMed);

}

Can anybody spot something/s that is/are wrong? Thanks.

Was it helpful?

Solution

Both functions are returning zero with some roundoff error.

Write down an algebraic expression for the values you are trying to calculate, and compare that with your code.

I don't know what calcAverage does, but it's not overloaded; you are calling it with a vector<double> both times. There is no way it can calculate both a mean and a median.

Hint: you seem to have missed the meanings of absolute and median

OTHER TIPS

The median absolute deviation (MAD) is calculated as the median of the absolute value of each value of vector minus the median of all elements. So you should provide in you code a sort() function for vector elements to find median of values and use fabs() to calculate differences that should be sorted again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top