# Problem in using the integral function to compute a partial expectation

3 views (last 30 days)
Mauricio Tejada on 27 Oct 2020
Answered: Vimal Rathod on 3 Dec 2020
I found a problem in using the integral function to compute a partial expectation for a lognormal distribution. Here is the code:
mu = 2.5652;
sig = 0.0055;
int = @(x) x.*lognpdf(x,mu,sig);
xx = (0.00001:0.1:20)';
f1 = zeros(length(xx),1);
f2 = zeros(length(xx),1);
for i = 1:length(xx)
f1(i) = integral(int,xx(i),Inf);
f2(i) = exp(mu+0.5*sig^2)*logncdf((mu+sig^2-log(xx(i)))/sig);
end
plot(xx,f1)
title('Using the integral function');
plot(xx,f2);
title('Using the formula for partial expectation');
Note that for that particular parametrization (with low variance), the partial expetation computed using the integral function drops abruptly to zero for some values of the low bound of the integral. This should no occur. In fact, when computing the partial expetation using the algebraic version (available for the lognomal) that problem does not ocurr. Is there a bug in the integral function? Vimal Rathod on 3 Dec 2020
Hi,
If you change the integral function a bit it will give out the result same as your other plot.
You just have to increase the absolute tolerance property of the function.
% in place of the below line
f1(i) = integral(int,xx(i),Inf);
%use this line
f1(i) = integral(int,xx(i),Inf,'AbsTol',1e-12);