Created
December 2, 2009 09:03
-
-
Save hryk/247065 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/local/bin/perl | |
use strict; | |
use warnings; | |
use Math::GSL::Statistics qw/:all/; | |
# gsl113.pdf p315 | |
# 20.9 中央値と百分位数 | |
# | |
# gsl_stats_quantile_from_sorted_data($sorted_data, $stride, $n, $f) | |
# | |
=begin | |
{ | |
double data[5] = {17.2, 18.1, 16.5, 18.3, 12.6}; | |
double median, upperq, lowerq; | |
printf("Original dataset: %g, %g, %g, %g, %g\n", | |
data[0], data[1], data[2], data[3], data[4]); | |
gsl_sort (data, 1, 5); | |
printf("Sorted dataset: %g, %g, %g, %g, %g\n", | |
data[0], data[1], data[2], data[3], data[4]); | |
median = gsl_stats_median_from_sorted_data (data, 1, 5); | |
upperq = gsl_stats_quantile_from_sorted_data(data, 1, 5, 0.75); | |
lowerq = gsl_stats_quantile_from_sorted_data(data, 1, 5, 0.25); | |
printf("The median is %g\n", median); | |
printf("The upper quartile is %g\n", upperq); | |
printf("The lower quartile is %g\n", lowerq); | |
return 0; | |
} | |
このプログラムは以下のように出力する。 | |
Original dataset: 17.2, 18.1, 16.5, 18.3, 12.6 | |
Sorted dataset: 12.6, 16.5, 17.2, 18.1, 18.3 | |
The median is 17.2 | |
The upper quartile is 18.1 | |
The lower quartile is 16.5 | |
20.11 | |
=cut | |
my $correct = <<EOF | |
Correct | |
Original dataset: 17.2, 18.1, 16.5, 18.3, 12.6 | |
Sorted dataset: 12.6, 16.5, 17.2, 18.1, 18.3 | |
The median is 17.2 | |
The upper quartile is 18.1 | |
The lower quartile is 16.5 | |
20.11 | |
EOF | |
; | |
print $correct; | |
my $sorted_data = [ sort {$a <=> $b} qw/17.2 18.1 16.5 18.3 12.6/ ]; | |
my $median = gsl_stats_median_from_sorted_data( $sorted_data, 1, scalar(@$sorted_data) ); | |
my $upperq = gsl_stats_quantile_from_sorted_data( $sorted_data, 1, scalar(@$sorted_data) , 0.75); | |
my $lowerq = gsl_stats_quantile_from_sorted_data( $sorted_data, 1, scalar(@$sorted_data) , 0.25); | |
print "The median is ${median}\n"; | |
print "The upper quantile is ${upperq}\n"; | |
print "The lower quantile is ${lowerq}\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment