You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于AR(8)模型的消费数据IRF的95%自助法置信区间构建问询

Hey there! Let's walk through exactly how to build those 95% bootstrap confidence intervals for your AR(8) impulse response function (IRF), and integrate this with your existing code. I'll break this down step by step so it's easy to follow.

Step 1: What to Bootstrap for AR Models

For univariate AR models like your AR(8), the standard approach is residual bootstrapping. Here's why:

  • We first estimate the original model to get coefficients and residuals (the unobserved error terms from the model).
  • Since AR models assume residuals are iid (independent and identically distributed), we resample these residuals with replacement to generate new simulated time series.
  • We then re-estimate the AR model on each simulated series and compute its IRF—this gives us a distribution of IRFs we can use to calculate confidence intervals.
Step 2: Integrate Bootstrap with Your IRF Code

Below is a modified, integrated version of your code that handles the bootstrap process, computes bootstrapped IRFs, and calculates the 95% CI. I've added detailed comments to explain each step:

% --- Configuration ---
L = 8;                  % AR(8) lag order
T = 20;                 % Number of periods for IRF (adjust to your needs)
B = 1000;               % Number of bootstrap replications (1000+ is ideal for stability)

% --- Step 1: Estimate Original AR(8) Model & IRF ---
clear;
% Load and demean data
X = xlsread('data.xls');
mean_u = mean(X);
X_demean = X - mean_u*ones(length(X),1);

% Construct lag matrix for AR estimation
LM = lagmatrix(X_demean, 1:L);
X_used = X_demean(L+1:end);  % Trim first L observations (no lags available)
LM_used = LM(L+1:end,:);

% Estimate AR coefficients
beta_original = LM_used \ X_used;

% Calculate original residuals (mean-zero adjusted)
e_original = X_used - LM_used * beta_original;
e_original = e_original - mean(e_original);  % Ensure residuals have mean 0

% Compute original IRF
F_original = companion(beta_original);
sigma_e = std(e_original);
irf_original = zeros(L, T);
irf_original(1,1) = sigma_e;  % Initial impulse to the first lag
for t = 2:T
    irf_original(:,t) = F_original * irf_original(:,t-1);
end
irf_original = irf_original(1,:);  % Keep only the response of the variable itself (univariate AR)

% --- Step 2: Bootstrap Loop ---
irf_boot = zeros(B, T);  % Store IRF for each bootstrap replication

rng(1);  % Set seed for reproducibility
for i = 1:B
    % 1. Resample residuals with replacement
    idx = randi(length(e_original), length(e_original), 1);
    e_boot = e_original(idx);
    
    % 2. Generate a bootstrap time series using original coefficients
    X_boot = zeros(length(X_demean), 1);
    X_boot(1:L) = X_demean(1:L);  % Initialize with original first L observations
    for t = L+1:length(X_demean)
        X_boot(t) = LM_used(t-L,:) * beta_original + e_boot(t-L);
    end
    
    % 3. Re-estimate AR(8) on the bootstrap sample
    LM_boot = lagmatrix(X_boot, 1:L);
    X_boot_used = X_boot(L+1:end);
    LM_boot_used = LM_boot(L+1:end,:);
    beta_boot = LM_boot_used \ X_boot_used;
    
    % 4. Compute IRF for this bootstrap replication
    F_boot = companion(beta_boot);
    irf_temp = zeros(L, T);
    irf_temp(1,1) = sigma_e;
    for t = 2:T
        irf_temp(:,t) = F_boot * irf_temp(:,t-1);
    end
    irf_boot(i,:) = irf_temp(1,:);  % Store the variable's response
end

% --- Step 3: Calculate 95% Confidence Intervals ---
ci_lower = quantile(irf_boot, 0.025, 1);  % 2.5th percentile
ci_upper = quantile(irf_boot, 0.975, 1);  % 97.5th percentile

% --- Step 4: Plot IRF with Confidence Intervals ---
figure;
plot(1:T, irf_original, 'b-', 'LineWidth', 2);
hold on;
plot(1:T, ci_lower, 'r--', 'LineWidth', 1.5);
plot(1:T, ci_upper, 'r--', 'LineWidth', 1.5);
fill([1:T, T:-1:1], [ci_lower, fliplr(ci_upper)], 'r', 'FaceAlpha', 0.1);  % Shaded CI

xlabel('Period');
ylabel('Impulse Response');
title('AR(8) Impulse Response Function with 95% Bootstrap Confidence Intervals');
legend('Original IRF', '95% CI Bounds');
grid on;
Step 3: Key Adjustments & Notes
  • Bootstrap replications (B): Use at least 1000 replications for stable confidence intervals. If computation time is an issue, 500 can work, but 1000 is more reliable.
  • Residual handling: We adjust residuals to have mean zero to match the AR model's assumption of mean-zero errors.
  • Initialization of bootstrap series: Using the original first L observations ensures the simulated series starts in a stationary state (critical for AR models).

内容的提问来源于stack exchange,提问作者I.B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:43:27