public static DoubleMatrix[] Quantile(
ReadOnlyDoubleMatrix data,
DoubleMatrix probabilities,
DataOperation dataOperation
)
Public Shared Function Quantile (
data As ReadOnlyDoubleMatrix,
probabilities As DoubleMatrix,
dataOperation As DataOperation
) As DoubleMatrix()
public:
static array<DoubleMatrix^>^ Quantile(
ReadOnlyDoubleMatrix^ data,
DoubleMatrix^ probabilities,
DataOperation dataOperation
)
static member Quantile :
data : ReadOnlyDoubleMatrix *
probabilities : DoubleMatrix *
dataOperation : DataOperation -> DoubleMatrix[]
The quantile of a distribution
for the probability
can be defined as follows:
By interpreting rows or columns of data as samples drawn from
random variables, this method returns the quantile estimates of their distributions
for the specified probabilities. They are computed as proposed by Hyndman and Fan (1996)[1].
Let be
the Length of probabilities and
define
Operating on rows
If dataOperation is OnRows,
then the method returns an array of DoubleMatrix instances, one for each row in data.
The i-th array entry contains the quantiles of the i-th
row. It has the same dimensions of probabilities.
Its k-th
linear position is occupied by the row quantile
corresponding to
,
say
Let be
the data number of columns and
let
be the values in the i-th row of data sorted in ascending order.
Value is taken as
the quantile corresponding to the probability
and quantile is obtained by
linear interpolation of the points
hence setting
Operating on columns
If dataOperation is OnColumns,
then the method returns an array of DoubleMatrix instances, one for each column in data.
The j-th array entry contains the quantiles of the j-th
column. It has the same dimensions of probabilities.
Its k-th
linear position is the column quantile
corresponding to
,
say
.
In the following example, column quantiles of the specified data are computed for probabilities .005, .50, .75, and .999.
using System;
namespace Novacta.Analytics.CodeExamples
{
public class QuantileExample1
{
public void Main()
{
// Create a matrix.
var matrix = DoubleMatrix.Dense(25, 4);
for (int m = 0; m < matrix.Count; m++) {
matrix[m] = 1 + m;
}
Console.WriteLine("The data matrix:");
Console.WriteLine(matrix);
// Create the probabilities.
var probabilities = DoubleMatrix.Dense(2, 2);
probabilities[0, 0] = .005;
probabilities[1, 0] = .50;
probabilities[0, 1] = .75;
probabilities[1, 1] = .999;
Console.WriteLine();
Console.WriteLine("Probabilities are:");
Console.WriteLine(probabilities);
// Compute the quantiles of data columns.
var quantilesOnColumns = Stat.Quantile(matrix, probabilities, DataOperation.OnColumns);
Console.WriteLine();
for (int j = 0; j < matrix.NumberOfColumns; j++) {
Console.WriteLine("Column {0} quantiles:", j);
Console.WriteLine();
Console.WriteLine(quantilesOnColumns[j]);
}
// Quantile is overloaded to accept data as a read-only matrix:
// compute the quantiles on columns using a read-only wrapper of the
// data matrix.
ReadOnlyDoubleMatrix readOnlyMatrix = matrix.AsReadOnly();
var percentilesOnReadOnlyColumns = Stat.Quantile(readOnlyMatrix, probabilities,
DataOperation.OnColumns);
Console.WriteLine();
Console.WriteLine("Using read-only data.");
Console.WriteLine();
for (int j = 0; j < readOnlyMatrix.NumberOfColumns; j++) {
Console.WriteLine("Column {0} quantiles:", j);
Console.WriteLine();
Console.WriteLine(percentilesOnReadOnlyColumns[j]);
}
}
}
}
// Executing method Main() produces the following output:
//
// The data matrix:
// 1 26 51 76
// 2 27 52 77
// 3 28 53 78
// 4 29 54 79
// 5 30 55 80
// 6 31 56 81
// 7 32 57 82
// 8 33 58 83
// 9 34 59 84
// 10 35 60 85
// 11 36 61 86
// 12 37 62 87
// 13 38 63 88
// 14 39 64 89
// 15 40 65 90
// 16 41 66 91
// 17 42 67 92
// 18 43 68 93
// 19 44 69 94
// 20 45 70 95
// 21 46 71 96
// 22 47 72 97
// 23 48 73 98
// 24 49 74 99
// 25 50 75 100
//
//
//
// Probabilities are:
// 0.005 0.75
// 0.5 0.999
//
//
//
// Column 0 quantiles:
//
// 1 19.3333333
// 13 25
//
//
// Column 1 quantiles:
//
// 26 44.3333333
// 38 50
//
//
// Column 2 quantiles:
//
// 51 69.3333333
// 63 75
//
//
// Column 3 quantiles:
//
// 76 94.3333333
// 88 100
//
//
//
// Using read-only data.
//
// Column 0 quantiles:
//
// 1 19.3333333
// 13 25
//
//
// Column 1 quantiles:
//
// 26 44.3333333
// 38 50
//
//
// Column 2 quantiles:
//
// 51 69.3333333
// 63 75
//
//
// Column 3 quantiles:
//
// 76 94.3333333
// 88 100
//
//
In the following example, row quantiles of the specified data are computed for probabilities .33 and .66.
using System;
namespace Novacta.Analytics.CodeExamples
{
public class QuantileExample2
{
public void Main()
{
// Create a matrix.
var matrix = DoubleMatrix.Dense(3, 5);
for (int m = 0; m < matrix.Count; m++) {
matrix[m] = 1 + m;
}
Console.WriteLine("The data matrix:");
Console.WriteLine(matrix);
// Create the probabilities.
var probabilities = DoubleMatrix.Dense(1, 2);
probabilities[0] = .33;
probabilities[1] = .66;
Console.WriteLine();
Console.WriteLine("Probabilities are:");
Console.WriteLine();
Console.WriteLine(probabilities);
// Compute the quantiles on rows.
var quantilesOnRows = Stat.Quantile(matrix, probabilities, DataOperation.OnRows);
Console.WriteLine();
for (int i = 0; i < matrix.NumberOfRows; i++) {
Console.WriteLine("Row {0} quantiles:", i);
Console.WriteLine();
Console.WriteLine(quantilesOnRows[i]);
}
// Quantile is overloaded to accept data as a read-only matrix:
// compute the quantiles on rows using a read-only wrapper of the
// data matrix.
ReadOnlyDoubleMatrix readOnlyMatrix = matrix.AsReadOnly();
var quantilesOnReadOnlyRows = Stat.Quantile(readOnlyMatrix, probabilities, DataOperation.OnRows);
Console.WriteLine();
Console.WriteLine("Using read-only data.");
Console.WriteLine();
for (int i = 0; i < readOnlyMatrix.NumberOfRows; i++) {
Console.WriteLine("Row {0} quantiles:", i);
Console.WriteLine();
Console.WriteLine(quantilesOnReadOnlyRows[i]);
}
}
}
}
// Executing method Main() produces the following output:
//
// The data matrix:
// 1 4 7 10 13
// 2 5 8 11 14
// 3 6 9 12 15
//
//
//
// Probabilities are:
//
// 0.33 0.66
//
//
//
// Row 0 quantiles:
//
// 4.28 9.56
//
//
// Row 1 quantiles:
//
// 5.28 10.56
//
//
// Row 2 quantiles:
//
// 6.28 11.56
//
//
//
// Using read-only data.
//
// Row 0 quantiles:
//
// 4.28 9.56
//
//
// Row 1 quantiles:
//
// 5.28 10.56
//
//
// Row 2 quantiles:
//
// 6.28 11.56
//
//
ArgumentNullException | data is null. -or- probabilities is null. |
ArgumentException | dataOperation is
not a field of
DataOperation. -or- probabilities has entries not belonging to the closed interval [0, 1]. |