zoukankan      html  css  js  c++  java
  • 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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    搭建App主流框架_纯代码搭建(OC)
    UIApplication,UIWindow,UIViewController,UIView(layer)
    插件类
    VIEWCONTROLLER的启动流程
    UIView你知道多少
    分析UIWindow
    创建控制器的3种方式、深入了解view的创建和加载顺序
    UIViewController的生命周期及iOS程序执行顺序
    ViewController加载顺序与self.view
    GCD
  • 原文地址:https://www.cnblogs.com/renfanzi/p/14688809.html
Copyright © 2011-2022 走看看