Statistic Functions 统计学函数

  TA-Lib提供了常用的基础统计学函数,基于时间序列移动窗口进行计算。注意TA-Lib的beta,示例中是求某只股票的最高价与最低价序列的移动beta值,默认时间周期为5日,而资本资产定价中一般是分析某只股票相对于市场(大盘指数)的波动情况。

BETA : Beta Coefficient Capita Asset Pricing Model(CAPM) 资本资产定价模型里的beta系数:

ta.BETA(high, low, timeperiod=5),求两个序列的移动beta值(回归分析)

CORREL : Pearson‘s Correlation Coefficient(r) 皮尔逊相关系数:

ta.CORREL(high, low, timeperiod=30)

LINEARREG : Linear Regression 线性回归:

ta.LINEARREG(close, timeperiod=14),收盘价序列对时间t的线性回归,并输出预测值

LINEARREG_ANGLE : Linear Regression Angle 线性回归斜率的正切角度:

ta.LINEEARREG_ANGLE(close, timeperiod=14)收盘价序列对时间t的线性回归,并输出预测值

LINEARREG_INTERCEPT : Linear Regression Angle 线性回归截距:

ta.LINEARREG_INTERCEPR(close, timeperiod=14)

LINEARREG_SLOPE : Linear Regression Slope 线性回归斜率:

ta.LINEARREG_SLOPE(close, timeperiod=14)

STDDEV : Standard Deviation 标准差:

ta.STDDEV(close, timeperiod=5, nbdev=1),默认每5个收盘价计算标准差

TSF : Time Series Forecast 时间序列预测:

ta.TSF(close, timeperiod=14)

VAR : Variance 方差:

ta.VAR(close, timeperiod=5, nbdev=1)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import talib as ta
import tushare as ts

plt.rcParams[‘font.sans-serif‘] = [‘SimHei‘]
plt.rcParams[‘axes.unicode_minus‘] = False

def get_data(code, start=‘2015-01-01‘):
    df = ts.get_k_data(code, start)
    df.index = pd.to_datetime(df.date)
    df = df.sort_index()
    return df

df = get_data(‘sh‘)[[‘open‘,‘close‘,‘high‘,‘low‘]]
df[‘linearreg‘] = ta.LINEARREG(df.close, timeperiod=14)
df[‘tsf‘] = ta.TSF(df.close, timeperiod=14)
df.loc[‘2018-08-01‘:, [‘close‘,‘linearreg‘,‘tsf‘]
      ].plot(figsize=(12,6))

df[‘beta‘] = ta.BETA(df.high, df.low, timeperiod=5)
df[‘correl‘] = ta.CORREL(df.high, df.low, timeperiod=30)
df[‘stdev‘] = ta.STDDEV(df.close, timeperiod=5, nbdev=1)

df[[‘close‘, ‘beta‘, ‘correl‘,‘stdev‘]
  ].plot(figsize=(18,8), subplots=True, layout=(2,2))
plt.subplots_adjust(wspace=0, hspace=0.2)

原文地址:https://www.cnblogs.com/wintalau/p/11618587.html

时间: 2024-08-29 22:16:54

Statistic Functions 统计学函数的相关文章

C++对象模型——Virtual Member Functions (虚拟成员函数)(第四章)

4.2 Virtual Member Functions (虚拟成员函数) 已经看过了 virtual function的一般实现模型:每一个 class 有一个 virtual table,内含该 class 中有作用的 virtual function的地址,然后每个object有一个vptr,指向 virtual table的所在. 为了支持 virtual function机制,必须首先能够对多态对象有某种形式的"执行期类型判断法(runtime type resolution)&quo

SQL Fundamentals || Single-Row Functions || 转换函数 Conversion function

SQL Fundamentals: Using Single-Row Functions to Customize Output使用单行函数自定义输出 SQL Fundamentals || Single-Row Functions || 字符函数 character functions SQL Fundamentals || Single-Row Functions || 数字函数number functions SQL Fundamentals || Single-Row Functions

@Helper辅助方法和@functions自定义函数

1.首先说下@helper辅助方法,当我们在多个视图中共用相同的方法的时候,可以把此方法剥离出来放到一个位置,此时就可以用到@Helper辅助方法,首先我们在解决方案右键添加 App_Code文件夹,然后添加一个cshtml文件,清空原有文件内容,然后把自定义的方法放进去,例如 @helper Show(int i) { if(i==1) { @:值为1 } else { @:值为其他 } } 2.当我们想实现更复杂的逻辑,比如想有返回值的时候可以通过@functions关键字来自定义函数,如下

arrow functions 箭头函数

ES6里新增加的,与普通方法不同的地方 1.this 的对象在定义函数的时候确定了,而不是在使用的时候才决定 2.不可以使用 new  ,也就不能当构造函数 3.this 的值一旦确定无法修改 在 ES5 下,定制sort比较函数: var result = values.sort(function(a, b) { return a - b; }); ES6中,使用arrow function: var result = values.sort((a, b) => a - b); //给函数传递

函数可重入问题reentrant functions(函数执行过程中可以被中断,允许多个副本)

最近经常听到这个名词,以前也听到过,不过接触更多的是“线程安全问题”,而且本人也一直理解的是两个名字的含义是一样的.今天仔细总结一下这个名词相关的概念. 引用博文:可重入函数和不可重入函数 (http://www.cppblog.com/franksunny/archive/2007/08/03/29269.html) 主要用于多任务环境中, 一个可重入的函数简单来说就是可以被中断的函数,也就是说,可以在这个函数执行的任何时刻中断它,转入OS调度下去执行另外一段代码,而返回控制时不会出现什么错误

c++11: trailing return type in functions(函数返回类型后置)

In C++03, the return type of a function template cannot be generalized if the return type relies on those of the template arguments. Here is an example, mul(T a, T b) is a function template that calculates the product of a and b. Arguments a and b ar

Using Single-Row Functions to Customize Output使用单行函数自定义输出

DUAL is a public table that you can use to view results from functions and calculations. SQL> select * from DUAL; D - X SQL> desc DUAL; Name                                      Null?    Type ----------------------------------------- -------- ------

razor自定义函数 @helper 和@functions小结

from:http://www.cnblogs.com/jiagoushi/p/3904995.html asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们的开发效率,减少开发bug的出现. Razor 采用的是@ 尾巴符号,正是这个符号成就了Mvc开发效率的提升.下面了解一下和@相关的两个可以重用的helper.functions. 作为现代化的

asp.net MVC helper 和自定义函数@functions小结

asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们的开发效率,减少开发bug的出现. Razor 采用的是@ 尾巴符号,正是这个符号成就了Mvc开发效率的提升.下面了解一下和@相关的两个可以重用的helper.functions. 作为现代化的程序员,我们尽可能的遵守一个原则.不要重复你自己.所以能够重构的代码我们都会合并,但是这是对于后台代码C#