• 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 += '平稳性检验结果
    '
        data_out += '检验结果
    '
        data_out += 'Test statistic:' + str(test_statistic) + '
    '
        data_out += ' p-value:' + str(p_value) + '
    '
        data_out += 'Number of lags used:' + str(use_lag) + '
    '
        data_out += 'Number of observations used for the ADF regression and calculation of the critical values:' + str(
            nobs) + '
    '
        data_out += 'Critical values for the test statistic at the 5 %:' + str(critical_1) + '
    '
        data_out += 'Critical values for the test statistic at the 1 %:' + str(critical_5) + '
    '
        data_out += 'Critical values for the test statistic at the 10 %:' + str(critical_10) + '
    '
    
        '''
        自相关图
        '''
        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()
    作者:沐禹辰
    出处:http://www.cnblogs.com/renfanzi/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    Bate版本控制报告
    【探路者】第五周立会报告6(总第32次)
    【探路者】第五周立会报告5(总第31次)
    【探路者】第五周立会报告4(总第30次)
    【探路者】第五周立会报告3(总第29次)
    【探路者】第五周立会报告2(总第28次)
    例行报告
    【探路者】第五周立会报告1(总第27次)
    【探路者】第四周立会报告7(总第26次)
    “Hello world!”团队第三周贡献分规则
  • 原文地址:https://www.cnblogs.com/renfanzi/p/14688809.html
Copyright © 2020-2023  润新知