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|)$.


1 Answer 1

data = Transpose[{RandomReal[10, 100], Normalize @ RandomReal[1, 100]}];

Try WeightedData:

wd = WeightedData @@ Transpose[data]


enter image description here

Compare with SmoothHistogram of data without the weights:


enter image description here

  • 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, 2019 at 20:00
  • $\begingroup$ @user67131, my pleasure. Thank you for the accept. And welcome to mma.se. $\endgroup$
    – kglr
    Aug 26, 2019 at 20:12

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.