{"id":25042480,"url":"https://github.com/s1dewalker/portfolio_analysis","last_synced_at":"2026-04-20T09:02:38.212Z","repository":{"id":259077396,"uuid":"875610407","full_name":"s1dewalker/Portfolio_Analysis","owner":"s1dewalker","description":"MPT in Python | Efficient Frontier modeling, Fama French 3 Factor Analysis, Monte Carlo Simulations and Portfolio Risk Analysis (Sharpe, VaR)","archived":false,"fork":false,"pushed_at":"2025-02-04T03:36:24.000Z","size":5062,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-04T04:27:36.598Z","etag":null,"topics":["efficient-frontier","equity","financial-research","monte-carlo-simulation","numpy","pandas","portfolio-analysis","portfolio-management","portfolio-optimization","python","quantitative-analysis","quantitative-finance","regression","regression-models","risk","risk-analysis","risk-management","risk-models","value-at-risk","volatility"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/s1dewalker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-10-20T12:51:01.000Z","updated_at":"2025-02-04T03:36:28.000Z","dependencies_parsed_at":"2024-12-08T02:22:28.701Z","dependency_job_id":"60d7a4a9-757a-4897-9c02-cc9fb89c9d54","html_url":"https://github.com/s1dewalker/Portfolio_Analysis","commit_stats":null,"previous_names":["s1dewalker/portfolio_analysis"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s1dewalker%2FPortfolio_Analysis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s1dewalker%2FPortfolio_Analysis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s1dewalker%2FPortfolio_Analysis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s1dewalker%2FPortfolio_Analysis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/s1dewalker","download_url":"https://codeload.github.com/s1dewalker/Portfolio_Analysis/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246390871,"owners_count":20769478,"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":["efficient-frontier","equity","financial-research","monte-carlo-simulation","numpy","pandas","portfolio-analysis","portfolio-management","portfolio-optimization","python","quantitative-analysis","quantitative-finance","regression","regression-models","risk","risk-analysis","risk-management","risk-models","value-at-risk","volatility"],"created_at":"2025-02-06T04:46:24.875Z","updated_at":"2026-04-20T09:02:38.193Z","avatar_url":"https://github.com/s1dewalker.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](screenshots/PRA.png)\n\nInspired by many **specialized ETFs** (Exchange-Traded Funds: An investment vehicle that pools a group of securities into a fund. It can be traded like an individual stock on an exchange). Creating a basket of stocks with weights for **specific investment strategies or weighting criteria beyond traditional market-capitalization indexes and treating them all as a unit.**\n\u003cbr/\u003e\n\nA portfolio, if selected properly, is less vulnerable to extreme highs and lows, and provides the benefits of **diversification**.\n\u003cbr/\u003e\n\nThis project offers optimization and modeling tools for investment strategies. Leveraging Efficient Frontier modeling, Monte Carlo simulation, risk metrics (Sharpe ratio, VaR), and Fama French factor models w/ python libraries like pandas, yfinance, and PyPortfolioOpt. \u003cbr/\u003e\n\u003cbr/\u003e\n\n\n### [View Risk Analysis w/ 15 risk metrics in Python](https://github.com/s1dewalker/Portfolio_Analysis/blob/main/py_files/RiskAnalysis.ipynb)\n### [Fama French 3-Factor Modeling (w/ model validation) of NYSE stocks in Python](https://github.com/s1dewalker/Portfolio_Analysis/blob/main/py_files/Multi_Factor_Analysis3.ipynb)\n### [Meet Min Volatility and Max Sharpe Ratio on the EfficientFrontier of NSE stocks](https://github.com/s1dewalker/Portfolio_Analysis/blob/main/py_files/ETFs.ipynb)\n\u003cbr/\u003e \n\n# 1. Data Extraction and Portfolio Construction \u003cbr/\u003e\nUtilized `yfinance` for data retrieval of NSE stocks, obtaining historical price data for a specified date range. \u003cbr/\u003e\n\u003cbr/\u003e\n'df' is the DataFrame that contains daily prices of stocks. \u003cbr/\u003e\n'weights' is the array that contains portfolio weights. \u003cbr/\u003e\n\n```python\nreturns = df.pct_change()\nreturns.dropna(inplace = True)\n```\n\nPortfolio construction w/ stock returns\n```python\nreturns_pf = returns.dot(weights)\n```\n\nPortfolio construction w/ stock values\n```python\npf_AUM = df.dot(weights)\n```\n\n# 2. Risk Analysis - Some Basic Portfolio Risk Metrics \u003cbr/\u003e\n\n## Annualized return \u003cbr/\u003e\n\n#### Q. Why annualize returns? \u003cbr/\u003e\n- To account for compounding effect\n- To compare portfolios with different time periods\n- Average return can be deceiving (example: if portfolio loses all its value in the end the average may be \u003e 0%, but final return is 0%)\n\u003cbr/\u003e\n\nAnnualizing returns:\n```python\ntotal_return = (pf_AUM[-1] - pf_AUM[0]) / pf_AUM[0]\nannualized_return = ((1 + total_return) * * (12 / months)) - 1\n```\n\n## Annualized Volatility \u003cbr/\u003e\n```python\npf_returns = pf_AUM.pct_change()\npf_vol = pf_returns.std()\npf_vol = pf_vol * np.sqrt(250)\n```\n\n#### Q. Why multiply with sqrt(250)\n250: trading days in a year (annual) \u003cbr/\u003e\nsqrt: variance annualized = 250 * variance daily. \u003cbr/\u003e\nTherefore, standard deviation annualized = sqrt(250) * standard deviation daily.\n\u003cbr/\u003e\n\n## Risk-adjusted return | Sharpe Ratio | Efficiency of risk taking \u003cbr/\u003e\n```python\nsharpe_ratio = ((annualized_return - rfr) / pf_vol)\n```\n\n## Portfolio Variance and Volatility \u003cbr/\u003e\n```python\ncov_matrix = (returns.cov())*250 \nport_variance = np.dot(weights.T, np.dot(cov_matrix, weights))\nport_standard_dev = np.sqrt(port_variance)\n```\n\n[View Portfolio Variance derivation](https://github.com/s1dewalker/Portfolio_Analysis/blob/main/Portfolio_variance.pdf) \u003cbr/\u003e\n\n#### Q. Why annualizing covariance?\nso that we calculate the annualized volatility later. \u003cbr/\u003e\n\n#### Q. Why portfolio variance formula instead of standard deviation?\n**Precision**: As stocks are **correlated** to each other, we need to account for **correlations** b/w stocks. This gives more **precision** in measuring volatility. \u003cbr/\u003e\nIt can be forward looking if covariance matrix is estimated or forecasted. \u003cbr/\u003e\n\n#### Q. Benefits of historical method?\nHistorical data method can include rebalancing. \u003cbr/\u003e\n\n## Skewness\nSkewness measures the asymmetry of the distribution of data around its mean. \u003cbr/\u003e\n```python\npf_returns.skew()\n```\nRisk management: Investors should seek positive skew as in the long run, few positive bets should create a positive expectancy \u003cbr/\u003e\n\n## Kurtosis\nKurtosis measures the \"tailedness\" of the data distribution, indicating the presence of outliers. \u003cbr/\u003e\n```python\npf_returns.kurtosis()\n ```\nk\u003e3: FAT (leptokurtic) | high risk-high reward\n\n\u003cbr/\u003e \n\n# 3. Risk Analysis - Value at Risk (VaR) \u003cbr/\u003e\n\n Finding var95 and cvar95:\u003cbr/\u003e\n```python\nvar = np.percentile(returns_pf, 5)\ncvar = returns_pf [returns_pf \u003c= var].mean()\n```\n\u003cimg src=\"screenshots/var2.JPG\" alt=\"Description\" width=\"500\"\u003e\n\n\nMonte Carlo Simulation:\u003cbr/\u003e\n\u003cimg src=\"screenshots/mcsim.JPG\" alt=\"Description\" width=\"500\"\u003e\n\nMonte Carlo VaR:\u003cbr/\u003e\n\u003cimg src=\"screenshots/mcvar.JPG\" alt=\"Description\" width=\"500\"\u003e\n\n\n### [View Complete Risk Analysis](https://github.com/s1dewalker/Portfolio_Analysis/blob/main/py_files/RiskAnalysis.ipynb) w/ 15 Portfolio Risk metrics including 5 VaR metrics\n\u003cbr/\u003e \n\n# 4. Factor Investing\n**The Fama-French 3 factor model** tells us **what drives portfolio returns** and **quantifies their contributions**. \u003cbr/\u003e\n```python\nFamaFrench_model = smf.ols(formula='Portfolio_Excess ~ Market_Excess + SMB + HML', data=FamaFrenchData_final)\n```\nRisk management: \n- Identifies exposure to specific factors (size, value)\n- Optimize portfolio by adjusting exposures\n- Evaluate performance\n\u003cbr/\u003e\n\u003cimg src=\"screenshots/alpha.JPG\" alt=\"Description\" width=\"1000\"\u003e\n\n#### Q. Why not use UFO sightings as a factor?\nbecause there might be a high correlation with their levels but not with their changes so correlation of % change or differences will be close to 0.\n\n\n### [View Multi Factor Analysis of NYSE stocks in Python](https://github.com/s1dewalker/Portfolio_Analysis/blob/main/py_files/Multi_Factor_Analysis3.ipynb)\n\u003cbr/\u003e \n\n# 5. Portfolio Optimization | Efficient Frontier\n\n\u003cimg src=\"screenshots/lagrangemultiplier.JPG\" alt=\"Description\" width=\"600\"\u003e\n\n## Optimal weights \u003cbr/\u003e\n\n\u003cimg src=\"screenshots/op_wts.JPG\" alt=\"Description\" width=\"600\"\u003e\n\n## Efficient Frontier \u003cbr/\u003e\n\n\n\u003cimg src=\"screenshots/eff_front3.JPG\" alt=\"Description\" width=\"600\"\u003e\n\n**The efficient frontier is the set of portfolios that achieve the highest return for a given risk or the lowest risk for a given return, representing optimal diversification.** \u003cbr/\u003e\n[View Portfolio Optimization maths](https://github.com/s1dewalker/Portfolio_Analysis/blob/main/Portfolio_Optimization.pdf) \u003cbr/\u003e\n\n### [View Complete Portfolio Optimization](https://github.com/s1dewalker/Portfolio_Analysis/blob/main/py_files/ETFs.ipynb) of NSE stocks\n\n\u003cbr/\u003e\n\n##### Python libraries: \n##### `pandas, numpy, yfinance, matplotlib, pypfopt` \n\u003cbr/\u003e\n\n\n##### [LinkedIn](https://www.linkedin.com/in/sujay-bhaumik-d12/) | s1dewalker23@gmail.com | [Discussions](https://github.com/s1dewalker/Portfolio_Analysis/discussions/1) | [Research Works](https://github.com/s1dewalker/Research-Works)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs1dewalker%2Fportfolio_analysis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fs1dewalker%2Fportfolio_analysis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs1dewalker%2Fportfolio_analysis/lists"}