python求残差_在python中如何计算点过程的残差
好的,你首先要做的就是绘制数据。为了简单起见,我复制了this figure,因为它只有8个事件发生,所以很容易看到系统的行为。以下代码:import numpy as np
import math, matplotlib
import matplotlib.pyplot
import matplotlib.lines
mu = 0.1 # Parameter values as found in the article http://jheusser.github.io/2013/09/08/hawkes.html Hawkes Process section.
alpha = 1.0
beta = 0.5
EventTimes = np.array([0.7, 1.2, 2.0, 3.8, 7.1, 8.2, 8.9, 9.0])
" Compute conditional intensities for all times using the Hawkes process. "
timesOfInterest = np.linspace(0.0, 10.0, 100) # Times where the intensity will be sampled.
conditionalIntensities = [] # Conditional intensity for every epoch of interest.
for t in timesOfInterest:
conditionalIntensities.append( mu + np.array( [alpha*math.exp(-beta*(t-ti)) if t > ti else 0.0 for ti in EventTimes] ).sum() ) # Find the contributions of all preceding events to the overall chance of another one occurring. All events that occur after t have no contribution.
" Plot the conditional intensity time history. "
fig = matplotlib.pyplot.figure()
ax = fig.gca()
labelsFontSize = 16
ticksFontSize = 14
fig.suptitle(r"$Conditional\ intensity\ VS\ time$", fontsize=20)
ax.grid(True)
ax.set_xlabel(r'$Time$',fontsize=labelsFontSize)
ax.set_ylabel(r'$\lambda$',fontsize=labelsFontSize)
matplotlib.rc('xtick', labelsize=ticksFontSize)
matplotlib.rc('ytick', labelsize=ticksFontSize)
eventsScatter = ax.scatter(EventTimes,np.ones(len(EventTimes))) # Just to indicate where the events took place.
ax.plot(timesOfInterest, conditionalIntensities, color='red', linestyle='solid', marker=None, markerfacecolor='blue', markersize=12)
fittedPlot = matplotlib.lines.Line2D([],[],color='red', linestyle='solid', marker=None, markerfacecolor='blue', markersize=12)
fig.legend([fittedPlot, eventsScatter], [r'$Conditional\ intensity\ computed\ from\ events$', r'$Events$'])
matplotlib.pyplot.show()
即使我选择的事件时间有点武断:
这也可以应用于5000笔交易的example set of data集合,方法是将数据绑定并将每个bin视为一个事件。然而,现在的情况是,由于每个箱子中发生的交易数量不同,每个事件的权重略有不同。
这也在the article将比特币交易到达拟合到霍克斯过程中的一节中提到,并提出了解决这个问题的方法:The only difference to the original dataset is that I added a random millisecond timestamp to all trades that share a timestamp with another trade. This is required as the model requires to distinguish every trade (i.e. every trade must have a unique timestamp).这一点包含在以下代码中:
^{pr2}$
这将生成以下拟合图:
所有这些看起来都不错,但是,当你仔细观察细节时,你会发现,仅仅通过取交易数量的一个向量并减去拟合的向量来计算残差是行不通的,因为它们的长度不同:
然而,有可能提取与经验数据记录相同时期的强度,然后计算残差。这使您能够找到经验数据和拟合数据的分位数,并将它们相互对比,从而生成QQ图:""" GENERATE THE QQ PLOT. """
" Process the data and compute the quantiles. "
orderStatistics=[]; orderStatistics2=[];
for i in range( empirical_1min.values.size ): # Make sure all the NANs are filtered out and both arrays have the same size.
if not np.isnan( empirical_1min.values[i] ):
orderStatistics.append(empirical_1min.values[i])
orderStatistics2.append(conditionalIntensities[i])
orderStatistics = np.array(orderStatistics); orderStatistics2 = np.array(orderStatistics2);
orderStatistics.sort(axis=0) # Need to sort data in ascending order to make a QQ plot. orderStatistics is a column vector.
orderStatistics2.sort()
smapleQuantiles=np.zeros( orderStatistics.size ) # Quantiles of the empirical data.
smapleQuantiles2=np.zeros( orderStatistics2.size ) # Quantiles of the data fitted using the Hawkes process.
for i in range( orderStatistics.size ):
temp = int( 100*(i-0.5)/float(smapleQuantiles.size) ) # (i-0.5)/float(smapleQuantiles.size) th quantile. COnvert to % as expected by the numpy function.
if temp<0.0:
temp=0.0 # Avoid having -ve percentiles.
smapleQuantiles[i] = np.percentile(orderStatistics, temp)
smapleQuantiles2[i] = np.percentile(orderStatistics2, temp)
" Make the quantile plot of empirical data first. "
fig2 = matplotlib.pyplot.figure()
ax2 = fig2.gca(aspect="equal")
fig2.suptitle(r"$Quantile\ plot$", fontsize=20)
ax2.grid(True)
ax2.set_xlabel(r'$Sample\ fraction\ (\%)$',fontsize=labelsFontSize)
ax2.set_ylabel(r'$Observations$',fontsize=labelsFontSize)
matplotlib.rc('xtick', labelsize=ticksFontSize)
matplotlib.rc('ytick', labelsize=ticksFontSize)
distScatter = ax2.scatter(smapleQuantiles, orderStatistics, c='blue', marker='o') # If these are close to the straight line with slope line these points come from a normal distribution.
ax2.plot(smapleQuantiles, smapleQuantiles, color='red', linestyle='solid', marker=None, markerfacecolor='red', markersize=12)
normalDistPlot = matplotlib.lines.Line2D([],[],color='red', linestyle='solid', marker=None, markerfacecolor='red', markersize=12)
fig2.legend([normalDistPlot, distScatter], [r'$Normal\ distribution$', r'$Empirical\ data$'])
matplotlib.pyplot.show()
" Make a QQ plot. "
fig3 = matplotlib.pyplot.figure()
ax3 = fig3.gca(aspect="equal")
fig3.suptitle(r"$Quantile\ -\ Quantile\ plot$", fontsize=20)
ax3.grid(True)
ax3.set_xlabel(r'$Empirical\ data$',fontsize=labelsFontSize)
ax3.set_ylabel(r'$Data\ fitted\ with\ Hawkes\ distribution$',fontsize=labelsFontSize)
matplotlib.rc('xtick', labelsize=ticksFontSize)
matplotlib.rc('ytick', labelsize=ticksFontSize)
distributionScatter = ax3.scatter(smapleQuantiles, smapleQuantiles2, c='blue', marker='x') # If these are close to the straight line with slope line these points come from a normal distribution.
ax3.plot(smapleQuantiles, smapleQuantiles, color='red', linestyle='solid', marker=None, markerfacecolor='red', markersize=12)
normalDistPlot2 = matplotlib.lines.Line2D([],[],color='red', linestyle='solid', marker=None, markerfacecolor='red', markersize=12)
fig3.legend([normalDistPlot2, distributionScatter], [r'$Normal\ distribution$', r'$Comparison\ of\ datasets$'])
matplotlib.pyplot.show()
这将生成以下绘图:
^{5美元
经验数据的分位数图不完全是the same as in the article,我不知道为什么,因为我不擅长统计学。但是,从编程的角度来看,这就是您可以完成所有这些的方法。在
python求残差_在python中如何计算点过程的残差相关推荐
- python求雅可比矩阵_在Python中计算神经网络的雅可比矩阵
通常,神经网络是一个多变量,矢量值函数,如下所示: 函数f有一些参数θ(神经网络的权重),它将一个N维向量x(即猫图片的N像素)映射到一个m维矢量(例如,x属于M个不同类别中的每个类别的概率): 在训 ...
- python求完全平方数_【Python】【demo实验6】【练习实例】【完全平方数相关】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. ...
- 用python求期望_用Python计算明日方舟2021龙门幸运墙期望
按照去年的惯例,方舟今年春节的时候也整了个红包盲盒. 比起去年简单粗暴的直接送,今年的盲盒实际上增加了两层隐性的保底机制:第一层是每天有两次机会而非一次,两次尝试取收益更高的结果:第二层是如果不幸成为 ...
- python求雅可比矩阵_用Python计算雅可比矩阵
Jacobian仅为向量值函数定义.不能使用填充有常数的数组来计算雅可比:必须知道基本函数及其偏导数,或它们的数值近似值.当你认为一个常数(关于某事物)的(偏)导数是0时,这是显而易见的. 在Pyth ...
- python求协方差矩阵_协方差矩阵python实现
当你有一个数据集,每一条数据都M种属性,然后你想知道M种属性对数据集的影响的时候.你需要用到协方差矩阵. 求协方差矩阵之前请一定要知道协方差矩阵是干嘛的,是表示属性之间关系的矩阵,协方差矩阵的规模只与 ...
- python 时间序列预测_使用Python进行动手时间序列预测
python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...
- python 概率分布模型_使用python的概率模型进行公司估值
python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...
- python求50的阶乘_python中求阶乘
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 我如何去计算python中的一个整数的阶乘?... 写一个猜数字的游戏,预先设定 ...
- python求50的阶乘_python中的阶乘
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 我如何去计算python中的一个整数的阶乘?... 问题描述 阶乘是我们在很多的 ...
- python长度多少_用Python求最长子串长度快速版
哈喽大家好,周二也是令人愉快的一天啊,今天天气不错,坐在窗户旁边边晒太阳边写文章,再泡杯热茶,真是舒服美好,废话不多说,今天说一下Python求最长子串长度,希望对大家有作用,raksmart服务器. ...
最新文章
- 理解神经网络,从简单的例子开始(1)7行python代码构建神经网络
- 出现运行时间错误_Excel VBA 运行错误,你知道为何突然出现错误的原因吗
- linux mysql 知乎_在 Linux 上安装 MariaDB 或 MySQL | Linux 中国
- spring集成kafka
- 三星笔试能带计算机吗,2021年三星笔试试题+经验谈
- diskgenius扩容c盘重启电脑卡住_电脑使用DiskGenius工具增加C盘空间的方法
- ubuntu12.04 安装中文输入法
- LeetCode 410——分割数组的最大值
- 七年级上册计算机重点知识点,初一上册数学重点知识点
- 任务七:实现常见的技术产品官网的页面架构及样式布局
- OSO.EXE病毒专杀工具
- Embedding技术在推荐系统中的应用
- Json序列反序列类型处理帮助类
- matlab数据归一化代码_深度学习amp;Matlab-LeNet实现图像分类
- 本地Apache服务器访问时502 Server dropped connection 错误解决方法
- layui 表单验证案例
- 一文读懂DeFi衍生品市场六大方向及底层发展逻辑 |链捕手
- Harbor中镜像清理
- Ubuntu 16.04 安装 uTorrent
- LCD-QC1602A v2.0