Numerical Scales | Extreme Optimization Numerical Libraries for .NET Professional |

It is often necessary to group numerical data into categories. The range of the data is divided into a number of intervals, where each interval becomes a category in a numerical scale. This type of numerical scale is implemented by the NumericalScale class. This class inherits from CategoricalScale, but provides some additional functionality.

The NumericalScale class has four constructors. They come in two pairs, each pair offering one way of defining the intervals that make up the scale.

The first constructor takes one argument: a Double array that contains the boundaries of the intervals. The values in this array must be in ascending order, or an ArgumentException will be thrown.

double[] bounds = new double[] {50, 62, 74, 88, 100}; NumericalScale scale1 = new NumericalScale(bounds);

The second constructor also has a Double array as its first argument, but has one additional argument: a SpecialBins value that specifies which special intervals to include in the scale.

The possible values are as follows:Name | TH |
---|---|

None | No special intervals are included. |

BelowMinimum | There is a special interval for values below the scale's minimum value. |

AboveMaximum | There is a special interval for values above the scale's maximum value. |

OutOfRange | There is a special interval for values that are outside the scale's range. |

Missing | There is a special interval for missing values. |

If BelowMinimum is included, an interval with lower bound Double.NegativeInfinity is inserted before all other intervals. If AboveMaximum is included, an interval with upper bound Double.PositiveInfinity is added at the end. The following creates a scale with the same boundaries as above, but with an extra interval to hold values less than 50:

double[] bounds = new double[] {50, 62, 74, 88, 100}; NumericalScale scale2 = new NumericalScale(bounds, SpecialBins.BelowMinimum);

The third constructor takes three arguments. The first two are the lower bound of the first interval, and the upper bound of the last interval. The third argument is the total number of intervals. This creates a scale with the specified number of intervals that are all equal in width. The fourth constructor has one additional argument: a SpecialBins value that indicates which special values should be tabulated in addition to those within the specified interval.

The code below creates a scale with five intervals for values between 50 and 100:

The Count property returns the number of intervals in the scale. The IsOrdered property indicates whether the scale is ordered or unordered. It always returns true.

The GetBounds method returns a Double array containing the boundaries of the intervals. If the BelowMinimum or AboveMaximum intervals were included, the return value includes these intervals as well.

The GetLowerBound and GetUpperBound methods take one argument and return the lower and upper bound of the interval with the specified index.

Console.WriteLine(scale3.GetLowerBound(2)); // Prints '70' Console.WriteLine(scale3.GetUpperBound(2)); // Prints '80'

The GetCaption method returns the text label of the level at the specified level index. The GetCaptions method returns a string array containing the captions for each level.

The GetEnumerator method returns an IEnumerator object that can be used to iterate through the levels of the scale.

Numerical scales convert numbers to intervals. The Map method provides this functionality.

This method is overloaded. The first overload takes any object as its only argument. This object is converted to a number using the Convert.ToDouble method. If this conversion succeeds, the index of the interval containing the number is returned. If the number is outside the scale, -1 is returned.

The second overload takes an array of objects and returns an integer array of the indexes corresponding to those objects. Each of these methods takes an optional IFormatProvider argument that is used to convert the object value to a number.

The NumericalScale class also provides type-safe overloads. One maps a number to the index of the interval. The second takes an array and returns an array of indexes.

The example below uses the scale defined earlier. Each line of code prints the same value (1):

Console.WriteLine(scale3.Map(63.5)); Console.WriteLine(scale3.Map("63.5")); IFormatProvider provider = System.Globalization.CultureInfo.CreateSpecificCulture("NL-BE").NumberFormat Console.WriteLine(scale3.Map("63,5", provider));

In the last line, the value is provided in a format that uses a comma as the decimal separator. The right NumberFormat is required to make the result correct.

Copyright Â© 2004-20116,
Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M
Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual
Studio.NET*, and the *Optimized for Visual Studio* logo

are
registered trademarks of Microsoft Corporation.