Integration for only positive values

Hi!
I would like to calculate the integral of an already estimated probability density function. This PDF was estimated with a SPLINE, which is "slightly" negative in certain areas. My SPLINE is saved as a "struct" and integrating it works just fine fnint(PDF). However, the resulting CDF is decreasing in those areas of negative densities, which I would like to avoid by simply replacing those areas of the PDF with 0.
My question: Is it possible to set those regions of the function equal to 0, or to integrate just over the positive values ot this PDF? I would like to avoid transforming the function into a vector of resulting densities, and instead use the (spline) function directly?
Thanks in advance!

回答(2 个)

the cyclist
the cyclist 2020-6-19
Can you just replace your PDF with max(0,PDF) and proceed?

9 个评论

No, unfortunately this doesn't work :/
It gives me the error message: "Error using max. Invalid data type. Second argument must be numeric or logical."
I guess this has something to do with the application of the max function on a spine? Because the function is saved as a combination of several polynomials, which are all in that struct.
Sorry, I meant my comment to be more conceptual that an exact MATLAB syntax. If you upload the data, it should be possible to give more specific advice.
NNPDF = @(x) max(0, PDF(x))
Unless your PDF is symbolic. If it is symbolic then
NNPDF = symfun(piecewise(PDF(x) < 0, 0, PDF(x)),x)
I tried to search for commands that act like a "max-function for individual elements", but for a whole function (I hope this makes sense, idk how to explain it any better). But unfortunately I couldn't find anything.
Sry, I'm pretty new to Matlab. How should I upload the data? Do you mean just the code or also the data set?
In the "Insert" section, there is an icon that looks like a paper-clip, that can be used to upload files. Uploading both code and data is usually helpful.
Thank you so much for helping me cyclist!
@Walter Robertson Thanks for your help!
Does this command apply a function on my function? Sry, I'm new here and I don't really know what this is doing...
For your @(x) max(0, PDF(x)) command I get the message
function_handle with value: @(x)max(0,PDF(x))
How do I continue from there? Do I need to define a vector of "x-values" and apply my NNPDF on those values?
Thanks a lot for your help!
It wasn't clear to me which field of the structure you need to modify, but for example if it is coefs,then
PDF.coefs = max(PDF.coefs,0)
Sry, I made a mistake: The whole file was too large to upload, so I just uploaded the data of an arbitrary PDF. By sheer coincidence, exatly this data set doesn't exhibit any negative densities... The data set I uploaded now should include negative densities.
Concerning your comment: This command just replaces negative coefficients with 0, right? But my intention would be to replace a potential negative output of each function of the spline with 0. In my case, the different parts of the spline are linear functions, so I would like to adjust the outcome f(x) of each part of the spline and not the coefficients itself.

请先登录,再进行评论。

How to integrate over the positive part of the function
lower_limit=-5; %Lower Limit of the integration
upper_limit=5; %Upper limit of the integration
Point_number=1000; %NUmber of points to be in the function
x_range=linspace(lower_limit,upper_limit,Point_number);
% Enter Function
syms x
f(x)=x^2+3*x-5
f(x) = 
y=f(x_range); %Getting function values
y_plus=subplus(y); %Getting only the Positive part
fprintf("Integration of the Positive Part %f",trapz(y_plus))
Integration of the Positive Part 5930.246783
%more plots
figure
plot(x_range,y_plus,"g","LineWidth",2);
hold on
plot(x_range,y,"r","LineWidth",1);
hold off
legend({'Positive Part','Input Function'});
xline(0);
yline(0);
title("Function")

类别

帮助中心File Exchange 中查找有关 Spline Postprocessing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by