python统计分析-平稳性检验

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# <editable>

def execute():
    # <editable>
    '''
    载入模块
    '''
    import warnings
    from statsmodels.graphics.tsaplots import plot_acf  # 绘制自相关图
    from statsmodels.tsa.stattools import adfuller as ADF  # 单位根检验
    import matplotlib.pyplot as plt
    warnings.filterwarnings("ignore")
    import pandas as pd
    from sqlalchemy import create_engine
    '''
    连接数据库
    '''
    engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis')
    '''
    选择目标数据
    '''
    params = {
        "sequence": "SUNACTIVITY",
    }
    inputs = {"table": '纯随机性检验'}
    data_sql = 'select ' + params['sequence'] + ' from ' + inputs['table']
    data_in = pd.read_sql_query(data_sql, engine)
    print(data_in)
    data_in = data_in.dropna()
    '''
    平稳性检验
    '''
    sequence = data_in[params['sequence']]
    adf_result = ADF(sequence)
    test_statistic = adf_result[0]
    p_value = adf_result[1]
    use_lag = adf_result[2]
    nobs = adf_result[3]
    critical_1 = adf_result[4]['5%']
    critical_5 = adf_result[4]['1%']
    critical_10 = adf_result[4]['10%']
    data_out = ''
    data_out += '平稳性检验结果\n'
    data_out += '检验结果\n'
    data_out += 'Test statistic:' + str(test_statistic) + '\n'
    data_out += ' p-value:' + str(p_value) + '\n'
    data_out += 'Number of lags used:' + str(use_lag) + '\n'
    data_out += 'Number of observations used for the ADF regression and calculation of the critical values:' + str(
        nobs) + '\n'
    data_out += 'Critical values for the test statistic at the 5 %:' + str(critical_1) + '\n'
    data_out += 'Critical values for the test statistic at the 1 %:' + str(critical_5) + '\n'
    data_out += 'Critical values for the test statistic at the 10 %:' + str(critical_10) + '\n'

    '''
    自相关图
    '''
    fig = plt.figure(figsize=(10, 4))
    ax1 = fig.add_subplot(111)
    plot_acf(sequence, ax=ax1, fft=True)
    plt.savefig('acf.png')

    '''
    生成报告
    '''
    print(data_out)

    '''
    数据示例
            SUNACTIVITY
    0           5.0
    1          11.0
    2          16.0
    3          23.0
    4          36.0
    5          40.4
    6          29.8
    7          15.2
    8           7.5
    9           2.9
    10         83.4
    11         47.7
    12         47.8
    13         30.7
    14         12.2
    15         40.4
    16         29.8
    17         15.2
    18          7.5
    19          2.9
    20         12.6
    平稳性检验结果
    检验结果
    Test statistic:-3.125280514027156
     p-value:0.0247380100963531
    Number of lags used:0
    Number of observations used for the ADF regression and calculation of the critical values:20
    Critical values for the test statistic at the 5 %:-3.0216450000000004
    Critical values for the test statistic at the 1 %:-3.8092091249999998
    Critical values for the test statistic at the 10 %:-2.6507125

    '''


# </editable>


if __name__ == '__main__':
    execute()

 

posted @ 2021-04-22 11:34  我当道士那儿些年  阅读(892)  评论(0编辑  收藏  举报