python統計分析-純隨機性檢驗


 

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

# <editable>


def execute():
    # <editable>
    '''
    載入模塊
    '''
    import warnings
    warnings.filterwarnings("ignore")
    from statsmodels.stats.diagnostic import acorr_ljungbox
    import statsmodels.api as sm
    import pandas as pd
    from sqlalchemy import create_engine
    '''
    連接數據庫
    '''
    engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis')
    '''
    選擇目標數據
    '''
    params = {
        "columns": "`YEAR`, SUNACTIVITY",
    }
    inputs = {"table": '純隨機性檢驗'}
    data_sql = 'select ' + params['columns'] + ' from ' + inputs['table']
    data_in = pd.read_sql_query(data_sql, engine)
    print(data_in)
    '''
    純隨機性檢驗
    '''
    # data = sm.datasets.sunspots.load_pandas().data    # 可以自動生成數據
    res = sm.tsa.ARMA(data_in["SUNACTIVITY"], (1, 1)).fit(disp=-1)
    # res.resid 殘差
    data_out = sm.stats.acorr_ljungbox(res.resid, lags=[10], return_df=True)

    '''
    將結果寫出
    '''
    print(data_out)
    '''
    數據示例
          YEAR  SUNACTIVITY
    0   1700.0          5.0
    1   1701.0         11.0
    2   1702.0         16.0
    3   1703.0         23.0
    4   1704.0         36.0
    5   1705.0         40.4
    6   1706.0         29.8
    7   1707.0         15.2
    8   1708.0          7.5
    9   1709.0          2.9
    10  1710.0         83.4
    11  1711.0         47.7
    12  1712.0         47.8
    13  1713.0         30.7
    14  1714.0         12.2
    15  1715.0         40.4
    16  1716.0         29.8
    17  1717.0         15.2
    18  1718.0          7.5
    19  1719.0          2.9
    20  1720.0         12.6
    
    =======================
         lb_stat  lb_pvalue
    10  7.282251   0.698557

    '''



# </editable>

if __name__ == '__main__':
    execute()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM