File size: 2,880 Bytes
d848cbc
e6b8a0f
 
d848cbc
 
 
 
02f7073
e6b8a0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d848cbc
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
title: Portfolio Volatility Analyzer
emoji: ๐Ÿ“Š
colorFrom: blue
colorTo: green
sdk: docker
pinned: false
short_description: Portfolio risk analysis with OCR and LaTeX formulas
---

# ๐Ÿ“Š Portfolio Volatility Analyzer

Analyze your investment portfolio risk using **Modern Portfolio Theory** with OCR, interactive visualizations, and beautiful mathematical formulas.

## Features

- ๐Ÿ“ธ **OCR Portfolio Parsing**: Upload screenshots of your portfolio and automatically extract tickers and amounts
- โœ๏ธ **Editable JSON**: Correct OCR errors with an intuitive JSON editor
- ๐Ÿ“ˆ **Historical Data**: Automatically fetch 1 year of price data from Yahoo Finance
- ๐Ÿงฎ **Full Calculations**:
  - Portfolio weights
  - Log returns
  - Covariance matrix
  - Portfolio variance and volatility
- ๐Ÿ“ **Beautiful LaTeX Formulas**: See every calculation step with symbolic and numerical formulas
- ๐Ÿ“Š **Detailed Variance Expansion**: Step-by-step breakdown showing how each asset contributes to portfolio risk
- ๐ŸŽš๏ธ **Interactive Rebalancing**: Adjust portfolio amounts with sliders and see volatility update in real-time

## How to Use

1. **Upload Portfolio Screenshot**: Take a screenshot of your portfolio (must show ticker symbols and dollar amounts)
2. **Edit Portfolio JSON**: Review and correct any OCR errors in the JSON editor
3. **Validate Portfolio**: Click "Validate Portfolio" to start analysis
4. **View Results**: See historical data, covariance matrix, and detailed formulas
5. **Rebalance**: Use interactive sliders to adjust positions and see impact on volatility

## Technical Details

### Formula Highlights

**Portfolio Variance:**
```
ฯƒยฒ_p = w^T ร— ฮฃ ร— w
```

Where:
- `w` = vector of portfolio weights
- `ฮฃ` = covariance matrix (annualized)

**Portfolio Volatility:**
```
ฯƒ_p = โˆš(ฯƒยฒ_p)
```

### Architecture

- **Frontend**: Streamlit
- **OCR**: Tesseract (pytesseract)
- **Financial Data**: yfinance (Yahoo Finance)
- **Math**: NumPy, Pandas, SymPy
- **Deployment**: Docker on Hugging Face Spaces

## Local Development

### Prerequisites
- Python 3.11+
- Tesseract OCR installed

### Setup
```bash
# Install dependencies
pip install -r requirements.txt

# Run the app
streamlit run app.py
```

### Docker Build
```bash
# Build
docker build -t portfolio-analyzer .

# Run
docker run -p 7860:7860 portfolio-analyzer
```

## Example Portfolio

Test the app with this JSON:
```json
{
  "AAPL": 5000,
  "GOOGL": 3000,
  "MSFT": 2000
}
```

## Notes

- Uses 252 trading days for annualization
- Calculates log returns: ln(P_t / P_{t-1})
- Smart truncation for portfolios with 4+ tickers
- 1-hour cache for historical data to reduce API calls

## Built With

- Modern Portfolio Theory
- LaTeX mathematical notation
- Real-time financial data

---

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference