1. Perl
  2. Module
  3. PDL

Statistical functions in PDL

Here are some basic statistical functions.

Maximum

To find the maximum from a one-dimensional PDL variable, use the max method .

use PDL;

# data
my $nums = pdl [4, 2, 7];

# Maximum value
my $max = $nums->max;

The PDL::Ufunc module provides functions for calculating statistics from PDL variable.

Minimum

Use the min method to find the minimum .

# minimum value
my $min = $nums->min;

Total

Use the sum method to find the sum .

# total
my $total = $nums->sum;

Average

Use the avg method to find the average .

my $average = $nums->avg;

Median

Use the median method to find the median .

my $median = $nums->median;

The median is the value that comes in the middle when all the values are listed.

Mode

Use the mode method to find the mode .

my $mode = $nums->mode;

The mode is the value that appears most often.

Sort

Use the qsort method to sort in ascending order.

# Ascending sort
my $nums_sort_asc = $nums->qsort;

To sort in descending order, combine ascending sort and slice.

use PDL::NiceSlice;
my $nums_sort_desc = $nums_sort_asc (-1: 0);
The meaning of "-1: 0" means that the first -1 is the last element. 0 means the first element. "-1: 0" means from the last element to the first element, and the order can be reversed.

Example

This is an example that can be executed.

use strict;
use warnings;

use PDL;
use PDL::NiceSlice;

# Create data
my $nums = pdl [4, 4, 2, 2, 2, 7];

# Maximum value
my $max = $nums->max;

# minimum value
my $min = $nums->min;

# total
my $total = $nums->sum;

# Average
my $average = $nums->avg;

# Median
my $median = $nums->median;

# Mode
my $mode = $nums->mode;

print "Max: $max\n";
print "Min: $min\n";
print "Total: $total\n";
print "Average: $average\n";
print "Median: $median\n";
print "Mode: $mode\n";

# Ascending sort
my $nums_sort_asc = $nums->qsort;

# Descending sort(combined with ascending sort and slice)
my $nums_sort_desc = $nums_sort_asc (-1: 0);

print "$nums_sort_asc\n";
print "$nums_sort_desc\n";

Install PDL::Stats::Basic

The following statistical functions are made available by a module called PDL::Stats::Basic . You need to install PDL::Stats.

cpan PDL::Stats;

Loading PDL::Stats::Basic makes the functions defined in PDL::Stats::Basic available from PDL variable.

use PDL::Stats::Basic;

Distributed

To find the distributed , use the var method.

use PDL;
use PDL::Stats::Basic;

# data
my $nums = pdl [4, 2, 7];

# Distributed
my $variance = $nums->var;

Bias of an estimulus

To find the unbiased variance , use the var_unbiased method.

# Unbiased dispersion
my $variance = $nums->var_unbiased;

Bias of an estimulus has a denominator of (n -1).

Standard deviation

To find the standard deviation , use the stdv method.

# standard deviation
my $stdv = $nums->stdv;

The meaning of the standard deviation is an indication of how scattered the data is. The standard deviation formula is the "positive square root of the variance". Variance also indicates how scattered the data is, but because the units are different from the original data, it is difficult to compare with the original data. If you take the positive square root and make it the standard deviation, the unit will be the same as the unit of the original data.

Bias of an estimulus

To find the unbiased standard deviation , use the stdv_unbiased method.

# Unbiased standard deviation
my $stdv = $nums->stdv_unbiased;

The unbiased standard deviation formula is the "positive square root of the unbiased variance".

Standard error

To find the standard error , use the se method.

# Standard error
my $se = $nums->se;

Sum of deviation squares

To find the sum of squared deviations , use the ss method.

# Deviation square sum
my $ss = $nums->ss;

Skewness

To find the skewness , use the skew method.

# Skewness
my $skew = $nums->skew;

Bias of an estimulus

To find the unbiased skewness , use the skew_unbiased method.

# Bias of an estimulus
my $skew_unbiased = $nums->skew_unbiased;

Kurtosis

To find the kurtosis , use the kurt method.

# Kurtosis
my $kurt = $nums->kurt;

Bias of an estimulus

To find the unbiased kurtosis , use the kurt_unbiased method.

# Bias of an estimulus
my $kurt_unbiased = $nums->kurt_unbiased;

Example

This is an example that can be executed.

use strict;
use warnings;

use PDL;
use PDL::Stats::Basic;

# data
my $nums = pdl [4, 2, 7, 3, 5];

# Distributed
my $variance = $nums->var;

# Unbiased dispersion
my $variance_unbiased = $nums->var_unbiased;

# standard deviation
my $stdv = $nums->stdv;

# Unbiased standard deviation
my $stdv_unbiased = $nums->stdv_unbiased;

# Standard error
my $se = $nums->se;

# Deviation square sum
my $ss = $nums->ss;

# Skewness
my $skew = $nums->skew;

# Bias of an estimulus
my $skew_unbiased = $nums->skew_unbiased;

# Kurtosis
my $kurt = $nums->kurt;

# Bias of an estimulus
my $kurt_unbiased = $nums->kurt_unbiased;

print "Variance: $variance\n";
print "Unbiased variance: $variance_unbiased\n ";
print "Standard deviation: $stdv\n";
print "Unbiased standard deviation: $stdv_unbiased\n";
print "Standard error: $se\n";
print "Sum of squared deviations: $ss\n";
print "Skew: $skew\n";
print "Unbiased Skew: $skew\n";
print "Kurtosis: $kurt\n";
print "Unbiased kurtosis: $kurt_unbiased\n";

Related Informatrion