如何从Matlab的ksdensity核估计器中获取解析公式
ksdensity Output Great question—extracting the analytical form of the kernel density estimate (KDE) from MATLAB's ksdensity is totally feasible once you understand how the function constructs the estimate under the hood. Let's break this down step by step:
Core Background: How KDE Works
First, remember that a kernel density estimate is a sum of scaled, shifted versions of a "kernel" function. For a dataset $x_1, x_2, ..., x_n$, the KDE at any point $t$ is:
$$
\hat{f}(t) = \frac{1}{n \cdot h} \sum_{i=1}^n K\left( \frac{t - x_i}{h} \right)
$$
Where:
- $n$ = number of samples
- $h$ = bandwidth (the smoothing parameter chosen by
ksdensity) - $K(\cdot)$ = the kernel function (a symmetric, normalized function that weights nearby points)
Step 1: Extract Key Parameters from ksdensity
MATLAB's ksdensity doesn't spit out the analytical formula directly, but it does let you retrieve all the pieces you need to build it yourself. Use the extended output syntax to get the bandwidth, kernel type, and other metadata:
% Example dataset x = randn(100, 1); % Get full output from ksdensity [f, xi, h, out] = ksdensity(x); % Print critical parameters disp(['Bandwidth h: ', num2str(h)]); disp(['Kernel type: ', out.KernelName]);
The out structure will tell you exactly which kernel ksdensity used (default is 'normal' for Gaussian, but you can specify others like 'epanechnikov' or 'uniform' via the 'Kernel' input argument).
Step 2: Define the Kernel's Analytical Form
Next, you need the mathematical expression for the kernel ksdensity is using. Here are the formulas for the most common kernels supported:
- Gaussian (normal): $K(u) = \frac{1}{\sqrt{2\pi}} e{-u2/2}$ (valid for all real $u$)
- Epanechnikov: $K(u) = \frac{3}{4}(1 - u^2)$ when $|u| \leq 1$, else $0$
- Uniform: $K(u) = \frac{1}{2}$ when $|u| \leq 1$, else $0$
- Triangular: $K(u) = 1 - |u|$ when $|u| \leq 1$, else $0$
Step 3: Build the Analytical KDE Formula
Once you have $h$, the kernel formula, and your dataset, you can construct the full analytical expression. For convenience, use MATLAB's Symbolic Math Toolbox to generate a symbolic formula automatically:
Example for Gaussian Kernel
syms t; n = length(x); kde_expr = 0; % Sum the scaled/shifted kernel for each sample for i = 1:n u = (t - x(i))/h; % Gaussian kernel term kernel_term = (1/sqrt(2*pi)) * exp(-u^2/2); kde_expr = kde_expr + kernel_term; end % Apply the 1/(n*h) scaling factor kde_expr = kde_expr / (n*h); % Simplify the expression (optional but helpful) kde_expr = simplify(kde_expr); % Display the result disp('Analytical KDE formula:'); disp(kde_expr);
Example for Epanechnikov Kernel
For piecewise kernels like Epanechnikov, use MATLAB's piecewise function to handle the conditional logic:
syms t; n = length(x); kde_expr = 0; for i = 1:n u = (t - x(i))/h; % Epanechnikov kernel term (piecewise) kernel_term = piecewise(abs(u) <= 1, (3/4)*(1 - u^2), 0); kde_expr = kde_expr + kernel_term; end kde_expr = kde_expr / (n*h); kde_expr = simplify(kde_expr);
Key Notes
- If you don't have the Symbolic Math Toolbox, you can still write the formula manually using your dataset, bandwidth, and kernel expression—just substitute each $x_i$ into the sum.
- For large datasets, the symbolic expression will be very long (since it's a sum of $n$ terms). In these cases, it's often more practical to work with the formula conceptually rather than expanding it fully.
- You can override
ksdensity's default bandwidth or kernel type using input arguments (e.g.,ksdensity(x, 'Kernel', 'epanechnikov', 'Bandwidth', 0.5)) to get exactly the KDE you want to formalize.
内容的提问来源于stack exchange,提问作者Pierre

