Fit Nonparametric and Parametric Distributions to Weighted Data
Fit Nonparametric and Parametric Distributions to Weighted Data
Generate random reals from
to
and weight them according to the PDF of a NormalDistribution using WeightedData:
data = RandomReal[{-5, 5}, 10 ^ 4];
wts = PDF[NormalDistribution[], data];Create a WeightedData object and compute its mean and variance:
𝒲 = WeightedData[data, wts];
{Mean[𝒲], Variance[𝒲]}Fit to a NormalDistribution using maximum likelihood estimation:
𝒟 = EstimatedDistribution[𝒲, NormalDistribution[μ, σ]];
{Mean[𝒟], Variance[𝒟]}Use HistogramDistribution to create a weighted histogram:
𝒟NP = HistogramDistribution[𝒲];
{Mean[𝒟], Variance[𝒟]}Labeled[Plot[Evaluate@{PDF[𝒟, t], PDF[𝒟NP, t]}, {t, -4, 4}, Exclusions -> None, Filling -> Axis, Axes -> {True, False}, ImageSize -> 450, PlotStyle -> Thick, PlotLegends -> Placed[Style[#, Bold, FontFamily -> "Helvetica"]& /@ {"NormalDistribution", "HistogramDistribution"}, Bottom], Frame -> True, GridLines -> Automatic, GridLinesStyle -> Directive[Gray, Dotted]], Column[{Style["Parametric and Nonparametric Estimates", Bold, FontFamily -> "Helvetica", FontSize -> 18], Style["Weighted Data", Bold, FontFamily -> "Helvetica", FontColor -> Gray, FontSize -> 14]}], {{Top, Left}}]