olmjo / rcpptn Goto Github PK
View Code? Open in Web Editor NEWRcpp-based Truncated Normal RNG
Home Page: olmjo.com/computing/RcppTN/
Rcpp-based Truncated Normal RNG
Home Page: olmjo.com/computing/RcppTN/
See the CRAN check page for your package and §1.6 of the manual. Standard math functions such as log and sqrt are not defined for integer arguments in C++.
Some of these are long-standing: please correct ASAP and by Dec 1 if you want to retain the package on CRAN. Make sure you check for similar occurrences and other issues shown on the CRAN results page before submitting a corrected update.
vtn(a=0, Inf, -8.1, 1)
etn(a=0, Inf, -8, 1)
Thanks for the package, I've found it's really useful!
I ran into overflow/underflow problems, and was able to re-implement them on the log scale, which fixed this problem. I thought I'd share it, in case this is a problem others have run into:
I did not do this using the truncated above and below, only for the separate ones, since those were the only two I'm using. Here's the c++ code:
`
using namespace Rcpp ;
/// No Truncation
double e0 (const double mean,
const double sd,
const double low,
const double high
) {
return(mean) ;
}
/// Truncated Below and Above
double e1 (const double mean,
const double sd,
const double low,
const double high
) {
double s_low = (low - mean) / sd ;
double s_high = (high - mean) / sd ;
double q1 = R::dnorm(s_low, 0.0, 1.0, false) ;
double q2 = R::dnorm(s_high, 0.0, 1.0, false) ;
double q3 = R::pnorm(s_low, 0.0, 1.0, true, false) ;
double q4 = R::pnorm(s_high, 0.0, 1.0, true, false) ;
return(mean + sd * ((q1 - q2) / (q4 - q3))) ;
}
/// Truncated Below
double e2 (const double mean,
const double sd,
const double low
) {
double s_low = (low - mean) / sd ;
double q1 = R::dnorm(s_low, 0.0, 1.0, true) ;
double q3 = R::pnorm(s_low, 0.0, 1.0, true, true) ;
return(mean + sd * exp((q1) - (1 - q3))) ;
}
/// Truncated Above
double e3 (const double mean,
const double sd,
const double high
) {
double s_high = (high - mean) / sd ;
double q2 = R::dnorm(s_high, 0.0, 1.0, false) ;
double q4 = R::pnorm(s_high, 0.0, 1.0, true, false) ;
return( mean - sd * exp(q2 - q4)) ;
}
/// Main Function
double etn2(const double mean,
const double sd,
const double low,
const double high
) {
// Init Useful Values
double out = NA_REAL ;
//
if (low == R_NegInf &&
high == R_PosInf
) {
// No Truncation
out = e0(mean, sd, low, high) ;
} else if (low == R_NegInf) {
// Truncated Above
out = e3(mean, sd, high) ;
} else if (high == R_PosInf) {
// Truncation Below
out = e2(mean, sd, low) ;
} else {
// Truncation Above and Below
out = e1(mean, sd, low, high) ;
}
// Check if Mirror Problem is Numerically Better
if (!std::isfinite(out)) {
return(-e1(-mean, sd, -high, -low)) ;
}
//
return(out) ;
}`
RcppTN/vignettes/framed.sty
Non-required packages must be used conditionally: see §1.1.3.1 of the manual.
For RcppTN this applies to microbenchmark, which only works on a few platforms.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.