如何使用符号数学定义累积正态分布 [英] How to define cumulative normal distribution using symbolic math

查看:207
本文介绍了如何使用符号数学定义累积正态分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有以下代码

I have the following code in MATLAB

x=sym('x',[1 2]);
DV=x(1)*ED+x(2);
sv=x(1)*DV;
DD=DV./sv;
p=normcdf(-DD);

其中DV和ED均为13242 x 1矢量.直到DD都可以.当我如上所述定义p时,我收到以下消息:

where DV and ED are both 13242 x 1 vectors. Up to DD all it is ok. When i define p as above, i obtain this message:

使用symfun> validateArgNames出错(第205行)

Error using symfun>validateArgNames (line 205)

第二个输入必须是唯一符号变量的标量或向量.

Second input must be a scalar or vector of unique symbolic variables.

当我定义p=1./(1+exp(-DD))时,一切正常.因此normcdf存在问题.

When i define p=1./(1+exp(-DD)) all it is ok. So there is a problem with normcdf.

有什么主意吗?

致谢

推荐答案

normcdf与统计"工具箱中的大多数功能一样,不支持符号输入.文档中没有对此进行明确说明,并且我同意错误消息极其无用(您可能与我们联系MathWorks并对此提出服务请求,以建议他们在统计信息"工具箱中添加对符号数学的支持.

normcdf, like most of the functions in the Statistics toolbox, does not support symbolic input. The documentation does not make this clear and I agree that the error message is extremely useless (you might contact MathWorks and file a service request about this to suggest that they add support for symbolic math to the Statistics toolbox).

normcdf函数没有任何神奇的作用.您可以使用p = 0.5*erfc(DD./sqrt(2))代替p = normcdf(-DD).这也将更快.在命令中键入edit normcdf以查看该功能的代码.有很多错误检查和特定于浮点的情况,这就是为什么符号输入会导致函数错误的原因.

The normcdf function doesn't do anything magical. You can use p = 0.5*erfc(DD./sqrt(2)) in place of p = normcdf(-DD). This will also be faster. Type edit normcdf in your command to see the code for the function. There's lots of error checking and cases specific to floating-point which is why the function errors with symbolic inputs.

另一种选择是使用MuPAD的 stats::normalCDF 在Matlab中(仅在最新版本中支持此功能).例如:

Another option is to use MuPAD's stats::normalCDF from within Matlab (this function might only be supported in recent releases). For example:

x = sym('x',[1 2]);
ED = ones(1,3);
DV = x(1)*ED+x(2);
sv = x(1)*DV;
DD = DV./sv;
DDstr = char(-DD);
p = evalin(symengine, ['f:=stats::normalCDF(0,1):map(' DDstr(9:end-2) ',x->f(x))'])

其中f定义了实现符号正常CDF的过程平均数为0,方差为1.还使用了MuPAD的 map 函数将其向量化.总而言之,此选项可能不是必需的.

where f defines a procedure implementing a symbolic normal CDF with mean 0 and variance 1. MuPAD's map function is also used to vectorize this. All in all, this option is probably not necessary.

这篇关于如何使用符号数学定义累积正态分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆