Asset Portfolio Optimization with CAPM






Optimal asset portfolios

By principles of the Capital Asset Pricing Model (CAPM), the script returns the normalized minimum variance and tangency vectors. These are the optimal portfolio weights for the two efficient portfolios.

How to run the script

Fork or clone
Script is initilized with OptimalPortfolioDiversification(), adding a numpy 2d array with asset returns (Stationary) as est_return_matrix arg and a Risk free return (Often just 0).
Portfolio weights are stored in (self.normalized_min_variance_vector, self.normalized_tangency_vector)
Expected return for the next period and given portfolio is stored in (self.expected_return_of_min_var_portfolio, self.expected_return_of_tangency_portfolio)
Standard deviation (Risk) for the next period and given portfolio is stored in (self.stdOfMinVariancePortfolio, self.stdOfTangencyPortfolio)

Usage examples

Im using the script to monitor daily risk in the danish C20CAP stock index, updated every 15 minutes by scraping data from borsen.dk
Tangency (Largest return given risk) https://plot.ly/~nstaalung/63
Minimum Variance (Lowest risk) https://plot.ly/~nstaalung/65

https://github.com/NicholaiStaalung/Oport.git


Program Structure in Korean


def covMatrix()
"""공분산 matrix 함수"""

class OptimalPortfolioDiversification()
"""CAPM의 원리에 따라 최적의 portfolio weights를 찾음"""
        def __init__(self, est_return_matrix, risk_free_return, lagrange=False):
        ""생성자"""
        try:
                """수익률 리턴 분석 및 할당(관찰 수, 그룹 수)"""
                """평균 수익률 matrices 생성"""

        """벡터 한개 더하기"""

        """특정 해 찾기, 만약 역변환이 불가능하면 라그랑주가 자동적으로 불려짐"""
     
        def varCovar(self):
                """수익률 매트릭스를 토대로 variance-covariance matrix를 구함"""
        def minVarPortfolio(self):
                """var-covar 매트릭스를 토대로 최소 variance 포트폴리오 weight를 계산,
                       역변환이 불가능 할때만 적용가능, 만약 아니라면 라그랑주"""
        def tangentPortfolio(self):
                """var-covar 매트릭스를 토대로 efficient/tangent 포트폴리오 weight를 계산,
                        역변환이 불가능 할때만 적용가능, 만약 아니라면 라그랑주"""
        def lagVarCovar(self):
                """다른 함수를 위해 매트릭스들을 prepping함"""
        def minVarLagrange(self):
                """라그랑주 사용해서 최소 variance portfolio 계산"""
        def tangentPortfolioLagrange(self):
                """라그랑주 사용해서 최소 variance portfolio 계산"""

0 개의 댓글:

댓글 쓰기