【FinE】统计与计量指标计算(Matlab)

article/2025/10/16 14:34:02

导航

CDF函数(normal distribution)

计算normal distribution拟合下累积分布函数

function [Fncap] = minux_CDF_normal(returns)% Normal cumulative distribution function, the prob. the return% is lower than or equal to the return in cell(i, j) of returns where% assuming returns follow a normal distribution.% input:% returns: matrix of stock returns% output:% Fncap: matrix of cumulative distribution function[n, m]=size(returns);Fncap = zeros(n, m);for j=1:mA = fitdist(returns(:, j), 'Normal'); % 使用正态分布拟合资产收益率for i = 1:nFncap(i, j)=normcdf(returns(i, j), A.mu, A.sigma);endendplot(sort(returns), sort(Fncap));
end

normal_cdf

CDF函数(t-location-scale distribution)

The t-location-scale distribution is preferred nub cases of leptokurtosis, as expressed by the distribution1 of returns(使用t分布可以更好地刻画收益率序列中的尖峰厚尾现象)

给出 T ( x , μ , σ , ν ) T(x, \mu, \sigma, \nu) T(x,μ,σ,ν)分布的pdf如下
T ( x , μ , σ , ν ) = Γ ( ν + 1 2 ) σ ν π Γ ( ν 2 ) [ ν + ( x − μ σ ) 2 ν ] − ν + 1 2 T(x, \mu, \sigma, \nu)=\frac{\Gamma(\frac{\nu+1}{2})}{\sigma\sqrt{\nu\pi}\Gamma(\frac{\nu}{2})}\bigg[\frac{\nu+(\frac{x-\mu}{\sigma})^2}{\nu}\bigg]^{-\frac{\nu+1}{2}} T(x,μ,σ,ν)=σνπ Γ(2ν)Γ(2ν+1)[νν+(σxμ)2]2ν+1
由于 Γ \Gamma Γ结构的存在,所以无法直接计算t-location-scaleCDF,可以使用标准化的方法,转为student t-distribution求解,pdf的转化关系如下
T ( y , μ , σ , ν ) = 1 σ PDF tStudent ( y − μ σ ) T(y, \mu, \sigma, \nu)=\frac{1}{\sigma}\text{PDF}_{\text{tStudent}}(\frac{y-\mu}{\sigma}) T(y,μ,σ,ν)=σ1PDFtStudent(σyμ)
得到CDF之间的转化关系
F t S t u ( y − μ σ ) = F t L o c ( y ) F_{tStu}(\frac{y-\mu}{\sigma})=F_{tLoc}(y) FtStu(σyμ)=FtLoc(y)

function [Ftcap] = minux_CDF_t(returns)% t-location scale cumulative distribution function% in cell(i,j) there is the prob. that the return is lower than or% equal to the return in cell of returns, if returns follow a% t-location scale distribution[n, m] = size(returns);Ftcap = zeros(n, m);for j = 1:mA = fitdist(returns(:, j), 'tLocationScale');for i = 1:nFtcap(i, j)=tcdf((returns(i, j)-A.mu)/A.sigma, A.nu);endendplot(sort(returns), sort(Ftcap));
end

t-cdf

CAPM模型 β \beta β计算

CAPM模型
E ( R i ) = R f + β i [ E ( R m ) − R f ] + ε i E(R_i)=R_f+\beta_i[E(R_m)-R_f]+\varepsilon_i E(Ri)=Rf+βi[E(Rm)Rf]+εi
可以使用简单线性回归的方法或者公式法计算 β i \beta_i βi的值

function [beta] = minux_beta(returns, retmkt)%% generate a column vector that contains the betas of all stocks% input:% returns: matrix of stocks returns% retmkt: column vector of returns of the market% output:% beta: column vector of stocks betas% 回归方法计算m = size(returns, 2);beta = zeros(m, 1);for i=1:mbeta(i, 1)=regress(returns(:, i), retmkt);end% 矩阵方法计算%{cov_mat = cov(returns, retmkt);beta = cov_mat(1, 2)/cov_mat(2, 2);%}
end

误差项 ε i \varepsilon_i εi计算

设置误差项 ε i \varepsilon_i εi为从如下概率空间抽取的独立随机变量(independent random variables).
ε i = σ i T t S t u ( ν i ) \varepsilon_i=\sigma_iT_{tStu}(\nu_i) εi=σiTtStu(νi)

function [e] = minux_err(returns, retmkt, beta, rf)% generate the error values for the CAPM formula from a process% following an fitted t-Location Scale Distribution% 根据CAPM模型计算误差项% input:% returns: matrix of stocks returns, 资产收益率矩阵% retmkt: matrix of market returns,市场收益率向量% beta,beta列向量% rf,无风险利率% output:% e, matrix of error terms for each stock[n, m] = size(returns);e = zeros(n, m);for i=1:me(:, i) = returns(:, i)-rf-beta(i, 1)*(retmkt-rf);end
end

The expected value of the returns of the market is calculated with the same method as the error term. With all the variables needed, we can simply follow the CAPM formula and calculate the expected returns of security i i i following a t t t-location scale distribution.

根据 t t t分布的性质模拟出场景代码如下

function [rbarStocks] = minux_SCENARIOS(retmkt, beta, rf, e)% this function creates the matrix of predicted returns following the% CAPM% input:% retmkt: matrix of market returns% beta: risk free rate of returns% e: error matrix% output:% rbarStocks: matrix of predicted stocks returns following a t-location% scale[n, m] = size(e);rbarStocks = zeros(n, m);dMarket = fitdist(retmkt, 'tLocationScale'); % 使用t分布拟合市场收益率序列for i = 1:mdErr = fitdist(e(:, i), 'tLocationScale'); % t分布拟合误差序列for j=1:nrbarStocks(j, i) = rf(j, 1)+beta(i, 1)* (random(dMarket)-rf(j, 1))+random(dErr); % 根据拟合市场序列,beta值,误差项生成stock scenariosendend
end

correlation and covariance

One of the major issues related to the use of the CAPM model is the correlation between the predicted returns and the error term. In fact it would mean that the model, with the current structure and variables is not explaining the behavior of returns in a complete way.

计算误差与模拟收益率序列之间的协方差代码如下

function [CovER, Cor] = minux_cov(rbarStocks, e)% input:% rbarStocks: matrix of predicted stock returns% e: matrix of error returns% CovER: 模拟序列和误差项之间的协方差% Cor: 模拟序列和误差项之间的相关系数[n, m] = size(rbarStocks);Cor = zeros(m, 1);for j=1:mA = cov(rbarStocks(:, j)', e(:, j));if j>=2CovER = [CovER; A(1, 2)] ;elseCovER = A(1, 2);endendfor j=1:mCor(j, 1) = corr(rbarStocks(:, j), e(:, j));end
end

t t t分布拟合序列

The prediction of scenarios coherent with the t − t- tlocation scale distribution, without using CAPM.

%% t-分布拟合
rbar_tloc = minux_t_fit(returns);
sample_ret = returns(:, 1);A = fitdist(sample_ret, 'tLocationScale');
xmin = min(sample_ret)-0.01;
xmax = max(sample_ret)+0.01;xp = xmin: 0.01:xmax;
hold on;
yyaxis left; % 激活左轴
yp = pdf(A, xp);
plot(xp, yp, 'b--');
yyaxis right; % 激活右轴
histogram(sample_ret, 'Normalization', 'probability', 'FaceAlpha', 0.4);
legend('t-fitted', 'returns');
hold off;

拟合函数为

function [rbarStocks_tloc] = minux_t_fit(rStocks)[n, m]=size(rStocks);rbarStocks_tloc = zeros(n, m);for j = 1:mA = fitdist(rStocks(:, j), 'tLocationScale'); % 将收益率序列使用t-dist拟合for i=1:nrbarStocks_tloc(i, j)=A.mu+A.sigma*trnd(A.nu);endend
end

t t t拟合后在双坐标轴上绘制pdf图像,可以发现重合度较高,可以描述收益率尖峰厚尾的现象

fit

对比使用正态分布的拟合图像如下
t-normal

Generalized Hyperbolic Distribution

广义双曲线分布(GH)的概率密度函数为
d G H ( λ , α , β , δ , μ ) ( x ) = a ( λ , α , β , δ , μ ) ( δ 2 + ( x − μ ) 2 ) ( 2 λ − 1 4 ) e β ( x − μ ) × K λ − 1 2 ( α δ 2 + ( x − μ ) 2 ) d_{{GH}(\lambda, \alpha, \beta, \delta, \mu)}(x)=a(\lambda, \alpha, \beta, \delta, \mu)(\delta^2+(x-\mu)^2)^{(\frac{2\lambda-1}{4})}e^{\beta(x-\mu)}\times K_{\lambda-\frac{1}{2}}(\alpha\sqrt{\delta^2+(x-\mu)^2}) dGH(λ,α,β,δ,μ)(x)=a(λ,α,β,δ,μ)(δ2+(xμ)2)(42λ1)eβ(xμ)×Kλ21(αδ2+(xμ)2 )
其中, α > 0 \alpha>0 α>0为形状参数,偏度由 β \beta β的绝对值确定,且 0 ≤ ∣ β ∣ < α 0\leq |\beta|<\alpha 0β<α μ ∈ R \mu\in R μR为位置参数, λ ∈ R \lambda\in R λR刻画尾部形状, δ > 0 \delta>0 δ>0描述尺度参数,正则化函数 a ( λ , α , β , δ , μ ) a(\lambda, \alpha, \beta, \delta, \mu) a(λ,α,β,δ,μ)
a ( λ , α , β , δ , μ ) = ( α 2 − β 2 ) λ 2 2 π α ( λ − 1 2 ) δ λ K λ ( δ α 2 − β 2 ) a(\lambda, \alpha, \beta, \delta, \mu)=\frac{(\alpha^2-\beta^2)^{\frac{\lambda}{2}}}{\sqrt{2\pi}\alpha^{(\lambda-\frac{1}{2})}\delta^\lambda K_\lambda(\delta\sqrt{\alpha^2-\beta^2})} a(λ,α,β,δ,μ)=2π α(λ21)δλKλ(δα2β2 )(α2β2)2λ
其中函数 K λ K_\lambda Kλ表示第三种modified Bessel function with index λ \lambda λ.

GH分布依赖于Generalized Inverse Gaussian Distribution (GIG),设 δ α 2 + β 2 = ζ \delta\sqrt{\alpha^2+\beta^2}=\zeta δα2+β2 =ζGH的期望值为
E ( G H ) = μ + β δ 2 ζ K λ + 1 ( ζ ) K λ ( ζ ) = μ + β E ( G I G ) \mathbb{E}(GH)=\mu+\frac{\beta \delta^2}{\zeta}\frac{K_{\lambda+1}(\zeta)}{K_\lambda(\zeta)}=\mu+\beta\mathbb{E}(GIG) E(GH)=μ+ζβδ2Kλ(ζ)Kλ+1(ζ)=μ+βE(GIG)

方差为
V ( G H ) = δ 2 ζ K λ + 1 ( ζ ) K λ ( ζ ) + β 2 δ 4 ζ 2 ( K λ + 2 ( ζ ) K λ ( ζ ) − K λ + 1 2 ( ζ ) K λ 2 ( ζ ) ) = E ( G I G ) + β 2 V ( G I G ) \mathbb{V}(GH)=\frac{\delta^2}{\zeta}\frac{K_{\lambda+1}(\zeta)}{K_\lambda(\zeta)}+\beta^2\frac{\delta^4}{\zeta^2}(\frac{K_{\lambda+2}(\zeta)}{K_\lambda(\zeta)}-\frac{K_{\lambda+1}^2(\zeta)}{K_\lambda^2(\zeta)})=\mathbb{E}(GIG)+\beta^2\mathbb{V}(GIG) V(GH)=ζδ2Kλ(ζ)Kλ+1(ζ)+β2ζ2δ4(Kλ(ζ)Kλ+2(ζ)Kλ2(ζ)Kλ+12(ζ))=E(GIG)+β2V(GIG)

approximation method 1

构造一个严格递增的函数 h ( x ; μ ) = F G H ( x ) − μ h(x; \mu)=F_{GH}(x)-\mu h(x;μ)=FGH(x)μ,其中 u u u是从 ( 0 , 1 ) (0, 1) (0,1)均匀分布中的随机抽样,可以得到数值解
h ( x ; u ) = ∫ ∞ x d G H ( λ , α , β , δ , μ ) ( x ) d x − μ h(x; u)=\int_\infty^xd_{GH(\lambda, \alpha, \beta, \delta, \mu)}(x)dx-\mu h(x;u)=xdGH(λ,α,β,δ,μ)(x)dxμ

approximation method 2

根据Newton-Raphson算法近似
{ x 1 start point x k + 1 = x k − F G H ( x k ) − μ d G H ( λ , α , β , δ , μ ) \begin{cases} x_1\quad\text{start point}\\ x_{k+1}=x_k-\frac{F_{GH}(x_k)-\mu}{d_{GH(\lambda, \alpha, \beta, \delta, \mu)}} \end{cases} {x1start pointxk+1=xkdGH(λ,α,β,δ,μ)FGH(xk)μ

Goodness of Fit

Anderson & Darling Test

Anderson & Darling test检测任意分布的公式为
A D = max ⁡ x ∈ R ∣ F e m p ( x ) − F e s t ( x ) ∣ F e s t ( x ) ( 1 − F e s t ( x ) ) AD = \max\limits_{x\in R}\frac{|F_{emp}(x)-F_{est}(x)|}{\sqrt{F_{est}(x)(1-F_{est}(x))}} AD=xRmaxFest(x)(1Fest(x)) Femp(x)Fest(x)
其中 F e m p ( x ) F_{emp}(x) Femp(x)表示empirical CDF F e s t ( x ) F_{est}(x) Fest(x)表示estimated CDF

Kolmogorv Distance

CDF函数差值的绝对值的上界
K D = sup ⁡ x ∈ R ∣ F e m p ( x ) − F e s t ( x ) ∣ KD=\sup_{x\in R}|F_{emp}(x)-F_{est}(x)| KD=xRsupFemp(x)Fest(x)

L1 Distance

L 1 = ∑ i ∣ F e m p ( x i ) − F e s t ( x i ) ∣ L_1=\sum_i|F_{emp}(x_i)-F_{est}(x_i)| L1=iFemp(xi)Fest(xi)

L2 Distance

L 2 = ∑ i ∣ F e m p ( x i ) − F e s t ( x i ) ∣ 2 L_2=\sqrt{\sum_i|F_{emp}(x_i)-F_{est}(x_i)|^2} L2=iFemp(xi)Fest(xi)2

References

optimization of Conditional Value-at-Risk


  1. Location-Scale Distributions ↩︎


http://chatgpt.dhexx.cn/article/lYnrCxBl.shtml

相关文章

【概率论与数理统计】python实验

实验一抛硬币试验的模拟 利用python产生一系列0和1的随机数&#xff0c;模拟抛硬币试验。验证抛一枚质地均匀的硬币&#xff0c;正面向上的频率的稳定值为0.5。 实验步骤 &#xff08;1&#xff09;生成0和1的随机数序列&#xff0c;将其放入列表count中&#xff1b;也可用函数…

matlab 数理统计,概率论和数理统计(matlab应用)1

概率论和数理统计(matlab应用)1 (2006-04-29 08:53:49) 12.1 概 述 自然界和社会上会发生各种各样的现象&#xff0c;其中有的现象在一定条件下是一定要发生的&#xff0c;有的则表现出一定的随机性&#xff0c;但总体上又有一定的规律可循。一般称前者为确定性事件&#xff0c…

正态分布中normpdf和normcdf的区别

同时&#xff0c;也可以扩展为其它函数的区别&#xff0c;即pdf与cdf

matlab中normcdf函数用法,Matlab中标准正态分布的密度函数是normcdf(x,0,1)

中标准正属于项目资本现金流量表中现金流出构成的是() 态分USCI模块中的波特率由分频器和调制器共同作用生成。 在MSP430F66xx时钟设置中XT1的XIN和XOUT引脚接32768Hz低频晶振&#xff0c;密度则UCSCTL6的UCSCTL6 & ~XT1OFF表示使能XT1&#xff0c;UCSCTL6 | XCAP_3表示选择…

matlab中normcdf和normpdf区别

最近准备参加个数学建模&#xff0c;其中校赛题目是有关于正态分布的排队论&#xff0c;在做的时候总是发现有问题&#xff0c;后来仔细研究才发现在matlab中关于正态分布有两个函数normcdf和normpdf。

【Matlab】正态分布常用函数normpdf_normcdf_norminv_normrnd_normfit

1.normpdf 功能&#xff1a;正态分布概率密度函数 用法 Y normpdf(X,mu,sigma) Y normpdf(X) % (mu 0, sigma 1) Y normpdf(X,mu) % (sigma 1)例子 % code1 % 画标准正态分布概率密度函数 x -10:0.01:10; y normpdf(x, 0, 1); plot(x,y); grid on;结果&#xff1a…

firefox iMacros

iMacros是一个基于firefox的插件&#xff0c;它可以帮助你自动填写表单&#xff0c;自动提交&#xff0c;自动上传下载等 iMacros就是一个可以帮助你实现自动化重复性的任务。无论你对Firefox做什么&#xff0c;iMacros可以自动执行它。网络设计人员可以使用的功能测试和回归测…

SEO必备的利器–iMacros

Via http://www.snailtoday.com/2012/02/imacros/ 今天向大家介绍一个做SEO必备的利器–iMacros&#xff0c;我们可以用它来自动发布博客评论、自动注册论坛账号、自动发布文章等&#xff0c;几乎可以和一些SEO软件相媲美。其实在之前翻译的一篇文章《10000美元/天是这样炼成的…

imacros:使用外部编辑器

2019独角兽企业重金招聘Python工程师标准>>> 在imacros中&#xff0c;右击iim文件&#xff0c;选择编辑宏&#xff0c;会调用imacros自带的编辑器进行编辑。 其实也可以自定义&#xff1a; 在Paths选项卡中选择外部编辑器&#xff1a; 转载于:https://my.oschina.ne…

FF的插件iMacros简单交流

今天组织了公司第一次软件测试沙龙&#xff0c;主题是自动化测试插件--iMacros&#xff0c;公司的一位同事分享了他关于iMacros使用的一些心得给我们分享&#xff0c;现小小总结一下&#xff1a; iMacros是一款功能比较强的FF插件&#xff08;当然也有IE和chrome的版本&#xf…

imacros:初次尝试

2019独角兽企业重金招聘Python工程师标准>>> 安装 打开火狐插件管理页面&#xff0c;搜索、安装&#xff1a; 当然&#xff0c;用的免费版本。 安装后&#xff0c;工具栏出现&#xff1a; 示例 VERSION BUILD9030808 RECORDERFX TAB T1 TAB CLOSEALLOTHERS URL GOT…

imacros:运行javascript

2019独角兽企业重金招聘Python工程师标准>>> 将#Current.iim重命名为js后缀的文件&#xff0c;例如test.js。注意&#xff0c;每次录制时都会有新的#Current.iim产生&#xff0c;若无则会新建&#xff0c;有则覆盖内容。 运行test.js&#xff1a; 这里的js有较多的限…

如何使用iMacros进行web程序中页面加载的性能测试

iMacros 的官方简介如下&#xff0c;这里就不做翻译了&#xff0c;本人觉得还是看原汁原味的介绍好。 iMacros is an extension for the Mozilla Firefox web browsers which adds record and replay functionality similar to that found in web testing and form filler sof…

火狐浏览器Firefox 如何使用iMacros 自动填写网页表单

1 我们首先访问一个想要自动填写表单的网站。我们以百度为例&#xff0c;右侧有登录窗口。 2 然后我们点开刚安装上的iMacros插件&#xff0c;一般安装之后就会自动出现在浏览器的某个地方&#xff0c;点击记录选项卡&#xff0c;再点击记录。 3 iMacros将立即开始记录宏命令…

如何使用iMacros自动化重复的网页浏览器任务

如何使用iMacros自动化重复的网页浏览器任务 计算机应该自动重复任务- 如果你发现自己提交或者重复地导航网站&#xff0c;试试 iMacros 。 是 easy-to-use - 你只需要做一次动作。 iMacros对于任何在web浏览器中执行重复任务的人都是理想的&#xff0c;无论是否重复提交复杂的…

使用 iMacros 来自动化日常的工作

不管在哪一个行业&#xff0c;测试已经成为制造&#xff0c;开发流程中&#xff0c;一个必要的环节。特别是指软件行业。 近年来由于 Web Application 的开发&#xff0c;是市场上热门的显学&#xff0c;虽然不到汉武帝“独尊儒术”的一统天下&#xff0c;但是根据有效的问卷调…

iMacros使用技巧

众所周知&#xff0c;iMacros是一款优秀的Chrome插件&#xff0c;可以方便的录制、运行浏览器宏命令&#xff0c;是抢课截胡的神器&#xff08;手动滑稽&#xff09;&#xff0c;在此简单记录一下它的使用技巧。 技巧一&#xff1a;合理使用WAIT SECONDS命令 WAIT SECONDS 等待…

imacros

为什么80%的码农都做不了架构师&#xff1f;>>> 软件测试 imacros Firefox火狐扩展iMacro脚本入门教程 http://macrotea.iteye.com/blog/1702554 转载于:https://my.oschina.net/lsjun/blog/335799

imacros自动跑代码_如何使用iMacros自动执行重复的Web浏览器任务

imacros自动跑代码 Computers are supposed to automate repetitive tasks – if you find yourself submitting forms over and over or repeatedly navigating a website by hand, try iMacros. It’s easy-to-use – all you have to do is perform an action once. 计算机应…

串联系统与并联系统可靠性计算

串联系统 每个环节都必须能够正常工作 所以串联系统的可靠度就是 R0的正常率 * R1的正常率 * R2的正常率 * R3的正常率 一直到结束 并联系统 仅当所有子系统都失效的时候该并联系统才会失效&#xff0c;所以改并联系统的失效率或者可靠性很好计算&#xff0c;就是用1 - 失效率…