# 預測公司危機發生 Logit & Probit

## 本文重點概要

• 文章難度：★★★☆☆
• 以迴歸模型加入不同解釋變數，討論台股公司之危機發生機率
• 閱讀建議：本文在模型方面，並不會深入探討回歸之架構組成，僅會分析模型結果及舉例，讓我們了解的資訊，因此需要讀者對統計模型有基本的觀念，先閱讀【量化分析】預測市場?!；此外，本文提及之標的僅供說明使用，不代表任何金融商品之推薦或建議。

## 前言

1. Altman z-score model (1968)
2. Shumway (2001)

## 編輯環境及模組需求

```###量化三寶、套件工具
import pandas as pd
import numpy as np
import tejapi
import statsmodels.formula.api as smf
import datetime as dt
from dateutil.relativedelta import *```
```tejapi.ApiConfig.api_key = "YOUR_KEY"
tejapi.ApiConfig.ignoretz = True ###時間欄位忽略時區```

## 資料庫使用

```df1 = tejapi.get('TWN/AIND', #從TEJ api撈取所需要的資料
chinese_column_name = True,
paginate = True,
opts={'columns':['coid','mdate', 'dflt_d','fail_fg']})```

`df2 = df1[df1['危機事件類別'] != ''] #把有發生違約事件的篩選出來`
`df2['年/月'] = df2[['危機發生日']].applymap(lambda x: x.strftime('%Y-%m')).astype('datetime64') #把日期都換成月初`
`df2['月'] = df2['年/月'].dt.month #取月份出來`
```df2.reset_index(inplace = True)for i in range(len(df2.index)):
if df2['月'][i] == 1 or df2['月'][i] == 4 or df2['月'][i] == 7 or df2['月'][i] == 10:
df2['年/月'][i] = df2['年/月'][i]+ relativedelta(months = +2)
if df2['月'][i] == 2 or df2['月'][i] == 5 or df2['月'][i] == 8 or df2['月'][i] == 11:
df2['年/月'][i] = df2['年/月'][i]+ relativedelta(months = +1)
#為了之後對上財報資料，先將日期處理好```
`df2['Y'] = 1 #把所有危機事件類別設成1`
`df2.rename(columns= {'公司簡稱':'公司'}, inplace=True)`

X1 = working capital(R678)/total asset (0010)

X2 = retained earnings (2341)/ total asset

X3 = EBIT (2402)/total asset

X4 = market value (MV)/ total debt (1000)

X5 = revenue/ total asset (R607)

X6 = ROA (R11V)

X7 = debt ratio (R505)

TEJ會計科目細項及代碼

`###每一Dataframe大概有30萬筆資料以此類推`
```a1 = tejapi.get('TWN/AIFIN', #從TEJ api撈取所需要的資料
chinese_column_name = True,
paginate = True,
mdate = {'gt':'2008-01-01', 'lt':'2011-01-01'},
acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])```
```a2 = tejapi.get('TWN/AIFIN',
chinese_column_name = True,
paginate = True,
mdate = {'gt':'2011-01-01', 'lt':'2014-01-01'},
acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])```
```a3 = tejapi.get('TWN/AIFIN',
chinese_column_name = True,
paginate = True,
mdate = {'gt':'2014-01-01', 'lt':'2017-01-01'},
acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])```
```a4 = tejapi.get('TWN/AIFIN',
chinese_column_name = True,
paginate = True,
mdate = {'gt':'2017-01-01', 'lt':'2020-01-01'},
acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])```
```a5 = tejapi.get('TWN/AIFIN',
chinese_column_name = True,
paginate = True,
mdate = {'gt':'2020-01-01'},
acc_code = ['R678', '0010','2341','2402','MV','1000', 'R607','R11V', 'R505'])```
```###合併資料
acc = pd.concat([a1,a2,a3,a4,a5])```

```acc1 = acc.pivot_table(values='數值', index=['公司','年/月'], columns='會計科目')
acc1['X1'] = (acc1['R678']/acc1['0010'])*100
acc1['X2'] = (acc1['2341']/acc1['0010'])*100
acc1['X3'] = (acc1['2402']/acc1['0010'])*100
acc1['X4'] = (acc1['MV']/acc1['1000'])*100
acc1 = acc1.rename(columns = {'R607':'X5', 'R11V':'X6', 'R505':'X7'})
acc2 = acc1[['X1','X2','X3','X4','X5','X6','X7']]```
```acc2.reset_index(inplace=True)
df3 = pd.merge(acc2, df2[['公司','年/月','Y']], how='outer')
df3['Y'] = df3['Y'].replace(np.nan, 0) #把沒有發生危機的設為0
df3 = df3.dropna()
df3['X4'] = df3['X4'].drop([59690,59688]) #把無限大的值去掉
df3 = df3.rename(columns = {'狀況':'Y'})```

Procesing