6

I'm using R to plot some data and I'd like to transform a distance variable with $[0,\infty)$ to a transparency parameter that accepts inputs $[0,1$]. I'd like $0$ distance to map to $1$ and increasing distances to map to decreasing numbers asymptotically approaching $0$. It's been a while since I've taken any math so the function I want might be obvious, but can anyone help me out? Bonus if you explain how the function would be scaled to alter the rate that the range approaches $0$.

Thanks!

Michael
  • 163

4 Answers4

6

$e^{-x}$ will do the trick. If it's any easier, the same is true for any base value greater than 1. So $2^{-x}$ also works. Higher values approach 0 more quickly with respect to $x$.

Josh Keneda
  • 2,907
6

Another possibility is hyperbolic decay, which is slower than exponential decay:

$$f_\alpha(x)=\frac{1}{\alpha x + 1} $$

Where $\alpha > 0$ is a scaling factor. The larger $\alpha$, the steeper the descent towards $0$.

Here's a plot to compare:

Plot of hyperbolic and vs. exponential decay

  • 2
    This function also has the advantage of being substantially faster to compute than the exponential, for circumstances where that can matter. – Steven Stadnicki Jan 14 '13 at 01:20
4

This function maps (0,inf) to (0,1):

$$f(x)=1 - \frac{2\arctan(x)}{\pi}$$

You can replace x in the formula with a polynomial or any other function mapping (0, inf) to (0, inf) to alter the rate of change.

Ricbit
  • 1,080
1

$f(x)=e^{-x}$ have all the properties you want. If you are using a computer and programming, then you can approximate the function by $\sum_{k=0}^N \frac{(-x)^k}{k!}$.

If you use bigger numbers than $e \sim 2,7$ then the function approaches $0$ more quickly.

Z. L.
  • 613
  • 1
    It would be better to use a language's built-in exp function, if it has one (and most do), instead of coding in the Maclaurin series. – Reid Jan 14 '13 at 00:58