- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 R 计算许多不同 alpha 和 beta 的 beta 分布的特征函数;不幸的是,我遇到了数值问题。
首先我使用的是包 CharFun
和函数 cfX_Beta(t, alpha, beta)
这在大多数情况下似乎工作正常,但例如对于 alpha=121.3618
和 beta=5041.483
它完全失败(见下面的例子,Re(cfX_Beta(t, alpha, beta))
和 Im(cfX_Beta(t, alpha, beta))
应该总是在 [-1,1] 区间内,但事实并非如此)。
然后我决定通过积分获得特征函数。此方法为 alpha=121.3618
提供了可信的结果和 beta=5041.483
但对于其他组合,集成失败。 (错误:“积分可能发散”)。增加 rel.tol
因为积分也没有帮助。相反,对于 alpha 和 beta 的其他值,我会得到错误:“检测到舍入错误”。
所以我的问题是:
对于 alpha 和 beta 的所有可能组合,是否有另一种方法可以获得 beta 分布特征函数的可靠结果?
我犯了什么明显的错误吗?
library(CharFun)
abc<-function(x,t,a,b) {
return( dbeta(x,a,b)*cos(t*x))
}
dfg<-function(x,t,a,b) {
return( dbeta(x,a,b)*sin(t*x))
}
hij<-function(t,a,b) {
intRe=rep(0,length(t))
intIm=rep(0,length(t))
i<-complex(1,0,1)
for (j in 1:length(t)) {
intRe[j]<-integrate(abc,lower=0,upper=1,t[j],a,b)$value
intIm[j]<-integrate(dfg,lower=0,upper=1,t[j],a,b)$value
}
return(intRe+intIm*i)
}
alpha<-1
beta<-1
t <- seq(-100, 100, length.out = 501)
par(mfrow=c(3,2))
alpha<-1
beta<-1
plotGraf(function(t)
hij(t, alpha, beta), t, title = "CF alpha=1
beta=1")
plotGraf(function(t)
cfX_Beta(t, alpha, beta), t, title = "CF Charfun alpha=1
beta=1")
alpha<-121.3618
beta<-5041.483
plotGraf(function(t)
hij(t, alpha, beta), t, title = "CF alpha=121.3618 beta=5041.483")
plotGraf(function(t)
cfX_Beta(t, alpha, beta), t, title = "CF Charfun alpha=121.3618 beta=5041.483")
alpha<-1
beta<-1/2
plotGraf(function(t)
hij(t, alpha, beta), t, title = "CF alpha=1
beta=1/2")
plotGraf(function(t)
cfX_Beta(t, alpha, beta), t, title = "CF Charfun alpha=1
beta=1/2")
alpha=beta=1
两种方法都提供相同的结果,
cfX_Beta(t, alpha, beta)
为
alpha=121.3618
疯狂和
beta=5041.483
整合的结果似乎是合理的。对于
alpha=1
和
beta=1/2
集成失败。
最佳答案
它似乎适用于 RcppNumerical
, 在不使用太小的公差的条件下 (1e-4
下面)。
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::depends(RcppNumerical)]]
#include <RcppNumerical.h>
using namespace Numer;
class BetaCDF_Re: public Func
{
private:
double a;
double b;
double t;
public:
BetaCDF_Re(double a_, double b_, double t_) : a(a_), b(b_), t(t_){}
double operator()(const double& x) const
{
return R::dbeta(x, a, b, 0) * cos(t*x);
}
};
class BetaCDF_Im: public Func
{
private:
double a;
double b;
double t;
public:
BetaCDF_Im(double a_, double b_, double t_) : a(a_), b(b_), t(t_) {}
double operator()(const double& x) const
{
return R::dbeta(x, a, b, 0) * sin(t*x);
}
};
// [[Rcpp::export]]
Rcpp::List integrate_test(double a, double b, double t)
{
BetaCDF_Re f1(a, b, t);
double err_est1;
int err_code1;
const double res1 = integrate(f1, 0, 1, err_est1, err_code1,
100, 1e-4, 1e-4,
Integrator<double>::GaussKronrod201);
BetaCDF_Im f2(a, b, t);
double err_est2;
int err_code2;
const double res2 = integrate(f2, 0, 1, err_est2, err_code2,
100, 1e-4, 1e-4,
Integrator<double>::GaussKronrod201);
return Rcpp::List::create(
Rcpp::Named("realPart") =
Rcpp::List::create(
Rcpp::Named("value") = res1,
Rcpp::Named("error_estimate") = err_est1,
Rcpp::Named("error_code") = err_code1
),
Rcpp::Named("imPart") =
Rcpp::List::create(
Rcpp::Named("value") = res2,
Rcpp::Named("error_estimate") = err_est2,
Rcpp::Named("error_code") = err_code2
)
);
}
> integrate_test(1, 0.5, 1)
$realPart
$realPart$value
[1] 0.7497983
$realPart$error_estimate
[1] 7.110548e-07
$realPart$error_code
[1] 0
$imPart
$imPart$value
[1] 0.5934922
$imPart$error_estimate
[1] 5.54721e-07
$imPart$error_code
[1] 0
t <- seq(-100, 100, length.out = 501)
x <- lapply(t, function(t) integrate_test(1,0.5,t))
realparts <- unlist(purrr::transpose(purrr::transpose(x)$realPart)$value)
imparts <- unlist(purrr::transpose(purrr::transpose(x)$imPart)$value)
plot(t, realparts, type="l", col="blue", ylim=c(-1,1))
lines(t, imparts, type="l", col="red")
关于r - Beta 分布的特征函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47621843/
设置 我希望能够定义一个特征,使得任何实现该特征的结构不仅必须实现函数,而且还必须为某些常量指定值。所以也许是这样的: trait MyTrait { const MY_CONST: u8;
在我的 Web 应用程序中,授权用户至少有 4 个“方面”:http session 相关数据、持久数据、facebook 数据、运行时业务数据。 我决定使用案例类组合而不是特征至少有两个原因: 性状
我正在尝试使用以下代码从类中获取完整数据成员的列表: import std.stdio; import std.traits; class D { static string[] integr
我正在尝试实现 From对于我的一种类型。它应该消耗任意长度的行(仅在运行时已知)并从行中获取数据。编译器提示 &[&str; 2]不是 &[&str] ,即它不能将固定大小的切片转换为任意长度的切片
有人可以请你这么好心,并指出一种提取拟合树中使用的列/特征的方法,使用如下代码: library(dplyr) library(caret) library(rpart) df % dplyr
假设我定义了一个 Group所有组操作的特征。是否可以创建一个包装器AGroup超过 Group无需手动派生所有操作? 基本上,我想要这个: #[derive (Copy, Debug, Clone,
最近浏览了Markus Stocker的博客他很好地解释了如何在使用 observation 时表示传感器观察结果。 SSN 的模块本体论。我完全理解他的解释,但我发现有一件事多余地代表了一个的两个特
我有以下情况/代码; trait Model { def myField: String } case class MyModel(myField: String) extends Model
我想让一个案例类扩展一个特征 以下是我的要求: 我需要为 child 使用案例类。这是一个硬性要求,因为 scopt ( https://github.com/scopt/scopt ) parent
最近浏览了Markus Stocker的博客他很好地解释了如何在使用 observation 时表示传感器观察结果。 SSN 的模块本体论。我完全理解他的解释,但我发现有一件事多余地代表了一个的两个特
我有以下情况/代码; trait Model { def myField: String } case class MyModel(myField: String) extends Model
不确定标题是否完全有意义,对此感到抱歉。我是机器学习新手,正在使用 Scikit 和决策树。 这就是我想做的;我想获取所有输入并包含一个独特的功能,即客户端 ID。现在,客户端 ID 是唯一的,无法以
我想读取具有 Eigen 的 MNIST 数据集,每个文件都由一个矩阵表示。我希望在运行时确定矩阵大小,因为训练集和测试集的大小不同。 Map> MNIST_dataset((uchar*)*_dat
在 MATLAB 中,我可以选择一个分散的子矩阵,例如: A = [1 ,2 ,3;4,5,6;7,8,9] A([1,3],[1,3]) = [1,3;7,9] 有没有用 Eigen 做到这一点的聪
我在执行 Into 时遇到问题Rust 中通用结构的特征。下面是我正在尝试做的简化版本: struct Wrapper { value: T } impl Into for Wrapper {
我有这段 matlab 代码,我想用 Eigen 编写: [V_K,D_K] = eig(K); d_k = diag(D_K); ind_k = find(d_k > 1e-8); d_k(ind_
我正在使用 Eigen C++ 矩阵库,我想获取对矩阵列的引用。文档说要使用 matrix_object.col(index),但这似乎返回了一个表示列的对象,而不是简单地引用原始矩阵对象中的列。我担
在乘以很多旋转矩阵之后,由于舍入问题(去正交化),最终结果可能不再是有效的旋转矩阵 重新正交化的一种方法是遵循以下步骤: 将旋转矩阵转换为轴角表示法 ( link ) 将轴角转换回旋转矩阵 ( lin
定义可由命名空间中的多个类使用的常量的最佳方法是什么?我试图避免太多的继承,所以扩展基类不是一个理想的解决方案,我正在努力寻找一个使用特征的好的解决方案。这在 PHP 5.4 中是可行的还是应该采用不
定义可由命名空间中的多个类使用的常量的最佳方法是什么?我试图避免太多的继承,所以扩展基类不是一个理想的解决方案,我正在努力寻找一个使用特征的好的解决方案。这在 PHP 5.4 中是可行的还是应该采用不
我是一名优秀的程序员,十分优秀!