Click or drag to resize

Baskets

Baskets return a set of minimal/maximal values of the argument array or vector.

Hereinafter the following convention is used:

DSxi denotes data container element, i = 0..n-1. For vector, array of doubles and array of nullable doubles DSxi 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:

DSBasket

where DSBasketI denotes index of the element in the sorted sequence.

Thus top of the data set for a specified number k is the following sequence:

DSTop, where DSTop Seq

Bottom of the data set for a specified number k is the following sequence:

DSBottom, where DSBottom Seq

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:

DSTop Complex, where DSTop Complex Seq

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.

Methods

Operation

Description

Performance

top

Returns a specified number of minimal values from container.

DSTop

where DSTop Seq

Out of place:

StaticSmallest(IEnumerableDouble, Double, Boolean)

StaticSmallest(IEnumerableNullableDouble, Double, Boolean)

Returning result:

StaticSmallest(IEnumerableDouble, Int32, Boolean)

StaticSmallest(IEnumerableNullableDouble, Int32, Boolean)

Method that return given amount of objects with smallest values gotten from specified delegate or class member:

StaticSmallestTSource(IEnumerableTSource, Int32, FuncTSource, Double, Boolean)

top indices

Returns an array of minimal values and an array of indices of minimal values from container.

DSTop - array of minimal values

DSTop Indices - array of minimal values indices

where DSTop Seq

Out of place:

StaticSmallestIndices(IEnumerableDouble, Double, Int32, Boolean)

StaticSmallestIndices(IEnumerableNullableDouble, Double, Int32, Boolean)

Returning result:

StaticSmallestIndices(IEnumerableDouble, Int32, Boolean)

StaticSmallestIndices(IEnumerableNullableDouble, Int32, Boolean)

bottom

Returns a specified number of maximal values from container.

DSBottom

where DSBottom Seq

Out of place:

StaticLargest(IEnumerableDouble, Double, Boolean)

StaticLargest(IEnumerableNullableDouble, Double, Boolean)

Returning result:

StaticLargest(IEnumerableDouble, Int32, Boolean)

StaticLargest(IEnumerableNullableDouble, Int32, Boolean)

Method that return given amount of objects with greatest values gotten from specified delegate or class member:

StaticLargestTSource(IEnumerableTSource, Int32, FuncTSource, Double, Boolean)

bottom indices

Returns an array of maximal values and an array of indices of maximal values from container.

DSBottom - array of maximal values,

DSBottom Indices - array of maximal values indices,

where DSBottom Seq

Out of place:

StaticLargestIndices(IEnumerableDouble, Double, Int32, Boolean)

StaticLargestIndices(IEnumerableNullableDouble, Double, Int32, Boolean)

Returning result:

StaticLargestIndices(IEnumerableDouble, Int32, Boolean)

StaticLargestIndices(IEnumerableNullableDouble, Int32, Boolean)

Code Sample

The example shows how to get top and top indices:

C#
 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}

Code Sample

The following example shows how to get basket of values compared by value gotten from specified delegate or class member:

C# GetTop By Delegate
 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();
C# GetTop By Class Member
 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();

See Also