Download api/routes_analysis.py from bhuvi3/CIOAgent: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/spaces/bhuvi3/CIOAgent/resolve/main/api/routes_analysis.py
- Command line
-
hf download hf://spaces/bhuvi3/CIOAgent/api/routes_analysis.py
-
curl -L -o routes_analysis.py https://huggingface.co/spaces/bhuvi3/CIOAgent/resolve/main/api/routes_analysis.py
2.09 kB
| import asyncio | |
| import numpy as np | |
| from fastapi import APIRouter, HTTPException | |
| from api.schemas import ( | |
| AnalysisResponse, | |
| FrontierResponse, | |
| PortfolioRequest, | |
| ) | |
| from metrics.frontier import compute_efficient_frontier | |
| from metrics.portfolio import compute_metrics | |
| router = APIRouter(prefix="/analysis", tags=["analysis"]) | |
| async def compute(req: PortfolioRequest): | |
| weights = np.array(req.weights) | |
| try: | |
| metrics = await asyncio.to_thread( | |
| compute_metrics, | |
| req.tickers, | |
| req.benchmarks, | |
| weights, | |
| req.start_date, | |
| req.rf, | |
| req.sims, | |
| req.horizon, | |
| ) | |
| except RuntimeError as e: | |
| raise HTTPException(status_code=422, detail=str(e)) | |
| return AnalysisResponse( | |
| performance=metrics["performance"], | |
| risk=metrics["risk"], | |
| benchmark=metrics["benchmark"], | |
| stress_test=metrics["stress_test"], | |
| ) | |
| async def frontier(req: PortfolioRequest): | |
| weights = np.array(req.weights) | |
| try: | |
| metrics = await asyncio.to_thread( | |
| compute_metrics, | |
| req.tickers, | |
| req.benchmarks, | |
| weights, | |
| req.start_date, | |
| req.rf, | |
| req.sims, | |
| req.horizon, | |
| ) | |
| frontier_data = await asyncio.to_thread( | |
| compute_efficient_frontier, | |
| metrics["returns_df"], | |
| req.tickers, | |
| req.rf, | |
| ) | |
| except RuntimeError as e: | |
| raise HTTPException(status_code=422, detail=str(e)) | |
| return FrontierResponse( | |
| frontier_points=frontier_data["frontier_points"], | |
| max_sharpe=frontier_data["max_sharpe"], | |
| min_vol=frontier_data["min_vol"], | |
| analysis=AnalysisResponse( | |
| performance=metrics["performance"], | |
| risk=metrics["risk"], | |
| benchmark=metrics["benchmark"], | |
| stress_test=metrics["stress_test"], | |
| ), | |
| ) | |