# ronaldkanyepi/portfolio-optimization-pyomo architecture

> Architecture diagram of the GitHub repository ronaldkanyepi/portfolio-optimization-pyomo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ronaldkanyepi/portfolio-optimization-pyomo
- Repository: https://github.com/ronaldkanyepi/portfolio-optimization-pyomo

## Overview

##### Project type and purpose
- This is a single‐page, data‐driven full-stack Streamlit application focused on portfolio optimization.
- It uses Streamlit for the UI, yfinance as an external data source, Pyomo (with IPOPT) as the optimization engine, and common data-science libraries for processing and visualization.

##### File structure
- At the top level you have:
- main.py – contains both the Streamlit UI code and calls into data retrieval, processing, optimization, and plotting.
- requirements.txt – declares dependencies.

##### Key components
a. Presentation Layer (Streamlit UI)
- User’s browser (or local host) interacts with the Streamlit server process in main.py.
- Displays form controls (ticker selection, date pickers) and renders charts/tables.
b. Data Retrieval Layer
- yfinance Python client fetches historical stock price data from Yahoo Finance API.
- External Service: Yahoo Finance (HTTP calls).
c. Data Processing Layer
- pandas & numpy compute returns, covariance matrices, etc.
d. Optimization Engine
- Pyomo model definition + IPOPT solver binary.
- Takes processed data and user’s target return constraint to solve the risk-minimization problem.
e. Visualization Layer
- seaborn/matplotlib produce the correlation heatmap and allocation bar charts.
f. Local Environment
- Python interpreter, virtual environment, and IPOPT binary installed on user’s machine.

##### Relationships & data flow
1. User → Streamlit UI: enters tickers + date range
2. Streamlit UI → Data Retrieval: calls yfinance to download price history
3. Retrieved data → Data Processing: calculate daily returns, covariance
4. Processed data + target return → Optimization Engine (Pyomo → IPOPT)
5. Solution (allocations, risk, expected return) → Streamlit UI
6. Streamlit UI → Visualization: call seaborn/matplotlib to render charts
7. Charts & tables → User browser

##### Architectural patterns & design principles
- Single-page reactive app: UI code and business logic co-located but conceptually layered.
- Modular data flow: clearly separate data retrieval, processing, optimization, and visualization.
- External dependency isolation: IPOPT is an out-of-process solver; yfinance is an external web service.

##### Capturing essential details
- That main.py hosts all these layers in one process.
- IPOPT as an external executable invoked by Pyomo.
- Network boundary around the Yahoo Finance calls.
- Optionally, note the Python venv as the deployment context, and requirements.txt as the dependency manifest.

## Diagram (Mermaid)

```mermaid
graph TD
    Browser["User Browser"]:::ui
    subgraph "main.py Process"
        UI["Presentation Layer (Streamlit UI)"]:::ui
        DR["Data Retrieval (yfinance client)"]:::data
        DP["Data Processing (pandas, numpy)"]:::data
        OPT["Optimization Engine (Pyomo + IPOPT)"]:::opt
        VIS["Visualization (seaborn, matplotlib)"]:::viz
    end
    subgraph "External Dependencies"
        YF["Yahoo Finance API"]:::external_service
        IPOPT_BIN["IPOPT Executable"]:::external_service
        VENV["Python venv"]:::external_env
        REQ["Dependency Manifest (requirements.txt)"]:::deps
    end

    Browser -->|"user input"| UI
    UI -->|"tickers+dates"| DR
    DR -->|"price data"| DP
    DP -->|"covariance matrix+target return"| OPT
    OPT -->|"optimal allocation|risk|return"| UI
    UI -->|"plot calls"| VIS
    VIS -->|"charts & tables"| Browser

    YF --- DR
    IPOPT_BIN --- OPT
    REQ --- UI
    VENV --- UI

    click UI "https://github.com/ronaldkanyepi/portfolio-optimization-pyomo/blob/main/main.py"
    click DR "https://github.com/ronaldkanyepi/portfolio-optimization-pyomo/blob/main/main.py"
    click DP "https://github.com/ronaldkanyepi/portfolio-optimization-pyomo/blob/main/main.py"
    click OPT "https://github.com/ronaldkanyepi/portfolio-optimization-pyomo/blob/main/main.py"
    click VIS "https://github.com/ronaldkanyepi/portfolio-optimization-pyomo/blob/main/main.py"
    click REQ "https://github.com/ronaldkanyepi/portfolio-optimization-pyomo/blob/main/requirements.txt"

    classDef ui fill:#ADD8E6,stroke:#000
    classDef data fill:#90EE90,stroke:#000
    classDef opt fill:#FFA500,stroke:#000
    classDef viz fill:#DA70D6,stroke:#000
    classDef external_service fill:#D3D3D3,stroke:#000,stroke-dasharray: 2 2
    classDef external_env fill:#D3D3D3,stroke:#000,stroke-dasharray: 2 2
    classDef deps fill:#D3D3D3,stroke:#000,stroke-dasharray: 2 2
```
