matlabstatisticsdistributionsymbolic-mathsymbolic-integration

How to calculate the integral of log-normal distribution with MATLAB


I want to calculate the part-expectation of log-normal distribution via:

m = 1;
v = 2;
mu = log((m^2)/sqrt(v+m^2));
sigma = sqrt(log(v/(m^2)+1));

syms x;
d = x*lognpdf(x,mu,sigma);

int(d, x, 0, 10);

However, MATLAB says:

Error using symfun>validateArgNames (line 211) Second input must be a scalar or vector of unique symbolic variables.

Error in symfun (line 45) y.vars = validateArgNames(inputs);

Error in sym/subsasgn (line 771) C = symfun(B,[inds{:}]);

Error in lognpdf (line 36) x(x <= 0) = Inf;

Error in untitled (line 7) d = x*lognpdf(x,mu,sigma);

I even tried to just calculate the integral of the pdf by:

m = 1;
v = 2;
mu = log((m^2)/sqrt(v+m^2));
sigma = sqrt(log(v/(m^2)+1));

syms x;
d = lognpdf(x,mu,sigma);

int(d, x, 0, 10); 

But there are still errors, and MATLAB says:

Error using symfun>validateArgNames (line 211) Second input must be a scalar or vector of unique symbolic variables.

Error in symfun (line 45) y.vars = validateArgNames(inputs);

Error in sym/subsasgn (line 771) C = symfun(B,[inds{:}]);

Error in lognpdf (line 36) x(x <= 0) = Inf;

Error in untitled (line 7) d = lognpdf(x,mu,sigma);

I really don't know what happened. Should the integral of the pdf be the cdf?


Solution

  • Similar to an answer several months ago, the Statistics Toolbox doesn't support the Symbolic Toolbox currently. Therefore, you can proceed by hard coding the PDF itself and integrating it:

    d = exp(-(log(x)-mu)^2/(2*sigma^2))/(x*sigma*sqrt(2*pi));
    int(d, x, 0, 10);
    

    Or you can use the logncdf function, which may be cleaner.