Package: pyportmgmt
PyPortfolioMgmtPyPortfolioMgmt is a library that implements concepts ofmulti-factormodel for portoflio management.
It aims to indentify, capture and relate the factors to asset prices, and use this information to
build portfolios.PyPortfolioMgmt is designed by keeping in mind the perspective of aresearcherwho wants to study and
analyse multiple factors that contribute to fluctuations in asset prices.Table of contentsTable of contentsGetting startedAn overview of portfolio managementFactor theoryCapital Asset Pricing ModelMulti-Factor ModelWorkflowGetting startedFor installation,pip install pyportmgmtAn overview of portfolio managementPortfolio Management is the art and science of combining assets with different expected returns and
volatilities, so that one can decide on mathematically optimal allocation which minimises the risk
for a target return - the set of all such optimal portfolio is reffered to as efficient frontier.
Harry Markowitz's 1952 paper is the undeniable classic which explains this.The key insights is,
A portfolio must haveminimised risk(for a given target return)maximised returns(for a given target risk)Factor theory"Factors are to assets, what nutritions are to food."Assets earn risk premiums because they are exposed to underlying factor risks.Takeaways from factor theory,Factors matters, not assets.Assets of bundles of factors.Different investor need different risk factor.Capital Asset Pricing ModelTheCapital Asset Pricing Model (CAPM), the first theory of factor risk, states that
assets that crash when the market loses money are risky and therefore must reward their
holders with high risk premiums. While the CAPM defines bad times as low market returns,
mutifactor models capture multiple definitions of bad times across many factors and states
of nature.Takeaways from CAPM model,Don't hold an individual asset, hold a factor.Each investor has his/her own optimal exposure of factor risk.The average investor holds the market.The factor risk premium has an economic story.Risk is factor exposure.Assets paying off in bad times,have low risk premiums.CAPM predicts that asset risk premiums depend only on the asset's beta and there is
only one factor that matters, the market portfolio.Functions of CAPM supported by the package,Calculating Optimum PortfolioObtaining Max Sharpe RatioPlotting Efficient FrontierPlotting Capital Allocation LinePlotting Security Market LineA quick examplefromdatetimeimportdatetimeasdtfrompyportmgmtimportPortfolioSetfrompyportmgmtimoprtPortfoliofrompyportmgmtimportCapm# start date and end dates of daily returns for portfolioSetstartDate=dt(2018,1,1)endDate=dt(2018,12,31)# creating portfolioSet object for given tickersportSet=PortfolioSet(tickers=["JNJ","PG","V"])# obtain daily prices and calc log returnsportSet.getReturns(startDate,endDate)# creating Capm object for the created PortfolioSet objectc=Capm(portSet)#obtain the optimum portfoliooptPort=c.calcOptPortfolio(riskFreeRate=0.004,nRandPortfolio=1000)c.plotSML(ticker="JNJ")Multi-Factor ModelMulti-factor model recognize that bad times can be defined more broadly than
just bad returns on the market portfolio. Each factor in a multi-factor model
provides its own definition of bad times.Fama-French Three Factor ModelFama-French three factor model is one such multi-factor model. This model
focuses on three factors,Capm market factorSMB (size factor)HML (value factor)A quick examplefromdatetimeimportdatetimeasdtfrompyportmgmtimportPortfolioSetfrompyportmgmtimoprtPortfoliofrompyportmgmtimportFamaFrench# start date and end dates of daily returns for portfolioSetstartDate=dt(2018,1,1)endDate=dt(2018,12,31)# creating portfolioSet object for given tickersportSet=PortfolioSet(tickers=["JNJ","PG","V"])# obtain daily prices and calc log returnsportSet.getReturns(startDate,endDate)# creating FamaFrench objectf=FamaFrench(portSet)f.printThreeFactorDataList()# index value obtained from results of previous functionf.getThreeFactorData(index=11)f.fitModel(ticker="PG")Workflow