1
$\begingroup$

I have a discrete signal. How can I calculate the area under the curve for $2\le x \le 4$? How can I fill the same area in the plot generated by SmoothHistogram?

For Example:

r5 = RandomReal[NormalDistribution[1, 2], 100]
p100x = SmoothHistogram[r5, 0.1, "PDF", Frame -> True, FrameLabel -> {{"\!\(\*SubscriptBox[\(P\), \(100\)]\)(x)",}, {"R, \ Ohm",}}, FrameTicks -> Automatic, PlotRange -> {{All, All}, {All, All}}, GridLines -> Automatic,  ImageSize -> 600, PlotLegends -> legend, ScalingFunctions -> {None, "Log"}]

SmoothHistogram (PDF)

$\endgroup$
4
  • 1
    $\begingroup$ Please check that I interpreted your meaning correctly when editing your post. In order to help you, we need the raw data, so we can play with it. Have you tried anything using NIntegrate? you might also want to remove the non-essential formatting options in your code. They don't matter much for your problem, and clutter the code. $\endgroup$
    – MarcoB
    Commented May 30, 2018 at 19:04
  • $\begingroup$ Thanks, I added data and example. $\endgroup$
    – Alex
    Commented May 30, 2018 at 19:46
  • $\begingroup$ I don't understand what a "discrete signal" is. Is that "time series" data that you're just mimicking with the use of SmoothHistogram? If so, do you want the area under the curve with the original values or the log of the values? The total area under a SmoothHistogram using the PDF option is exactly 1. Does that match to the "discrete signal" data? $\endgroup$
    – JimB
    Commented May 30, 2018 at 22:25
  • $\begingroup$ I got right answer for my question, below from to 2 users. But I mean real discrete signal, which consist 20000 values and I want calcuate the area under values 2<x<4, example. In top example, I generated random signal, because i didn't want paste a large code with the import file of true values. $\endgroup$
    – Alex
    Commented May 31, 2018 at 10:27

2 Answers 2

4
$\begingroup$

You could also use CDF and avoid having to integrate:

cdf[x_] = CDF[SmoothKernelDistribution[r5, .1], x];

Then, finding the area is a simple matter of subtraction:

cdf[4] - cdf[2]

0.264359

This returns the same answer as using NIntegrate and PDF:

NIntegrate[PDF[SmoothKernelDistribution[r5, .1], x], {x, 2, 4}]

0.264359

$\endgroup$
1
  • $\begingroup$ Thanks for your help, friend! $\endgroup$
    – Alex
    Commented May 31, 2018 at 7:57
3
$\begingroup$

I don't have your data, but calculating the area under the curve should be as simple as

NIntegrate[PDF[SmoothKernelDistribution[r20, .1], x], {x, 8, 10}]

To fill in the corresponding area under the curve, have a look at the plot option Filling.

Update: here's a start at an answer on how you can get the filling over a given range of abscissae:

 SeedRandom[111222];
 r5 = RandomReal[NormalDistribution[1, 2], 100];
 f = PDF[SmoothKernelDistribution[r5, 0.5], x];
 Plot[{f, Piecewise[{{f, 2 < x < 4}}, _]}, {x, -8, 8},
    Filling->{2 -> {Axis, Yellow}}, PlotStyle->{Blue, Yellow}]

Here I've used a bit more smoothing (bw = 0.5, rather than 0.1), and I didn't bother with your special plot options. I couldn't figure out how to properly utilize the Opacity[] directive to get a lower opacity for the yellow filling.

$\endgroup$
2
  • $\begingroup$ Hello, my friend, I added data and example, but I tried your solution and I think, that it is true. Thanks. But I don't find solution in helper, how filling the area between 2 and 4, for example. $\endgroup$
    – Alex
    Commented May 30, 2018 at 19:50
  • $\begingroup$ Thanks for your help. $\endgroup$
    – Alex
    Commented May 30, 2018 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.