Finding parameters values given the solution with two non-linear equations in R -
i have 2 equations describe moments (mean , variance) of truncated gaussian distribution. built following function compute them:
trunc_moments <- function(moments, a, b){ require(truncnorm) mu <- moments[1] sigma <- moments[2] alpha <- (a - mu) / sigma; beta <- (b - mu) / sigma mu_trunc <- mu + sigma* ((dnorm(alpha, mu, sigma) - dnorm(beta, mu, sigma)) / (pnorm(beta, mu, sigma) - pnorm(alpha, mu, sigma))) sigma_trunc <- sigma^2 * (1 + ((alpha*dnorm(alpha, mu, sigma) - beta*dnorm(beta, mu, sigma)) / (pnorm(beta, mu, sigma) - pnorm(alpha, mu, sigma))) - ((dnorm(alpha, mu, sigma) - dnorm(beta, mu, sigma)) / (pnorm(beta, mu, sigma) - pnorm(alpha, mu, sigma)))^2) return(c(mu_trunc, sigma_trunc)) }
given mu
, sigma
, function returns mu_trunc
, sigma_trunc
.
trunc_moments(c(0.25, 0.02), a=0, b=1)
now, reverse results: given function, mu_trunc
, sigma_trunc
, can obtain values of mu
, sigma
?
i tried nleqslv r package not sure looking for.
library(nleqslv) nleqslv(c(0.25, 0.0004), trunc_moments, = 0, b = 1)$x
you need write function takes output of trunc_moments
, calculates required input trunc_moments
. can so:
trunc_inverse <- function(minput,a,b) { temp <- trunc_moments(minput,a,b) y <- c(0.25,0.02) - temp y }
minput
result of trunc_moments
given input passed in argument moments
function trunc_moments
. return value of trunc_inverse
after solving should original input. there problem.
if this:
nleqslv(c(0.25, 0.0004), trunc_inverse, a=0, b=1)
a part of output is
$x [1] 0.2500000 0.1414214
which not original input.
if insert these values in call of trunc_moments
this
trunc_moments(c(0.25,0.1414214),a=0,b=1)
the result is
[1] 0.25000000 0.02000001
so original function give same output different combinations of moments
input. implies function trunc_moments
give same output different inputs.
you have work do.
Comments
Post a Comment