Integrate over range of a vector using trapz
9 次查看(过去 30 天)
显示 更早的评论
I have a random lognormally distributed variable shock defined over the range:
shock = 0.5:0.001:2;
rb = 1.0204;
sigma = 0.174;
mu = 1;
Then I calculate a function of it:
LossF = @(LTV,shock) rb*(1 - (mu*(1 - normcdf((0.5*(sigma)^2 -log(LTV'*shock))/sigma, 0, 1))./(LTV'*shock) + 1 - logncdf(LTV'*shock, -0.5*(sigma^2), sigma))./(mu*(1 - normcdf((0.5*(sigma)^2 -log(LTV'*ones(1,size(shock,2))))/sigma, 0, 1))./(LTV'*ones(1,size(shock,2))) + 1 - logncdf(LTV'*ones(1,size(shock,2)), -0.5*(sigma^2), sigma)) );
Loss = LossF(0.8,shock);
And calculate the pdf of Loss using the theorem for pdf of a function of a random variable:
pdfL = lognpdf(shock, -0.5*(sigma^2), sigma).*gradient(shock);
Checks:
When I integrate over the pdf of shock I get 1 as it should be:
trapz(shock,lognpdf(shock, -0.5*(sigma^2), sigma))
However when I integrate over the pdf of Loss i get:
trapz(Loss,pdfL) = 1.4236e-04
However2 integrating over the pdf of Loss without range gives 1:
trapz(pdfL) = 1
When I plot Loss and pdfL it seems that the range of Loss(1) and Loss(end) cover the entire range of the pdf function. How does an integral over that range return such a small number: 1.4236e-04. When integral over the same function without first argument returns 1?
0 个评论
采纳的回答
Jordan Ross
2016-9-19
Hello Konstantin,
The reason that you receive such a small number when you execute the following code:
trapz(Loss,pdfL)
is that the range "Loss" does not correspond to the PDF "pdfL". When you define "pdfL" you pass "shock" as the parameter 'X' so therefore the return value "pdfL" corresponds to the values of "shock". See the documentation of "lognpdf" for more information: http://www.mathworks.com/help/stats/lognpdf.html
Furthermore, when you plot "pdfL" as follows:
plot(pdfL)
you see the full curve for the PDF.
When you plot:
plot(Loss,pdfL)
you see that the x-values used do not return the same curve for the PDF.
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!