{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f52748d7", "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'defs'", "output_type": "error", "traceback": [ "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[1;31mModuleNotFoundError\u001B[0m Traceback (most recent call last)", "Cell \u001B[1;32mIn[1], line 5\u001B[0m\n\u001B[0;32m 3\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mstatsmodels\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mapi\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m \u001B[38;5;21;01msm\u001B[39;00m\n\u001B[0;32m 4\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mscipy\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mstats\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m f\n\u001B[1;32m----> 5\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mdefs\u001B[39;00m\n\u001B[0;32m 6\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mlinearmodels\u001B[39;00m\n", "\u001B[1;31mModuleNotFoundError\u001B[0m: No module named 'defs'" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "import statsmodels.api as sm\n", "from scipy.stats import f\n", "import defs\n", "import linearmodels" ] }, { "cell_type": "markdown", "source": [], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 1, "id": "56199258", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'pd' is not defined", "output_type": "error", "traceback": [ "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[1;31mNameError\u001B[0m Traceback (most recent call last)", "Cell \u001B[1;32mIn[1], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m data_factors \u001B[38;5;241m=\u001B[39m \u001B[43mpd\u001B[49m\u001B[38;5;241m.\u001B[39mread_csv(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mE:\u001B[39m\u001B[38;5;124m\\\u001B[39m\u001B[38;5;124m大三下study\u001B[39m\u001B[38;5;124m\\\u001B[39m\u001B[38;5;124m蒋志强\u001B[39m\u001B[38;5;124m\\\u001B[39m\u001B[38;5;124m実験3\u001B[39m\u001B[38;5;124m\\\u001B[39m\u001B[38;5;124m学习通资料\u001B[39m\u001B[38;5;124m\\\u001B[39m\u001B[38;5;124mData_FFFactors.csv\u001B[39m\u001B[38;5;124m'\u001B[39m, encoding\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mGB2312\u001B[39m\u001B[38;5;124m'\u001B[39m, usecols\u001B[38;5;241m=\u001B[39m[\u001B[38;5;241m2\u001B[39m, \u001B[38;5;241m6\u001B[39m, \u001B[38;5;241m7\u001B[39m, \u001B[38;5;241m8\u001B[39m])\u001B[38;5;66;03m##全部是总市值加权\u001B[39;00m\n\u001B[0;32m 2\u001B[0m data_factors\u001B[38;5;241m.\u001B[39mcolumns \u001B[38;5;241m=\u001B[39m [\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mdate\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mmkt\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124msmb\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mhml\u001B[39m\u001B[38;5;124m'\u001B[39m]\n\u001B[0;32m 3\u001B[0m data_factors[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mdate\u001B[39m\u001B[38;5;124m'\u001B[39m]\u001B[38;5;241m=\u001B[39mpd\u001B[38;5;241m.\u001B[39mto_datetime(data_factors[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mdate\u001B[39m\u001B[38;5;124m'\u001B[39m])\n", "\u001B[1;31mNameError\u001B[0m: name 'pd' is not defined" ] } ], "source": [] }, { "cell_type": "code", "execution_count": 5, "id": "c27c9ae7", "metadata": {}, "outputs": [ { "data": { "text/plain": " code date return\n0 32 2010-01-08 -0.0137\n1 32 2010-01-15 -0.0246\n2 32 2010-01-22 -0.0682\n3 32 2010-01-29 -0.0489\n4 32 2010-02-05 -0.0313\n... ... ... ...\n4650 41 2022-12-02 -0.0073\n4651 41 2022-12-09 -0.0291\n4652 41 2022-12-16 -0.0112\n4653 41 2022-12-23 -0.0402\n4654 41 2022-12-30 0.0526\n\n[4655 rows x 3 columns]", "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
codedatereturn
0322010-01-08-0.0137
1322010-01-15-0.0246
2322010-01-22-0.0682
3322010-01-29-0.0489
4322010-02-05-0.0313
............
4650412022-12-02-0.0073
4651412022-12-09-0.0291
4652412022-12-16-0.0112
4653412022-12-23-0.0402
4654412022-12-300.0526
\n

4655 rows × 3 columns

\n
" }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "index=pd.read_csv('index.csv',encoding='utf-8',usecols=[0,1,3])\n", "index.columns=['code','date','return']\n", "index['date']=pd.to_datetime(index['date'])\n", "index" ] }, { "cell_type": "code", "execution_count": 7, "id": "d7674a90", "metadata": {}, "outputs": [ { "data": { "text/plain": "array([32, 33, 34, 35, 38, 40, 41], dtype=int64)" }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "indexcode=np.unique(index['code'])\n", "indexcode" ] }, { "cell_type": "code", "execution_count": 8, "id": "63a027b5", "metadata": {}, "outputs": [ { "data": { "text/plain": " code date return32\n0 32 2010-01-08 -0.0137\n1 32 2010-01-15 -0.0246\n2 32 2010-01-22 -0.0682\n3 32 2010-01-29 -0.0489\n4 32 2010-02-05 -0.0313\n.. ... ... ...\n660 32 2022-12-02 -0.0100\n661 32 2022-12-09 -0.0068\n662 32 2022-12-16 -0.0216\n663 32 2022-12-23 -0.0488\n664 32 2022-12-30 0.0023\n\n[665 rows x 3 columns]", "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
codedatereturn32
0322010-01-08-0.0137
1322010-01-15-0.0246
2322010-01-22-0.0682
3322010-01-29-0.0489
4322010-02-05-0.0313
............
660322022-12-02-0.0100
661322022-12-09-0.0068
662322022-12-16-0.0216
663322022-12-23-0.0488
664322022-12-300.0023
\n

665 rows × 3 columns

\n
" }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "index32=index[index['code']==indexcode[0]]\n", "index32.columns=['code','date','return32']\n", "index32" ] }, { "cell_type": "code", "execution_count": 36, "id": "2d59f930", "metadata": {}, "outputs": [ { "data": { "text/plain": " code date return34\n1330 34 2010-01-08 -0.0089\n1331 34 2010-01-15 0.0385\n1332 34 2010-01-22 -0.0274\n1333 34 2010-01-29 -0.0472\n1334 34 2010-02-05 -0.0242\n... ... ... ...\n1990 34 2022-12-02 0.0083\n1991 34 2022-12-09 0.0050\n1992 34 2022-12-16 -0.0244\n1993 34 2022-12-23 -0.0516\n1994 34 2022-12-30 0.0226\n\n[665 rows x 3 columns]", "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
codedatereturn34
1330342010-01-08-0.0089
1331342010-01-150.0385
1332342010-01-22-0.0274
1333342010-01-29-0.0472
1334342010-02-05-0.0242
............
1990342022-12-020.0083
1991342022-12-090.0050
1992342022-12-16-0.0244
1993342022-12-23-0.0516
1994342022-12-300.0226
\n

665 rows × 3 columns

\n
" }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "index33=index[index['code']==indexcode[1]]\n", "index34=index[index['code']==indexcode[2]]\n", "index35=index[index['code']==indexcode[3]]\n", "index38=index[index['code']==indexcode[4]]\n", "index40=index[index['code']==indexcode[5]]\n", "index41=index[index['code']==indexcode[6]]\n", "index33.columns=['code','date','return33']\n", "index34.columns=['code','date','return34']\n", "index35.columns=['code','date','return35']\n", "index38.columns=['code','date','return38']\n", "index40.columns=['code','date','return40']\n", "index41.columns=['code','date','return41']\n", "index34" ] }, { "cell_type": "code", "execution_count": 37, "id": "04d0190e", "metadata": {}, "outputs": [ { "data": { "text/plain": " 日期_Date 周无风险收益率_WkRFRet\n0 2010/01/01 0.000352\n1 2010/01/08 0.000352\n2 2010/01/15 0.000356\n3 2010/01/22 0.000361\n4 2010/01/29 0.000365\n.. ... ...\n674 2022/12/02 0.000422\n675 2022/12/09 0.000429\n676 2022/12/16 0.000443\n677 2022/12/23 0.000457\n678 2022/12/30 0.000464\n\n[679 rows x 2 columns]", "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
日期_Date周无风险收益率_WkRFRet
02010/01/010.000352
12010/01/080.000352
22010/01/150.000356
32010/01/220.000361
42010/01/290.000365
.........
6742022/12/020.000422
6752022/12/090.000429
6762022/12/160.000443
6772022/12/230.000457
6782022/12/300.000464
\n

679 rows × 2 columns

\n
" }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rf=pd.read_csv('rfreturn.csv',encoding='utf-8',usecols=[0,2])\n", "rf" ] }, { "cell_type": "code", "execution_count": 38, "id": "d6cdf89f", "metadata": {}, "outputs": [ { "data": { "text/plain": " date rfreturn\n0 2010-01-01 0.000352\n1 2010-01-08 0.000352\n2 2010-01-15 0.000356\n3 2010-01-22 0.000361\n4 2010-01-29 0.000365\n.. ... ...\n674 2022-12-02 0.000422\n675 2022-12-09 0.000429\n676 2022-12-16 0.000443\n677 2022-12-23 0.000457\n678 2022-12-30 0.000464\n\n[679 rows x 2 columns]", "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
daterfreturn
02010-01-010.000352
12010-01-080.000352
22010-01-150.000356
32010-01-220.000361
42010-01-290.000365
.........
6742022-12-020.000422
6752022-12-090.000429
6762022-12-160.000443
6772022-12-230.000457
6782022-12-300.000464
\n

679 rows × 2 columns

\n
" }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rf.columns=['date','rfreturn']\n", "rf['date']=pd.to_datetime(rf['date'])\n", "rf" ] }, { "cell_type": "code", "execution_count": 39, "id": "eaeea2a4", "metadata": {}, "outputs": [ { "data": { "text/plain": " date mkt smb hml rfreturn\n0 2010-01-08 -0.0256 0.0270 -0.0021 0.000352\n1 2010-01-15 0.0082 0.0362 -0.0087 0.000356\n2 2010-01-22 -0.0302 0.0068 -0.0137 0.000361\n3 2010-01-29 -0.0449 0.0040 -0.0178 0.000365\n4 2010-02-05 -0.0173 0.0193 0.0024 0.000367\n.. ... ... ... ... ...\n626 2022-12-02 0.0174 0.0125 -0.0153 0.000422\n627 2022-12-09 0.0178 -0.0163 -0.0080 0.000429\n628 2022-12-16 -0.0107 -0.0030 -0.0019 0.000443\n629 2022-12-23 -0.0364 -0.0118 -0.0014 0.000457\n630 2022-12-30 0.0125 0.0051 -0.0066 0.000464\n\n[631 rows x 5 columns]", "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
datemktsmbhmlrfreturn
02010-01-08-0.02560.0270-0.00210.000352
12010-01-150.00820.0362-0.00870.000356
22010-01-22-0.03020.0068-0.01370.000361
32010-01-29-0.04490.0040-0.01780.000365
42010-02-05-0.01730.01930.00240.000367
..................
6262022-12-020.01740.0125-0.01530.000422
6272022-12-090.0178-0.0163-0.00800.000429
6282022-12-16-0.0107-0.0030-0.00190.000443
6292022-12-23-0.0364-0.0118-0.00140.000457
6302022-12-300.01250.0051-0.00660.000464
\n

631 rows × 5 columns

\n
" }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_factors = pd.read_csv('FFF3factors.csv', encoding='utf-8', usecols=[2, 6, 7, 8])##全部是总市值加权\n", "data_factors.columns = ['date', 'mkt', 'smb', 'hml']\n", "data_factors['date']=pd.to_datetime(data_factors['date'])\n", "data_factors\n", "data_matrix=pd.merge(left=data_factors,right=rf,on='date',how='inner')\n", "data_matrix" ] }, { "cell_type": "code", "execution_count": 40, "id": "877f79a5", "metadata": {}, "outputs": [ { "data": { "text/plain": " date mkt smb hml rfreturn return32\n0 2010-01-08 -0.0256 0.0270 -0.0021 0.000352 -0.0137\n1 2010-01-15 0.0082 0.0362 -0.0087 0.000356 -0.0246\n2 2010-01-22 -0.0302 0.0068 -0.0137 0.000361 -0.0682\n3 2010-01-29 -0.0449 0.0040 -0.0178 0.000365 -0.0489\n4 2010-02-05 -0.0173 0.0193 0.0024 0.000367 -0.0313\n.. ... ... ... ... ... ...\n626 2022-12-02 0.0174 0.0125 -0.0153 0.000422 -0.0100\n627 2022-12-09 0.0178 -0.0163 -0.0080 0.000429 -0.0068\n628 2022-12-16 -0.0107 -0.0030 -0.0019 0.000443 -0.0216\n629 2022-12-23 -0.0364 -0.0118 -0.0014 0.000457 -0.0488\n630 2022-12-30 0.0125 0.0051 -0.0066 0.000464 0.0023\n\n[631 rows x 6 columns]", "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
datemktsmbhmlrfreturnreturn32
02010-01-08-0.02560.0270-0.00210.000352-0.0137
12010-01-150.00820.0362-0.00870.000356-0.0246
22010-01-22-0.03020.0068-0.01370.000361-0.0682
32010-01-29-0.04490.0040-0.01780.000365-0.0489
42010-02-05-0.01730.01930.00240.000367-0.0313
.....................
6262022-12-020.01740.0125-0.01530.000422-0.0100
6272022-12-090.0178-0.0163-0.00800.000429-0.0068
6282022-12-16-0.0107-0.0030-0.00190.000443-0.0216
6292022-12-23-0.0364-0.0118-0.00140.000457-0.0488
6302022-12-300.01250.0051-0.00660.0004640.0023
\n

631 rows × 6 columns

\n
" }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_matrix=pd.merge(left=data_matrix,right=index32[['date','return32']],on='date',how='inner')\n", "data_matrix" ] }, { "cell_type": "code", "execution_count": 41, "id": "e2fcd7bd", "metadata": {}, "outputs": [ { "data": { "text/plain": " date mkt smb hml rfreturn return32 return33 \\\n0 2010-01-08 -0.0256 0.0270 -0.0021 0.000352 -0.0137 -0.0190 \n1 2010-01-15 0.0082 0.0362 -0.0087 0.000356 -0.0246 0.0016 \n2 2010-01-22 -0.0302 0.0068 -0.0137 0.000361 -0.0682 -0.0672 \n3 2010-01-29 -0.0449 0.0040 -0.0178 0.000365 -0.0489 -0.0556 \n4 2010-02-05 -0.0173 0.0193 0.0024 0.000367 -0.0313 -0.0318 \n.. ... ... ... ... ... ... ... \n626 2022-12-02 0.0174 0.0125 -0.0153 0.000422 -0.0100 0.0108 \n627 2022-12-09 0.0178 -0.0163 -0.0080 0.000429 -0.0068 0.0376 \n628 2022-12-16 -0.0107 -0.0030 -0.0019 0.000443 -0.0216 -0.0305 \n629 2022-12-23 -0.0364 -0.0118 -0.0014 0.000457 -0.0488 -0.0441 \n630 2022-12-30 0.0125 0.0051 -0.0066 0.000464 0.0023 0.0121 \n\n return34 return35 return38 return40 return41 \n0 -0.0089 -0.0317 -0.0378 -0.0002 -0.0152 \n1 0.0385 0.0548 -0.0329 0.0925 0.0205 \n2 -0.0274 -0.0227 -0.0003 -0.0636 -0.0218 \n3 -0.0472 -0.0441 -0.0696 -0.0230 -0.0436 \n4 -0.0242 -0.0154 -0.0003 -0.0700 -0.0054 \n.. ... ... ... ... ... \n626 0.0083 0.0488 0.0144 0.0510 -0.0073 \n627 0.0050 0.0284 0.0394 -0.0145 -0.0291 \n628 -0.0244 -0.0067 -0.0087 -0.0220 -0.0112 \n629 -0.0516 -0.0290 -0.0253 -0.0223 -0.0402 \n630 0.0226 0.0004 0.0177 0.0148 0.0526 \n\n[631 rows x 12 columns]", "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 \n \n \n \n \n \n \n \n \n \n \n \n
datemktsmbhmlrfreturnreturn32return33return34return35return38return40return41
02010-01-08-0.02560.0270-0.00210.000352-0.0137-0.0190-0.0089-0.0317-0.0378-0.0002-0.0152
12010-01-150.00820.0362-0.00870.000356-0.02460.00160.03850.0548-0.03290.09250.0205
22010-01-22-0.03020.0068-0.01370.000361-0.0682-0.0672-0.0274-0.0227-0.0003-0.0636-0.0218
32010-01-29-0.04490.0040-0.01780.000365-0.0489-0.0556-0.0472-0.0441-0.0696-0.0230-0.0436
42010-02-05-0.01730.01930.00240.000367-0.0313-0.0318-0.0242-0.0154-0.0003-0.0700-0.0054
.......................................
6262022-12-020.01740.0125-0.01530.000422-0.01000.01080.00830.04880.01440.0510-0.0073
6272022-12-090.0178-0.0163-0.00800.000429-0.00680.03760.00500.02840.0394-0.0145-0.0291
6282022-12-16-0.0107-0.0030-0.00190.000443-0.0216-0.0305-0.0244-0.0067-0.0087-0.0220-0.0112
6292022-12-23-0.0364-0.0118-0.00140.000457-0.0488-0.0441-0.0516-0.0290-0.0253-0.0223-0.0402
6302022-12-300.01250.0051-0.00660.0004640.00230.01210.02260.00040.01770.01480.0526
\n

631 rows × 12 columns

\n
" }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_matrix=pd.merge(left=data_matrix,right=index33[['date','return33']],on='date',how='inner')\n", "data_matrix=pd.merge(left=data_matrix,right=index34[['date','return34']],on='date',how='inner')\n", "data_matrix=pd.merge(left=data_matrix,right=index35[['date','return35']],on='date',how='inner')\n", "data_matrix=pd.merge(left=data_matrix,right=index38[['date','return38']],on='date',how='inner')\n", "data_matrix=pd.merge(left=data_matrix,right=index40[['date','return40']],on='date',how='inner')\n", "data_matrix=pd.merge(left=data_matrix,right=index41[['date','return41']],on='date',how='inner')\n", "data_matrix" ] }, { "cell_type": "code", "execution_count": 42, "id": "ea7534ee", "metadata": {}, "outputs": [ { "data": { "text/plain": "array([[-0.0256, 0.027 , -0.0021],\n [ 0.0082, 0.0362, -0.0087],\n [-0.0302, 0.0068, -0.0137],\n ...,\n [-0.0107, -0.003 , -0.0019],\n [-0.0364, -0.0118, -0.0014],\n [ 0.0125, 0.0051, -0.0066]])" }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = data_matrix.loc[:, ['mkt', 'smb', 'hml']].values\n", "ret_rf = data_matrix.loc[:, ['rfreturn']].values\n", "ret_stock = data_matrix.loc[:, ['return32']].values\n", "x" ] }, { "cell_type": "code", "execution_count": 45, "id": "a54afc33", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: y R-squared: 0.754\n", "Model: OLS Adj. R-squared: 0.752\n", "Method: Least Squares F-statistic: 639.0\n", "Date: Tue, 18 Apr 2023 Prob (F-statistic): 3.52e-190\n", "Time: 17:41:34 Log-Likelihood: 1622.8\n", "No. Observations: 631 AIC: -3238.\n", "Df Residuals: 627 BIC: -3220.\n", "Df Model: 3 \n", "Covariance Type: nonrobust \n", "==============================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "const -0.0006 0.001 -0.817 0.414 -0.002 0.001\n", "x1 1.1281 0.026 43.013 0.000 1.077 1.180\n", "x2 0.0830 0.040 2.063 0.040 0.004 0.162\n", "x3 -0.0648 0.044 -1.466 0.143 -0.152 0.022\n", "==============================================================================\n", "Omnibus: 62.667 Durbin-Watson: 2.078\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 150.548\n", "Skew: 0.537 Prob(JB): 2.04e-33\n", "Kurtosis: 5.138 Cond. No. 66.4\n", "==============================================================================\n", "\n", "Notes:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n" ] } ], "source": [ "import statsmodels.api as sm\n", "X = sm.add_constant(x)##增加一列全为1的列向量,即为截距项。\n", "Y = ret_stock - ret_rf\n", "model = sm.OLS(Y, X)\n", "results = model.fit()\n", "print(results.summary())" ] }, { "cell_type": "code", "execution_count": 46, "id": "180b333a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: y R-squared: 0.754\n", "Model: OLS Adj. R-squared: 0.752\n", "Method: Least Squares F-statistic: 639.0\n", "Date: Tue, 18 Apr 2023 Prob (F-statistic): 3.52e-190\n", "Time: 17:43:29 Log-Likelihood: 1622.8\n", "No. Observations: 631 AIC: -3238.\n", "Df Residuals: 627 BIC: -3220.\n", "Df Model: 3 \n", "Covariance Type: nonrobust \n", "==============================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "const -0.0006 0.001 -0.817 0.414 -0.002 0.001\n", "x1 1.1281 0.026 43.013 0.000 1.077 1.180\n", "x2 0.0830 0.040 2.063 0.040 0.004 0.162\n", "x3 -0.0648 0.044 -1.466 0.143 -0.152 0.022\n", "==============================================================================\n", "Omnibus: 62.667 Durbin-Watson: 2.078\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 150.548\n", "Skew: 0.537 Prob(JB): 2.04e-33\n", "Kurtosis: 5.138 Cond. No. 66.4\n", "==============================================================================\n", "\n", "Notes:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n" ] } ], "source": [ "ret_stock = data_matrix.loc[:, ['return33']].values\n", "Y = ret_stock - ret_rf\n", "model = sm.OLS(Y, X)\n", "results = model.fit()\n", "print(results.summary())" ] }, { "cell_type": "code", "execution_count": 47, "id": "304fbab1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: y R-squared: 0.797\n", "Model: OLS Adj. R-squared: 0.796\n", "Method: Least Squares F-statistic: 820.9\n", "Date: Tue, 18 Apr 2023 Prob (F-statistic): 1.26e-216\n", "Time: 17:48:12 Log-Likelihood: 1720.8\n", "No. Observations: 631 AIC: -3434.\n", "Df Residuals: 627 BIC: -3416.\n", "Df Model: 3 \n", "Covariance Type: nonrobust \n", "==============================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "const -0.0003 0.001 -0.469 0.640 -0.002 0.001\n", "x1 1.1072 0.022 49.311 0.000 1.063 1.151\n", "x2 -0.0461 0.034 -1.339 0.181 -0.114 0.022\n", "x3 0.0345 0.038 0.910 0.363 -0.040 0.109\n", "==============================================================================\n", "Omnibus: 309.540 Durbin-Watson: 1.953\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 5270.758\n", "Skew: 1.750 Prob(JB): 0.00\n", "Kurtosis: 16.719 Cond. No. 66.4\n", "==============================================================================\n", "\n", "Notes:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n" ] } ], "source": [ "ret_stock = data_matrix.loc[:, ['return34']].values\n", "Y = ret_stock - ret_rf\n", "model = sm.OLS(Y, X)\n", "results = model.fit()\n", "print(results.summary())" ] }, { "cell_type": "code", "execution_count": 48, "id": "ee839b5a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: y R-squared: 0.816\n", "Model: OLS Adj. R-squared: 0.815\n", "Method: Least Squares F-statistic: 928.5\n", "Date: Tue, 18 Apr 2023 Prob (F-statistic): 3.79e-230\n", "Time: 17:49:07 Log-Likelihood: 1779.0\n", "No. Observations: 631 AIC: -3550.\n", "Df Residuals: 627 BIC: -3532.\n", "Df Model: 3 \n", "Covariance Type: nonrobust \n", "==============================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "const -0.0004 0.001 -0.626 0.531 -0.001 0.001\n", "x1 1.0230 0.020 49.959 0.000 0.983 1.063\n", "x2 0.0206 0.031 0.654 0.513 -0.041 0.082\n", "x3 -0.4994 0.035 -14.463 0.000 -0.567 -0.432\n", "==============================================================================\n", "Omnibus: 27.815 Durbin-Watson: 1.946\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 57.594\n", "Skew: 0.248 Prob(JB): 3.12e-13\n", "Kurtosis: 4.395 Cond. No. 66.4\n", "==============================================================================\n", "\n", "Notes:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n" ] } ], "source": [ "ret_stock = data_matrix.loc[:, ['return35']].values\n", "Y = ret_stock - ret_rf\n", "model = sm.OLS(Y, X)\n", "results = model.fit()\n", "print(results.summary())" ] }, { "cell_type": "code", "execution_count": 50, "id": "3b95aa15", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: y R-squared: 0.799\n", "Model: OLS Adj. R-squared: 0.798\n", "Method: Least Squares F-statistic: 828.4\n", "Date: Tue, 18 Apr 2023 Prob (F-statistic): 1.31e-217\n", "Time: 17:51:40 Log-Likelihood: 1757.0\n", "No. Observations: 631 AIC: -3506.\n", "Df Residuals: 627 BIC: -3488.\n", "Df Model: 3 \n", "Covariance Type: nonrobust \n", "==============================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "const 6.727e-05 0.001 0.112 0.911 -0.001 0.001\n", "x1 1.0075 0.021 47.518 0.000 0.966 1.049\n", "x2 -0.3149 0.033 -9.685 0.000 -0.379 -0.251\n", "x3 0.3760 0.036 10.518 0.000 0.306 0.446\n", "==============================================================================\n", "Omnibus: 28.040 Durbin-Watson: 2.030\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 77.696\n", "Skew: 0.078 Prob(JB): 1.34e-17\n", "Kurtosis: 4.712 Cond. No. 66.4\n", "==============================================================================\n", "\n", "Notes:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n" ] } ], "source": [ "ret_stock = data_matrix.loc[:, ['return38']].values\n", "Y = ret_stock - ret_rf\n", "model = sm.OLS(Y, X)\n", "results = model.fit()\n", "print(results.summary())" ] }, { "cell_type": "code", "execution_count": 51, "id": "e9fd7f11", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: y R-squared: 0.681\n", "Model: OLS Adj. R-squared: 0.679\n", "Method: Least Squares F-statistic: 446.0\n", "Date: Tue, 18 Apr 2023 Prob (F-statistic): 4.92e-155\n", "Time: 17:52:20 Log-Likelihood: 1477.7\n", "No. Observations: 631 AIC: -2947.\n", "Df Residuals: 627 BIC: -2930.\n", "Df Model: 3 \n", "Covariance Type: nonrobust \n", "==============================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "const -0.0007 0.001 -0.777 0.438 -0.003 0.001\n", "x1 1.0021 0.033 30.359 0.000 0.937 1.067\n", "x2 0.6144 0.051 12.136 0.000 0.515 0.714\n", "x3 -0.4269 0.056 -7.670 0.000 -0.536 -0.318\n", "==============================================================================\n", "Omnibus: 127.986 Durbin-Watson: 2.077\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 580.694\n", "Skew: 0.838 Prob(JB): 8.02e-127\n", "Kurtosis: 7.391 Cond. No. 66.4\n", "==============================================================================\n", "\n", "Notes:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n" ] } ], "source": [ "ret_stock = data_matrix.loc[:, ['return40']].values\n", "Y = ret_stock - ret_rf\n", "model = sm.OLS(Y, X)\n", "results = model.fit()\n", "print(results.summary())" ] }, { "cell_type": "code", "execution_count": 53, "id": "32dc363c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: y R-squared: 0.651\n", "Model: OLS Adj. R-squared: 0.649\n", "Method: Least Squares F-statistic: 390.0\n", "Date: Tue, 18 Apr 2023 Prob (F-statistic): 7.28e-143\n", "Time: 17:57:13 Log-Likelihood: 1650.5\n", "No. Observations: 631 AIC: -3293.\n", "Df Residuals: 627 BIC: -3275.\n", "Df Model: 3 \n", "Covariance Type: nonrobust \n", "==============================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "const -0.0007 0.001 -0.937 0.349 -0.002 0.001\n", "x1 0.7993 0.025 31.844 0.000 0.750 0.849\n", "x2 0.2669 0.038 6.932 0.000 0.191 0.342\n", "x3 0.3009 0.042 7.108 0.000 0.218 0.384\n", "==============================================================================\n", "Omnibus: 89.501 Durbin-Watson: 1.856\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 532.758\n", "Skew: 0.447 Prob(JB): 2.06e-116\n", "Kurtosis: 7.412 Cond. No. 66.4\n", "==============================================================================\n", "\n", "Notes:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n" ] } ], "source": [ "ret_stock = data_matrix.loc[:, ['return41']].values\n", "Y = ret_stock - ret_rf\n", "model = sm.OLS(Y, X)\n", "results = model.fit()\n", "print(results.summary())" ] }, { "cell_type": "code", "execution_count": 54, "id": "cd55852e", "metadata": {}, "outputs": [ { "data": { "text/plain": "array([-1.09385290e-03, -6.05465781e-04, -2.97166333e-04, -3.62089810e-04,\n 6.72710287e-05, -7.23986734e-04, -6.64024956e-04])" }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T = len(Y)##数据项数\n", "N = len(indexcode)\n", "K = 3\n", "y = data_matrix.iloc[:, 5:].values - data_matrix.loc[:, ['rfreturn']].values\n", "x = sm.add_constant(x)\n", "XTX = np.dot(np.transpose(X), X)\n", "yTX = np.dot(np.transpose(y),X)\n", "AB_hat = np.dot(yTX,np.linalg.inv(XTX))\n", "AB_hat=np.transpose(AB_hat)\n", "ALPHA=AB_hat[0]\n", "ALPHA" ] }, { "cell_type": "code", "execution_count": 56, "id": "47482709", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "三因子模型的多资产检验结果\n", " alpha1, alpha2, alpha3, alpha4, alpha5, alpha6, alpha7, GRS, pvalue\n", "-0.0011,-0.0006,-0.0003,-0.0004, 0.0001,-0.0007,-0.0007, 0.6544, 0.7108\n" ] } ], "source": [ "from scipy.stats import f\n", "RESD = y - np.dot(x, AB_hat)##np.dot(x, np.transpose(AB_hat))将每个时间点上,对于每个指数根据估计出来的系数计算出值\n", "COV = np.dot(np.transpose(RESD), RESD)/T\n", "invCOV = np.linalg.inv(COV)\n", "\n", "fs = x[:, [1, 2, 3]]\n", "muhat = np.mean(fs, axis=0).reshape((3, 1))##这里的reshape没有到作用了,因为np.mean(fs, axis=0)本来是1行*3列了\n", "# a = np.array([1, 2, 3, 4, 5, 6]) reshape用例\n", "# b = a.reshape((2, 3))\n", "# print(b)\n", "fs = fs - np.mean(fs, axis=0)##这里是对fs中的每一行进行减的操作\n", "omegahat = np.dot(np.transpose(fs), fs)/T\n", "invOMG = np.linalg.inv(omegahat)\n", "xxx = np.dot(np.dot(np.transpose(muhat), invOMG), muhat)\n", "yyy = np.dot(np.dot(np.transpose(ALPHA), invCOV), ALPHA)\n", "GRS = (T-N-K)/N*(1/(1+xxx))*yyy\n", "pvalue = 1 - f.cdf(GRS[0][0], N, T-N-K)\n", "print('三因子模型的多资产检验结果')\n", "print('{:>7s},{:>7s},{:>7s},{:>7s},{:>7s},{:>7s},{:>7s},{:>7s},{:>7s}'.format('alpha1', 'alpha2', 'alpha3', 'alpha4', 'alpha5', 'alpha6', 'alpha7', 'GRS', 'pvalue'))\n", "print('{:7.4f},{:7.4f},{:7.4f},{:7.4f},{:7.4f},{:7.4f},{:7.4f},{:7.4f},{:7.4f}'.format(ALPHA[0], ALPHA[1], ALPHA[2], ALPHA[3], ALPHA[4], ALPHA[5], ALPHA[6], GRS[0][0], pvalue))" ] }, { "cell_type": "markdown", "source": [], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 41, "id": "5ed828da", "metadata": {}, "outputs": [ { "data": { "text/plain": "[{1}, {2}, {3}, {4}]" }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rows = sum([[var, f'{var}_t'] for var in ['const'] + ['reg_order']],[])\n", "reg_names=None\n", "if reg_names is None:\n", " reg_names = [f'({i+1})' for i in range(4)]\n" ] }, { "cell_type": "code", "execution_count": 96, "id": "b790610c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "name": "python3", "language": "python", "display_name": "Python 3 (ipykernel)" }, "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": 5 }