# How to smoothly approximate a sign function

I have a function that defined as following

$$f(x) = \begin{cases} 1, & \text{if x > 0 } \\ 0, & \text{if x=0 } \\ -1, & \text{if x<0 } \end{cases}$$ In practice, the $f(x)$ is approximated by a smooth $\tanh(kx)$ or Heaviside function as bellow figure. Could you have other way to represent the $f(x)$ function? What are the benefits of your way?

Update: $\tanh(kx)$ function $k$ controls the smoothness of the sign function. As $k \to \infty$, the function defined in $f(x)=\tanh(kx)$ converges to standard sign function. Similarly, the derivative of $\tanh (x)$ also converges to Dirac delta function as $k \to \infty$. If $k$ is too small, the evolution equation for $x$ acts locally only on a few values around $\{x=0\}$. Hence, the $\tanh(kx)$ function is sensitive with parameter $k$. The parameter $k$ must choose carefully.

Hence, My purposed want to reduce/ignore the affect of k, but remains the above smoothly approximation.

• Of course, one can improve the approximation by taking $\tanh \lambda x$ for some $\lambda \gg 0$. – Travis Willse May 3 '15 at 14:36
• what sort of benefits are you looking for? – Andrea May 3 '15 at 14:37
• It would help if you explain why you want something other than the $\tanh$ function. What is it about $\tanh$ that doesn't work for your purposes? Otherwise, people may waste a lot of time coming up with other functions that also won't work for you. – Nate Eldredge May 3 '15 at 14:44
• Check out the function $f(x) = e^{-1/x^2} x>0; =0$ for $x<0$. It is infinitely differentiable at 0 and you can use it to glue together stuff. – Asvin May 3 '15 at 14:44
• Another suggestion; for $x\geq 0$ we have $f(x) = 1-e^{-\lambda x}$ and for $x<0$, $f(x)=e^{\lambda x}-1$. With some $\lambda \gg 0$. – Rammus May 3 '15 at 14:54

You could try $\frac 2 \pi \arctan \lambda x$ for various $\lambda >0$. If you do not tell us what exactly you are looking for, we shall not be able to help.

• Yes. I update my question. Let see my analyze and my goal – John May 3 '15 at 15:04

What you are describing is usually called "sigmoid" functions in the machine learning community, of which some can be found here: http://en.wikipedia.org/wiki/Sigmoid_function

Another way to do it is to perform local integration on the sign function:

$$a_1(x_0) = \frac{1}{2\delta_x}\int_{x_0-\delta_x}^{x_0+\delta_x} s(x)dx$$

And you could do this over and over, recursively:

$$a_n(x_0) = \frac{1}{2\delta_x}\int_{x_0-\delta_x}^{x_0+\delta_x} a_{n-1}(x)dx$$

You would gain one degree of differentiability every time you do so.

• Any sigmoid function has the problem that the OP mentioned in his edit. – Alex M. May 3 '15 at 15:15
• So just multiply with 2 and then subtract 1. – mathreadler May 3 '15 at 15:17
• Why remove the comment afterwards? Destroys the communication. – mathreadler May 3 '15 at 15:18
• Multiplying by 2 and subtracting one gives you $\tanh(\frac{x}{2})$ and OP seems to be against use of tanh. Apologies. – Francesco Gramano May 3 '15 at 15:22
• Not for all sigmoid functions. There is a long list at the end of the wikipedia page. – mathreadler May 3 '15 at 15:23