Inverse Mills Ratio in Matlab

感情迁移 提交于 2019-12-25 01:44:50

问题


I am trying to program in Matlab a conditional expectation of the form:

E[x|A<=x<=B] where X~N(u,s^2) (sorry, apparently the math editing here isn't what I am used to)

In Matlab, I have written up the following code:

Y=u+s*(normpdf(A,u,s)-normpdf(B,u,s))/(normcdf(B,u,s)-normcdf(A,u,s))

the problem is that it breaks down at higher values of A and B. For example, let u=0, s=1, A=10 and B=11. Simple logic says the answer should be between 10 and 11, but Matlab gives me back Inf because the denominator essentially becomes 0 while the numerator is 10^-23.

Any suggestions to make the formula give real numbers for all inputs?


回答1:


One way is to do the numerical integration yourself:

x = linspace(A,B,1000);
trapz(x,x.*normpdf(x,u,s)) / trapz(x,normpdf(x,u,s))

With your example values, this gives 10.0981, and it is pretty fast



来源:https://stackoverflow.com/questions/17713981/inverse-mills-ratio-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!