0
$\begingroup$

I am trying to do a GMM(Generalized Method of Moments) by NMinimize, it comes up with the warning 'The objective function is not a scalar', here's what I have tried:

  1. I adjusted the function step by step and found that the problem is about the minus e=y-y`Pred, after I delete the minus, it worked.But I really can't understand why.
  2. When I use the functions directly, it gives a scalar. I think this is the evidence that my dimension of vector and matrix is right, which I have checked also.

I am new with the matrix calculate, I think this problem must be somehow stupid, but I can't fix it.Sorry :(

ClearAll[data`x, data`y, Winv, params, gmmObjective, beta]

(*Setting*)
data`x = {{1, 2, 3}, {4, 5, 6}};

data`y = {1, 2};

Winv = IdentityMatrix[3];

(*Original function*)
gmmObjective[params_, y_, x_, Matr_] := 
 Module[{y`Pred, e, moments, avgMoment, ju`value},
  y`Pred = x . params;
  e = y - y`Pred;
  moments = e . x;
  ju`value = moments . Matr . Transpose[moments];
  Return[ju`value];
  ]

NMinimize[{gmmObjective[beta, data`y, data`x, Winv], 
  Element[beta, Vectors[3, Reals]]}, beta]


(*Adjusted function*)
gmmObjective[params_, y_, x_, Matr_] := 
 Module[{y`Pred, e, moments, avgMoment, ju`value},
  y`Pred = x . params;
  e = y`Pred;
  moments = e . x;
  ju`value = moments . Matr . Transpose[moments];
  Return[ju`value];
  ]

NMinimize[{gmmObjective[beta, data`y, data`x, Winv], 
  Element[beta, Vectors[3, Reals]]}, beta]
$\endgroup$
1
  • 2
    $\begingroup$ The porblem is with beta... try NMinimize[ gmmObjective[{beta1, beta2, beta3}, datay, datax, Winv], {beta1, beta2, beta3}] with your original objective and it gives an answer. $\endgroup$
    – bill s
    Commented Nov 25, 2023 at 3:21

1 Answer 1

2
$\begingroup$

You define beta in NMinimize as a scalar. Replacing beta by: 1 and evaluating:

gmmObjective[1, data`y, data`x, Winv]

{{1, 2, 3}, {4, 5, 6}} . 1 . {{1, 2, 3}, {4, 5, 6}} . 
 Transpose[{{1, 2, 3}, {4, 5, 6}} . 1 . {{1, 2, 3}, {4, 5, 6}}]

You see: "...6}}.1.{{1,2..." You can not use the Dot product for a scalar. Your statement:

y`Pred = x . params

is wrong.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.