MDI_solve: Solve MDI with linear equality constraints using a Monte...

Description Usage Arguments Details Value See Also Examples

View source: R/solver.R

Description

This function solves a Minimum Discrimination Information problem with linear equality constraints, as described in the package vignette.

Usage

1
2
MDI_solve(X, f, ..., m, l_start = rep(0, k + 1), method = "Newton",
  control = list(trace = 1))

Arguments

X

an S x n matrix containing S samples from p

f

a function returning a vector of size k that evaluates the LHS of restrictions

m

a vector of size k with the RHS of the restrictions

l_start

a guess of the optimal k + 1 lambdas. Default is rep(0, k + 1)

control

a list of parameters passed to nleqslv

Details

The non - linear set of equations is solved using package nleqslv. By default, the method is set to Newton, as this usually results in faster convergences. The rest of the parameters of the solver that are not passed through the control list are left in their default values.

This method assumes that you are able to sample from the p distribution in order to produce the matrix X. If this is not the case, then a robust alternative is to use an MCMC method (such as the Metropolis algorithm), assuming that you can at least evaluate the density of p.

The function f should take as input a vector x of size n (the dimension of the space where x lives) and return a vector of size k (the number of non-trivial conditions), such that the first component of f(x) corresponds to the value of the first restriction function evaluated at x, the second component to the second restriction, and so on.

Value

A list as returned by nleqslv

See Also

nleqslv for details on this non - linear equation solver, and mcmc for an implementation of the Metropolis algorithm.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#################################################################
# Toy example:
#
# p: multivariate normal (0, \Sigma)
# Restrictions: force 0.5 mass of the marginals to lie above some
#               randomly chosen thresholds (chi).
# The above implies:
#                   n = k
#                   f(x) = (x > chi)
#                   m = rep(0.5, n)
#################################################################

library(mcMDISolver)

set.seed(1313) # for reproducibility

n = 9 # number of variables
k = n # number of restrictions
S = 50000 # number of samples

# restrictions
chi = rnorm(n, 1.96, 0.1) # critical values
f = function(x, c = chi){ # vectorized over the size of x (n)
  return(x > c)
}
m = rep(.5, k) # rhs of restrictions

# p-sampler
# p is mvnorm with average cor = rho
rho = 0.6 # average correlation
R = diag(n)
R[upper.tri(R)] = pmin(pmax(rnorm(n*(n-1)/2, rho, 0.01), -1), 1)
R[lower.tri(R)] = t(R)[lower.tri(R)]

## SOLVE: serial implementation
start.time = proc.time()
fit_MDI = mcMDISolver::MDI_solve(mvtnorm::rmvnorm(n = S, sigma = R), f = f, m = m)
print(proc.time() - start.time)
print(fit_MDI$x)

## Examine q distribution
# Draw samples using Metropolis algorithm

# log density of q
log_dq = function(x, l = fit_MDI$x){
  return(mvtnorm::dmvnorm(x, sigma = R, log = T) - as.numeric(crossprod(l, c(1, f(x)))))
}

# draw from q
mcmc_S = 2 * S
burn = trunc(0.1 * mcmc_S) # number of samples to burn
sample_q = mcmc::metrop(log_dq, initial = chi, nbatch = mcmc_S + burn)
X_q = sample_q$batch[-(1:burn), ]

# histograms
# note the sudden increase in density around chi
par(mfrow = c(3, 3))
for(i in 1:n) hist(X_q[,i], main = paste("chi =", round(chi[i], 2)), xlab = "")
par(mfrow = c(1, 1))

# check restrictions close to zero
rowMeans(apply(X_q, 1, f)) - m

miguelbiron/mcMDISolver documentation built on May 22, 2019, 10:50 p.m.