{"id":23894510,"url":"https://github.com/jordicorbilla/riskoptima","last_synced_at":"2025-04-15T05:48:48.643Z","repository":{"id":243277385,"uuid":"806215825","full_name":"JordiCorbilla/RiskOptima","owner":"JordiCorbilla","description":"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 assessme","archived":false,"fork":false,"pushed_at":"2025-04-13T20:39:39.000Z","size":102905,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-15T05:48:37.824Z","etag":null,"topics":["cvar-optimization","monte-carlo","portfolio-optimization","risk-management","var-optimization"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JordiCorbilla.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-26T17:37:37.000Z","updated_at":"2025-04-13T20:39:42.000Z","dependencies_parsed_at":"2024-12-30T18:18:46.661Z","dependency_job_id":"6f8aa521-f8c4-4b80-8dc6-71dadfe77eb6","html_url":"https://github.com/JordiCorbilla/RiskOptima","commit_stats":null,"previous_names":["jordicorbilla/portfolio_risk_kit","jordicorbilla/riskoptima"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JordiCorbilla%2FRiskOptima","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JordiCorbilla%2FRiskOptima/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JordiCorbilla%2FRiskOptima/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JordiCorbilla%2FRiskOptima/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JordiCorbilla","download_url":"https://codeload.github.com/JordiCorbilla/RiskOptima/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249016316,"owners_count":21198832,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cvar-optimization","monte-carlo","portfolio-optimization","risk-management","var-optimization"],"created_at":"2025-01-04T14:59:22.797Z","updated_at":"2025-04-15T05:48:48.636Z","avatar_url":"https://github.com/JordiCorbilla.png","language":"Jupyter Notebook","funding_links":["https://www.buymeacoffee.com/jordicorbilla"],"categories":[],"sub_categories":[],"readme":"# RiskOptima\n\n![image](https://github.com/user-attachments/assets/b9bc3bd0-d8fa-4f01-97e6-44bf4b886bcb)\n\n\nRiskOptima 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.\n\n## Stats\nhttps://pypistats.org/packages/riskoptima\n\n## Key Features\n\n- Portfolio Optimization: Includes mean-variance optimization, efficient frontier calculation, and maximum Sharpe ratio portfolio construction.\n- Risk Management: Compute key financial risk metrics such as Value at Risk (VaR), Conditional Value at Risk (CVaR), volatility, and drawdowns.\n- Backtesting Framework: Simulate historical performance of investment strategies and analyze portfolio dynamics over time.\n- Machine Learning Integration: Future-ready for implementing machine learning models for predictive analytics and advanced portfolio insights.\n- Monte Carlo Simulations: Perform extensive simulations to analyze potential portfolio outcomes. See example here https://github.com/JordiCorbilla/efficient-frontier-monte-carlo-portfolio-optimization\n- Comprehensive Financial Metrics: Calculate returns, Sharpe ratios, covariance matrices, and more.\n\n## Installation\n\nSee the project here: https://pypi.org/project/riskoptima/\n\n```\npip install riskoptima\n```\n## Usage\n\n### Example 1: Setting up your portfolio\n\nCreate your portfolio table similar to the below:\n\n| Asset | Weight | Label                         | MarketCap |\n|-------|--------|-------------------------------|-----------|\n| MO    | 0.04   | Altria Group Inc.             | 110.0e9   |\n| NWN   | 0.14   | Northwest Natural Gas         | 1.8e9     |\n| BKH   | 0.01   | Black Hills Corp.             | 4.5e9     |\n| ED    | 0.01   | Con Edison                    | 30.0e9    |\n| PEP   | 0.09   | PepsiCo Inc.                  | 255.0e9   |\n| NFG   | 0.16   | National Fuel Gas             | 5.6e9     |\n| KO    | 0.06   | Coca-Cola Company             | 275.0e9   |\n| FRT   | 0.28   | Federal Realty Inv. Trust     | 9.8e9     |\n| GPC   | 0.16   | Genuine Parts Co.             | 25.3e9    |\n| MSEX  | 0.05   | Middlesex Water Co.           | 2.4e9     |\n\n```python\nimport pandas as pd\nfrom riskoptima import RiskOptima\n\nimport warnings\nwarnings.filterwarnings(\n    \"ignore\", \n    category=FutureWarning, \n    message=\".*DataFrame.std with axis=None is deprecated.*\"\n)\n\n# Define your current porfolio with your weights and company names\nasset_data = [\n    {\"Asset\": \"MO\",    \"Weight\": 0.04, \"Label\": \"Altria Group Inc.\",       \"MarketCap\": 110.0e9},\n    {\"Asset\": \"NWN\",   \"Weight\": 0.14, \"Label\": \"Northwest Natural Gas\",   \"MarketCap\": 1.8e9},\n    {\"Asset\": \"BKH\",   \"Weight\": 0.01, \"Label\": \"Black Hills Corp.\",         \"MarketCap\": 4.5e9},\n    {\"Asset\": \"ED\",    \"Weight\": 0.01, \"Label\": \"Con Edison\",                \"MarketCap\": 30.0e9},\n    {\"Asset\": \"PEP\",   \"Weight\": 0.09, \"Label\": \"PepsiCo Inc.\",              \"MarketCap\": 255.0e9},\n    {\"Asset\": \"NFG\",   \"Weight\": 0.16, \"Label\": \"National Fuel Gas\",         \"MarketCap\": 5.6e9},\n    {\"Asset\": \"KO\",    \"Weight\": 0.06, \"Label\": \"Coca-Cola Company\",         \"MarketCap\": 275.0e9},\n    {\"Asset\": \"FRT\",   \"Weight\": 0.28, \"Label\": \"Federal Realty Inv. Trust\", \"MarketCap\": 9.8e9},\n    {\"Asset\": \"GPC\",   \"Weight\": 0.16, \"Label\": \"Genuine Parts Co.\",         \"MarketCap\": 25.3e9},\n    {\"Asset\": \"MSEX\",  \"Weight\": 0.05, \"Label\": \"Middlesex Water Co.\",       \"MarketCap\": 2.4e9}\n]\nasset_table = pd.DataFrame(asset_data)\n\ncapital = 100_000\n\nasset_table['Portfolio'] = asset_table['Weight'] * capital\n\nANALYSIS_START_DATE = RiskOptima.get_previous_year_date(RiskOptima.get_previous_working_day(), 1)\nANALYSIS_END_DATE   = RiskOptima.get_previous_working_day()\nBENCHMARK_INDEX     = 'SPY'\nRISK_FREE_RATE      = 0.05\nNUMBER_OF_WEIGHTS   = 10_000\nNUMBER_OF_MC_RUNS   = 1_000\n```\n\n### Example 1: Creating a Portfolio Area Chart\n\nIf you want to know visually how's your portfolio doing right now\n\n```python\nRiskOptima.create_portfolio_area_chart(\n    asset_table,\n    end_date=ANALYSIS_END_DATE,\n    lookback_days=2,\n    title=\"Portfolio Area Chart\"\n)\n```\n![portfolio_area_chart_20250212_095626](https://github.com/user-attachments/assets/e54899e2-8592-48bb-906b-53bdd774d367)\n\n### Example 2: Efficient Frontier - Monte Carlo Portfolio Optimization\n```python\nRiskOptima.plot_efficient_frontier_monte_carlo(\n    asset_table,\n    start_date=ANALYSIS_START_DATE,\n    end_date=ANALYSIS_END_DATE,\n    risk_free_rate=RISK_FREE_RATE,\n    num_portfolios=NUMBER_OF_WEIGHTS,\n    market_benchmark=BENCHMARK_INDEX,\n    set_ticks=False,\n    x_pos_table=1.15,    # Position for the weight table on the plot\n    y_pos_table=0.52,    # Position for the weight table on the plot\n    title=f'Efficient Frontier - Monte Carlo Simulation {ANALYSIS_START_DATE} to {ANALYSIS_END_DATE}'\n)\n```\n![efficient_frontier_monter_carlo_20250203_205339](https://github.com/user-attachments/assets/f48f9f44-38cd-4d4c-96f2-48e767d7316e)\n\n### Example 3: Portfolio Optimization using Mean Variance and Machine Learning\n```python\nRiskOptima.run_portfolio_optimization_mv_ml(\n    asset_table=asset_table,\n    training_start_date='2022-01-01',\n    training_end_date='2023-11-27',\n    model_type='Linear Regression',    \n    risk_free_rate=RISK_FREE_RATE,\n    num_portfolios=100000,\n    market_benchmark=[BENCHMARK_INDEX],\n    max_volatility=0.25,\n    min_weight=0.03,\n    max_weight=0.2\n)\n```\n![machine_learning_optimization_20250203_210953](https://github.com/user-attachments/assets/0fae24a6-8d1d-45e7-b3d2-16939a1aadf7)\n\n### Example 4: Portfolio Optimization using Probability Analysis\n```python\nRiskOptima.run_portfolio_probability_analysis(\n    asset_table=asset_table,\n    analysis_start_date=ANALYSIS_START_DATE,\n    analysis_end_date=ANALYSIS_END_DATE,\n    benchmark_index=BENCHMARK_INDEX,\n    risk_free_rate=RISK_FREE_RATE,\n    number_of_portfolio_weights=NUMBER_OF_WEIGHTS,\n    trading_days_per_year=RiskOptima.get_trading_days(),\n    number_of_monte_carlo_runs=NUMBER_OF_MC_RUNS\n)\n```\n![probability_distributions_of_final_fund_returns20250205_212501](https://github.com/user-attachments/assets/8ea20d1f-e74f-4559-b66f-41ee657dd63b)\n\n### Example 5: Macaulay Duration\n\n```python\nfrom riskoptima import RiskOptima\ncf = RiskOptima.bond_cash_flows_v2(4, 1000, 0.06, 2)  # 2 years, semi-annual, hence 4 periods\nmd_2 = RiskOptima.macaulay_duration_v3(cf, 0.05, 2)\nmd_2\n```\n![image](https://github.com/user-attachments/assets/8bf54461-7256-4162-9230-f29aeeef4a10)\n\n### Example 6:  Market Turns with SPY \u0026 VIX Divergence\n\n```python\nANALYSIS_START_DATE = RiskOptima.get_previous_year_date(RiskOptima.get_previous_working_day(), 1)\nANALYSIS_END_DATE   = RiskOptima.get_previous_working_day()\n\ndf_signals, df_exits, returns = RiskOptima.run_index_vol_divergence_signals(start_date=ANALYSIS_START_DATE, \n                                                                            end_date=ANALYSIS_END_DATE)\n```\n![riskoptima_index_vol_divergence_signals_entry_20250316_200414](https://github.com/user-attachments/assets/fe9b0e73-34f2-4175-bad6-80c75b514fe5)\n\n\n## Documentation\n\nFor complete documentation and usage examples, visit the GitHub repository:\n\n[RiskOptima GitHub](https://github.com/JordiCorbilla/RiskOptima)\n\n## Contributing\n\nWe welcome contributions! If you'd like to improve the package or report issues, please visit the GitHub repository.\n\n## License\n\nRiskOptima is licensed under the MIT License.\n\n### Support me\n\n\u003ca href=\"https://www.buymeacoffee.com/jordicorbilla\" target=\"_blank\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/default-orange.png\" alt=\"Buy Me A Coffee\" height=\"41\" width=\"174\"\u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjordicorbilla%2Friskoptima","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjordicorbilla%2Friskoptima","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjordicorbilla%2Friskoptima/lists"}