Python SDK for the RiskModels API (ERM3 factors, hedge ratios, explained risk)
Project description
riskmodels-py
Python SDK for the RiskModels API (ERM3 factor model: hedge ratios, explained risk, batch portfolio analysis).
Install
pip install riskmodels-py
# Optional xarray panel interface:
pip install riskmodels-py[xarray]
From this monorepo:
cd sdk && pip install -e ".[dev]"
Requires Python 3.10+.
Quickstart
from riskmodels import RiskModelsClient
client = RiskModelsClient.from_env() # RISKMODELS_API_KEY or OAuth client env vars
df = client.get_metrics("NVDA", as_dataframe=True)
pa = client.analyze({"NVDA": 0.5, "AAPL": 0.5}) # alias for analyze_portfolio
print(pa.portfolio_hedge_ratios["l3_market_hr"])
print(pa.to_llm_context())
Environment variables:
RISKMODELS_API_KEY— static Bearer token, orRISKMODELS_CLIENT_ID+RISKMODELS_CLIENT_SECRET— OAuth2 client credentials (JWT ~15m),RISKMODELS_BASE_URL(defaulthttps://riskmodels.app/api),RISKMODELS_OAUTH_SCOPE(optional).
Agent-native helpers (vibe coding)
Use these so agents and humans never guess wire names or ERM3 semantics:
| Tool | Purpose |
|---|---|
client.discover() |
Markdown or JSON digest (format="json", to_stdout=False): each method includes description, parameters (name, type, required, defaults, enums), returns, plus tool_definition_hints for Claude Desktop / MCP-style tool synthesis. |
| Ticker alias | Curated remap (e.g. GOOGL→GOOG) logs info and emits ValidationWarning (Warning: … Fix:) so agents refresh symbols. |
to_llm_context(obj) |
One call → Markdown tables + lineage + semantic cheatsheet + ERM3 legend (obj = DataFrame, PortfolioAnalysis, xarray.Dataset, or dict). |
df.attrs["legend"] |
Short ERM3 text on every tabular result from the client (same as SHORT_ERM3_LEGEND). |
df.attrs["riskmodels_semantic_cheatsheet"] |
Wire→semantic map + column hints + units (JSON + bullet list). Ground truth for field names. |
df.attrs["riskmodels_lineage"] |
JSON string: model version, as-of, factor set, universe size when the API sent them. |
df.attrs["riskmodels_kind"] |
What produced the frame (ticker_returns, portfolio_per_ticker, tickers_universe, …). |
validate="warn" | "error" | "off" |
ER sum + HR sign checks; Error: / Warning: … Fix: strings for self-correction. |
attach_sdk_metadata / ensure_dataframe_legend |
If you build a DataFrame manually, attach the same attrs so to_llm_context stays consistent. |
build_semantic_cheatsheet_md() |
Standalone cheatsheet string for custom prompts. |
Semantic names (always use in code and LLM explanations): l3_market_hr, l3_sector_hr, l3_subsector_hr, l3_market_er, … — not raw V3 keys like l3_mkt_hr. Batch Parquet/CSV wire columns l1/l2/l3 are renamed to those three L3 component HR series (not “L1/L2/L3 model levels”). Full reference (repo root):
SEMANTIC_ALIASES.md(same file as../../SEMANTIC_ALIASES.mdwhen this README is viewed inside the monorepo)docs/ERM3_ZARR_API_PARITY.md
Tip for agents: Prefer get_metrics(..., as_dataframe=True) so you get attrs; the plain dict return has no attrs.
Cursor: .cursorrules (math, naming, batch semantics).
PyPI distribution name vs import
- Install from PyPI:
pip install riskmodels-py(and optionallypip install riskmodels-py[xarray]). - Import in Python:
from riskmodels import …— the distribution on PyPI isriskmodels-py; the package directory isriskmodels.
Core runtime dependencies are pandas, pyarrow, and httpx (HTTP). xarray is optional ([xarray] extra). The SDK does not depend on requests.
Publishing to PyPI (maintainers)
Build and upload from this directory (sdk/, not packages/riskmodels/ — that layout was retired; see repo CHANGELOG.md).
- Bump
versioninpyproject.tomlfor every upload (PyPI rejects duplicate versions). - Build and verify:
cd sdk
python3 -m pip install -U build twine
python3 -m build
python3 -m twine check dist/*
- TestPyPI (optional):
python3 -m twine upload --repository testpypi dist/*— install with
pip install --index-url https://test.pypi.org/simple/ riskmodels-py - Production:
python3 -m twine upload dist/*— use PyPI username__token__and an API token as the password.
License
Proprietary — same terms as RiskModels API access.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file riskmodels_py-0.2.0.tar.gz.
File metadata
- Download URL: riskmodels_py-0.2.0.tar.gz
- Upload date:
- Size: 27.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df510380a1501aa48b187b95181af2c235d13bab0709a6e4f7cdaed15693e75c
|
|
| MD5 |
5f317b7491c1e55adf5bac9c41e2a95a
|
|
| BLAKE2b-256 |
81285b44dddf9de4a989003046f0783a4ace9f8db57db19865d497ff8197f752
|
File details
Details for the file riskmodels_py-0.2.0-py3-none-any.whl.
File metadata
- Download URL: riskmodels_py-0.2.0-py3-none-any.whl
- Upload date:
- Size: 25.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
120e05b9b441736db7402be041b31936e1f8f569cc2481d913fff8bd4040e1ce
|
|
| MD5 |
b7123d734b38078bbe12b138a9c81d0f
|
|
| BLAKE2b-256 |
a92c4cf79f41cdfeee99ed58cc0e4a17d4bfdbf113d8e58e9faf0f550f7ab126
|