如何用符号数学定义累积正态分布

时间:2022-02-28 00:32:46

I have the following code in MATLAB

我在MATLAB中有以下代码

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

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:

DV和ED都是13242 x 1向量。到DD为止一切正常。当我将p定义为上述时,我得到以下信息:

Error using symfun>validateArgNames (line 205)

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

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

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

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

当我定义p=1./(1+exp(-DD))时,一切正常。所以normcdf有个问题。

Any idea?

任何想法?

Regards

问候

3 个解决方案

#1


0  

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不支持符号输入。文档没有说明这一点,我同意错误消息是非常无用的(您可以联系MathWorks,并为此提交一个服务请求,建议他们向统计工具箱添加对符号数学的支持)。

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.

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

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

另一种选择是使用MuPAD的stats::normalCDF从Matlab中获取(这个函数可能只在最近的版本中支持)。例如:

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))'])

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.

其中f定义了一个实现均值为0,方差为1的符号正规CDF的过程。MuPAD的映射函数也用于向量化。总而言之,这个选项可能是不必要的。

#2


0  

From the MATLAB help of normcdf

从MATLAB对normcdf的帮助。

x can be a vector, matrix, or multidimensional array.

x可以是向量、矩阵或多维数组。

I.e., it cannot be a symbolic variable.

即。,它不能是一个符号变量。

Try

试一试

syms y;
p=normcdf(y)

Doesn't work. Since DD is a symbolic variable, it will not work. Why would you want a vector of a few thousand symbolic values anyway? Wouldn't it be more efficient to run the script for a specific, non-symbolic value for x?

是行不通的。由于DD是一个符号变量,所以它不能工作。为什么你想要一个有几千个符号值的向量呢?为特定的、非符号的x值运行脚本不是更有效吗?

#3


0  

I figure out how to it. Instead of using the command normcdf, i must define the equation of the cumulative density function as it is, where X=-DD

我想办法。我必须定义累积密度函数的方程,而不是使用normcdf命令,其中X=-DD

#1


0  

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不支持符号输入。文档没有说明这一点,我同意错误消息是非常无用的(您可以联系MathWorks,并为此提交一个服务请求,建议他们向统计工具箱添加对符号数学的支持)。

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.

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

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

另一种选择是使用MuPAD的stats::normalCDF从Matlab中获取(这个函数可能只在最近的版本中支持)。例如:

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))'])

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.

其中f定义了一个实现均值为0,方差为1的符号正规CDF的过程。MuPAD的映射函数也用于向量化。总而言之,这个选项可能是不必要的。

#2


0  

From the MATLAB help of normcdf

从MATLAB对normcdf的帮助。

x can be a vector, matrix, or multidimensional array.

x可以是向量、矩阵或多维数组。

I.e., it cannot be a symbolic variable.

即。,它不能是一个符号变量。

Try

试一试

syms y;
p=normcdf(y)

Doesn't work. Since DD is a symbolic variable, it will not work. Why would you want a vector of a few thousand symbolic values anyway? Wouldn't it be more efficient to run the script for a specific, non-symbolic value for x?

是行不通的。由于DD是一个符号变量,所以它不能工作。为什么你想要一个有几千个符号值的向量呢?为特定的、非符号的x值运行脚本不是更有效吗?

#3


0  

I figure out how to it. Instead of using the command normcdf, i must define the equation of the cumulative density function as it is, where X=-DD

我想办法。我必须定义累积密度函数的方程,而不是使用normcdf命令,其中X=-DD