# amidamaru161/pfm architecture

> Architecture diagram of the GitHub repository amidamaru161/pfm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/amidamaru161/pfm
- Repository: https://github.com/amidamaru161/pfm

## Overview

This repository implements reinforcement-learning portfolio optimization: market-price data is prepared into features, a DRL policy proposes portfolio weights, and a trading environment simulates rebalancing and returns. The agent layer coordinates training and validation; prediction supports inference and simulation from saved weights. The README establishes the broad capabilities, while the excerpts show only selected calls, so unsampled preprocessing-to-training wiring is documented rather than assumed.

Main entry points: [`prediction/predict_future_distribution.py`](https://github.com/amidamaru161/pfm/blob/main/prediction/predict_future_distribution.py) (Distribution prediction), [`agents/policy_gradient.py`](https://github.com/amidamaru161/pfm/blob/main/agents/policy_gradient.py) (Policy gradient), [`agents/hyper_policy.py`](https://github.com/amidamaru161/pfm/blob/main/agents/hyper_policy.py) (Hyper policy), [`environments/portfolio_memory.py`](https://github.com/amidamaru161/pfm/blob/main/environments/portfolio_memory.py) (Portfolio memory)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Market data"]
  node_market_files[("Market CSVs")]
  node_feature_engineering["Feature engineering"]
  node_market_utilities["Market utilities<br/>[utils.py]"]
end

subgraph group_learning["RL training"]
  node_agent_api["DRL agent<br/>[dlr_agent.py]"]
  node_policy_gradient["Policy gradient<br/>[policy_gradient.py]"]
  node_hyper_policy["Hyper policy<br/>[hyper_policy.py]"]
  node_portfolio_memory["Portfolio memory"]
end

subgraph group_networks["Policy networks"]
  node_standard_models["EIIE / EI3 / GPM<br/>[models.py]"]
  node_hyper_network["Hyper network<br/>[hyper_net.py]"]
  node_hyper_network_base["Hyper network base<br/>[hyper_net_base.py]"]
end

subgraph group_simulation["Portfolio simulation"]
  node_portfolio_environment["Portfolio environment<br/>[portfolio_env.py]"]
end

subgraph group_inference["Prediction"]
  node_saved_weights[("Saved weights")]
  node_distribution_prediction["Distribution prediction"]
end

node_moex["MOEX ISS"]

node_market_files -->|"prepares features"| node_feature_engineering
node_market_utilities -->|"downloads candles"| node_moex
node_feature_engineering -->|"uses indicators"| node_market_utilities
node_agent_api -->|"selects algorithm"| node_policy_gradient
node_agent_api -->|"selects algorithm"| node_hyper_policy
node_policy_gradient -.->|"steps environment"| node_portfolio_environment
node_hyper_policy -->|"steps environment"| node_portfolio_environment
node_policy_gradient -->|"uses memory"| node_portfolio_memory
node_hyper_policy -->|"uses memory"| node_portfolio_memory
node_policy_gradient -->|"uses policies"| node_standard_models
node_hyper_policy -->|"uses policy"| node_hyper_network
node_distribution_prediction -->|"loads model"| node_standard_models
node_distribution_prediction -->|"steps simulation"| node_portfolio_environment
node_distribution_prediction -->|"retrieves actions"| node_portfolio_memory
node_distribution_prediction -->|"loads weights"| node_saved_weights

click node_market_files "https://github.com/amidamaru161/pfm/tree/main/data"
click node_feature_engineering "https://github.com/amidamaru161/pfm/blob/main/preprocessing/feature_creating.py"
click node_market_utilities "https://github.com/amidamaru161/pfm/blob/main/utils/utils.py"
click node_agent_api "https://github.com/amidamaru161/pfm/blob/main/agents/dlr_agent.py"
click node_policy_gradient "https://github.com/amidamaru161/pfm/blob/main/agents/policy_gradient.py"
click node_hyper_policy "https://github.com/amidamaru161/pfm/blob/main/agents/hyper_policy.py"
click node_portfolio_memory "https://github.com/amidamaru161/pfm/blob/main/environments/portfolio_memory.py"
click node_standard_models "https://github.com/amidamaru161/pfm/blob/main/models/models.py"
click node_hyper_network "https://github.com/amidamaru161/pfm/blob/main/models/hyper_net.py"
click node_hyper_network_base "https://github.com/amidamaru161/pfm/blob/main/models/hyper_net_base.py"
click node_portfolio_environment "https://github.com/amidamaru161/pfm/blob/main/environments/portfolio_env.py"
click node_saved_weights "https://github.com/amidamaru161/pfm/tree/main/saved_models"
click node_distribution_prediction "https://github.com/amidamaru161/pfm/blob/main/prediction/predict_future_distribution.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_market_files,node_feature_engineering,node_market_utilities toneBlue
class node_agent_api,node_policy_gradient,node_hyper_policy,node_portfolio_memory toneAmber
class node_standard_models,node_hyper_network,node_hyper_network_base toneMint
class node_portfolio_environment toneRose
class node_saved_weights,node_distribution_prediction,node_moex toneIndigo
```

## Components

### Market data

- [Market CSVs](https://github.com/amidamaru161/pfm/tree/main/data): `data`
- [Feature engineering](https://github.com/amidamaru161/pfm/blob/main/preprocessing/feature_creating.py): `preprocessing/feature_creating.py`
- [Market utilities](https://github.com/amidamaru161/pfm/blob/main/utils/utils.py): `utils/utils.py`

### RL training

- [DRL agent](https://github.com/amidamaru161/pfm/blob/main/agents/dlr_agent.py): `agents/dlr_agent.py`
- [Policy gradient](https://github.com/amidamaru161/pfm/blob/main/agents/policy_gradient.py): `agents/policy_gradient.py`
- [Hyper policy](https://github.com/amidamaru161/pfm/blob/main/agents/hyper_policy.py): `agents/hyper_policy.py`
- [Portfolio memory](https://github.com/amidamaru161/pfm/blob/main/environments/portfolio_memory.py): `environments/portfolio_memory.py`

### Policy networks

- [EIIE / EI3 / GPM](https://github.com/amidamaru161/pfm/blob/main/models/models.py): `models/models.py`
- [Hyper network](https://github.com/amidamaru161/pfm/blob/main/models/hyper_net.py): `models/hyper_net.py`
- [Hyper network base](https://github.com/amidamaru161/pfm/blob/main/models/hyper_net_base.py): `models/hyper_net_base.py`

### Portfolio simulation

- [Portfolio environment](https://github.com/amidamaru161/pfm/blob/main/environments/portfolio_env.py): `environments/portfolio_env.py`

### Prediction

- [Saved weights](https://github.com/amidamaru161/pfm/tree/main/saved_models): `saved_models`
- [Distribution prediction](https://github.com/amidamaru161/pfm/blob/main/prediction/predict_future_distribution.py): `prediction/predict_future_distribution.py`

### Other components

- **MOEX ISS**

## Connections

- Market CSVs → Feature engineering: prepares features (evidence: `README.md`)
- Market utilities → MOEX ISS: downloads candles (evidence: `utils/utils.py`)
- Feature engineering → Market utilities: uses indicators (evidence: `preprocessing/feature_creating.py`)
- DRL agent → Policy gradient: selects algorithm (evidence: `agents/dlr_agent.py`)
- DRL agent → Hyper policy: selects algorithm (evidence: `agents/dlr_agent.py`)
- Policy gradient → Portfolio environment: steps environment
- Hyper policy → Portfolio environment: steps environment (evidence: `agents/hyper_policy.py`)
- Policy gradient → Portfolio memory: uses memory (evidence: `agents/policy_gradient.py`)
- Hyper policy → Portfolio memory: uses memory (evidence: `agents/hyper_policy.py`)
- Policy gradient → EIIE / EI3 / GPM: uses policies (evidence: `agents/policy_gradient.py`)
- Hyper policy → Hyper network: uses policy (evidence: `agents/hyper_policy.py`)
- Distribution prediction → EIIE / EI3 / GPM: loads model (evidence: `prediction/predict_future_distribution.py`)
- Distribution prediction → Portfolio environment: steps simulation (evidence: `prediction/predict_future_distribution.py`)
- Distribution prediction → Portfolio memory: retrieves actions (evidence: `prediction/predict_future_distribution.py`)
- Distribution prediction → Saved weights: loads weights (evidence: `prediction/predict_future_distribution.py`)
