Baskets |
Baskets return a set of minimal/maximal values of the argument array or vector.
Hereinafter the following convention is used:
denotes data container element, i = 0..n-1. For vector, array of doubles and array of nullable doubles denotes an array element or vector element.
After we sort the elements from the smallest to the largest one, we will get the following sequence:
where denotes index of the element in the sorted sequence.
Thus top of the data set for a specified number k is the following sequence:
, where
Bottom of the data set for a specified number k is the following sequence:
, where
It is not always the values of elements that user needs to compare. Sometimes you need to compare values of some function or property of the elements. It is possible to get given amount of objects with smallest values gotten from specified delegate or class member. For example, the top of the set of complex numbers, compared by their magnitude:
, where
Aside from a delegate user may specify a String value as a parameter of GetTop/GetBottom method. The specified string value denotes the name of a property, field or method which takes no parameters. See examples.
The example shows how to get top and top indices:
1using System; 2using FinMath.LinearAlgebra; 3using FinMath.Statistics; 4using FinMath.DataStructures; 5 6namespace FinMath.Samples 7{ 8 class VectorBasketingSample 9 { 10 static void Main() 11 { 12 // Input parameters. 13 const Int32 numberOfElements = 10; 14 const Int32 topCount = 3; 15 16 // Genereating input vectors. 17 Vector vector = Vector.Random(numberOfElements); 18 Console.WriteLine("Input vector:"); 19 Console.WriteLine(" " + vector.ToString("0.000")); 20 21 // Allocate memory for outputs. 22 Vector top = new Vector(topCount); 23 IntegerArray topIndices = new IntegerArray(topCount); 24 // Find top and top indices. 25 vector.SmallestIndices((Double[])top, (Int32[])topIndices); 26 27 // Output results. 28 Console.WriteLine("Top elements:"); 29 Console.WriteLine(" " + top.ToString("0.000")); 30 Console.WriteLine("Top indices:"); 31 Console.WriteLine(" " + topIndices); 32 } 33 } 34}
The following example shows how to get basket of values compared by value gotten from specified delegate or class member:
1// Generating input vector. 2Vector vector = Vector.Random(observationsCount, normalDistribution); 3Console.WriteLine("First input vector:"); 4Console.WriteLine(vector.ToString("0.00")); 5 6// Compare by value of basic function Math.Abs of each vector element. 7double[] top1 = vector.GetTop(topCount, Math.Abs); 8Console.WriteLine($"top1: {(Vector)top1:F}"); 9 10// Compare by value gotten from specified delegate. 11double[] top2 = vector.GetTop(topCount, delegate (double x) { return Math.Abs(x); }); 12Console.WriteLine($"top2: {(Vector)top2:F}"); 13Console.WriteLine();
1// Generating input array of complex numbers. 2Complex[] complexArray = new Complex[observationsCount]; 3Console.Write("Second input vector:"); 4for (int i = 0; i < observationsCount; ++i) 5{ 6 complexArray[i] = new Complex(normalDistribution.Sample(), normalDistribution.Sample()); 7 Console.Write($" {complexArray[i]:F}"); 8} 9Console.WriteLine(); 10 11// Compare by value gotten from specified class member. 12Complex[] top3 = complexArray.GetTop(topCount, x => x.Magnitude); 13Console.Write("top3: "); 14foreach (Complex val in top3) 15 Console.Write($"{val:F} "); 16Console.WriteLine();