[R-SIG-Finance] the package nmof

Enrico Schumann e@ @end|ng |rom enr|co@chum@nn@net
Sat Jan 26 20:50:14 CET 2019

>>>>> "m" == mmm ammm <mmmammm1900 using gmail.com> writes:

    m> Dear all,
    m> i'm hoping that one of you can help me in the following code that is
    m> used for asset selection based on nmof package and please guide me
    m> where is the mistake:

    m> the error message is: "Error in colSums(x) : 'x' must be an array of
    m> at least two dimensions".

    m> The entire code is below (it works with DEopt) but does not with PSO;
    m> it is for asset selection exaclty from the package nmof.

    m> require("NMOF")
    m> na<-31

    m> nn<- read.table("n.txt") # n is the a 31*31 matrix.
    m> Sigma <- data.matrix(nn)

    m> OF2 <- function(x, data) {
    m> #  res <- colSums (data$Sigma %*% x * x)
    m> res <- colSums (Sigma %*% x * x)
    m> #z<-c(x,x)
    m> n <- colSums (x); res <- res / n^2
    m> }
    m> #######  pso  #############
    m> data <- list(
    m> na = na,
    m> max = rep( 0.05, na),
    m> min = rep(-0.05, na)
    m> )
    m> algo <- list(nP = 31L,
    m> nG = 1000L,
    m> c1 = 0.5,
    m> c2 = 1.5,
    m> #min = data$min, max = data$max,
    m> max = rep( 0.05, na), min = rep(-0.05, na),
    m> #repair = repair, pen = penalty,
    m> iner = 0.7, initV = 1, maxV = 0.2
    m> #printBar = FALSE, printDetail = TRUE
    m> )
    m> #x<-array(x, c(2,2))

    m> system.time(sol <- PSopt(OF = OF2,algo = algo, data))

You could get rid of the error by setting 'loopOF' to
FALSE (as part of the settings passed with list
'algo'). I will explain below what this setting does.

But in any case, are you sure your objective function
does what it should?  If I read it correctly, it
assumes that 'x' is logical. But both DEopt and PSopt
work with numeric (i.e. real-valued) vectors.

What 'loopOF' does: Differential Evolution and Particle
Swarm Optimisation are multiple-solution methods, aka
population-based methods. The NMOF implementations
'DEopt' and 'PSopt' arrange the populations as
matrices; every column in such a matrix represents one
solution.  To compute the objective function of the
solutions, with the default settings both 'DEopt' and
'PSopt' use a loop. The objective function should thus
receive a single solution as input, and should evaluate
to a single number.

Sometimes an objective function may be computed for the
whole population (i.e. all solutions) in one step. In
such a case, the objective function should expect the
population matrix (i.e. all solutions) as input, and
should evaluate to a vector: the objective-function
values corresponding to the columns of the population
matrix.  However, since the user specifies the
objective function, 'DEopt'/'PSopt' cannot know
automatically in what way the objective function is
written; so you need to tell the functions by setting
'loopOF' to TRUE (the default) or to FALSE.

kind regards

Enrico Schumann (maintainer of package NMOF)
Lucerne, Switzerland

More information about the R-SIG-Finance mailing list