Skip to content

esosetrov/recursive_least_squares_rls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

recursive_least_squares_rls

Recursive Least Squares: theoretical foundations, practical applications with copper and monetary datasets, structural break analysis, and comparisons with alternative time-varying parameter methodologies.

Overview

This notebook provides an in-depth exploration of Recursive Least Squares (RLS), an adaptive filtering technique for estimating time-varying parameters in linear regression models. Unlike traditional batch estimation methods, RLS updates parameter estimates recursively as new data becomes available, making it particularly valuable for real-time applications, structural break detection, and analyzing evolving relationships in economic and financial data.

Key Features

1. Theoretical Foundation

  • Mathematical derivation of RLS algorithm with forgetting factors
  • Connection to Kalman filtering framework
  • Comparison with Ordinary Least Squares (OLS) and other estimation methods

2. Practical Implementations

  • Copper Market Analysis: World copper consumption modeling (1951-1975)
  • Quantity Theory of Money: Testing Lucas's hypothesis on money growth and inflation
  • Structural Break Detection: CUSUM and CUSUM of squares tests

3. Methodological Extensions

  • Linear constraints implementation
  • Forgetting factor sensitivity analysis
  • Comparison with state space models
  • Bayesian and machine learning alternatives
  • Predictive performance evaluation

Applications

Economic Modeling

  • Time-varying parameter estimation
  • Monetary policy analysis
  • Commodity market dynamics
  • Structural stability testing

Statistical Analysis

  • Real-time parameter tracking
  • Outlier detection via recursive residuals
  • Model stability diagnostics
  • Forecast error analysis

Comparative Methods

  • Rolling window OLS comparison
  • State space model benchmarking
  • Bayesian inference approaches
  • Machine learning alternatives

Technical Implementation

Core Libraries

  • statsmodels: RLS implementation and statistical testing
  • numpy: Numerical computations and array operations
  • pandas: Data manipulation and time series handling
  • matplotlib: Visualization and plotting
  • pandas-datareader: Financial and economic data retrieval

Advanced Features

  1. Recursive Coefficient Tracking: Visualize parameter evolution over time
  2. Stability Diagnostics: CUSUM and CUSUM of squares tests
  3. Forecast Evaluation: One-step ahead prediction errors
  4. Model Comparison: RLS vs OLS vs state space models
  5. Custom Implementations: Forgetting factor adjustments and constraints

Notebook Structure

Section 1: Environment Setup

  • Library imports and configuration
  • Data loading and preprocessing utilities

Section 2: Theoretical Background

  • RLS algorithm derivation
  • Mathematical formulation with forgetting factors
  • Advantages and limitations discussion

Section 3: Copper Market Case Study

  • Complete analysis of world copper consumption
  • Parameter stability assessment
  • Alternative estimation method comparisons

Section 4: Quantity Theory of Money

  • Testing Lucas's hypothesis (1959-2015)
  • Structural break detection
  • State space model comparison

Section 5: Advanced Features

  • Linear constraint implementation
  • Forgetting factor analysis
  • Predictive performance metrics

Section 6: Methodological Extensions

  • Bayesian approaches
  • Machine learning alternatives
  • Industry applications

Section 7: Best Practices

  • Implementation guidelines
  • Model validation strategies
  • Future research directions

Section 8: References

  • Academic literature
  • Software documentation
  • Data sources

Getting Started

Prerequisites

pip install numpy pandas matplotlib statsmodels pandas-datareader

Basic Usage

import statsmodels.api as sm

# Initialize RLS model
mod = sm.RecursiveLS(endog, exog)
res = mod.fit()

# Access recursive coefficients
coefficients = res.recursive_coefficients.filtered

# Test for structural breaks
res.plot_cusum()
res.plot_cusum_squares()

Key Methods

  • fit(): Estimate model parameters
  • plot_recursive_coefficient(): Visualize parameter evolution
  • plot_cusum(): Structural break detection
  • from_formula(): Formula interface for model specification
  • constrained estimation: Linear constraints on parameters

Key Insights

1. Parameter Stability

  • RLS effectively detects structural breaks in economic relationships
  • CUSUM tests provide visual diagnostics for model stability
  • The quantity theory of money shows evidence of parameter instability post-1990

2. Estimation Efficiency

  • RLS converges faster than stochastic gradient methods
  • Computational complexity scales quadratically with parameters
  • Forgetting factors balance responsiveness to new data vs. stability

3. Practical Applications

  • Real-time economic monitoring
  • Adaptive trading strategies
  • Policy evaluation with time-varying effects

Educational Value

This notebook serves as both a practical implementation guide and theoretical reference for:

  • Graduate students in econometrics and time series analysis
  • Researchers studying time-varying parameter models
  • Practitioners implementing real-time estimation systems
  • Data scientists working with streaming data and adaptive algorithms

Theoretical Contributions

  1. Algorithmic Understanding: Clear derivation of RLS updating equations
  2. Methodological Comparison: Contrast with Kalman filtering and Bayesian methods
  3. Practical Implementation: Real-world case studies with economic interpretation
  4. Diagnostic Framework: Comprehensive model validation and stability testing

Limitations and Considerations

  1. Computational Complexity: O(k²) per update where k is number of parameters
  2. Initialization Sensitivity: Results dependent on initial parameter values
  3. Linearity Assumption: Restricted to linear-in-parameters models
  4. Memory Requirements: Recursive storage of coefficient matrices

Future Extensions

  1. High-Dimensional RLS: Regularization techniques for many predictors
  2. Nonlinear Extensions: Kernel methods and neural network integration
  3. Distributed Computation: Parallel implementation for big data
  4. Automated Diagnostics: Machine learning for model specification

Citation

If using this notebook in research or publications, please acknowledge the methodological framework and cite relevant academic references from Section 8.

Contributing

Contributions to extend functionality, add new examples, or improve documentation are welcome. Please ensure code follows the existing structure and includes appropriate documentation.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Note

This notebook is designed for educational and research purposes. Real-world applications may require additional considerations and validation.

About

A deep exploration of Recursive Least Squares: theoretical foundations, practical applications with copper and monetary datasets, structural break analysis, and comparisons with alternative time-varying parameter methodologies.

Topics

Resources

License

Stars

Watchers

Forks

Contributors