std::vector<double> RigStatisticsMath::calculateNearestRankPercentiles(const std::vector<double> & inputValues, const std::vector<double>& pValPositions) { std::vector<double> sortedValues; sortedValues.reserve(inputValues.size()); for (size_t i = 0; i < inputValues.size(); ++i) { if (inputValues[i] != HUGE_VAL) { sortedValues.push_back(inputValues[i]); } } std::sort(sortedValues.begin(), sortedValues.end()); std::vector<double> percentiles(pValPositions.size(), HUGE_VAL); if (sortedValues.size()) { for (size_t i = 0; i < pValPositions.size(); ++i) { double pVal = HUGE_VAL; size_t pValIndex = static_cast<size_t>(sortedValues.size() * cvf::Math::abs(pValPositions[i]) / 100); if (pValIndex >= sortedValues.size() ) pValIndex = sortedValues.size() - 1; pVal = sortedValues[pValIndex]; percentiles[i] = pVal; } } return percentiles; };
Vector<T> CDFTools<T>::percentile( const vector<const Vector<T>*>& x, const Vector<T>& xEvals, const float percent) const { Matrix<T> matrix; Vectorf percents( 1 ); percents[0] = percent; matrix = percentiles( x, xEvals, percents ); return matrix[0]; /* const int numSamples = x.size(); const int numEvals = xEvals.getSize(); Matrix<T> matrix( numEvals, numSamples ); Vector<T> y( numEvals ); for (int i = 0; i < numSamples; i++) { matrix.setColumn( i, cdf(*x[i],xEvals) ); } for (int j = 0; j < numEvals; j++) { matrix[j].sort(); y[j] = matrix[j].percentile( percent ); } return y;*/ }
Matrix<T> CDFTools<T>::percentiles( const Matrix<T>& x, const Vector<T>& xEvals, const Vectorf& percents) const { const int n = x.getSize1(); vector<const Vector<T>*> v( n ); for (int i = 0; i < n; ++i) { v[i] = &x[i]; } return percentiles( v, xEvals, percents ); }
//-------------------------------------------------------------------------------------------------- /// Calculate the percentiles of /a inputValues at the pValPosition percentages by interpolating input values. /// This method treats HUGE_VAL as "undefined" values, and ignores these. Will return HUGE_VAL if /// the inputValues does not contain any valid values //-------------------------------------------------------------------------------------------------- std::vector<double> RigStatisticsMath::calculateInterpolatedPercentiles(const std::vector<double> & inputValues, const std::vector<double>& pValPositions) { std::vector<double> sortedValues; sortedValues.reserve(inputValues.size()); for (size_t i = 0; i < inputValues.size(); ++i) { if (inputValues[i] != HUGE_VAL) { sortedValues.push_back(inputValues[i]); } } std::sort(sortedValues.begin(), sortedValues.end()); std::vector<double> percentiles(pValPositions.size(), HUGE_VAL); if (sortedValues.size()) { for (size_t i = 0; i < pValPositions.size(); ++i) { double pVal = HUGE_VAL; double doubleIndex = (sortedValues.size() - 1) * cvf::Math::abs(pValPositions[i]) / 100.0; size_t lowerValueIndex = static_cast<size_t>(floor(doubleIndex)); size_t upperValueIndex = lowerValueIndex + 1; double upperValueWeight = doubleIndex - lowerValueIndex; assert(upperValueWeight < 1.0); if (upperValueIndex < sortedValues.size()) { pVal = (1.0 - upperValueWeight) * sortedValues[lowerValueIndex] + upperValueWeight * sortedValues[upperValueIndex]; } else { pVal = sortedValues[lowerValueIndex]; } percentiles[i] = pVal; } } return percentiles; }