# 基于Python预测股价的那些人那些坑，请认真看完！【系列52】

来源：AI科技大本营（rgznai100）

B是给别人装的！钱是给自己挣的！

﻿ ﻿

﻿ ﻿

1. 调整结果，让我们看起来像是成功了

2. 隐藏结果，所以没有人会注意到

3. 公开我们所有的结果和方法，以便其他人（以及我们自己）可以从中吸取经验和教训

Github代码地址：

https://github.com/WillKoehrsen/Data-Analysis/tree/master/stocker

Stocker是一款用于探索股票情况的Python工具。一旦我们安装了所需的库（查看文档），我们可以在脚本的同一文件夹中启动一个Jupyter Notebook，并导入Stocker类：

`      `fromstockerimportStocker``

`      `amazon = Stocker('AMZN')``

AMZN Stocker Initialized. Data covers 1997-05-16 to 2018-01-18.

`      `amazon.plot_stock()``
```Maximum Adj. Close = 1305.20 on 2018-01-12.
Minimum Adj. Close = 1.40 on 1997-05-22.
Current Adj. Close = 1293.32.      ```

﻿﻿

Stocker的分析功能可以用来发现数据中的整体趋势和模式，但我们将重点关注预测股票未来的价格上。Stocker中的预测功能是使用一个加性模型来实现的，该模型将时间序列视为季节性（如每日、每周和每月）的整体趋势组合。Stocker使用Facebook开发的智能软件包进行加性建模，用一行代码就可以创建模型并进行预测：

`      `model, model_data = amazon.create_prophet_model(days=90)``

Predicted Price on 2018-04-18 = \$1336.98

`      `amazon.evaluate_prediction()``

Prediction Range: 2017-01-18 to 2018-01-18.

```Predicted price on 2018-01-17 = \$814.77.
Actual price on    2018-01-17 = \$1295.00.

Average Absolute Error on Training Data = \$18.21.
Average Absolute Error on Testing  Data = \$183.86.

When the model predicted an increase, the price increased 57.66% of the time.
When the model predicted a  decrease, the price decreased  44.64% of the time.

The actual value was within the 80% confidence interval 20.40% of the time.      ```

﻿

`      `amazon.changepoint_prior_analysis(changepoint_priors=[0.001,0.05,0.1,0.2])``

﻿

`      `amazon.changepoint_prior_validation(start_date='2016-01-04',        end_date='2017-01-03', changepoint_priors=[0.001,0.05,0.1,0.2])``
```Validation Range 2016-01-04 to 2017-01-03.

cps  train_err  train_range    test_err  test_range
0  0.001  44.475809   152.600078  149.373638  152.564766
1  0.050  11.203019    35.820696  152.033810  139.505624
2  0.100  10.722908    34.593207  152.903481  172.654255
3  0.200   9.725255    31.895204  127.604543  324.376524```

Stocker先验验证还可以通过两条线来阐述这些点：

﻿ ﻿

﻿

`      `amazon.changepoint_prior_validation(start_date='2016-01-04',        end_date='2017-01-03', changepoint_priors=[0.15,0.2,0.25,0.4,0.5,0.6])``

﻿改进后的训练和测试曲线

`      `amazon.changepoint_prior_scale =0.5``

`      `amazon.evaluate_prediction()``
```Prediction Range: 2017-01-18 to 2018-01-18.

Predicted price on 2018-01-17 = \$1160.43.
Actual price on    2018-01-17 = \$1295.00.

Average Absolute Error on Training Data = \$10.21.
Average Absolute Error on Testing  Data = \$99.99.

When the model predicted an increase, the price increased 56.90% of the time.
When the model predicted a  decrease, the price decreased  44.00% of the time.

The actual value was within the 80% confidence interval 95.20% of the time.      ```

﻿

﻿现在看起来好多了！ 这显示了模型优化的重要性。使用默认值可以提供第一次合理猜测，但是我们需要确定，我们正在使用正确的模型“设置”，就像我们试图通过调整平衡和淡入淡出来优化立体声的声音那样（很抱歉引用了一个过时的例子）。

1、当模型预测股价会上涨的那一天，我们开始买入，并在一天结束时卖出。当模型预测股价下跌时，我们就不买入任何股票；

2、如果我们购买股票的价格在当天上涨，那么我们就把股票上涨的幅度乘以我们购买的股票的数量；

3、如果我们购买的股票价格下跌，我们就把下跌的幅度乘以股票的数量，计作我们的损失。

`      `amazon.evaluate_prediction(nshares=1000)``
```You played the stock market in AMZN from 2017-01-18 to 2018-01-18 with 1000 shares.

When the model predicted an increase, the price increased 57.99% of the time.
When the model predicted a  decrease, the price decreased  46.25% of the time.

The total profit using the Prophet model = \$299580.00.
The Buy and Hold strategy profit =         \$487520.00.

Thanks for playing the stock market!      ```

`      `amazon.predict_future(days=10)        amazon.predict_future(days=100)``

This post first appeared on IT瘾 | IT社区推荐资讯, please read the originial post: here

×

# Subscribe to It瘾 | It社区推荐资讯

Get updates delivered right to your inbox!

×