Examples Overview
Relevant source files
The following files were used as context for generating this wiki page:
AlphaPy provides specialized pipelines tailored for two primary domains: financial markets and professional sports. These examples are implemented through the MarketFlow and SportFlow modules, which leverage the core AlphaPy machine learning pipeline to test predictive models, develop trading systems, or analyze sporting events.
The framework is designed to handle the entire lifecycle of a predictive model, from domain-specific feature engineering (such as calculating "deltas" between team stats or financial technical indicators) to model evaluation and visualization.
Sources: README.rst:11-20, setup.py:10-12
MarketFlow: Financial Markets Example
MarketFlow is the primary module for financial speculators. It automates the process of fetching market data, applying technical variables, and running a system-based analysis or machine learning model.
MarketFlow Components
MarketFlow integrates several sub-systems to move from raw price data to a portfolio analysis:
- Analysis: Handles the logic for lagging periods, forecast periods, and identifies "leaders" in the data.
- System: Defines entry/exit logic (Long Entry, Short Entry, etc.) and holding periods.
- Portfolio: Generates tear sheets using libraries like
pyfolioto analyze trading performance.
Sources: alphapy/market_flow.py:161-196, README.rst:18-20
MarketFlow Data Pipeline
The following diagram illustrates how market data flows through the specialized MarketFlow pipeline:
The pipeline allows users to either build a machine learning model to predict market movements or directly run a rules-based trading system.
Sources: alphapy/market_flow.py:161-203
SportFlow: Sports Betting Example
SportFlow is designed for predicting sporting events, specifically focusing on metrics like point margins, spreads, and over/under totals. The NCAAB (College Basketball) example provided in the repository serves as a template for these configurations.
Feature Engineering for Sports
Unlike standard datasets, SportFlow generates "delta" features. It calculates the difference between the home and away teams' statistics (e.g., delta.wins, delta.losses) to create a single row representing the matchup.
| Feature Category | Description |
|---|---|
| Point Metrics | won_on_points, point_margin_game, point_margin_season_avg |
| Spread Metrics | won_on_spread, cover_margin_game, cover_win_streak |
| Over/Under | overunder_margin, over_streak, total_points |
| Deltas | Calculations representing home_stat - away_stat |
Sources: alphapy/sport_flow.py:73-118, alphapy/sport_flow.py:339-349
SportFlow Execution Flow
Sources: alphapy/sport_flow.py:365-515
Domain-Specific Configurations
Examples are driven by YAML configuration files. The NCAAB example illustrates how a user defines the target variable and features to exclude from the training set to prevent data leakage.
Example: NCAAB model.yml
The configuration for the NCAA Basketball example specifies:
- Target:
won_on_spread(Binary Classification) - Algorithms:
['RF', 'XGB'](Random Forest and XGBoost) - Drop List: Prevents the model from seeing the actual final score or margin during training.
- Factors: Identifies key variables like
line,delta.wins, anddelta.cover_win_streak.
Sources: alphapy/examples/NCAAB/config/model.yml:1-100
Execution Commands
AlphaPy provides console scripts to run these examples directly after installation:
mflow: Executes the MarketFlow pipeline.sflow: Executes the SportFlow pipeline.alphapy: Executes the generic machine learning pipeline.
Sources: setup.py:65-71
Conclusion
The examples in AlphaPy demonstrate the framework's flexibility in handling time-series and event-based data. By providing pre-built flows for markets and sports, the project allows developers to focus on feature engineering and model selection rather than pipeline infrastructure.