5
$\begingroup$

I have a large list $L$ ($|L| \sim 100000$) of pairs $\{x_i,w_i\} \in \mathbf{R} \times [0,1]$ subject to the constraint that

$\qquad \sum w_i = 1.$

I would like to use $L$ to graph a smooth function which "approximates" the PDF

$\qquad \sum w_i \cdot \delta(x - x_i)$

If all the $w_i$ were equal to $1/|L|$, then I could simply take the list of the numbers $x_i$ and apply SmoothHistogram. Is there any way to do a "weighted" smooth histogram? If not, some other way?

In practice, the numbers $w_i$ satisfy $5/|L| \ge w_i \ge 1/(5 |L|)$.

$\endgroup$
10
$\begingroup$
SeedRandom[1]
data = Transpose[{RandomReal[10, 100], Normalize @ RandomReal[1, 100]}];

Try WeightedData:

wd = WeightedData @@ Transpose[data]

SmoothHistogram[wd]

enter image description here

Compare with SmoothHistogram of data without the weights:

SmoothHistogram[data[[All,1]]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ This completely and succinctly answers my question, and includes a working example that makes it easy for me to reproduce. Thanks! $\endgroup$ – user67131 Aug 26 at 20:00
  • $\begingroup$ @user67131, my pleasure. Thank you for the accept. And welcome to mma.se. $\endgroup$ – kglr Aug 26 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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