{"id":27091493,"url":"https://github.com/tzabcoder/pyfinrisk","last_synced_at":"2025-07-13T10:37:52.214Z","repository":{"id":278956559,"uuid":"937279846","full_name":"tzabcoder/pyFinRisk","owner":"tzabcoder","description":"Python financial risk management framework.","archived":false,"fork":false,"pushed_at":"2025-03-26T11:12:25.000Z","size":348,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T12:25:07.043Z","etag":null,"topics":["finance","finance-management","python3","risk-analysis","risk-management","valueatrisk"],"latest_commit_sha":null,"homepage":"","language":"Python","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/tzabcoder.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":"2025-02-22T18:59:02.000Z","updated_at":"2025-03-18T23:39:31.000Z","dependencies_parsed_at":"2025-02-22T19:34:52.533Z","dependency_job_id":"d17b1d43-50f9-4833-9056-03dbe5d904a5","html_url":"https://github.com/tzabcoder/pyFinRisk","commit_stats":null,"previous_names":["tzabcoder/pyrisk"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tzabcoder%2FpyFinRisk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tzabcoder%2FpyFinRisk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tzabcoder%2FpyFinRisk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tzabcoder%2FpyFinRisk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tzabcoder","download_url":"https://codeload.github.com/tzabcoder/pyFinRisk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247451581,"owners_count":20940946,"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":["finance","finance-management","python3","risk-analysis","risk-management","valueatrisk"],"created_at":"2025-04-06T07:38:34.822Z","updated_at":"2025-04-06T07:38:35.355Z","avatar_url":"https://github.com/tzabcoder.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pyFinRisk\n\npyFinRisk (Python Financial Risk) is a Python package for quantifying and measuring financial risk assumed by a portfolio of assets. The risk tools focus heavily on Value at Risk (VaR).\n\n**Overview**\n\n---\n\nValue at Risk (VaR) is a method of assessing financial risk using standard statistical techniques summarizing the worst-loss over a target horizon that will not be exceeded with a given confidence interval.\n\nEx: Take the 99% confidence interval. VaR is the cutoff such that the probability of experiencing a greater loss than X is less than 1%.\n\n* **Portfolio VaR** - Portfolio VaR measures the total value at risk of a portfolio of components (risk factors).\n\n$$\n\\sigma_p^2 = \\begin{bmatrix} w_1 \u0026 w_2 \u0026 ... \u0026 w_n \\end{bmatrix}\n\\begin{bmatrix} \\sigma_1^2 \u0026 \\sigma_{12} \u0026 ... \u0026 \\sigma_{1N} \\\\\n... \u0026 ... \u0026 ... \u0026 ... \\\\\n\\sigma_{N1} \u0026 \\sigma_{N2} \u0026 ... \u0026 \\sigma_N^2 \\end{bmatrix}\n\\begin{bmatrix} w_1 \\\\ w_2 \\\\ ... \\\\ w_n \\end{bmatrix}\n$$\n\n$$\nσ_p^2 = w'Σw\n$$\n\nwhere, Σ is the covariance matrix and w is a vector of weights.\n\n$$\nPortfolio\\hspace{0.1cm}VaR = ασ_pw = α\\sqrt{w'Σw}\n$$\n\n* **Conditional VaR** - Value at Risk measures the estimated average loss in extreme scenarios beyond the VaR limit.\n\n$$\nCVaR_α=E[X|X\u003c=VaR_α]\n$$\n\n* **Marginal VaR** - *Change* in Portfolio VaR resulting from taking additional (marginal) exposure to a given risk factor. Partial (linear) derivative with respect to the component's position.\n\n$$\nΔVaR_i=\\frac{δVaR}{δw_i}=α(β_i*σ_p)=\\frac{VaR}{W}*β_i\n$$\n\nwhere $β_i$ is the component beta, and $W$ is the complete portfolio value (marked-to-market).\n\n* **Incremental VaR** - *Change* in Portfolio VaR resulting from the modification of a portfolio component.\n\n$$\nIVaR_i = ΔVaR_i*a\n$$\n\nwhere a is a change, either positive or negative, in the component. It must be noted that this is only an approximation.\n\n* **Component VaR** - *Change* in Portfolio VaR resulting from the removal of a risk factor.\n\n$$\nCVaR_i=(\\Delta VaR_i)w_iW=\\frac{VaR_i*{\\beta_i}}{W}w_iW=VaR_i*{\\beta_i}w_i\n$$\n\nwhere $w_i$ is the weight of the risk factor and $β_i$ is the component beta. It must be noted that this is only an approximation and works well with larger portfolios.\n\n**Features**\n\n---\n\nFor equity-based portfolios:\n\n* Beta\n* Individual (positional) VaR\n* Portfolio VaR\n* Conditional VaR\n* Marginal VaR\n* Incremental VaR\n* Component VaR\n* First-Order Statistical Measures\n* Metric Plotting\n\n**Quick Start**\n\n---\n\nFor a comprehensive demonstration of usage, view the .py files in the examples folder. For test calculations, see the docs folder for the spreadsheet.\n\n```python\n# Global Imports\nimport yfinance as yf\n\n# Local Imports\nfrom RiskEngine.StockRiskEngine import StockRiskEngine\n\n# Create the portfolio details (each ticker index corresponds with\n# the shares index)\ntickers = [\n    'AAPL','NVDA','GOOG','UNH','LLY',\n    'GAP','HD','COST','KO','AMGN',\n    'VZ','BMY','META','DIS','JPM',\n    'MA','NKE','PG','WMT','PM'\n]\n\nshares = [\n    40, 40, 40, 40, 40,\n    30, 30, 30, 30, 30,\n    20, 20, 20, 20, 20,\n    10, 10, 10, 10, 10\n]\n\nprices = []\nfor ticker in tickers:\n    # Download the past 1 years worth of data\n    prices.append(yf.download(ticker, period='1y', interval='1d', auto_adjust=False)['Close'][ticker].to_list())\n\nportfolio_details = {\n    'Symbols' : tickers,\n    'Shares' : shares,\n    'Prices' : prices\n}\n\n# Download the market prices\nmarket_prices = yf.download('SPY', period='1y', interval='1d', auto_adjust=False)['Close'][ticker].to_list()\n\n# Create the risk engine\nriskEngine = StockRiskEngine(portfolio_details, market_prices)\n\n# Display and plot the portfolio statistics\nriskEngine.DisplayPortfolioStatistics(plot=True)\n\n# Calculate the Individual VaR for a component\nindividual_var = riskEngine.IndividualVAR(symbol='JPM', confidence_interval=0.95)\n\n# Calculate the Portfolio VaR\nportfolio_var_pct = riskEngine.PortfolioVAR(confidence_interval=0.99) # Returns %\nportfolio_var_dollar = riskEngine.PortfolioVAR(confidence_interval=0.99, dollar_based=True) # Returns $\n\n# Calculate the Marginal VaR\nmarginal_var = riskEngine.MarginalLocalVAR(symbol='AAPL')\n\n# Calculate the Incremental VaR\nincremental_var = riskEngine.IncrementalLocalVAR(symbol='COST', weight_change=0.05)\n\n# Calculate the Component VaR\ncomponent_var = riskEngine.ComponentLocalVAR(symbol='LLY')\n```\n\n**Testing**\n\n---\n\nFor running the unit tests, execute the following command from the top-level directory:\n\n`python -m test.test`\n\nFor running the example, execute the following command from the top-level directory:\n\n`python -m examples.stock_risk_ex`\n\n**Contributing**\n\n---\n\nIf you would like to contribute to this project or notice any issues, please [raise an issue](https://github.com/tzabcoder/pyFinRisk/issues) or [create a pull request](https://github.com/tzabcoder/pyFinRisk/pulls).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftzabcoder%2Fpyfinrisk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftzabcoder%2Fpyfinrisk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftzabcoder%2Fpyfinrisk/lists"}