Matlab与Python中Garch(1,1)模型估计结果差异咨询
arch Package and MATLAB's fmincon (and How to Fix It) Great question—this discrepancy between Python's arch package and MATLAB's fmincon for GARCH(1,1) estimates is super common, and it almost always comes down to subtle (or not-so-subtle) differences in how each tool approaches model fitting. Let's break down the most likely causes and actionable fixes:
Common Causes of Discrepancies
1. Misaligned Objective Function & Optimization Constraints
GARCH models rely on maximizing a log-likelihood function, but:
- The
archpackage automatically enforces critical GARCH parameter constraints: $\omega > 0$, $\alpha \geq 0$, $\beta \geq 0$, and $\alpha + \beta < 1$. If you manually coded the log-likelihood in MATLAB withfminconand didn't set these constraints properly (e.g., allowing $\omega$ to be negative, or $\alpha+\beta$ to exceed 1), you'll get invalid or suboptimal parameter estimates. - The
archpackage uses SciPy's optimization algorithms (default: L-BFGS-B) tailored for constrained optimization, whilefmincondefaults to an interior-point method. Different optimizers can converge to different local minima if the log-likelihood surface is non-convex (which GARCH often is).
2. Different Initial Parameter Guesses
Optimization algorithms for GARCH are highly sensitive to starting values. The arch package calculates default initial guesses based on sample variance and autocorrelation of squared returns, while if you picked arbitrary starting values in MATLAB (e.g., [0,0,0]), the optimizer might converge to a different local minimum instead of the global optimum.
3. Inconsistent Data Preprocessing
Even tiny differences in data can shift results:
- Did you use log returns in both tools? GARCH models are designed for log returns, but if you used simple returns in one and log returns in the other, estimates will diverge.
- Are your datasets identical? Check for missing values, truncated time ranges, or different data cleaning steps (e.g., one tool dropped outliers and the other didn't).
4. Mismatched Distribution Assumptions
The log-likelihood calculation depends entirely on the assumed distribution of residuals:
- The
archpackage's defaultGARCHmodel uses a normal distribution, but if you coded a Student's t-distribution (or another heavy-tailed distribution) in MATLAB, your parameter estimates will be drastically different. Conversely, if you used a t-distribution inarch(viadist='StudentsT') but normal in MATLAB, you'll see gaps.
Step-by-Step Fixes
1. Align Constraints & Objective Function
- In MATLAB, explicitly enforce GARCH constraints in
fmincon:% Constraints: ω > 0, α ≥ 0, β ≥ 0, α + β < 1 lb = [1e-6, 0, 0]; % Small lower bound for ω to avoid numerical issues A = [0 1 1]; b = 0.999; % Use 0.999 instead of 1 for numerical stability options = optimoptions('fmincon', 'Algorithm', 'L-BFGS-B'); % Match arch's default optimizer - Ensure your MATLAB log-likelihood function matches
arch's logic. For a normal distribution, the negative log-likelihood should look like this:function nll = garch11_nll(params, returns) ω = params(1); α = params(2); β = params(3); T = length(returns); σ² = zeros(T, 1); σ²(1) = var(returns); % Match arch's initial variance assumption for t = 2:T σ²(t) = ω + α * returns(t-1)^2 + β * σ²(t-1); end nll = 0.5 * sum(log(σ²) + returns.^2 ./ σ²); end
2. Use Consistent Initial Values
- Steal the initial guesses from
archand use them in MATLAB (or vice versa) to eliminate initialization bias. In Python, you can getarch's default initial values like this:from arch import arch_model model = arch_model(returns, vol='Garch', p=1, q=1) print(model.start_params) # Use these values as x0 in MATLAB's fmincon - Alternatively, force
archto use MATLAB's initial values:fit_result = model.fit(start_params=[0.278061, 0.457286, 0.0328433], disp='off')
3. Standardize Data
- Export the exact same dataset from one tool to the other (e.g., save Python's
returnsto a CSV and load it in MATLAB) to ensure no differences in observations or preprocessing. - Double-check that both tools are using log returns (calculate as
log(price/price.shift(1))in Python,log(price(2:end)./price(1:end-1))in MATLAB).
4. Match Distribution Assumptions
- If you used a normal distribution in
arch, make sure your MATLAB code does the same. If you need a heavy-tailed distribution, explicitly set it in both tools:- Python:
arch_model(returns, vol='Garch', p=1, q=1, dist='StudentsT') - MATLAB: Modify your log-likelihood function to use the Student's t-distribution's probability density function.
- Python:
5. Verify Convergence
- Check that both optimizers actually converged to a stable solution:
- In Python, look at
fit_result.summary()for convergence flags (e.g.,Convergence: True). - In MATLAB, check
fmincon's exit flag (a value of 1 means successful convergence). If convergence failed, adjust tolerance settings (e.g.,optimoptions('fmincon', 'TolFun', 1e-8)).
- In Python, look at
Final Note
Once you align all these variables—data, constraints, initial values, distribution, and optimizer—your arch and fmincon estimates should be nearly identical. GARCH estimation is finicky, but small tweaks to consistency will resolve most discrepancies.
内容的提问来源于stack exchange,提问作者vitoco8391

