Clear Filters
Clear Filters

how to plot prediction and confidence interval for non-parametric distribution?

14 views (last 30 days)
predint cannot compute prediction intervals for non-parametric regression methods such as Interpolant, Lowess, and Spline.So how to compute and plot prediction and confidence interval for non-parametric distribution if i follow ksdensity or quantile regression?

Answers (1)

Aditya
Aditya on 21 Feb 2024
For non-parametric regression methods like ksdensity (Kernel Density Estimation) or quantile regression, the prediction intervals are not as straightforward to compute as they are for parametric models. However, there are ways to estimate these intervals.
Kernel Density Estimation (ksdensity):You can use the bootstrap method to estimate the prediction intervals for a kernel density estimate. The bootstrap method involves resampling your data with replacement and then recomputing the density estimate multiple times to get a distribution of estimates.
% Assume 'data' is your sample data
% Number of bootstrap samples
numBootstrap = 1000;
% Range for which to compute the density
x_values = linspace(min(data), max(data), 100);
% Store density estimates from each bootstrap sample
bootstrapDensities = zeros(numBootstrap, length(x_values));
% Bootstrap loop
for i = 1:numBootstrap
% Resample data with replacement
bootstrapSample = datasample(data, length(data));
% Compute density for the bootstrap sample
[f, xi] = ksdensity(bootstrapSample, x_values);
% Store the density estimate
bootstrapDensities(i, :) = f;
end
% Compute percentiles to get prediction intervals
lowerBound = prctile(bootstrapDensities, 2.5); % 2.5th percentile
upperBound = prctile(bootstrapDensities, 97.5); % 97.5th percentile
% Plotting the original density estimate with prediction intervals
[f, xi] = ksdensity(data, x_values);
plot(xi, f, 'LineWidth', 2); % Original density
hold on;
fill([xi, fliplr(xi)], [lowerBound, fliplr(upperBound)], 'b', 'FaceAlpha', 0.1);
Quantile Regression:Quantile regression directly estimates the conditional quantiles of the response variable, so you can use it to obtain confidence intervals by estimating the appropriate quantiles.
% Assume 'X' is the predictor data and 'Y' is the response data
% Quantile levels for the lower and upper bounds
lowerQuantile = 0.025;
upperQuantile = 0.975;
% Fit the quantile regression models
lowerModel = fitqr(X, Y, lowerQuantile);
upperModel = fitqr(X, Y, upperQuantile);
% Predict the response for the lower and upper quantile models
lowerPrediction = predict(lowerModel, X);
upperPrediction = predict(upperModel, X);
% Plot the data and the confidence interval
plot(X, Y, 'o'); % Original data
hold on;
plot(X, lowerPrediction, 'r-', 'LineWidth', 2); % Lower quantile prediction
plot(X, upperPrediction, 'g-', 'LineWidth', 2); % Upper quantile prediction
fill([X; flipud(X)], [lowerPrediction; flipud(upperPrediction)], 'k', 'FaceAlpha', 0.1);

Community Treasure Hunt

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

Start Hunting!