How to make sense (handle) when computes logarithm of zero in prior information

152 views Asked by At

I am working in image classification. I am using an information that called prior probability (in Bayesian rule). It has range in [0,1]. And it requires computing in logarithm. However, as you know, logarithm of zero number is Inf. For example, given an pixel x in image I (size 3 by 3) with an cost function such as

Cost(x)=30+log(prior(x))

where prior is an matrix 3 by 3

prior=[  0  0  0.5;
         1  1  0.2;
       0.4  0    0]
I    =[  1  2    3;
         4  5    6;
         7  8    9]

I want to compute cost of x=1 then

cost(x=1)=30+log(0)

Now, log(0) is Inf. Then result cost(x=1) also Inf. Based on my assumption that prior=0 that mean the given pixel belongs to background, and prior=1 that mean the given pixel belongs to foreground.

My question is that how to compute log(prior) satisfy my assumption.

I am using Matlab to do it. I think that log(0) becomes very small negative value. And I just set it is -9 as my code

%% Handle with log(0)
prior(prior==0.0) = NaN; 
%% Compute log
log_prior=log(prior);
%% Assume that e^-9 very near 0.
log_prior(isnan(log_prior)) = -9; 

UPDATE: To make clearly what I am doing. Let see the Bayesian rule. My task is that how to assign an given pixel x belongs to Background (BG) or Foreground (FG). It will depends on the probability

P(x∈BG|x)=P(x|x∈BG)P(x∈BG)/P(x)

In which P(x|x∈BG) is likelihood function and assume that it is approximated by Gaussian distribution, P(x∈BG) is prior term and P(x) can be ignore due to it is const

Using Maximum-a-Posteriori (MAP) Estimation we can map the above equation in to log space (to resolve exponential in Gaussian function)

Cost(x)=log(P(x∈BG|x))=log(P(x|x∈BG))+log(P(x∈BG))

To make simple, let assume log(P(x|x∈BG))=30, log(P(x∈BG)) is log(prior) then my cost function can rewritten as

Cost(x)=30+log(prior(x))

Now problem is that prior is within [0,1] then it logarithm is -Inf. As the chepner said, we can add eps value as

log(prior+eps)

However, log(eps) is very a lager negative number. It will be affected my cost function (also becomes very large negative number). Then the first term in my cost function (30) becomes not necessary. Based on my assumption that log(x)=1 then the pixel x will be BG and prior(x)=1 will be FG. How to make handle with my log(prior) when I compute my cost function?

1

There are 1 answers

0
Francesco Callari On

The correct thing to do, before fiddling with Matlab, is to try to understand your problem. Ask yourself "what does it mean for the prior probability to vanish?". The answer is given by Bayes theorem, one form of which is:

posterior = likelihood * prior / normalization

So places where the prior is nil are, by definition, places where you are certain that your events (the things whose probabilities you are computing) cannot happen, regardless of their apparent likelihood (i.e. "cost"). So they are not interesting for you. You just recognize that and skip them.