

Dear all,
i'm hoping that one of you can help me in the following code that is
used for asset selection based on nmof package and please guide me
where is the mistake:
the error message is: "Error in colSums(x) : 'x' must be an array of
at least two dimensions".
The entire code is below (it works with DEopt) but does not with PSO;
it is for asset selection exaclty from the package nmof.
require("NMOF")
na<31
nn< read.table("n.txt") # n is the a 31*31 matrix.
Sigma < data.matrix(nn)
OF2 < function(x, data) {
# res < colSums (data$Sigma %*% x * x)
res < colSums (Sigma %*% x * x)
#z<c(x,x)
n < colSums (x); res < res / n^2
}
####### pso #############
data < list(
na = na,
max = rep( 0.05, na),
min = rep(0.05, na)
)
algo < list(nP = 31L,
nG = 1000L,
c1 = 0.5,
c2 = 1.5,
#min = data$min, max = data$max,
max = rep( 0.05, na), min = rep(0.05, na),
#repair = repair, pen = penalty,
iner = 0.7, initV = 1, maxV = 0.2
#printBar = FALSE, printDetail = TRUE
)
#x<array(x, c(2,2))
system.time(sol < PSopt(OF = OF2,algo = algo, data))
_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.


Ok, it may be that 'x' is a data structure, need to turn it into an array,
also you need to transpose it 'x' when multiplying.
Good luck,
Pedro
< https://www.avast.com/sigemail?utm_medium=email&utm_source=link&utm_campaign=sigemail&utm_content=webmail>
Virusfree.
www.avast.com
< https://www.avast.com/sigemail?utm_medium=email&utm_source=link&utm_campaign=sigemail&utm_content=webmail>
<#DAB4FAD82DD740BBA1B84E2AA1F9FDF2>
On Fri, 25 Jan 2019 at 21:26, mmm ammm < [hidden email]> wrote:
> Dear all,
> i'm hoping that one of you can help me in the following code that is
> used for asset selection based on nmof package and please guide me
> where is the mistake:
>
> the error message is: "Error in colSums(x) : 'x' must be an array of
> at least two dimensions".
>
> The entire code is below (it works with DEopt) but does not with PSO;
> it is for asset selection exaclty from the package nmof.
>
> require("NMOF")
> na<31
>
> nn< read.table("n.txt") # n is the a 31*31 matrix.
> Sigma < data.matrix(nn)
>
> OF2 < function(x, data) {
> # res < colSums (data$Sigma %*% x * x)
> res < colSums (Sigma %*% x * x)
> #z<c(x,x)
> n < colSums (x); res < res / n^2
> }
> ####### pso #############
> data < list(
> na = na,
> max = rep( 0.05, na),
> min = rep(0.05, na)
> )
> algo < list(nP = 31L,
> nG = 1000L,
> c1 = 0.5,
> c2 = 1.5,
> #min = data$min, max = data$max,
> max = rep( 0.05, na), min = rep(0.05, na),
> #repair = repair, pen = penalty,
> iner = 0.7, initV = 1, maxV = 0.2
> #printBar = FALSE, printDetail = TRUE
> )
> #x<array(x, c(2,2))
>
> system.time(sol < PSopt(OF = OF2,algo = algo, data))
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rsigfinance>  Subscriberposting only. If you want to post, subscribe first.
>  Also note that this is not the rhelp list where general R questions
> should go.
>
[[alternative HTML version deleted]]
_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.


>>>>> "m" == mmm ammm < [hidden email]> 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. realvalued) vectors.
What 'loopOF' does: Differential Evolution and Particle
Swarm Optimisation are multiplesolution methods, aka
populationbased 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 objectivefunction
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

Enrico Schumann (maintainer of package NMOF)
Lucerne, Switzerland
http://enricoschumann.net_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.


Dear Enrico,
Thank you so much. It works now.
But, the function DEopt worked without changing this setting and gave
approximately the same result; could you please explain this for me?
Many thanks
On 26/01/2019, Enrico Schumann < [hidden email]> wrote:
>>>>>> "m" == mmm ammm < [hidden email]> 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. realvalued) vectors.
>
> What 'loopOF' does: Differential Evolution and Particle
> Swarm Optimisation are multiplesolution methods, aka
> populationbased 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 objectivefunction
> 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
>
>
> 
> Enrico Schumann (maintainer of package NMOF)
> Lucerne, Switzerland
> http://enricoschumann.net>
_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.


>>>>> "m" == mmm ammm < [hidden email]> writes:
m> Dear Enrico,
m> Thank you so much. It works now.
m> But, the function DEopt worked without changing this setting and gave
m> approximately the same result; could you please explain this for me?
m> Many thanks
The reason is that 'DEopt' does not drop the dimension
when a solution is selected and passed to the objective
function; a single solution remains a matrix (of one
column).
Such a conversion is simple to do in the objective
function: use 'as.matrix()' to create a column vector;
or 'c()' or 'drop()' to drop the dimension and create a
vector.
kind regards
Enrico
m> On 26/01/2019, Enrico Schumann < [hidden email]> wrote:
>>>>>>> "m" == mmm ammm < [hidden email]> 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. realvalued) vectors.
>>
>> What 'loopOF' does: Differential Evolution and Particle
>> Swarm Optimisation are multiplesolution methods, aka
>> populationbased 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 objectivefunction
>> 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
>>
>>
>> 
>> Enrico Schumann (maintainer of package NMOF)
>> Lucerne, Switzerland
>> http://enricoschumann.net >>

Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.


Many thanks Enrico for your help.
On 28/01/2019, Enrico Schumann < [hidden email]> wrote:
>>>>>> "m" == mmm ammm < [hidden email]> writes:
>
> m> Dear Enrico,
> m> Thank you so much. It works now.
> m> But, the function DEopt worked without changing this setting and
> gave
> m> approximately the same result; could you please explain this for me?
>
> m> Many thanks
>
> The reason is that 'DEopt' does not drop the dimension
> when a solution is selected and passed to the objective
> function; a single solution remains a matrix (of one
> column).
>
> Such a conversion is simple to do in the objective
> function: use 'as.matrix()' to create a column vector;
> or 'c()' or 'drop()' to drop the dimension and create a
> vector.
>
> kind regards
> Enrico
>
> m> On 26/01/2019, Enrico Schumann < [hidden email]> wrote:
> >>>>>>> "m" == mmm ammm < [hidden email]> 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. realvalued) vectors.
> >>
> >> What 'loopOF' does: Differential Evolution and Particle
> >> Swarm Optimisation are multiplesolution methods, aka
> >> populationbased 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 objectivefunction
> >> 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
> >>
> >>
> >> 
> >> Enrico Schumann (maintainer of package NMOF)
> >> Lucerne, Switzerland
> >> http://enricoschumann.net> >>
>
> 
> Enrico Schumann
> Lucerne, Switzerland
> http://enricoschumann.net>
_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rsigfinance Subscriberposting only. If you want to post, subscribe first.
 Also note that this is not the rhelp list where general R questions should go.

