{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "4장 자본자산가격결정 모델(CAPM)(2)",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tSpJmHnplQUb"
},
"source": [
"간단한 최적화 예#1"
]
},
{
"cell_type": "code",
"metadata": {
"id": "GGV6_765kZq2"
},
"source": [
"from scipy.optimize import minimize\n",
"import numpy as np"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "93-BwHUZkmqB"
},
"source": [
"def objective1(x):\n",
" return x + 1\n",
"\n",
"def constraint(x):\n",
" return x - 3\n",
"\n",
"x0= [ -1 ]\n",
"b = ( -1, 6 )\n",
"bnd = ( b, )\n",
"con = {'type':'ineq','fun':constraint}\n",
"\n",
"sol= minimize(objective1, x0, method='SLSQP', bounds=bnd, constraints=con)"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "M8XA54OpkpcZ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2f116e0a-b6c8-49e8-ca72-49e4bb951f22"
},
"source": [
"print(sol.x)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"[3.]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "k1VFlcTylXvv"
},
"source": [
"간단한 최적화 예#2"
]
},
{
"cell_type": "code",
"metadata": {
"id": "UcmWU1x1kw3f"
},
"source": [
"def objective2(x):\n",
" x1=x[0]\n",
" x2=x[1]\n",
" x3=x[2]\n",
" x4=x[3]\n",
" return x1*x4*(x1+x2+x3)+x3\n",
"\n",
"def constraint1(x):\n",
" return x[0]*x[1]*x[2]*x[3]-25\n",
"\n",
"def constraint2(x):\n",
" sum_sq = np.sum(np.square(x))\n",
" return sum_sq-40\n",
"\n",
"x0 = [ 1, 5, 5, 1 ]\n",
"b = ( 1, 5 )\n",
"bnds = ( b, b, b, b )\n",
"con1 = { 'type':'ineq','fun':constraint1 }\n",
"con2 = { 'type':'eq','fun':constraint2 }\n",
"cons = [ con1, con2 ]\n",
"\n",
"sol = minimize(objective2, x0, method='SLSQP',bounds=bnds,constraints=cons)"
],
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "_0UHQWpSkzs3",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "24af1dc4-467a-408d-fe8d-e3242c0805cf"
},
"source": [
"print(sol)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
" fun: 17.01401724556073\n",
" jac: array([14.57227039, 1.37940764, 2.37940764, 9.56415081])\n",
" message: 'Optimization terminated successfully.'\n",
" nfev: 30\n",
" nit: 5\n",
" njev: 5\n",
" status: 0\n",
" success: True\n",
" x: array([1. , 4.74299607, 3.82115466, 1.37940764])\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ITP5o91FX0zY"
},
"source": [
"3장 평균-분산 포트폴리오 이론에서 이어지는 내용"
]
},
{
"cell_type": "code",
"metadata": {
"id": "rcN2bjYVbVRH"
},
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from pandas_datareader import data as web\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib as mpl \n",
"\n",
"tickers = ['AAPL', 'F', 'AMZN', 'GE', 'TSLA']\n",
"pxclose = pd.DataFrame()\n",
"\n",
"for t in tickers:\n",
" pxclose[t] = web.DataReader(t, data_source='yahoo',start='01-01-2019', end='31-12-2019')['Adj Close']"
],
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "2t-OQM7x5Nts"
},
"source": [
"ret_daily = pxclose.pct_change()\n",
"ret_annual = ret_daily.mean() * 250\n",
"cov_daily = ret_daily.cov()\n",
"cov_annual = cov_daily * 250"
],
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "mdqSMn40IgDE",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 228
},
"outputId": "a3188b47-5a14-4beb-8619-8a9520f7eef6"
},
"source": [
"ret_daily.head()"
],
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"
| \n", " | AAPL | \n", "F | \n", "AMZN | \n", "GE | \n", "TSLA | \n", "
|---|---|---|---|---|---|
| Date | \n", "\n", " | \n", " | \n", " | \n", " | \n", " |
| 2019-01-02 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
| 2019-01-03 | \n", "-0.099607 | \n", "-0.01519 | \n", "-0.025242 | \n", "0.001242 | \n", "-0.031472 | \n", "
| 2019-01-04 | \n", "0.042689 | \n", "0.03856 | \n", "0.050064 | \n", "0.021092 | \n", "0.057697 | \n", "
| 2019-01-07 | \n", "-0.002226 | \n", "0.02599 | \n", "0.034353 | \n", "0.061968 | \n", "0.054361 | \n", "
| 2019-01-08 | \n", "0.019063 | \n", "0.00965 | \n", "0.016612 | \n", "-0.020595 | \n", "0.001164 | \n", "