Created
January 4, 2019 07:56
-
-
Save kohnakagawa/437e96dea624e0913a625dcb5953ce33 to your computer and use it in GitHub Desktop.
GSLでquantileを計算する例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <tuple> | |
#include <vector> | |
#include <algorithm> | |
#include <utility> | |
#include <gsl/gsl_statistics.h> | |
template<typename T> | |
std::tuple<double, double, double> make_quantile(const std::vector<T>& input) { | |
std::vector<double> sorted_buffer(input.size()); | |
for (size_t i = 0; i < input.size(); i++) sorted_buffer[i] = input[i]; | |
std::sort(sorted_buffer.begin(), sorted_buffer.end()); | |
return { | |
gsl_stats_quantile_from_sorted_data(sorted_buffer.data(), 1, sorted_buffer.size(), 0.25), | |
gsl_stats_median_from_sorted_data(sorted_buffer.data(), 1, sorted_buffer.size()), | |
gsl_stats_quantile_from_sorted_data(sorted_buffer.data(), 1, sorted_buffer.size(), 0.75) | |
}; | |
} | |
int main() { | |
std::vector<double> sorted_data {-2, -1, -1, -1, 2, 3, 3, 4, 4, 5, 6, 10, 12, 13}; | |
const auto [q0, median, q1] = make_quantile(sorted_data); | |
std::cout << q0 << std::endl; | |
std::cout << q1 << std::endl; | |
std::cout << median << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment