Skip to content

Instantly share code, notes, and snippets.

@tej87681088
Created August 10, 2022 09:33
Show Gist options
  • Select an option

  • Save tej87681088/f991706a7c08f5c3fa8742a4e6b4df37 to your computer and use it in GitHub Desktop.

Select an option

Save tej87681088/f991706a7c08f5c3fa8742a4e6b4df37 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 277,
"id": "3c57717b",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import tejapi\n",
"import statsmodels.formula.api as smf\n",
"import datetime as dt\n",
"from dateutil.relativedelta import *\n",
"tejapi.ApiConfig.api_key = \"YOUR_KEY\"\n",
"tejapi.ApiConfig.ignoretz = True"
]
},
{
"cell_type": "code",
"execution_count": 144,
"id": "1c7ffcc9",
"metadata": {},
"outputs": [],
"source": [
"df1 = tejapi.get('TWN/AIND', #從TEJ api撈取所需要的資料\n",
" chinese_column_name = True,\n",
" paginate = True,\n",
" opts={'columns':['coid','mdate', 'dflt_d','fail_fg']})"
]
},
{
"cell_type": "code",
"execution_count": 320,
"id": "d05794cc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"None\n",
"1939 2022-08-08\n",
"2953 2022-08-05\n",
"2399 2022-08-03\n",
"1382 2022-07-28\n",
"745 2022-07-26\n",
" ... \n",
"3106 NaT\n",
"3107 NaT\n",
"3108 NaT\n",
"3109 NaT\n",
"3110 NaT\n",
"Name: 危機發生日, Length: 3111, dtype: datetime64[ns]"
]
},
"execution_count": 320,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1['危機發生日'].sort_values(ascending=False)"
]
},
{
"cell_type": "code",
"execution_count": 323,
"id": "2a775364",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"公司簡稱 6103\n",
"目前狀態 2022-01-01 00:00:00\n",
"危機發生日 2022-08-08 00:00:00\n",
"危機事件類別 E\n",
"Name: 1939, dtype: object"
]
},
"execution_count": 323,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1.loc[1939]"
]
},
{
"cell_type": "code",
"execution_count": 170,
"id": "79b717d9",
"metadata": {},
"outputs": [],
"source": [
"df2 = df1[df1['危機事件類別'] != ''] #把有發生違約事件的篩選出來"
]
},
{
"cell_type": "code",
"execution_count": 175,
"id": "43839450",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/gn/7b_mmd0s64q0kkr42rncl0jc0000gn/T/ipykernel_26068/2932515843.py:1: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" df2['年/月'] = df2[['危機發生日']].applymap(lambda x: x.strftime('%Y-%m')).astype('datetime64')\n"
]
}
],
"source": [
"df2['年/月'] = df2[['危機發生日']].applymap(lambda x: x.strftime('%Y-%m')).astype('datetime64') #把日期都換成月初"
]
},
{
"cell_type": "code",
"execution_count": 177,
"id": "c427ae78",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/gn/7b_mmd0s64q0kkr42rncl0jc0000gn/T/ipykernel_26068/1595404685.py:1: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" df2['月'] = df2['年/月'].dt.month\n"
]
}
],
"source": [
"df2['月'] = df2['年/月'].dt.month #取月份出來"
]
},
{
"cell_type": "code",
"execution_count": 193,
"id": "713d7510",
"metadata": {},
"outputs": [],
"source": [
"df2.reset_index(inplace = True)"
]
},
{
"cell_type": "code",
"execution_count": 202,
"id": "703a1e7e",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/gn/7b_mmd0s64q0kkr42rncl0jc0000gn/T/ipykernel_26068/3712162270.py:3: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" df2['年/月'][i] = df2['年/月'][i]+ relativedelta(months = +2)\n",
"/var/folders/gn/7b_mmd0s64q0kkr42rncl0jc0000gn/T/ipykernel_26068/3712162270.py:5: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" df2['年/月'][i] = df2['年/月'][i]+ relativedelta(months = +1)\n"
]
}
],
"source": [
"for i in range(len(df2.index)): #為了之後對上財報資料,先將日期處理好\n",
" if df2['月'][i] == 1 or df2['月'][i] == 4 or df2['月'][i] == 7 or df2['月'][i] == 10:\n",
" df2['年/月'][i] = df2['年/月'][i]+ relativedelta(months = +2)\n",
" if df2['月'][i] == 2 or df2['月'][i] == 5 or df2['月'][i] == 8 or df2['月'][i] == 11:\n",
" df2['年/月'][i] = df2['年/月'][i]+ relativedelta(months = +1)"
]
},
{
"cell_type": "code",
"execution_count": 207,
"id": "b00cbdcd",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/gn/7b_mmd0s64q0kkr42rncl0jc0000gn/T/ipykernel_26068/2072358197.py:1: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" df2['Y'] = 1\n"
]
}
],
"source": [
"df2['Y'] = 1 #把所有危機事件類別設成1"
]
},
{
"cell_type": "code",
"execution_count": 209,
"id": "ccdfcc10",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py:5039: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" return super().rename(\n"
]
}
],
"source": [
"df2.rename(columns= {'公司簡稱':'公司'}, inplace=True)"
]
},
{
"cell_type": "markdown",
"id": "ad7e6266",
"metadata": {},
"source": [
"抓變數會用到的財報資料"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "035e60c4",
"metadata": {},
"outputs": [],
"source": [
"a1 = tejapi.get('TWN/AIFIN', #從TEJ api撈取所需要的資料\n",
" chinese_column_name = True,\n",
" paginate = True,\n",
" mdate = {'gt':'2008-01-01', 'lt':'2011-01-01'},\n",
" acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "fe174209",
"metadata": {},
"outputs": [],
"source": [
"a2 = tejapi.get('TWN/AIFIN', #從TEJ api撈取所需要的資料\n",
" chinese_column_name = True,\n",
" paginate = True,\n",
" mdate = {'gt':'2011-01-01', 'lt':'2014-01-01'},\n",
" acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "0e888b40",
"metadata": {},
"outputs": [],
"source": [
"a3 = tejapi.get('TWN/AIFIN', #從TEJ api撈取所需要的資料\n",
" chinese_column_name = True,\n",
" paginate = True,\n",
" mdate = {'gt':'2014-01-01', 'lt':'2017-01-01'},\n",
" acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "80381492",
"metadata": {},
"outputs": [],
"source": [
"a4 = tejapi.get('TWN/AIFIN', #從TEJ api撈取所需要的資料\n",
" chinese_column_name = True,\n",
" paginate = True,\n",
" mdate = {'gt':'2017-01-01', 'lt':'2020-01-01'},\n",
" acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "ebaee0ff",
"metadata": {},
"outputs": [],
"source": [
"a5 = tejapi.get('TWN/AIFIN', #從TEJ api撈取所需要的資料\n",
" chinese_column_name = True,\n",
" paginate = True,\n",
" mdate = {'gt':'2020-01-01'},\n",
" acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])"
]
},
{
"cell_type": "code",
"execution_count": 213,
"id": "f42506b4",
"metadata": {},
"outputs": [],
"source": [
"acc = pd.concat([a1,a2,a3,a4,a5])"
]
},
{
"cell_type": "code",
"execution_count": 214,
"id": "0c0f0a0b",
"metadata": {},
"outputs": [],
"source": [
"acc1 = acc.pivot_table(values='數值', index=['公司','年/月'], columns='會計科目') #用pivot table將會計科目放到columns上"
]
},
{
"cell_type": "code",
"execution_count": 215,
"id": "732fb69c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>會計科目</th>\n",
" <th>0010</th>\n",
" <th>1000</th>\n",
" <th>2341</th>\n",
" <th>2402</th>\n",
" <th>MV</th>\n",
" <th>R11V</th>\n",
" <th>R505</th>\n",
" <th>R607</th>\n",
" <th>R678</th>\n",
" </tr>\n",
" <tr>\n",
" <th>公司</th>\n",
" <th>年/月</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"5\" valign=\"top\">000016</th>\n",
" <th>2008-06-01</th>\n",
" <td>853115.0</td>\n",
" <td>206761.0</td>\n",
" <td>96354.0</td>\n",
" <td>38116.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>24.24</td>\n",
" <td>0.67</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2008-12-01</th>\n",
" <td>794817.0</td>\n",
" <td>136689.0</td>\n",
" <td>108128.0</td>\n",
" <td>18324.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>17.20</td>\n",
" <td>0.63</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2009-06-01</th>\n",
" <td>789105.0</td>\n",
" <td>127192.0</td>\n",
" <td>113626.0</td>\n",
" <td>30467.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>16.12</td>\n",
" <td>0.50</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2009-12-01</th>\n",
" <td>828313.0</td>\n",
" <td>158918.0</td>\n",
" <td>121108.0</td>\n",
" <td>11891.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>19.19</td>\n",
" <td>0.52</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2010-06-01</th>\n",
" <td>723437.0</td>\n",
" <td>293459.0</td>\n",
" <td>129978.0</td>\n",
" <td>28978.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>40.56</td>\n",
" <td>0.71</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"5\" valign=\"top\">Z5389</th>\n",
" <th>2021-03-01</th>\n",
" <td>671435.0</td>\n",
" <td>506622.0</td>\n",
" <td>-69187.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>3.12</td>\n",
" <td>75.45</td>\n",
" <td>0.38</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-06-01</th>\n",
" <td>666327.0</td>\n",
" <td>504462.0</td>\n",
" <td>-72135.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>2.66</td>\n",
" <td>75.71</td>\n",
" <td>0.29</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-09-01</th>\n",
" <td>741529.0</td>\n",
" <td>477884.0</td>\n",
" <td>-78355.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1.59</td>\n",
" <td>64.45</td>\n",
" <td>0.24</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-12-01</th>\n",
" <td>669558.0</td>\n",
" <td>413791.0</td>\n",
" <td>-86233.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>0.44</td>\n",
" <td>61.80</td>\n",
" <td>0.24</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2022-03-01</th>\n",
" <td>791483.0</td>\n",
" <td>503928.0</td>\n",
" <td>-54445.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>4.35</td>\n",
" <td>63.67</td>\n",
" <td>0.46</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>125489 rows × 9 columns</p>\n",
"</div>"
],
"text/plain": [
"會計科目 0010 1000 2341 2402 MV R11V R505 \\\n",
"公司 年/月 \n",
"000016 2008-06-01 853115.0 206761.0 96354.0 38116.0 NaN NaN 24.24 \n",
" 2008-12-01 794817.0 136689.0 108128.0 18324.0 NaN NaN 17.20 \n",
" 2009-06-01 789105.0 127192.0 113626.0 30467.0 NaN NaN 16.12 \n",
" 2009-12-01 828313.0 158918.0 121108.0 11891.0 NaN NaN 19.19 \n",
" 2010-06-01 723437.0 293459.0 129978.0 28978.0 NaN NaN 40.56 \n",
"... ... ... ... ... .. ... ... \n",
"Z5389 2021-03-01 671435.0 506622.0 -69187.0 NaN NaN 3.12 75.45 \n",
" 2021-06-01 666327.0 504462.0 -72135.0 NaN NaN 2.66 75.71 \n",
" 2021-09-01 741529.0 477884.0 -78355.0 NaN NaN 1.59 64.45 \n",
" 2021-12-01 669558.0 413791.0 -86233.0 NaN NaN 0.44 61.80 \n",
" 2022-03-01 791483.0 503928.0 -54445.0 NaN NaN 4.35 63.67 \n",
"\n",
"會計科目 R607 R678 \n",
"公司 年/月 \n",
"000016 2008-06-01 0.67 NaN \n",
" 2008-12-01 0.63 NaN \n",
" 2009-06-01 0.50 NaN \n",
" 2009-12-01 0.52 NaN \n",
" 2010-06-01 0.71 NaN \n",
"... ... ... \n",
"Z5389 2021-03-01 0.38 NaN \n",
" 2021-06-01 0.29 NaN \n",
" 2021-09-01 0.24 NaN \n",
" 2021-12-01 0.24 NaN \n",
" 2022-03-01 0.46 NaN \n",
"\n",
"[125489 rows x 9 columns]"
]
},
"execution_count": 215,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"acc1"
]
},
{
"cell_type": "code",
"execution_count": 216,
"id": "69bbf484",
"metadata": {},
"outputs": [],
"source": [
"acc1['X1'] = (acc1['R678']/acc1['0010'])*100\n",
"acc1['X2'] = (acc1['2341']/acc1['0010'])*100\n",
"acc1['X3'] = (acc1['2402']/acc1['0010'])*100\n",
"acc1['X4'] = (acc1['MV']/acc1['1000'])*100\n",
"acc1 = acc1.rename(columns = {'R607':'X5', 'R11V':'X6', 'R505':'X7'})\n",
"acc2 = acc1[['X1','X2','X3','X4','X5','X6','X7']]"
]
},
{
"cell_type": "code",
"execution_count": 228,
"id": "0fa739d0",
"metadata": {},
"outputs": [],
"source": [
"acc2.reset_index(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 231,
"id": "1aef424d",
"metadata": {},
"outputs": [],
"source": [
"df3 = pd.merge(acc2, df2[['公司','年/月','Y']], how='outer')"
]
},
{
"cell_type": "code",
"execution_count": 233,
"id": "1ab75cab",
"metadata": {},
"outputs": [],
"source": [
"df3['Y'] = df3['Y'].replace(np.nan, 0) #把沒有發生危機的設為0"
]
},
{
"cell_type": "code",
"execution_count": 237,
"id": "3b0274a4",
"metadata": {},
"outputs": [],
"source": [
"df3 = df3.dropna()"
]
},
{
"cell_type": "code",
"execution_count": 253,
"id": "909ecbf5",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/gn/7b_mmd0s64q0kkr42rncl0jc0000gn/T/ipykernel_26068/227073911.py:1: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" df3['X4'] = df3['X4'].drop([59690,59688]) #把無限大的值去掉\n"
]
}
],
"source": [
"df3['X4'] = df3['X4'].drop([59690,59688]) #把無限大的值去掉"
]
},
{
"cell_type": "markdown",
"id": "a0d5a98c",
"metadata": {},
"source": [
"linear probility model"
]
},
{
"cell_type": "code",
"execution_count": 254,
"id": "b541091f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" OLS Regression Results \n",
"==============================================================================\n",
"Dep. Variable: Y R-squared: 0.002\n",
"Model: OLS Adj. R-squared: 0.002\n",
"Method: Least Squares F-statistic: 19.83\n",
"Date: Wed, 10 Aug 2022 Prob (F-statistic): 9.62e-27\n",
"Time: 10:54:15 Log-Likelihood: 76114.\n",
"No. Observations: 74040 AIC: -1.522e+05\n",
"Df Residuals: 74032 BIC: -1.521e+05\n",
"Df Model: 7 \n",
"Covariance Type: nonrobust \n",
"==============================================================================\n",
" coef std err t P>|t| [0.025 0.975]\n",
"------------------------------------------------------------------------------\n",
"Intercept 0.0031 0.001 2.217 0.027 0.000 0.006\n",
"X1 1.213e-05 1.91e-05 0.634 0.526 -2.54e-05 4.96e-05\n",
"X2 3.098e-06 3.15e-06 0.984 0.325 -3.07e-06 9.27e-06\n",
"X3 -0.0004 5.53e-05 -6.467 0.000 -0.000 -0.000\n",
"X4 1.653e-08 6.83e-08 0.242 0.809 -1.17e-07 1.5e-07\n",
"X5 -0.0033 0.001 -5.486 0.000 -0.004 -0.002\n",
"X6 0.0004 7.26e-05 4.966 0.000 0.000 0.001\n",
"X7 0.0002 2.33e-05 7.115 0.000 0.000 0.000\n",
"==============================================================================\n",
"Omnibus: 121208.189 Durbin-Watson: 2.013\n",
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 51221200.304\n",
"Skew: 11.340 Prob(JB): 0.00\n",
"Kurtosis: 129.842 Cond. No. 2.09e+04\n",
"==============================================================================\n",
"\n",
"Notes:\n",
"[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
"[2] The condition number is large, 2.09e+04. This might indicate that there are\n",
"strong multicollinearity or other numerical problems.\n"
]
}
],
"source": [
"result_ols = smf.ols('Y ~ X1 + X2 + X3 + X4 + X5 + X6 + X7', data=df3).fit()\n",
"print(result_ols.summary())"
]
},
{
"cell_type": "markdown",
"id": "7be640e0",
"metadata": {},
"source": [
"probit model"
]
},
{
"cell_type": "code",
"execution_count": 255,
"id": "26a0e86f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimization terminated successfully.\n",
" Current function value: 0.043805\n",
" Iterations 8\n",
" Probit Regression Results \n",
"==============================================================================\n",
"Dep. Variable: Y No. Observations: 74040\n",
"Model: Probit Df Residuals: 74032\n",
"Method: MLE Df Model: 7\n",
"Date: Wed, 10 Aug 2022 Pseudo R-squ.: 0.01514\n",
"Time: 10:54:18 Log-Likelihood: -3243.3\n",
"converged: True LL-Null: -3293.2\n",
"Covariance Type: nonrobust LLR p-value: 1.229e-18\n",
"==============================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"------------------------------------------------------------------------------\n",
"Intercept -2.6246 0.068 -38.690 0.000 -2.758 -2.492\n",
"X1 0.0004 0.001 0.488 0.625 -0.001 0.002\n",
"X2 0.0001 0.000 0.606 0.545 -0.000 0.001\n",
"X3 -0.0030 0.001 -2.674 0.008 -0.005 -0.001\n",
"X4 3.902e-07 3.78e-06 0.103 0.918 -7.01e-06 7.8e-06\n",
"X5 -0.1777 0.033 -5.315 0.000 -0.243 -0.112\n",
"X6 0.0036 0.002 1.466 0.143 -0.001 0.008\n",
"X7 0.0071 0.001 6.652 0.000 0.005 0.009\n",
"==============================================================================\n"
]
}
],
"source": [
"result_probit = smf.probit('Y ~ X1 + X2 + X3 + X4 + X5 + X6 + X7 ', data=df3).fit()\n",
"print(result_probit.summary())"
]
},
{
"cell_type": "markdown",
"id": "bc2a0a24",
"metadata": {},
"source": [
"logit model"
]
},
{
"cell_type": "code",
"execution_count": 256,
"id": "5840a778",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimization terminated successfully.\n",
" Current function value: 0.043828\n",
" Iterations 9\n",
" Logit Regression Results \n",
"==============================================================================\n",
"Dep. Variable: Y No. Observations: 74040\n",
"Model: Logit Df Residuals: 74032\n",
"Method: MLE Df Model: 7\n",
"Date: Wed, 10 Aug 2022 Pseudo R-squ.: 0.01461\n",
"Time: 10:54:19 Log-Likelihood: -3245.0\n",
"converged: True LL-Null: -3293.2\n",
"Covariance Type: nonrobust LLR p-value: 6.405e-18\n",
"==============================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"------------------------------------------------------------------------------\n",
"Intercept -5.3641 0.183 -29.274 0.000 -5.723 -5.005\n",
"X1 0.0010 0.002 0.399 0.690 -0.004 0.006\n",
"X2 0.0005 0.001 0.774 0.439 -0.001 0.002\n",
"X3 -0.0064 0.002 -3.094 0.002 -0.010 -0.002\n",
"X4 4.392e-07 1.09e-05 0.040 0.968 -2.1e-05 2.18e-05\n",
"X5 -0.4753 0.093 -5.118 0.000 -0.657 -0.293\n",
"X6 0.0092 0.006 1.460 0.144 -0.003 0.021\n",
"X7 0.0181 0.003 6.525 0.000 0.013 0.024\n",
"==============================================================================\n"
]
}
],
"source": [
"result_Logit = smf.logit('Y ~ X1 + X2 + X3 + X4 + X5 + X6 + X7 ', data=df3).fit()\n",
"print(result_Logit.summary())"
]
},
{
"cell_type": "markdown",
"id": "926e3482",
"metadata": {},
"source": [
"台積電"
]
},
{
"cell_type": "code",
"execution_count": 257,
"id": "6e203d86",
"metadata": {},
"outputs": [],
"source": [
"TSMC = df3[df3['公司']=='2330']"
]
},
{
"cell_type": "code",
"execution_count": 258,
"id": "58b0d1c6",
"metadata": {},
"outputs": [],
"source": [
"a = TSMC.iloc[-1:,:] #取得最新一筆資料"
]
},
{
"cell_type": "code",
"execution_count": 286,
"id": "703b8351",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>公司</th>\n",
" <th>年/月</th>\n",
" <th>X1</th>\n",
" <th>X2</th>\n",
" <th>X3</th>\n",
" <th>X4</th>\n",
" <th>X5</th>\n",
" <th>X6</th>\n",
" <th>X7</th>\n",
" <th>Y</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>22741</th>\n",
" <td>2330</td>\n",
" <td>2022-03-01</td>\n",
" <td>22.525489</td>\n",
" <td>51.050002</td>\n",
" <td>5.734811</td>\n",
" <td>926.351819</td>\n",
" <td>0.51</td>\n",
" <td>5.66</td>\n",
" <td>41.86</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 公司 年/月 X1 X2 X3 X4 X5 \\\n",
"22741 2330 2022-03-01 22.525489 51.050002 5.734811 926.351819 0.51 \n",
"\n",
" X6 X7 Y \n",
"22741 5.66 41.86 0.0 "
]
},
"execution_count": 286,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 259,
"id": "9fa5eb34",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"22741 0.008783\n",
"dtype: float64"
]
},
"execution_count": 259,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_ols.predict(a) #linear probility model"
]
},
{
"cell_type": "code",
"execution_count": 260,
"id": "5a43c5c6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"22741 0.008256\n",
"dtype: float64"
]
},
"execution_count": 260,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_probit.predict(a) #probit model"
]
},
{
"cell_type": "code",
"execution_count": 261,
"id": "4246afa1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"22741 0.008292\n",
"dtype: float64"
]
},
"execution_count": 261,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_Logit.predict(a) #logit model"
]
},
{
"cell_type": "markdown",
"id": "594f0721",
"metadata": {},
"source": [
"英瑞ky"
]
},
{
"cell_type": "code",
"execution_count": 262,
"id": "ff6c8c1d",
"metadata": {},
"outputs": [],
"source": [
"Enterex = df3[df3['公司']=='1592']"
]
},
{
"cell_type": "code",
"execution_count": 263,
"id": "8fc7fb2c",
"metadata": {},
"outputs": [],
"source": [
"b = Enterex.iloc[-1:,:]"
]
},
{
"cell_type": "code",
"execution_count": 264,
"id": "1ca80cb6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12385 0.015805\n",
"dtype: float64"
]
},
"execution_count": 264,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_ols.predict(b) #linear probility model"
]
},
{
"cell_type": "code",
"execution_count": 265,
"id": "03394ea5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12385 0.017519\n",
"dtype: float64"
]
},
"execution_count": 265,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_probit.predict(b) #probit model"
]
},
{
"cell_type": "code",
"execution_count": 266,
"id": "56f10a3b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12385 0.016884\n",
"dtype: float64"
]
},
"execution_count": 266,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_Logit.predict(b) #logit model"
]
},
{
"cell_type": "markdown",
"id": "4939ea95",
"metadata": {},
"source": [
"華映"
]
},
{
"cell_type": "code",
"execution_count": 267,
"id": "8881ecef",
"metadata": {},
"outputs": [],
"source": [
"CPT = df3[df3['公司']=='2475']"
]
},
{
"cell_type": "code",
"execution_count": 335,
"id": "85d11755",
"metadata": {},
"outputs": [],
"source": [
"d = CPT.iloc[-1:,:]"
]
},
{
"cell_type": "code",
"execution_count": 269,
"id": "c233994d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"28962 0.031108\n",
"dtype: float64"
]
},
"execution_count": 269,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_ols.predict(d) #linear probility model"
]
},
{
"cell_type": "code",
"execution_count": 270,
"id": "b54522f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"28962 0.030952\n",
"dtype: float64"
]
},
"execution_count": 270,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_probit.predict(d) #probit model"
]
},
{
"cell_type": "code",
"execution_count": 271,
"id": "0c2ef353",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"28962 0.028548\n",
"dtype: float64"
]
},
"execution_count": 271,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_Logit.predict(d) #logit model"
]
},
{
"cell_type": "markdown",
"id": "beccf0d7",
"metadata": {},
"source": [
"聯發科"
]
},
{
"cell_type": "code",
"execution_count": 336,
"id": "7d48013b",
"metadata": {},
"outputs": [],
"source": [
"mediatek = df3[df3['公司']=='2454']"
]
},
{
"cell_type": "code",
"execution_count": 337,
"id": "fd903bd0",
"metadata": {},
"outputs": [],
"source": [
"e = mediatek.iloc[-1:,:]"
]
},
{
"cell_type": "code",
"execution_count": 338,
"id": "7d7ffef3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"27973 0.005946\n",
"dtype: float64"
]
},
"execution_count": 338,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_ols.predict(e) #linear probility model"
]
},
{
"cell_type": "code",
"execution_count": 339,
"id": "7433a656",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"27973 0.006113\n",
"dtype: float64"
]
},
"execution_count": 339,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_probit.predict(e) #probit model"
]
},
{
"cell_type": "code",
"execution_count": 340,
"id": "6a1f75b6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"27973 0.006254\n",
"dtype: float64"
]
},
"execution_count": 340,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result_Logit.predict(e) #logit model"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment