Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Logistic Function with Large Arguments

Subject: Logistic Function with Large Arguments

From: Mitch

Date: 3 Dec, 2010 19:09:05

Message: 1 of 5

Hello,

I am trying to create a logistic function, f(x)=exp(x)/(1+exp(x)), for use with maximum likelihood estimation. The main difficulty is to avoid returning NaN when x is very large or very small, and to instead return 0 or 1. If x=[0;1000], I would like to return logistic(x) = [.5;1], and not [.5;NaN].

I would imagine that the logistic function would be defined in a toolbox somewhere, but I couldn’t find it.

On my own, I first tried:

function h=logit_mitch(x);
h = exp(x)./(1+exp(x));
end

but this faces the NaN problems. Another approach is to try to allow for different events:

function h= logit_mitch (x);
if x>=-500 & x<=500
    h = exp(x)./(1+exp(x));
elseif x<-500
    h = 0;
elseif x>500
    h=1;
end
end

but this doesn’t work either. Using one & causes the function not to evaluate and using && doesn’t work because the arguments must be scalars. A third approach is to use:

h = (exp(x)./(1+exp(x))).*(x>=-500).*(x<=500) + 0.*(x<500)+ 1.*(x>500)

but this doesn’t work because it will lead to NaN*0, which equals NaN.

Any thoughts on how to write a function that will return [.5;1], or better, whether there already exists a good logistic function?

Thanks,
Mitch

Subject: Logistic Function with Large Arguments

From: Sean de

Date: 3 Dec, 2010 19:23:20

Message: 2 of 5

"Mitch " <mh51515@gmail.com> wrote in message <idbf8h$kj6$1@fred.mathworks.com>...
> Hello,
>
> I am trying to create a logistic function, f(x)=exp(x)/(1+exp(x)), for use with maximum likelihood estimation. The main difficulty is to avoid returning NaN when x is very large or very small, and to instead return 0 or 1. If x=[0;1000], I would like to return logistic(x) = [.5;1], and not [.5;NaN].
>
> I would imagine that the logistic function would be defined in a toolbox somewhere, but I couldn’t find it.
>
> On my own, I first tried:
>
> function h=logit_mitch(x);
> h = exp(x)./(1+exp(x));
> end
>
> but this faces the NaN problems. Another approach is to try to allow for different events:
>
> function h= logit_mitch (x);
> if x>=-500 & x<=500
> h = exp(x)./(1+exp(x));
> elseif x<-500
> h = 0;
> elseif x>500
> h=1;
> end
> end
>
> but this doesn’t work either. Using one & causes the function not to evaluate and using && doesn’t work because the arguments must be scalars. A third approach is to use:
>
> h = (exp(x)./(1+exp(x))).*(x>=-500).*(x<=500) + 0.*(x<500)+ 1.*(x>500)
>
> but this doesn’t work because it will lead to NaN*0, which equals NaN.
>
> Any thoughts on how to write a function that will return [.5;1], or better, whether there already exists a good logistic function?
>
> Thanks,
> Mitch

How about
h(isnan(h)) = 1;
?

Subject: Logistic Function with Large Arguments

From: Peter Perkins

Date: 3 Dec, 2010 21:35:15

Message: 3 of 5

On 12/3/2010 2:09 PM, Mitch wrote:
> Hello,
>
> I am trying to create a logistic function, f(x)=exp(x)/(1+exp(x)), for
> use with maximum likelihood estimation. The main difficulty is to avoid
> returning NaN when x is very large or very small, and to instead return
> 0 or 1. If x=[0;1000], I would like to return logistic(x) = [.5;1], and
> not [.5;NaN].

Does this help?

 >> logit1 = @(x) exp(x)./(1+exp(x));
logit1([-1000 0 1000])
ans =
             0 0.5 NaN
 >> logit2 = @(x) 1./(1+exp(-x));
logit2([-1000 0 1000])
ans =
             0 0.5 1

Really, you ought to choose one or the other depending on whether the
inputs are pos or neg, but the second one-liner above is simple and
pretty good.

Subject: Logistic Function with Large Arguments

From: Walter Roberson

Date: 3 Dec, 2010 21:48:38

Message: 4 of 5

On 10-12-03 01:09 PM, Mitch wrote:

> I am trying to create a logistic function, f(x)=exp(x)/(1+exp(x)), for
> use with maximum likelihood estimation. The main difficulty is to avoid
> returning NaN when x is very large or very small, and to instead return
> 0 or 1. If x=[0;1000], I would like to return logistic(x) = [.5;1], and
> not [.5;NaN].

I do not seem to be able to find "very small" x that generates a NaN for that
formula. For example, x=-1e100 leads to 0, and x=1e-100 leads to 0.5 .

The formula *does* return nan for large enough x, at the point where you get
infinity divided by infinity.

Unless you are able to come up with a very small x that generates nan, just
modify the formula to

f(x) = min(exp(x)/(1+exp(x)),1)

This works because 1 is the natural upper bound on the formula and because
min(nan,x) is x for all numeric x.

Subject: Logistic Function with Large Arguments

From: Greg Heath

Date: 5 Dec, 2010 18:42:31

Message: 5 of 5

On Dec 3, 2:09 pm, "Mitch " <mh51...@gmail.com> wrote:
> Hello,
>
> I am trying to create a logistic function, f(x)=exp(x)/(1+exp(x)), for use with maximum likelihood estimation. The main difficulty is to avoid returning NaN when x is very large or very small, and to instead return 0 or 1. If x=[0;1000], I would like to return logistic(x) = [.5;1], and not [.5;NaN].
>
> I would imagine that the logistic function would be defined in a toolbox somewhere, but I couldn’t find it.
>
> On my own, I first tried:
>
> function h=logit_mitch(x);
> h = exp(x)./(1+exp(x));
> end
>
> but this faces the NaN problems. Another approach is to try to allow for different events:
>
> function h= logit_mitch (x);
> if x>=-500 & x<=500
> h = exp(x)./(1+exp(x));
> elseif x<-500
> h = 0;
> elseif x>500
> h=1;
> end
> end
>
> but this doesn’t work either. Using one & causes the function not to evaluate and using && doesn’t work because the arguments must be scalars. A third approach is to use:
>
> h = (exp(x)./(1+exp(x))).*(x>=-500).*(x<=500) + 0.*(x<500)+ 1.*(x>500)
>
> but this doesn’t work because it will lead to NaN*0, which equals NaN.
>
> Any thoughts on how to write a function that will return [.5;1],

for x = -10000:1000:10000
    y = 1./(1+exp(-x))
end

>or better, whether there already exists a good logistic function?

Neural Network Toolbox

help logsig
doc logsig
type logsig

Hope this helps.

Greg

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us