0
点赞
收藏
分享

微信扫一扫

python:用OLS 求企业的净利润年平均增长率

以长春高新 2006年-2021年的净利润 为样本数据,用OLS求企业的净利润年平均增长率。

先看数据 000661.txt 净利润 单位:百万元

year,jlr
2006,5.26
2007,6.5
2008,20
2009,73
2010,90
2011,110
2012,300
2013,280
2014,320
2015,380
2016,480
2017,660
2018,1006
2019,1775
2020,3047
2021,4170

ols_model_1.py

# coding=utf-8
import os, sys
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

# 用 statsmodels库做一元线性回归分析
if len(sys.argv) ==2:
    fcode = sys.argv[1]
else:
    print('usage: python ols_model_1.py fcode ')
    sys.exit(1)

if len(fcode) !=6:
    print(' fcode is char(6)')
    sys.exit(2)

file1 = "./" +fcode +'.txt'
if not os.path.exists(file1):
    print(file1 +' is not exists.')
    sys.exit(3)

# 用pandas 读取csv
df = pd.read_csv(file1)
y = df['jlr'].values # 净利润

# 构造变量
x = np.arange(0,len(y),1) # x值
X = sm.add_constant(x) # 回归方程添加一列 x0=1
 
# 建回归方程
# OLS(endog,exog=None,missing='none',hasconst=None) (endog:因变量,exog=自变量)
modle = sm.OLS(y,X) # 最小二乘法
res = modle.fit()   # 拟合数据
beta = res.params   # 取系数
print(res.summary())  # 回归分析摘要
print('beta=',beta)

# 画图
Y = res.fittedvalues    # 预测值
fig, ax = plt.subplots(figsize=(10,6))
ax.plot(x, y, '-', label='jz')  # 原始数据
ax.plot(x, Y, 'r--.',label='fit') # 拟合数据
ax.legend(loc='upper left') # 图例,显示label
plt.title('predict fund net value: ' +fcode)
plt.xlabel('x')
plt.ylabel('jz')
plt.grid()
plt.show()

运行 python ols_model_1.py 000661

                            OLS Regression Results
==============================================================================
Dep. Variable:                      y   R-squared:                       0.621
Model:                            OLS   Adj. R-squared:                  0.594
Method:                 Least Squares   F-statistic:                     22.98
Date:                Sun, 20 Feb 2022   Prob (F-statistic):           0.000286
Time:                        14:16:26   Log-Likelihood:                -127.94
No. Observations:                  16   AIC:                             259.9
Df Residuals:                      14   BIC:                             261.4
Df Model:                           1
Covariance Type:            nonrobust
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const       -702.8378    366.772     -1.916      0.076   -1489.486      83.810
x1           199.7347     41.662      4.794      0.000     110.378     289.092
==============================================================================
Omnibus:                        5.197   Durbin-Watson:                   0.316
Prob(Omnibus):                  0.074   Jarque-Bera (JB):                2.844
Skew:                           0.996   Prob(JB):                        0.241
Kurtosis:                       3.543   Cond. No.                         17.0
==============================================================================

beta= [-702.83779412  199.73470588]

结论:企业的净利润年平均增长率 62.1%

附录:回归结果详细说明

    Dep.Variable: y 因变量
    Model:OLS 最小二乘模型
    Method: Least Squares 最小二乘
    No. Observations: 样本数据的数量
    Df Residuals:残差自由度(degree of freedom of residuals)
    Df Model:模型自由度(degree of freedom of model)
    Covariance Type:nonrobust 协方差阵的稳健性
    R-squared:R 判定系数
    Adj. R-squared: 修正的判定系数
    F-statistic: 统计检验 F 统计量
    Prob (F-statistic): F检验的 P值
    Log likelihood: 对数似然

    coef:自变量和常数项的系数,b1,b2,...bm,b0
    std err:系数估计的标准误差
    t:统计检验 t 统计量
    P>|t|:t 检验的 P值
    [0.025, 0.975]:估计参数的 95%置信区间的下限和上限
    Omnibus:基于峰度和偏度进行数据正态性的检验
    Prob(Omnibus):基于峰度和偏度进行数据正态性的检验概率
    Durbin-Watson:检验残差中是否存在自相关
    Skewness:偏度,反映数据分布的非对称程度
    Kurtosis:峰度,反映数据分布陡峭或平滑程度
    Jarque-Bera(JB):基于峰度和偏度对数据正态性的检验
    Prob(JB):Jarque-Bera(JB)检验的 P值。
    Cond. No.:检验变量之间是否存在精确相关关系或高度相关关系。
举报

相关推荐

利润率的加权平均

0 条评论