Computer Programming/Min-Max Normalisation
SQL
editSET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Colm Rice
-- Description: Gets min-max normalised value of a variable
-- Bog standard version (based upon a linear eqn of the form: Ax+B)
-- Transforms a value into the range [0,1]
-- Bulletproof checks:
-- NB: If division by zero returning -1.0 to alert the user!
-- Any nulls going down are replaced with zero
-- =============================================
ALTER FUNCTION dbo.GetMinMaxNorm
(
@valueToBeNormalised decimal(10,2),
@maxValue decimal(10,2),
@minValue decimal(10,2)
)
RETURNS decimal(10,2)
AS
BEGIN
DECLARE @range decimal(10,2)
DECLARE @result decimal(10,2)
SET @range = ISNULL(@maxValue, 0.0) - ISNULL(@minValue, 0.0)
if(@range = 0) --Bulletproof
return -1.0
else
SET @result = (ISNULL(@valueToBeNormalised, 0) - ISNULL(@minValue, 0)) / @range
return @result
END
GO
VB.Net
edit ''' <summary>
''' Gets the min-max normalised value of a variable
''' Bog standard version (based upon a linear eqn)
''' </summary>
''' <param name="value">Value to be tranformed</param>
''' <param name="maxValue">Max value of variable set</param>
''' <param name="minValue">Min value of variable set</param>
''' <returns>Normalised value in the range [0,1]</returns>
''' <remarks>Division by zero check. Returns NaN to alert user!</remarks>
Public Function GetMinMaxNorm(ByVal value As Double, ByVal maxValue As Double, ByVal minValue As Double) As Double
Dim range As Double
range = maxValue - minValue
If (range = 0) Then 'Bulletproof
Return Double.NaN
Else
Return ((value - minValue) / range)
End If
End Function
C#
edit /// <summary>
/// Gets the min-max normalised value of a variable
/// Bog standard version (based up on a linear eqn)
/// NB: Division by zero return NaN to alert user
/// </summary>
/// <param name="value">Value to be transformed</param>
/// <param name="minValue">Min value over the variable set</param>
/// <param name="maxValue">Max value of the variable set</param>
/// <returns>Value in the range [0,1]</returns>
public double GetMinMaxNorm(double value, double maxValue, double minValue)
{
double range = maxValue - minValue;
if (range == 0) //Bulletproofing
return double.NaN;
else
return (value - minValue) / range;
}