Last chance! 50% off unlimited learning
Sale ends in
Approximation of truncated, marginal and conditional densities, moments and cumulative probabilities of multivariate distributions via Hermite polynomial based approach proposed by Gallant and Nychka in 1987.
Density approximating function is scale adjusted product of two terms.
The first one is squared multivariate polynomial of pol_degrees
degrees with pol_coefficients
coefficients vector.
The second is product of independent normal random variables' densities with
expected values and standard deviations given by mean
and sd
vectors correspondingly. Approximating function satisfies properties of
density function thus generating a broad family of distributions.
Characteristics of these distributions
(moments, quantiles, probabilities and so on)
may provide accurate approximations to characteristic of other
distributions. Moreover it is usually possible to provide arbitrary close
approximation by the means of polynomial degrees increase.
dhpa(
x,
pol_coefficients = numeric(0),
pol_degrees = numeric(0),
given_ind = logical(0),
omit_ind = logical(0),
mean = numeric(0),
sd = numeric(0),
is_parallel = FALSE,
log = FALSE,
is_validation = TRUE
)phpa(
x,
pol_coefficients = numeric(0),
pol_degrees = numeric(0),
given_ind = logical(0),
omit_ind = logical(0),
mean = numeric(0),
sd = numeric(0),
is_parallel = FALSE,
log = FALSE,
is_validation = TRUE
)
ihpa(
x_lower = matrix(1, 1),
x_upper = matrix(1, 1),
pol_coefficients = numeric(0),
pol_degrees = numeric(0),
given_ind = logical(0),
omit_ind = logical(0),
mean = numeric(0),
sd = numeric(0),
is_parallel = FALSE,
log = FALSE,
is_validation = TRUE
)
ehpa(
x = matrix(1, 1),
pol_coefficients = numeric(0),
pol_degrees = numeric(0),
given_ind = logical(0),
omit_ind = logical(0),
mean = numeric(0),
sd = numeric(0),
expectation_powers = numeric(0),
is_parallel = FALSE,
is_validation = TRUE
)
etrhpa(
tr_left = matrix(1, 1),
tr_right = matrix(1, 1),
pol_coefficients = numeric(0),
pol_degrees = numeric(0),
mean = numeric(0),
sd = numeric(0),
expectation_powers = numeric(0),
is_parallel = FALSE,
is_validation = TRUE
)
dtrhpa(
x,
tr_left = matrix(),
tr_right = matrix(),
pol_coefficients = numeric(0),
pol_degrees = numeric(0),
given_ind = logical(0),
omit_ind = logical(0),
mean = numeric(0),
sd = numeric(0),
is_parallel = FALSE,
log = FALSE,
is_validation = TRUE
)
itrhpa(
x_lower = matrix(1, 1),
x_upper = matrix(1, 1),
tr_left = matrix(1, 1),
tr_right = matrix(1, 1),
pol_coefficients = numeric(0),
pol_degrees = numeric(0),
given_ind = logical(0),
omit_ind = logical(0),
mean = numeric(0),
sd = numeric(0),
is_parallel = FALSE,
log = FALSE,
is_validation = TRUE
)
dhpaDiff(
x,
pol_coefficients = numeric(0),
pol_degrees = numeric(0),
given_ind = logical(0),
omit_ind = logical(0),
mean = numeric(0),
sd = numeric(0),
type = "pol_coefficients",
is_parallel = FALSE,
log = FALSE,
is_validation = TRUE
)
ihpaDiff(
x_lower = matrix(1, 1),
x_upper = matrix(1, 1),
pol_coefficients = numeric(0),
pol_degrees = numeric(0),
given_ind = logical(0),
omit_ind = logical(0),
mean = numeric(0),
sd = numeric(0),
type = "pol_coefficients",
is_parallel = FALSE,
log = FALSE,
is_validation = TRUE
)
numeric matrix of density function arguments and
conditional values. Note that x
rows are points (observations)
while random vectors components (variables) are columns.
numeric vector of polynomial coefficients.
non-negative integer vector of polynomial degrees (orders).
logical vector indicating whether corresponding random
vector component is conditioned. By default it is a logical
vector of FALSE
values. If give_ind[i]
is TRUE
then i
-th column of x
matrix will contain conditional values.
logical vector indicating whether corresponding
component is omitted. By default it is a logical vector
of FALSE
values. If omit_ind[i]
is TRUE
then values in
i
-th column of x
matrix will be ignored.
numeric vector of expected values.
positive numeric vector of standard deviations.
if TRUE
then multiple cores will be
used for some calculations. It usually provides speed advantage for
large enough samples (about more than 1000 observations).
logical; if TRUE
then probabilities p are given as log(p)
or derivatives will be given respect to log(p)
logical value indicating whether function input
arguments should be validated. Set it to FALSE
for slight
performance boost (default value is TRUE
).
numeric matrix of lower integration limits.
Note that x_lower
rows are observations while variables are columns.
numeric matrix of upper integration limits.
Note that x_upper
rows are observations while variables
are columns.
integer vector of random vector components powers.
numeric matrix of left (lower) truncation limits.
Note that tr_left
rows are observations while variables are columns.
If tr_left
and tr_right
are single row matrices then the same
truncation limits will be applied to all observations that are determined
by the first rows of these matrices.
numeric matrix of right (upper) truncation limits.
Note that tr_right
rows are observations while variables are columns.
If tr_left
and tr_right
are single row matrices then the same
truncation limits will be applied to all observations that are determined
by the first rows of these matrices.
determines the partial derivatives to be included into the
gradient. If type="pol_coefficients"
then gradient will contain
partial derivatives respect to polynomial coefficients listed in the
same order as pol_coefficients
. Other available types are
type = "mean"
and type = "sd"
.
For function dhpaDiff
it is possible to take
gradient respect to the x points setting type="x"
.
For function ihpaDiff
it is possible to take
gradient respect to the x lower and upper points setting
type = "x_lower"
or type = "upper"
correspondingly.
In order to get full gradient please set type="all"
.
Functions dhpa
, phpa
and
dtrhpa
return vector of probabilities of length
nrow(x)
.
Functions ihpa
and
itrhpa
return vector of probabilities of length
nrow(x_upper)
.
If x
argument has not been provided or is a single row
matrix then function
ehpa
returns moment value. Otherwise it returns vector of
length nrow(x)
containing moments values.
If tr_left
and tr_right
arguments are single row matrices then
function etrhpa
returns moment value.
Otherwise it returns vector of length
max(nrow(tr_left), nrow(tr_right))
containing moments values.
Functions dhpaDiff
and ihpaDiff
return Jacobin matrix. The number
of columns depends on type
argument. The number of rows is
nrow(x)
for dhpaDiff
and
nrow(x_upper)
for
ihpaDiff
If mean
or sd
are not specified they assume the default
values of x_lower
is not specified then it is the matrix of the
same size as x_upper
containing negative infinity values only. If
expectation_powers
is not specified then it is
Please see 'Details' section for additional information.
It is possible to approximate densities
dhpa
, cumulative probabilities
phpa
, ihpa
, moments
ehpa
as well as their truncated
dtrhpa
, itrhpa
,
etrhpa
forms
and gradients dhpaDiff
, ihpaDiff
.
Note that phpa
is special of ihpa
where x
corresponds to x_upper
while x_lower
is matrix of
negative infinity values. So phpa
intended to approximate
cumulative
distribution functions while ihpa
approximates
probabilities that
random vector components will be between values determined by rows of
x_lower
and x_upper
matrices. Further details are given below.
Since density approximating function is non-negative and integrates
to 1 it is density function for some
x
matrix in dhpa
.
pol_coefficients[k]
element. In order to investigate
correspondence between k
and polynomialIndex
function 'Details', 'Value' and 'Examples' sections. Note that if pol_coefficients[k]
simply corresponds to
pol_degrees
argument so pol_degrees[i]
determines
mean[t]
and sd[t]
arguments values.
normalMoment
allows to calculate and differentiate normal
random variable's moments.
Therefore dhpa
allows to calculate x
matrix given polynomial
degrees pol_degrees
(mean
(sd
(pol_coefficients
(mean
, sd
and pol_degrees
are
pol_coefficients
has
prod(pol_degrees + 1)
elements.
Cumulative probabilities could be approximated as follows:
where:
mean[t]
and sd[t]
arguments
values.
truncatedNormalMoment
allows to calculate and
differentiate truncated normal random variable's moments.
x_upper
matrix in ihpa
.
x_lower
matrix in ihpa
.
Therefore ihpa
allows to calculate interval distribution
function x_lower
(x_upper
(dhpa
.
Expected value powered product approximation is as follows:
where expectation_powers
argument of ehpa
so
expectation_powers[t]
assigns x
in ehpa
allows to determined conditional values.
Expanding polynomial degrees mean
, sd
and pol_coefficients
values
selection. In order to get sample estimates of these parameters please apply
hpaML
function.
In order to perform calculation for marginal distribution of some
omit_ind
argument.
For examples if ones assume omit_ind = c(FALSE, TRUE, FALSE, TRUE, FALSE)
indicating that omit_ind
are
TRUE
.
Then x
still should be t
components of t
-variate density approximating function.
In order to perform calculation for conditional distribution i.e. given
fixed values for some given_ind
argument.
For example if ones assume given_ind = c(FALSE, TRUE, FALSE, TRUE, FALSE)
and
x[2] = 8
, x[4] = 10
where for simplicity it is assumed that
x
is single row x
rows.
Note that it is possible to combine given_ind
and omit_ind
arguments. However it is wrong to set both given_ind[i]
and
omit_ind[i]
to TRUE
. Also at least one value should be
FALSE
both for given_ind
and omit_ind
.
In order to consider truncated distribution of tr_left
and tr_right
arguments correspondingly. Note that if lower truncation
points are negative infinite and upper truncation points are positive
infinite then dtrhpa
, itrhpa
and
etrhpa
are similar to dhpa
,
ihpa
and ehpa
correspondingly.
In order to calculate Jacobian of dhpaDiff
and ihpaDiff
functions correspondingly specifying
parameters of interest via type
argument. If x
or
x_lower
and x_upper
are single row matrices then gradients
will be calculated.
For further information please see 'Examples' section. Note that examples are given separately for each function.
Densities Hermite polynomial approximation approach has been proposed by A. Gallant and D. W. Nychka in 1987. The main idea is to approximate unknown distribution density with scaled Hermite polynomial. For more information please refer to the literature listed below.
A. Gallant and D. W. Nychka (1987) <doi:10.2307/1913241>
# NOT RUN {
## Example demonstrating dhpa function application.
## Let's approximate some three random variables (i.e. X1, X2 and X3)
## joint density function at points x = (0,1, 0.2, 0.3) and
## y = (0.5, 0.8, 0.6) with Hermite polynomial of (1, 2, 3) degrees which
## polynomial coefficients equal 1 except coefficient related to x1*(x^3)
## polynomial element which equals 2. Also suppose that normal density
## related mean vector equals (1.1, 1.2, 1.3) while standard deviations
## vector is (2.1, 2.2, 2.3).
# Prepare initial values
x <- matrix(c(0.1, 0.2, 0.3), nrow = 1) # x point as a single row matrix
y <- matrix(c(0.5, 0.8, 0.6), nrow = 1) # y point as a single row matrix
x_y <- rbind(x, y) # matrix which rows are x and y
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
# Create polynomial powers and indexes correspondence matrix
pol_ind <- polynomialIndex(pol_degrees)
# Set all polynomial coefficients to 1
pol_coefficients <- rep(1, ncol(pol_ind))
pol_degrees_n <- length(pol_degrees)
# Assign coefficient 2 to the polynomial element (x1 ^ 1)*(x2 ^ 0)*(x3 ^ 2)
pol_coefficients[apply(pol_ind, 2, function(x) all(x == c(1, 0, 2)))] <- 2
# Visualize correspondence between polynomial
# elements and their coefficients
as.data.frame(rbind(pol_ind, pol_coefficients),
row.names = c("x1 power", "x2 power", "x3 power", "coefficients"),
optional = TRUE)
printPolynomial(pol_degrees, pol_coefficients)
# Calculate density approximation
# at point x (note that x should be a matrix)
dhpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd)
# at points x and y
dhpa(x = x_y,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd)
# Condition second component to be 0.5 i.e. X2 = 0.5.
# Substitute x and y second components with conditional value 0.5
x <- matrix(c(0.1, 0.5, 0.3), nrow = 1) # or simply x[2] <- 0.5
y <- matrix(c(0.4, 0.5, 0.6), nrow = 1) # or simply y[2] <- 0.5
x_y <- rbind(x, y)
# Set TRUE to the second component indicating that it is conditioned
given_ind <- c(FALSE, TRUE, FALSE)
# Calculate conditional (on X2 = 0.5) density approximation
# at point x
dhpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind)
# at points x and y
dhpa(x = x_y,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind)
# Consider third component marginal distribution conditioned on the
# second component 0.5 value i.e. (X3 | X2 = 0.5).
# Set TRUE to the first component indicating that it is omitted
omit_ind <- c(TRUE, FALSE, FALSE)
# Calculate conditional (on x2 = 0.5) marginal (for x3) density approximation
# at point x
dhpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
omit_ind = omit_ind)
# at points x and y
dhpa(x = x_y,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
omit_ind = omit_ind)
## Example demonstrating phpa function application.
## Let's approximate some three random variables (X1, X2, X3)
## joint cumulative distribution function (cdf) at point (0,1, 0.2, 0.3)
## with Hermite polynomial of (1, 2, 3) degrees which polynomial
## coefficients equal 1 except coefficient related to x1*(x^3) polynomial
## element which equals 2. Also suppose that normal density related
## mean vector equals (1.1, 1.2, 1.3) while standard deviations
## vector is (2.1, 2.2, 2.3).
## Prepare initial values
x <- matrix(c(0.1, 0.2, 0.3), nrow = 1)
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
# Create polynomial powers and indexes correspondence matrix
pol_ind <- polynomialIndex(pol_degrees)
# Set all polynomial coefficients to 1
pol_coefficients <- rep(1, ncol(pol_ind))
pol_degrees_n <- length(pol_degrees)
# Assign coefficient 2 to the polynomial element (x1 ^ 1)*(x2 ^ 0)*(x3 ^ 2)
pol_coefficients[apply(pol_ind, 2, function(x) all(x == c(1, 0, 2)))] <- 2
# Visualize correspondence between polynomial
# elements and their coefficients
as.data.frame(rbind(pol_ind, pol_coefficients),
row.names = c("x1 power", "x2 power",
"x3 power", "coefficients"),
optional = TRUE)
printPolynomial(pol_degrees, pol_coefficients)
# Calculate cdf approximation at point x
phpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd)
# Condition second component to be 0.5
# Substitute x second component with conditional value 0.5
x <- matrix(c(0.1, 0.5, 0.3), nrow = 1) # or simply x[2] <- 0.5
# Set TRUE to the second component indicating that it is conditioned
given_ind <- c(FALSE, TRUE, FALSE)
# Calculate conditional (on X2 = 0.5) cdf approximation at point x
phpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind)
# Consider third component marginal distribution
# conditioned on the second component 0.5 value
# Set TRUE to the first component indicating that it is omitted
omit_ind <- c(TRUE, FALSE, FALSE)
# Calculate conditional (on X2 = 0.5) marginal (for X3) cdf
# approximation at point x
phpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
omit_ind = omit_ind)
## Example demonstrating ihpa function application.
## Let's approximate some three random variables (X1, X2, X3) joint interval
## distribution function (intdf) at lower and upper points (0,1, 0.2, 0.3)
## and (0,4, 0.5, 0.6) correspondingly with Hermite polynomial of (1, 2, 3)
## degrees which polynomial coefficients equal 1 except coefficient related
## to x1*(x^3) polynomial element which equals 2. Also suppose that normal
## density related mean vector equals (1.1, 1.2, 1.3) while standard
## deviations vector is (2.1, 2.2, 2.3).
## Prepare initial values
x_lower <- matrix(c(0.1, 0.2, 0.3), nrow=1)
x_upper <- matrix(c(0.4, 0.5, 0.6), nrow=1)
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
# Create polynomial powers and indexes correspondence matrix
pol_ind <- polynomialIndex(pol_degrees)
# Set all polynomial coefficients to 1
pol_coefficients <- rep(1, ncol(pol_ind))
pol_degrees_n <- length(pol_degrees)
# Assign coefficient 2 to the polynomial element (x1 ^ 1)*(x2 ^ 0)*(x3 ^ 2)
pol_coefficients[apply(pol_ind, 2, function(x) all(x == c(1, 0, 2)))] <- 2
# Visualize correspondence between polynomial
# elements and their coefficients
as.data.frame(rbind(pol_ind, pol_coefficients),
row.names = c("x1 power", "x2 power",
"x3 power", "coefficients"),
optional = TRUE)
printPolynomial(pol_degrees, pol_coefficients)
# Calculate intdf approximation at points x_lower and x_upper
ihpa(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd)
# Condition second component to be 0.7
# Substitute x second component with conditional value 0.7
x_upper <- matrix(c(0.4, 0.7, 0.6), nrow = 1)
# Set TRUE to the second component indicating that it is conditioned
given_ind <- c(FALSE, TRUE, FALSE)
# Calculate conditional (on X2 = 0.5) intdf approximation
# at points x_lower and x_upper
ihpa(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind)
# Consider third component marginal distribution
# conditioned on the second component 0.7 value
# Set TRUE to the first component indicating that it is omitted
omit_ind <- c(TRUE, FALSE, FALSE)
# Calculate conditional (on X2 = 0.5) marginal (for X3)
# intdf approximation at points x_lower and x_upper
ihpa(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind, omit_ind = omit_ind)
## Example demonstrating ehpa function application.
## Let's approximate some three random variables (X1, X2, X3) powered product
## expectation for powers (3, 2, 1) with Hermite polynomial of (1, 2, 3)
## degrees which polynomial coefficients equal 1 except coefficient
## related to x1*(x^3) polynomial element which equals 2.
## Also suppose that normal density related mean vector equals
## (1.1, 1.2, 1.3) while standard deviations vector is (2.1, 2.2, 2.3).
# Prepare initial values
expectation_powers = c(3,2,1)
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
# Create polynomial powers and indexes correspondence matrix
pol_ind <- polynomialIndex(pol_degrees)
# Set all polynomial coefficients to 1
pol_coefficients <- rep(1, ncol(pol_ind))
pol_degrees_n <- length(pol_degrees)
#Assign coefficient 2 to the polynomial element (x1 ^ 1)*(x2 ^ 0)*(x3 ^ 2)
pol_coefficients[apply(pol_ind, 2, function(x) all(x == c(1, 0, 2)))] <- 2
# Visualize correspondence between polynomial elements and their coefficients
as.data.frame(rbind(pol_ind, pol_coefficients),
row.names = c("x1 power", "x2 power",
"x3 power", "coefficients"),
optional = TRUE)
printPolynomial(pol_degrees, pol_coefficients)
# Calculate expected powered product approximation
ehpa(pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
expectation_powers = expectation_powers)
# Condition second component to be 0.5
# Substitute x second component with conditional value 0.5
x <- matrix(c(NA, 0.5, NA), nrow = 1)
#Set TRUE to the second component indicating that it is conditioned
given_ind <- c(FALSE, TRUE, FALSE)
# Calculate conditional (on X2 = 0.5) expected powered product approximation
ehpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
expectation_powers = expectation_powers,
given_ind = given_ind)
# Consider third component marginal distribution
# conditioned on the second component 0.5 value
# Set TRUE to the first component indicating that it is omitted
omit_ind <- c(TRUE, FALSE, FALSE)
# Calculate conditional (on X2 = 0.5) marginal (for X3) expected powered
# product approximation at points x_lower and x_upper
ehpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
expectation_powers = expectation_powers,
given_ind = given_ind,
omit_ind = omit_ind)
## Example demonstrating etrhpa function application.
## Let's approximate some three truncated random variables (X1, X2, X3)
## powered product expectation for powers (3, 2, 1) with Hermite polynomial
## of (1,2,3) degrees which polynomial coefficients equal 1 except
## coefficient related to x1*(x^3) polynomial element which equals 2. Also
## suppose that normal density related mean vector equals (1.1, 1.2, 1.3)
## while standard deviations vector is (2.1, 2.2, 2.3). Suppose that lower
## and upper truncation points are (-1.1,-1.2,-1.3) and (1.1,1.2,1.3)
## correspondingly.
# Prepare initial values
expectation_powers = c(3,2,1)
tr_left = matrix(c(-1.1,-1.2,-1.3), nrow = 1)
tr_right = matrix(c(1.1,1.2,1.3), nrow = 1)
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
# Create polynomial powers and indexes correspondence matrix
pol_ind <- polynomialIndex(pol_degrees)
# Set all polynomial coefficients to 1
pol_coefficients <- rep(1, ncol(pol_ind))
pol_degrees_n <- length(pol_degrees)
# Assign coefficient 2 to the polynomial element (x1 ^ 1)*(x2 ^ 0)*(x3 ^ 2)
pol_coefficients[apply(pol_ind, 2, function(x) all(x == c(1, 0, 2)))] <- 2
# Visualize correspondence between polynomial elements and their coefficients
as.data.frame(rbind(pol_ind, pol_coefficients),
row.names = c("x1 power", "x2 power",
"x3 power", "coefficients"),
optional = TRUE)
printPolynomial(pol_degrees, pol_coefficients)
# Calculate expected powered product approximation for truncated distribution
etrhpa(pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
expectation_powers = expectation_powers,
tr_left = tr_left, tr_right = tr_right)
## Example demonstrating dtrhpa function application.
## Let's approximate some three random variables (X1, X2, X3) joint density
## function at point (0,1, 0.2, 0.3) with Hermite polynomial of (1,2,3)
## degrees which polynomial coefficients equal 1 except coefficient related
## to x1*(x^3) polynomial element which equals 2. Also suppose that normal
## density related mean vector equals (1.1, 1.2, 1.3) while standard
## deviations vector is (2.1, 2.2, 2.3). Suppose that lower and upper
## truncation points are (-1.1,-1.2,-1.3) and (1.1,1.2,1.3) correspondingly.
# Prepare initial values
x <- matrix(c(0.1, 0.2, 0.3), nrow=1)
tr_left = matrix(c(-1.1,-1.2,-1.3), nrow = 1)
tr_right = matrix(c(1.1,1.2,1.3), nrow = 1)
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
# Create polynomial powers and indexes correspondence matrix
pol_ind <- polynomialIndex(pol_degrees)
# Set all polynomial coefficients to 1
pol_coefficients <- rep(1, ncol(pol_ind))
pol_degrees_n <- length(pol_degrees)
# Assign coefficient 2 to the polynomial element (x1 ^ 1)*(x2 ^ 0)*(x3 ^ 2)
pol_coefficients[apply(pol_ind, 2, function(x) all(x == c(1, 0, 2)))] <- 2
# Visualize correspondence between polynomial elements and their coefficients
as.data.frame(rbind(pol_ind, pol_coefficients),
row.names = c("x1 power", "x2 power",
"x3 power", "coefficients"),
optional = TRUE)
printPolynomial(pol_degrees, pol_coefficients)
# Calculate density approximation at point x
dtrhpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
tr_left = tr_left,
tr_right = tr_right)
# Condition second component to be 0.5
# Substitute x second component with conditional value 0.5
x <- matrix(c(0.1, 0.5, 0.3), nrow = 1)
# Set TRUE to the second component indicating that it is conditioned
given_ind <- c(FALSE, TRUE, FALSE)
# Calculate conditional (on x2 = 0.5) density approximation at point x
dtrhpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
tr_left = tr_left, tr_right = tr_right)
# Consider third component marginal distribution
# conditioned on the second component 0.5 value
# Set TRUE to the first component indicating that it is omitted
omit_ind <- c(TRUE, FALSE, FALSE)
# Calculate conditional (on X2 = 0.5) marginal (for X3)
# density approximation at point x
dtrhpa(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind, omit_ind = omit_ind,
tr_left = tr_left, tr_right = tr_right)
## Example demonstrating itrhpa function application.
## Let's approximate some three truncated random variables (X1, X2, X3) joint
## interval distribution function at lower and upper points (0,1, 0.2, 0.3)
## and (0,4, 0.5, 0.6) correspondingly with Hermite polynomial of (1 ,2, 3)
## degrees which polynomial coefficients equal 1 except coefficient
## related to x1*(x^3) polynomial element which equals 2. Also suppose
## that normal density related mean vector equals (1.1, 1.2, 1.3) while
## standard deviations vector is (2.1, 2.2, 2.3). Suppose that lower and
## upper truncation are (-1.1,-1.2,-1.3) and (1.1,1.2,1.3) correspondingly.
# Prepare initial values
x_lower <- matrix(c(0.1, 0.2, 0.3), nrow=1)
x_upper <- matrix(c(0.4, 0.5, 0.6), nrow=1)
tr_left = matrix(c(-1.1,-1.2,-1.3), nrow = 1)
tr_right = matrix(c(1.1,1.2,1.3), nrow = 1)
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
# Create polynomial powers and indexes correspondence matrix
pol_ind <- polynomialIndex(pol_degrees)
# Set all polynomial coefficients to 1
pol_coefficients <- rep(1, ncol(pol_ind))
pol_degrees_n <- length(pol_degrees)
# Assign coefficient 2 to the polynomial element (x1 ^ 1)*(x2 ^ 0)*(x3 ^ 2)
pol_coefficients[apply(pol_ind, 2, function(x) all(x == c(1, 0, 2)))] <- 2
# Visualize correspondence between polynomial
# elements and their coefficients
as.data.frame(rbind(pol_ind, pol_coefficients),
row.names = c("x1 power", "x2 power",
"x3 power", "coefficients"),
optional = TRUE)
printPolynomial(pol_degrees, pol_coefficients)
# Calculate intdf approximation at points x_lower and x_upper
itrhpa(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
tr_left = tr_left, tr_right = tr_right)
# Condition second component to be 0.7
# Substitute x second component with conditional value 0.7
x_upper <- matrix(c(0.4, 0.7, 0.6), nrow = 1)
# Set TRUE to the second component indicating that it is conditioned
given_ind <- c(FALSE, TRUE, FALSE)
# Calculate conditional (on X2 = 0.5) intdf
# approximation at points x_lower and x_upper
itrhpa(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
tr_left = tr_left, tr_right = tr_right)
# Consider third component marginal distribution
# conditioned on the second component 0.7 value
# Set TRUE to the first component indicating that it is omitted
omit_ind <- c(TRUE, FALSE, FALSE)
# Calculate conditional (on X2 = 0.5) marginal (for X3) intdf
# approximation at points x_lower and x_upper
itrhpa(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind, omit_ind = omit_ind,
tr_left = tr_left, tr_right = tr_right)
## Example demonstrating dhpaDiff function application.
## Let's approximate some three random variables (X1, X2, X3) joint density
## function at point (0,1, 0.2, 0.3) with Hermite polynomial of (1,2,3)
## degrees which polynomial coefficients equal 1 except coefficient related
## to x1*(x^3) polynomial element which equals 2. Also suppose that normal
## density related mean vector equals (1.1, 1.2, 1.3) while standard
## deviations vector is (2.1, 2.2, 2.3). In this example let's calculate
## density approximating function's gradient respect to various parameters
# Prepare initial values
x <- matrix(c(0.1, 0.2, 0.3), nrow = 1)
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
# Create polynomial powers and indexes correspondence matrix
pol_ind <- polynomialIndex(pol_degrees)
# Set all polynomial coefficients to 1
pol_coefficients <- rep(1, ncol(pol_ind))
pol_degrees_n <- length(pol_degrees)
# Assign coefficient 2 to the polynomial element (x1 ^ 1)*(x2 ^ 0)*(x3 ^ 2)
pol_coefficients[apply(pol_ind, 2, function(x) all(x == c(1, 0, 2)))] <- 2
# Visualize correspondence between polynomial
# elements and their coefficients
as.data.frame(rbind(pol_ind, pol_coefficients),
row.names = c("x1 power", "x2 power",
"x3 power", "coefficients"),
optional = TRUE)
printPolynomial(pol_degrees, pol_coefficients)
# Calculate density approximation gradient
# respect to polynomial coefficients at point x
dhpaDiff(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd)
# Condition second component to be 0.5
# Substitute x second component with conditional value 0.5
x <- matrix(c(0.1, 0.5, 0.3), nrow = 1)
# Set TRUE to the second component indicating that it is conditioned
given_ind <- c(FALSE, TRUE, FALSE)
# Calculate conditional (on x2 = 0.5) density approximation's
# gradient respect to polynomial coefficients at point x
dhpaDiff(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind)
# Consider third component marginal distribution
# conditioned on the second component 0.5 value
# Set TRUE to the first component indicating that it is omitted
omit_ind <- c(TRUE, FALSE, FALSE)
# Calculate conditional (on X2 = 0.5) marginal (for X3) density
# approximation's gradient respect to:
# polynomial coefficients
dhpaDiff(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
omit_ind = omit_ind)
# mean
dhpaDiff(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
omit_ind = omit_ind,
type = "mean")
# sd
dhpaDiff(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
omit_ind = omit_ind,
type = "sd")
# x
dhpaDiff(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
omit_ind = omit_ind,
type = "x")
## Example demonstrating ihpaDiff function application.
## Let's approximate some three random variables (X1, X2, X3 ) joint interval
## distribution function (intdf) at lower and upper points (0,1, 0.2, 0.3)
## and (0,4, 0.5, 0.6) correspondingly with Hermite polynomial of (1, 2, 3)
## degrees which polynomial coefficients equal 1 except coefficient
## related to x1*(x^3) polynomial element which equals 2.
## Also suppose that normal density related mean vector equals
## (1.1, 1.2, 1.3) while standard deviations vector is (2.1, 2.2, 2.3).
## In this example let's calculate interval distribution approximating
## function gradient respect to polynomial coefficients.
# Prepare initial values
x_lower <- matrix(c(0.1, 0.2, 0.3), nrow=1)
x_upper <- matrix(c(0.4, 0.5, 0.6), nrow=1)
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
# Create polynomial powers and indexes correspondence matrix
pol_ind <- polynomialIndex(pol_degrees)
# Set all polynomial coefficients to 1
pol_coefficients <- rep(1, ncol(pol_ind))
pol_degrees_n <- length(pol_degrees)
# Assign coefficient 2 to the polynomial element (x1 ^ 1)*(x2 ^ 0)*(x3 ^ 2)
pol_coefficients[apply(pol_ind, 2, function(x) all(x == c(1, 0, 2)))] <- 2
# Visualize correspondence between polynomial
# elements and their coefficients
as.data.frame(rbind(pol_ind, pol_coefficients),
row.names = c("x1 power", "x2 power",
"x3 power", "coefficients"),
optional = TRUE)
printPolynomial(pol_degrees, pol_coefficients)
# Calculate intdf approximation gradient respect to
# polynomial coefficients at points x_lower and x_upper
ihpaDiff(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd)
# Condition second component to be 0.7
# Substitute x second component with conditional value 0.7
x_upper <- matrix(c(0.4, 0.7, 0.6), nrow = 1)
# Set TRUE to the second component indicating that it is conditioned
given_ind <- c(FALSE, TRUE, FALSE)
# Calculate conditional (on X2 = 0.5) intdf approximation
# respect to polynomial coefficients at points x_lower and x_upper
ihpaDiff(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind)
# Consider third component marginal distribution
# conditioned on the second component 0.7 value
# Set TRUE to the first component indicating that it is omitted
omit_ind <- c(TRUE, FALSE, FALSE)
# Calculate conditional (on X2 = 0.5) marginal (for X3) intdf approximation
# respect to:
# polynomial coefficients
ihpaDiff(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind, omit_ind = omit_ind)
# mean
ihpaDiff(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind, omit_ind = omit_ind,
type = "mean")
# sd
ihpaDiff(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind, omit_ind = omit_ind,
type = "sd")
# x_lower
ihpaDiff(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind, omit_ind = omit_ind,
type = "x_lower")
# x_upper
ihpaDiff(x_lower = x_lower, x_upper = x_upper,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind, omit_ind = omit_ind,
type = "x_upper")
# }
Run the code above in your browser using DataLab