#!/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()