Skip to main content

The RiskOptima toolkit is a comprehensive Python solution designed to assist investors in evaluating, managing, and optimizing the risk of their investment portfolios. This package implements advanced financial metrics and models to compute key risk indicators, including Value at Risk (VaR), Conditional Value at Risk (CVaR), and volatility assessment

Project description

RiskOptima

image

RiskOptima is a comprehensive Python toolkit for evaluating, managing, and optimizing investment portfolios. This package is designed to empower investors and data scientists by combining financial risk analysis, backtesting, mean-variance optimization, and machine learning capabilities into a single, cohesive package.

Stats

https://pypistats.org/packages/riskoptima

Key Features

  • Portfolio Optimization: Includes mean-variance optimization, efficient frontier calculation, and maximum Sharpe ratio portfolio construction.
  • Risk Management: Compute key financial risk metrics such as Value at Risk (VaR), Conditional Value at Risk (CVaR), volatility, and drawdowns.
  • Backtesting Framework: Simulate historical performance of investment strategies and analyze portfolio dynamics over time.
  • Machine Learning Integration: Future-ready for implementing machine learning models for predictive analytics and advanced portfolio insights.
  • Monte Carlo Simulations: Perform extensive simulations to analyze potential portfolio outcomes. See example here https://github.com/JordiCorbilla/efficient-frontier-monte-carlo-portfolio-optimization
  • Comprehensive Financial Metrics: Calculate returns, Sharpe ratios, covariance matrices, and more.

Installation

See the project here: https://pypi.org/project/riskoptima/

pip install riskoptima

Usage

Example 1: Efficient Frontier - Monte Carlo Portfolio Optimization

import pandas as pd
from riskoptima import RiskOptima

# Define your current porfolio with your weights and company names
asset_data = [
    {"Asset": "MO",    "Weight": 0.04, "Label": "Altria Group Inc.",       "MarketCap": 110.0e9},
    {"Asset": "NWN",   "Weight": 0.14, "Label": "Northwest Natural Gas",   "MarketCap": 1.8e9},
    {"Asset": "BKH",   "Weight": 0.01, "Label": "Black Hills Corp.",         "MarketCap": 4.5e9},
    {"Asset": "ED",    "Weight": 0.01, "Label": "Con Edison",                "MarketCap": 30.0e9},
    {"Asset": "PEP",   "Weight": 0.09, "Label": "PepsiCo Inc.",              "MarketCap": 255.0e9},
    {"Asset": "NFG",   "Weight": 0.16, "Label": "National Fuel Gas",         "MarketCap": 5.6e9},
    {"Asset": "KO",    "Weight": 0.06, "Label": "Coca-Cola Company",         "MarketCap": 275.0e9},
    {"Asset": "FRT",   "Weight": 0.28, "Label": "Federal Realty Inv. Trust", "MarketCap": 9.8e9},
    {"Asset": "GPC",   "Weight": 0.16, "Label": "Genuine Parts Co.",         "MarketCap": 25.3e9},
    {"Asset": "MSEX",  "Weight": 0.05, "Label": "Middlesex Water Co.",       "MarketCap": 2.4e9}
]
asset_table = pd.DataFrame(asset_data)

capital = 100_000

asset_table['Portfolio'] = asset_table['Weight'] * capital

start_date = '2024-01-01'
end_date = RiskOptima.get_previous_working_day()

RiskOptima.plot_efficient_frontier_monte_carlo(
    asset_table,
    start_date=start_date,
    end_date=end_date,
    risk_free_rate=0.05,
    num_portfolios=10000,
    market_benchmark='SPY',
    set_ticks=False,
    x_pos_table=1.15,    # Position for the weight table on the plot
    y_pos_table=0.52,    # Position for the weight table on the plot
    title=f'Efficient Frontier - Monte Carlo Simulation {start_date} to {end_date}'
)

efficient_frontier_monter_carlo_20250203_205339

Example 2: Portfolio Optimization using Mean Variance and Machine Learning

RiskOptima.run_portfolio_optimization_mv_ml(
    asset_table=asset_table,
    training_start_date='2022-01-01',
    training_end_date='2023-11-27',
    model_type='Linear Regression',    
    risk_free_rate=0.05,
    num_portfolios=100000,
    market_benchmark=['SPY'],
    max_volatility=0.15,
    min_weight=0.03,
    max_weight=0.2
)

machine_learning_optimization_20250203_210953

Example 3: Macaulay Duration

from riskoptima import RiskOptima
cf = RiskOptima.bond_cash_flows_v2(4, 1000, 0.06, 2)  # 2 years, semi-annual, hence 4 periods
md_2 = RiskOptima.macaulay_duration_v3(cf, 0.05, 2)
md_2

image

Documentation

For complete documentation and usage examples, visit the GitHub repository:

RiskOptima GitHub

Contributing

We welcome contributions! If you'd like to improve the package or report issues, please visit the GitHub repository.

License

RiskOptima is licensed under the MIT License.

Support me

Buy Me A Coffee

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

riskoptima-1.16.0.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

riskoptima-1.16.0-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

Details for the file riskoptima-1.16.0.tar.gz.

File metadata

  • Download URL: riskoptima-1.16.0.tar.gz
  • Upload date:
  • Size: 33.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.7 Windows/10

File hashes

Hashes for riskoptima-1.16.0.tar.gz
Algorithm Hash digest
SHA256 bc4ac12829c9f4df92aceef3c4c3be76e7d540fe03ae4dc18d7b4aebf81cf787
MD5 ec95b8d311d532cb8ac782b4e69ed7f3
BLAKE2b-256 3d8e50945e5bd913728c0fe0c3b4b4b03883bcc93fac5fea1cb760f165c84aee

See more details on using hashes here.

File details

Details for the file riskoptima-1.16.0-py3-none-any.whl.

File metadata

  • Download URL: riskoptima-1.16.0-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.12.7 Windows/10

File hashes

Hashes for riskoptima-1.16.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0db2e7ba524ffe4e06e1bab01464f8b717e551152199e07a377d5d8bef1a0c5
MD5 74efd1058e532dbc8d02a1f131dcf739
BLAKE2b-256 eaf4d9cff7e3cfdc9adff252dffb698519f55e9f635bc600522136f96aa18b0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page