티스토리 뷰

차기작 : R을 배우자

Against underflow

quantlab 2015. 10. 10. 00:07

수학자의 무한히 정확한 세계에서는 underflow 따위는 없지만, 컴퓨터에는 있다.

In the world of Mathematics where infinite precision is guaranteed, there's no room for Underflow, but computers has it.


최근에 IRT 모형의 일종인 GRM 모형의 log Likelihood 최대화를 하는 과정에서 underflow의 의한 심각한 문제가 발생했다.

I recently programmed a routine to maximize the log likelihood of GRM, which is one kind of IRT models and found some problems related to UNDERFLOW!


다음의 R 명령어를 보자.

Here's R code.


> #Underflow ATTACK

> 1-1/(1+exp(-36))

[1] 2.220446e-16

> 1-1/(1+exp(-38))

[1] 0

> 10/(1-1/(1+exp(-38)))

[1] Inf

> exp(-36)/(1+exp(-36))

[1] 2.319523e-16

> exp(-38)/(1+exp(-38))

[1] 3.139133e-17


분명히 1-1/(1+exp(-38))은 0이 아니다. 뭐 0보다 많이 크지 않으니 상관없다고 생각할 수도 있다.

Definitely, 1-1/(1+exp(-38)) is different from 0. Well, not much bigger... so whyt bother?


문제는 어떤 상수를 1-1/(1+exp(-38))로 나눠야 하는 경우에 생긴다. 무한이 나왔다!

The problem arises when you have to divide some number by 1-1/(1+exp(-38)). You divide finite number by 0 and you get Inifinity!


반면에 1-1/(1+exp(-38))를 exp(-38)/(1+exp(-38))로 구하면 값을 구할 수 있다. 

But if we enter "exp(-38)/(1+exp(-38))" instead of "1-1/(1+exp(-38))", we get the correct answer.


1-1/(1+exp(-38))과 exp(-38)/(1+exp(-38))과 동일하다! 수학적으로...

Be aware! 1-1/(1+exp(-38)) is identical to exp(-38)/(1+exp(-38)). At least, mathematically...


다음의 경우도 주의하자!

Here's another case.


> 1/(1+exp(-50))

[1] 1

> 1/(1+exp(-40))

[1] 1

> 1/(1+exp(-50))-1/(1+exp(-40))

[1] 0

> (exp(-40)-exp(-50))/((1+exp(-40))*(1+exp(-50)))

[1] 4.248161e-18

> (exp(-40)-exp(-50))

[1] 4.248161e-18


언젠가 컴퓨터가 1-1/(1+exp(-10000))쯤은 껌으로 여길 수 있는 정확성을 지니길, 그래서 상상만으로 컴퓨터로 구현할 수 있는 미래를 기대해 본다.

I hope someday computers get much better so that 1-1/(1+exp(-10000)) is nothing, as it is!


'차기작 : R을 배우자' 카테고리의 다른 글

another log-sum-exp blogging : implementation  (0) 2015.10.23
log-sum-exp trick  (0) 2015.10.15
fitting data to gpcm(ltm or mirt?)  (0) 2015.09.26
Polychoric correlations  (2) 2015.09.22
rJava loading 문제  (0) 2015.05.17
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함