# 預測市場?!

• 線性回歸 介紹&應用
• 羅吉斯回歸 介紹&應用

## 線性回歸

### 匯入套件

`import pandas as pdimport numpy as npimport matplotlib.pyplot as pltplt.style.use('seaborn')plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']import tejapitejapi.ApiConfig.api_key = 'your key'tejapi.ApiConfig.ignoretz = True`

### 資料撈取

`data = tejapi.get('TWN/EWPRCD',                  coid='2454',                  paginate = True,                  chinese_column_name = True                 )return_ = tejapi.get('TWN/EWPRCD2',                  coid='2454',                  paginate = True,                  chinese_column_name = True                 )data['報酬率％'] = return_['日報酬率(%)']`

### 檢定

⏰ 發現股價為非定態資料後，常見的解決方法為：

• 對股價做一階差分再去跑回歸

### 線性回歸分析

`# 用昨天(第t-1日)的報酬率去預測今天(第t日)的報酬率# 因前兩個位置的值為NA，故取第3個位置以後的數值x = data['報酬率％'].shift(1)[2:].valuesy = data['報酬率％'][2:].valuesreg = np.polyfit(x,y,deg=1)# 繪圖plt.figure(figsize=(10,6))plt.plot(x, y, 'bo', label='data')plt.plot(x, np.polyval(reg,x),'r',label='linear regression')plt.legend(loc=0)`

### 加入更多變數

`# 落後一期至三期的報酬率data['R_t-1'] = data['報酬率％'].shift(1)data['R_t-2'] = data['報酬率％'].shift(2)data['R_t-3'] = data['報酬率％'].shift(3)x = data[['R_t-1','R_t-2','R_t-3']][4:].valuesy = data['報酬率％'][4:].valuesreg_t3 = np.linalg.lstsq(x,y, rcond=None)[0]# 繪圖plt.figure(figsize=(10,6))plt.plot(y[1000:1300],label = 'true_value')plt.plot(np.dot(x, reg_t3)[1000:1300],color = 'green',label = 'predict')plt.legend()`

## 羅吉斯回歸

### Python 程式碼

📈羅吉斯回歸分析📈

np.where(條件, 符合條件輸出, 不符合輸出)：

``````data['signal'] = np.where(data['報酬率％']>0,1,-1)
# 繪圖
data[:100].plot(x = '日期',
y = 'signal',
style = 'ro',
figsize = (10,6)
)``````
``````from sklearn import linear_model
lm = linear_model.LogisticRegression(solver = 'lbfgs')
R_t1 = x.reshape(1,-1).T
signal = y
lm.fit(R_t1,signal)
preditions = lm.predict(R_t1)
plt.figure(figsize = (10, 6))
plt.plot(x, y, 'ro', label = 'data')
plt.plot(x, preditions, 'b', label = 'preditions')
plt.legend(loc = 0)``````

### 預測準確率

``````from sklearn.metrics import accuracy_score
preditions = lm.predict(R_t1)
accuracy_score(preditions, y)``````

Procesing