### Tags: basic, estimation, following, function, generalized, gmm, matlab, method, moments, pretty, programming, stuckt

# Generalized Method of Moments

On Programmer » Matlab

10,268 words with 9 Comments; publish: Thu, 08 May 2008 01:01:00 GMT; (20078.13, « »)

Hi, I have the following problem with Generalized Method of Moments

(GMM) estimation. That is pretty basic but I am stuckt.

I have the function to be minimized with respect to parameter called

'param':

function f = myfunction(param)

g = ... %g: data and 'param' depending vector valued function

W =... % W: matrix fixed in the first iteration, dependent from

'param' and data

f = g'*inv(W)*g --> (*)

now I minimize f with respect to 'param':

[param]=fminunc(.matlab.todaysummary.com.myfunction, 'starting value for param')

and get vector 'param' which optimizes 'myfunction'. Then I calculate

W with respect to (new) 'param'-->get W1 and want to apply

received matrix W1 in 'myfunction', simply replace in the function

formula so f = g'*W1*g see(*).

how to do it automatically'

Then optimize myfunction (with W1) with respect to 'param' get new W

say W2, then apply to (*) and iterate in that manner.

I am sure all GMM users know the problem.

best, robert

*http://matlab.todaysummary.com/q_matlab_20063.html*

All Comments

Leave a comment...

- 9 Comments
- Rob,
The structure of the code would be along these lines.

while (stopping_condition == false)

...

W=...

fhandle = .matlab.todaysummary.com.(x)myfunction(x,W)

[param]=fminunc(fhandle, 'starting value for param')

...

loop

Notice that "myfunction" is nested within an anonymous function, that

way the function passed on to fminunc, changes according to new values

of W in each iteration.

#1; Thu, 08 May 2008 01:02:00 GMT

- Rob,
- Hi Phil, thanks a lot for help.
If you don't mind please have a look at the following. I tried to

apply your suggestions.

Is it correct the following code? since it stucked on

fhandle = .matlab.todaysummary.com.(x)myfunction(x,W) telling that:

"identifier" expected, "(" found. (**)

I changed a little bit and wrote:

while (stopping_condition == false) (***)

W = eye(4); % as starting matrix (fixed one)

fhandle = myfunction(.matlab.todaysummary.com.param,W); %here I changed as response to

indicated above (**), correct'

st = [0.01 0.2 0.01];

[param] = fminunc(fhandle,st)

W = autocov(param); % generates new W matrix by external function

'autocov'

loop

end

now matlab asks me about the line (***)

"Expected a variable, function, or constant, found "% comment".", I

don't know the stopping loop value of param since I want to estimate

it. What kind of stopping_value I need ' is it the number of

iterations?

would very appreciate your corrections to my version.

best, rob

Phil wrote:

>

> Rob,

> The structure of the code would be along these lines.

> while (stopping_condition == false)

> ...

> W=...

> fhandle = .matlab.todaysummary.com.(x)myfunction(x,W)

> [param]=fminunc(fhandle, 'starting value for param')

> ...

> loop

> Notice that "myfunction" is nested within an anonymous function,

> that

> way the function passed on to fminunc, changes according to new

> values

> of W in each iteration.

>

#2; Thu, 08 May 2008 01:03:00 GMT

- Hi Phil, thanks a lot for help.
- robert wrote:
>

> Hi Phil, thanks a lot for help.

> If you don't mind please have a look at the following. I tried to

> apply your suggestions.

> Is it correct the following code? since it stucked on

> fhandle = .matlab.todaysummary.com.(x)myfunction(x,W) telling that:

> "identifier" expected, "(" found. (**)

You cannot use anonymous functions

unless you upgrade your matlab release.

They were not introduced until release

14.

John

#3; Thu, 08 May 2008 01:04:00 GMT

- robert wrote:
- Rob,
First of all, did u change your "myfunction" to accept W as one of its?

i.e.

gmm_value = myfunction(params,W)

having done so, the instruction

fhandle = .matlab.todaysummary.com.(x)myfunction(x,W)

should work properly.

Further, you have to initialize W outside of the while...loop

statement, if not in each iteration you'll have W=eye(4) as the input

to "myfunction".

It should then look like this:

W = eye(4); % as starting matrix (fixed one)

while (stopping_condition == false)

fhandle = .matlab.todaysummary.com.(x)myfunction(x,W);

st = [0.01 0.2 0.01];

[param] = fminunc(fhandle,st)

W = autocov(param)

loop

(by the way...as u are trying to get GMM work, I'm at the moment

trying to code a quasi-maximum likelihood estimation)

robert wrote:

> Hi Phil, thanks a lot for help.

> If you don't mind please have a look at the following. I tried to

> apply your suggestions.

> Is it correct the following code? since it stucked on

> fhandle = .matlab.todaysummary.com.(x)myfunction(x,W) telling that:

> "identifier" expected, "(" found. (**)

> I changed a little bit and wrote:

> while (stopping_condition == false) (***)

> W = eye(4); % as starting matrix (fixed one)

> fhandle = myfunction(.matlab.todaysummary.com.param,W); %here I changed as response to

> indicated above (**), correct'

> st = [0.01 0.2 0.01];

> [param] = fminunc(fhandle,st)

> W = autocov(param); % generates new W matrix by external function

> 'autocov'

> loop

> end

> now matlab asks me about the line (***)

> "Expected a variable, function, or constant, found "% comment".", I

> don't know the stopping loop value of param since I want to estimate

> it. What kind of stopping_value I need ' is it the number of

> iterations?

> would very appreciate your corrections to my version.

> best, rob

> Phil wrote:

#4; Thu, 08 May 2008 01:05:00 GMT

- Rob,
- Phil, I followed your suggestions about myfunction(x,W) but still my
procedure stucks in line:

> fhandle = .matlab.todaysummary.com.(x)myfunction(x,W)

telling:

"identifier" expected, "(" found.

(concerns the bracket before x)

I work on matlab release 13th so as John posted anonymous functions

are not supported until release 14th..

Do you have any suggestions, since I am not s much familiar with

handling procedure' in matlab

rob

Phil wrote:

>

> Rob,

> First of all, did u change your "myfunction" to accept W as one of

> its?

> i.e.

> gmm_value = myfunction(params,W)

> having done so, the instruction

> fhandle = .matlab.todaysummary.com.(x)myfunction(x,W)

> should work properly.

> Further, you have to initialize W outside of the while...loop

> statement, if not in each iteration you'll have W=eye(4) as the

> input

> to "myfunction".

> It should then look like this:

> W = eye(4); % as starting matrix (fixed one)

> while (stopping_condition == false)

> fhandle = .matlab.todaysummary.com.(x)myfunction(x,W);

> st = [0.01 0.2 0.01];

> [param] = fminunc(fhandle,st)

> W = autocov(param)

> loop

> (by the way...as u are trying to get GMM work, I'm at the moment

> trying to code a quasi-maximum likelihood estimation)

>

>

>

> robert wrote:

to

function

comment".",

> I

> estimate

> function,

to new

>

#5; Thu, 08 May 2008 01:06:00 GMT

- Phil, I followed your suggestions about myfunction(x,W) but still my
- Hi John,
yes it seems to be like that, do you have any suggestions how to

overcome this obstacle in Matlab release 13th'

will very appreciate any help

rob

John D'Errico wrote:

>

> robert wrote:

to

> You cannot use anonymous functions

> unless you upgrade your matlab release.

> They were not introduced until release

> 14.

> John

#6; Thu, 08 May 2008 01:07:00 GMT

- Hi John,
- rob wrote:
>

> Hi John,

> yes it seems to be like that, do you have any suggestions how to

> overcome this obstacle in Matlab release 13th'

> will very appreciate any help

> rob

As I expected. There are just so many R13

holdouts, even R11 and 12.

Here is a snippet of code from a previous

response in this thread. I'ver repaired

it in a few places, as well as changed

the call to fminunc to use the older

style for calling it.

W = eye(4); % as starting matrix (fixed one)

param0 = [0.01 0.2 0.01];

tol = 1.e-6;

delta = inf;

while (delta > tol)

[param] = fminunc('myfunction',param0,[],W);

W = autocov(param);

delta= norm(param-param0);

param0 = param;

end

You must also have defined the function

'myfunction' to take TWO arguments: param

and W, in that order.

Choose your own value for tol as desired.

HTH,

John D'Errico

#7; Thu, 08 May 2008 01:08:00 GMT

- rob wrote:
- it is working! thanks a lot
one more question, how to count the number of iterations in this

fminunc specification' since options=optimset( iterations) fails

rob

John D'Errico wrote:

>

> rob wrote:

to

> As I expected. There are just so many R13

> holdouts, even R11 and 12.

> Here is a snippet of code from a previous

> response in this thread. I'ver repaired

> it in a few places, as well as changed

> the call to fminunc to use the older

> style for calling it.

> W = eye(4); % as starting matrix (fixed one)

> param0 = [0.01 0.2 0.01];

> tol = 1.e-6;

> delta = inf;

> while (delta > tol)

> [param] = fminunc('myfunction',param0,[],W);

> W = autocov(param);

> delta= norm(param-param0);

> param0 = param;

> end

> You must also have defined the function

> 'myfunction' to take TWO arguments: param

> and W, in that order.

> Choose your own value for tol as desired.

> HTH,

> John D'Errico

#8; Thu, 08 May 2008 01:09:00 GMT

- it is working! thanks a lot
- rob wrote:
>

> it is working! thanks a lot

> one more question, how to count the number of iterations in this

> fminunc specification' since options=optimset( iterations) fails

> rob

Why does optimset fail? It will only

fail if you fail to pass in options to

fminunc.

Note the 3rd argument to fminunc.

Are you asking how to count the number

of iterations on W? If so, nothing stops

you from counting them.

John

#9; Thu, 08 May 2008 01:10:00 GMT

- rob wrote: