2
$\begingroup$

I need a function (for a heatmap algorithm) that takes a percentage difference between two values, and returns a number between 0 and 1.

The output will be used in coloring parts of the screen. The output will be passed to an alpha channel.

The catch is, most of the time the percentage value I want to plot will be small and it will be hard to discern the difference between the colors.

That's why I want to magnify small values and compress the large ones.

The output should be something like this:

f(0) = 0

f(1) = 1

f(0.001) = 0.1

f(0.002) = 0.2

f(0.7) = 0.75

f(0.8) = 0.78

...

Do you see the pattern?

I've tried log, but I need the function to be defined at f(0). I've also tried x^a where a < 1, but in order to get the magnification I need I need to use a << 1 and that makes all the output so small it can't be discerned or the resolution is so small that the graphics assume it's the same color.

  • 0
    For example the average $f(x)=\frac{x^a+x}2$ for some $a$ between $0$ and $1$ will magnify the lower end, but at the same time make sure that the compression ratio is $2:1$ at worst. However, this is non-linear for small inputs, so you won't have $f(2\delta)\approx 2 f(\delta)$ for small values of $\delta$. With this function the magnification ratio grows without bound as $\delta\to0$.2012-06-15

2 Answers 2

5

Any function that goes through $(0,0)$ and $(1,1)$ passing above the diagonal satisfies the basic shape you are looking for. If you also want the function to be symmetric, perhaps the simplest choice is a hyperbolic segment such as

$f(x)={{(n+1) x}\over{n x+1}}$ where $n>0$

plotted here for $n=10,5,$ and $1$:

enter image description here

As a result, by choosing $n$ you can select the amount compression/magnification.

  • 0
    +1: This is a simple way of controlling the maximum magnification ($1:(n+1)$) as well as the minimum compression ($(n+1):1$). Let's wait for the OP to comment, whether this meets the requirements.2012-06-15
1

Picking up on your idea of using $\log$, since you want $f(0)=0$ and $f(1)=1$ the function must be of the form $f(x)=\frac{1}{\log\left(\frac{b-1}{b}\right)}\log\left(\frac{b-x}{b}\right), b\in(-\infty,0)$ Depending on how much emphasis you want to put on the smaller numbers you can change the value of $b$. If you want a lot of emphasis on the smaller numbers you want $b$ to be close to 0. Here is an animation to illustrate what I mean:

enter image description here

$b\in[-5,-0.001]$

If you want a lot of emphasis on the smaller numbers, this is an example when $b=-0.005$:

enter image description here

Note that in the example you give, you want $f(0.8)=0.78$ and $f(1)=1$. That would mean that you would need to emphasize numbers when to $x$ is close to $1$. This would possibly defeat the purpose of your function. For a function to do what you wish $f(x)\ge x, \forall x\in[0,1]$