## 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 ehpaDiff function application.
## Let's approximate some three random variables (X1, X2, X3) expectation
## of the form E((X1 ^ 3) * (x2 ^ 1) * (X3 ^ 2)) and calculate the gradient
# Distribution parameters
mean <- c(1.1, 1.2, 1.3)
sd <- c(2.1, 2.2, 2.3)
pol_degrees <- c(1, 2, 3)
pol_coefficients_n <- prod(pol_degrees + 1)
pol_coefficients <- rep(1, pol_coefficients_n)
# Set powers for expectation
expectation_powers <- c(3, 1, 2)
# Calculate expectation approximation gradient
# respect to all parameters
ehpaDiff(pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
expectation_powers = expectation_powers,
type = "all")
# Let's calculate gradient of E(X1 ^ 3 | (X2 = 1, X3 = 2))
x <- c(0, 1, 2) # x[1] may be arbitrary (not NA) values
expectation_powers <- c(3, 0, 0) # expectation_powers[2:3] may be
# arbitrary (not NA) values
given_ind <- c(2, 3)
ehpaDiff(x = x,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = given_ind,
expectation_powers = expectation_powers,
type = "all")
## 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")
## Examples demonstrating qhpa function application.
## Sub-example 1 - univariate distribution
## Consider random variable X
# Distribution parameters
mean <- 1
sd <- 2
pol_degrees <- 2
pol_coefficients <- c(1, 0.1, -0.01)
# The level of quantile
p <- 0.7
# Calculate quantile of X
qhpa(p = p,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd)
## Sub-example 2 - marginal distribution
## Consider random vector (X1, X2) and quantile of X1
# Distribution parameters
mean <- c(1, 1.2)
sd <- c(2, 3)
pol_degrees <- c(2, 2)
pol_coefficients <- c(1, 0.1, -0.01, 0.2, 0.012,
0.0013, 0.0042, 0.00025, 0)
# The level of quantile
p <- 0.7
# Calculate quantile of X1
qhpa(p = p,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
omit_ind = 2) # set omitted variable index
## Sub-example 3 - marginal and conditional distribution
## Consider random vector (X1, X2, X3) and
## quantiles of X1|X3 and X1|(X2,X3)
mean <- c(1, 1.2, 0.9)
sd <- c(2, 3, 2.5)
pol_degrees <- c(1, 1, 1)
pol_coefficients <- c(1, 0.1, -0.01, 0.2, 0.012,
0.0013, 0.0042, 0.00025)
# The level of quantile
p <- 0.7
# Calculate quantile of X1|X3 = 0.2
qhpa(p = p,
x = matrix(c(NA, NA, 0.2), nrow = 1), # set any values to
# unconditioned and
# omitted components
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
omit_ind = 2, # set omitted variable index
given_ind = 3) # set conditioned variable index
# Calculate quantile of X1|(X2 = 0.5, X3 = 0.2)
qhpa(p = p,
x = matrix(c(NA, 0.5, 0.2), nrow = 1), # set any values to
# unconditioned and
# omitted components
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd,
given_ind = c(2, 3)) # set conditioned
# variables indexes
## Examples demonstrating rhpa function application.
# Set seed for reproducibility
set.seed(123)
# Distribution parameters
mean <- 1
sd <- 2
pol_degrees <- 2
pol_coefficients <- c(1, 0.1, -0.01)
# Simulate two observations from this distribution
rhpa(n = 2,
pol_coefficients = pol_coefficients,
pol_degrees = pol_degrees,
mean = mean, sd = sd)
Run the code above in your browser using DataLab