Transforming range of variable to 0 to 1 and custom ranges

Created:18 Jul 2020 00:39:07 , in  Maths

One useful skill when it comes to dealing with variables is to be able to change their range. One of the most useful and desirable ranges is [0,1]. Occasionally you might need a different range like [-1,1], too. This article describes a couple of methods of scaling and descaling a variable's range.

Quick and easy transformation to [0,1]

One way to scale range of variable x to range from 0 to 1 is based on the following function:

$y=x/\left(1+x\right)$

scaled variable x, called y, will be in the range between 0 and 1.

Scaling a to custom range

If a range from a to b, where a is the minimum and b is the maximum allowed value, is required, one of the three following formulas can be used:

1)

$y=\frac{\left(\mathit{ax}+b\right)}{\left(1+x\right)}$

2)

$y=a+\left(b-a\right)\left(1-{e}^{-x}\right)$

where e is the Euler's number

3)

$y=a+2\cdot \left(b-a\right)\left(\mathrm{arctan}\left(x\right)\right)/\mathrm{\pi }$

Scaling variable from [0,1] to a custom range

If a variable y is in range from 0 to 1, it can be scaled to a variable z which is in range between a and b as follows:

$z=a+\left(b-a\right)\ast y$

Min-max scaling

Min-max scaling, also known as min-max normalization, is another easy way, which is frequently used for transforming variable's range.

The formula for min-max scaling is as follows:

$y=\frac{\left(x-a\right)}{\left(b-a\right)}$

where a and b denote minimum and maximum values of x in a set of data respectively.

Descaling variable y back to x can be done using:

$x=y\cdot \left(b-a\right)+a$

Min-max scaling in PHP

Two functions given below perform min-max scaling and descaling as described in the previous section.


function minMaxNormalize($x,$a, $b) {$y = false;
try {
$y = ($x - $a) / ($b - $a); }catch(DivisionByZeroError$e){
echo $e -> getMessage(),"\n"; } return$y;
}

function minMaxDenormalize($y,$a, $b) {$x = ($y * ($b - $a) +$a);
return \$x;
}


Here is how to use these functions

Suppose that the range of values of x is from -10 to 10, but we want it to be between 0 and 1. To transform the range, do the following:


minMaxNormalize(7, -10, 10);
// => 0.85


To transform variables to be back in the range [-10,10] :


minMaxDenormalize(0.85,-10,10);
// => 7


Conclusion

As can be seen in this article, there are quiet a few ways of transforming a range of a variable both to [0,1] or to a custom range. I hope you have found at least one of them sufficient for your purposes. If you have something to add, you are welcome to post a comment below.

This post was updated on 18 Jul 2020 01:30:45

Tags:  php

Author

Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.

©Copyright, 2022 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

Citation

Wojnowski, Sylwester. "Transforming range of variable to 0 to 1 and custom ranges." From sWWW - Code For The Web . https://wojnowski.net.pl//main/index/transforming-range-of-variable-to-0-to-1-and-custom-ranges

You might also like

MATHS

MATHS

MATHS

MATHS

• Recursively updating deeply nested arrays in PHP

MATHS

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]

I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Nobody has commented on this post yet. Be first!