{ "cells": [ { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
日期市场指数组合1组合2组合3组合4组合5无风险利率市场溢酬因子市值因子账面市值比因子
0200901091904.861727.011281.301591.432469.131822.580.0000430.02040.00830.0010
1200901121900.351715.861300.271590.852488.001829.900.0000420.00440.0102-0.0021
2200901131863.371667.971273.801551.212407.011774.560.000041-0.0249-0.00190.0074
3200901141928.871750.081329.631607.212463.771817.240.0000410.04270.0029-0.0022
4200901151920.211739.641334.241614.492456.651814.100.0000400.00330.0079-0.0031
....................................
2422201812242527.011289.121634.661869.087276.915275.890.0000880.0056-0.0008-0.0045
2423201812252504.821259.941605.201846.487256.915251.070.000090-0.0088-0.0034-0.0057
2424201812262498.291250.421598.941843.407216.065193.210.000091-0.00300.00480.0030
2425201812272483.091236.361587.711831.417233.595144.720.000091-0.0080-0.00950.0024
2426201812282493.901225.711592.221848.197349.075177.930.0000920.0039-0.0031-0.0009
\n", "

2427 rows × 11 columns

\n", "
" ], "text/plain": [ " 日期 市场指数 组合1 组合2 组合3 组合4 组合5 \\\n", "0 20090109 1904.86 1727.01 1281.30 1591.43 2469.13 1822.58 \n", "1 20090112 1900.35 1715.86 1300.27 1590.85 2488.00 1829.90 \n", "2 20090113 1863.37 1667.97 1273.80 1551.21 2407.01 1774.56 \n", "3 20090114 1928.87 1750.08 1329.63 1607.21 2463.77 1817.24 \n", "4 20090115 1920.21 1739.64 1334.24 1614.49 2456.65 1814.10 \n", "... ... ... ... ... ... ... ... \n", "2422 20181224 2527.01 1289.12 1634.66 1869.08 7276.91 5275.89 \n", "2423 20181225 2504.82 1259.94 1605.20 1846.48 7256.91 5251.07 \n", "2424 20181226 2498.29 1250.42 1598.94 1843.40 7216.06 5193.21 \n", "2425 20181227 2483.09 1236.36 1587.71 1831.41 7233.59 5144.72 \n", "2426 20181228 2493.90 1225.71 1592.22 1848.19 7349.07 5177.93 \n", "\n", " 无风险利率 市场溢酬因子 市值因子 账面市值比因子 \n", "0 0.000043 0.0204 0.0083 0.0010 \n", "1 0.000042 0.0044 0.0102 -0.0021 \n", "2 0.000041 -0.0249 -0.0019 0.0074 \n", "3 0.000041 0.0427 0.0029 -0.0022 \n", "4 0.000040 0.0033 0.0079 -0.0031 \n", "... ... ... ... ... \n", "2422 0.000088 0.0056 -0.0008 -0.0045 \n", "2423 0.000090 -0.0088 -0.0034 -0.0057 \n", "2424 0.000091 -0.0030 0.0048 0.0030 \n", "2425 0.000091 -0.0080 -0.0095 0.0024 \n", "2426 0.000092 0.0039 -0.0031 -0.0009 \n", "\n", "[2427 rows x 11 columns]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.read_csv('3-B_EXAM4.csv', encoding='GB2312')\n", "data" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1727.01, 1715.86, 1667.97, ..., 1250.42, 1236.36, 1225.71])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = data.iloc[:, 2].values\n", "p2 = data.iloc[:, 3].values\n", "p3 = data.iloc[:, 4].values\n", "p4 = data.iloc[:, 5].values\n", "p5 = data.iloc[:, 6].values\n", "rf = data.iloc[:, 7].values\n", "mkt = data.iloc[:, 8].values\n", "p1" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.00647718, -0.02830709, 0.04805418, ..., -0.00758461,\n", " -0.01130792, -0.00865131])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r1 = np.log(p1[1:]) - np.log(p1[:-1])\n", "r2 = np.log(p2[1:]) - np.log(p2[:-1])\n", "r3 = np.log(p3[1:]) - np.log(p3[:-1])\n", "r4 = np.log(p4[1:]) - np.log(p4[:-1])\n", "r5 = np.log(p5[1:]) - np.log(p5[:-1])\n", "r1" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-0.00651918, -0.02834809, 0.04801318, ..., -0.00767561,\n", " -0.01139892, -0.00874331])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rexc1 = r1 - rf[1:]\n", "rexc2 = r2 - rf[1:]\n", "rexc3 = r3 - rf[1:]\n", "rexc4 = r4 - rf[1:]\n", "rexc5 = r5 - rf[1:]\n", "rexc1" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "R = np.concatenate([rexc1[:, None], \n", " rexc2[:, None],\n", " rexc3[:, None], \n", " rexc4[:, None],\n", " rexc5[:, None]], \n", " axis=1)\n", "type(R)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Cov_Sample = np.mat(np.cov(R, rowvar=False))\n", "Cov_Sample ##方法1:样本方差-协方差矩阵\n", "type(np.cov(R, rowvar=False))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[ 1. , 0.0044],\n", " [ 1. , -0.0249],\n", " [ 1. , 0.0427],\n", " ...,\n", " [ 1. , -0.003 ],\n", " [ 1. , -0.008 ],\n", " [ 1. , 0.0039]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = np.mat(np.concatenate([np.ones((len(mkt)-1, 1)), mkt[1:, None]], axis=1))\n", "X #这里就是的X就是常数项和市场超额收益率的系数" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[9.02498281e-05, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00],\n", " [0.00000000e+00, 6.25901678e-05, 0.00000000e+00, 0.00000000e+00,\n", " 0.00000000e+00],\n", " [0.00000000e+00, 0.00000000e+00, 4.46072446e-05, 0.00000000e+00,\n", " 0.00000000e+00],\n", " [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 7.58804669e-05,\n", " 0.00000000e+00],\n", " [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,\n", " 1.13345790e-04]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Y = np.mat(R)\n", "AB_hat = (X.T*X).I*(X.T*Y)\n", "ALPHA = AB_hat[0]#这里取的是AB_hat的第一行,总之就是常数项的系数\n", "BETA = AB_hat[1]#这里取的是AB_hat的第二行,总之就是市场超额收益率的系数\n", "RESD = Y - X*AB_hat\n", "covfactor = np.cov(mkt[1:])\n", "covresidual = np.diag(np.diag(np.cov(RESD, rowvar=False)))\n", "Cov_Factor = BETA.T*covfactor*BETA + covresidual\n", "Cov_Factor##方法3:因子模型估计法\n", "covresidual" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[0.00036161, 0.00030257, 0.00026619, 0.00022084, 0.00020533],\n", " [0.00030257, 0.00036965, 0.00028306, 0.00024205, 0.00023079],\n", " [0.00026619, 0.00028306, 0.00029853, 0.0002197 , 0.00020996],\n", " [0.00022084, 0.00024205, 0.0002197 , 0.00026741, 0.00020887],\n", " [0.00020533, 0.00023079, 0.00020996, 0.00020887, 0.0002925 ]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = 0.5\n", "Cov_Shrink = c*Cov_Sample + (1-c)*Cov_Factor\n", "##这里要注意c是加反的\n", "Cov_Shrink" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0.32532288]\n", " [-0.21248216]\n", " [ 0.38898507]\n", " [ 0.23881412]\n", " [ 0.25936009]]\n", "\n", "[[ 0.39904587]\n", " [-0.32861957]\n", " [ 0.38202395]\n", " [ 0.23869463]\n", " [ 0.30885513]]\n" ] }, { "data": { "text/plain": [ "matrix([[7.49300704e-05],\n", " [1.00000000e+00]])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "uhat = np.mean(R, axis=0)\n", "A = np.mat(np.concatenate([uhat[:, None], np.ones((len(uhat), 1))], axis=1)).T\n", "up = np.mean(uhat)\n", "b = np.mat(np.array([up, 1])[:, None])\n", "\n", "omega_Shrink = Cov_Shrink.I*A.T*(A*Cov_Shrink.I*A.T).I*b\n", "print(omega_Shrink)\n", "omega_Sample = Cov_Sample.I*A.T*(A*Cov_Sample.I*A.T).I*b\n", "print()\n", "print(omega_Sample)\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "base" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 1 }