2

I am following a very detailed post from Quantitative Finance, however, my problem is a coding one.

I am trying to estimate a GARCH(1,1) model (not using a statistical toolbox and rather a long hand method, the reason for this is I really want to understand the in's and outs of the model).

I have posted a picture of the steps I need to complete for ease,

From linked post

I am stuck on how I can write this log-likelihood in MATLAB. I essentially need to maximize the log-likehood over the iterations:

enter image description here

My attempt:

custlogpdf = @(u1,sigma) -1/2*sum( log(2*pi) + log(sigma^2) + (u1^2)./sigma^2 );
phat = mle(u1,'nloglf', custlogpdf, 'start' 0.05)

Could anyone point me in the right direction to use maximum likelihood estimation of the function?

The error I am getting from my attempt:

    Error in test (line 40)
phat = mle(u1,'nloglf', custlogpdf, 'start', 0.05)

Caused by:
    Error using test>@(u1,sigma)-1/2*sum(log(2*pi)+log(sigma^2)+((u1)^2)/sigma^2)
    Too many input arguments.
Tony Chivers
  • 191
  • 11
  • 1
    Please explain what about your attempt doesn't work (wrong result? error? ...). – Dev-iL Jan 21 '19 at 13:20
  • Error: File: test.m Line: 39 Column: 16 Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters. – Tony Chivers Jan 21 '19 at 13:21
  • 1
    It's best if you [edit] your post and add the error message **in its entirety, exactly as it appears in the console** (so that we also see which expression it refers to). Regardless, this error sounds like a simple typo of missing `(` or `)` (i.e. not even a "runtime" error, but a "compilation" error). Are you using the MATLAB editor for your code files? If yes - doesn't it show a red line under the problematic expression/line? If you're using some other text editor, try running [`checkcode`](https://www.mathworks.com/help/matlab/ref/checkcode.html) on your script. – Dev-iL Jan 21 '19 at 13:27
  • Thanks, I have added the error exactly as it appears, its about to many input arguments @Dev-iL – Tony Chivers Jan 21 '19 at 13:37
  • Please @Dev-iL any help or ideas? – Tony Chivers Jan 21 '19 at 13:51

1 Answers1

0

The documentation of mle states that:

This custom function accepts the following input arguments: params, data, cens, freq.

nloglf must accept all four arguments even if you do not use the 'Censoring' or 'Frequency' name-value pair arguments. You can write 'nloglf' to ignore cens and freq arguments in that case.

nloglf returns a scalar negative loglikelihood value and optionally, a negative loglikelihood gradient vector (see the 'GradObj' field in 'Options').

...but your function handle only accepts 2 inputs => MATLAB is trying to pass 4 inputs into a function that only accepts 2 => Error: Too many input arguments..

What you should probably do is define a proper function which has 4 inputs and 1 or 2 outputs, then refer to it using @funcName when calling mle, i.e.:

function test
...
initialGuess = [valForParam1, valForParamN];
phat = mle(u1, 'nloglf', custlogpdf, 'start', initialGuess );
...

% function [likelihood, likeGrad] = custlogpdf(params, data, cens, freq)
function [likelihood, likeGrad] = custlogpdf(params, data, ~, ~)
likelihood = ... % some function of params + data
if nargout == 2
  likeGrad = ... % some other function of the inputs
end
Community
  • 1
  • 1
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • do can I keep my function the same, and then add something to line 40 like this? phat = mle(u1,'nloglf', custlogpdf, 'start', 0.05, 'cens', nloglf, 'freq' nloglg) – Tony Chivers Jan 21 '19 at 15:28
  • 1
    That's absolutely not what I said. The problem is with how you define the function `custlogpdf`, not with how you call `mle`. In anonymous-function form it should be something like `custlogpdf = @(params, data, cens, freq) ...`, and since this would likely take more than one line to define - I suggested using a "named" function and not an anonymous one. – Dev-iL Jan 21 '19 at 15:56
  • which named function do you suggest, sorry but I really don't understand, thanks so much for the help by the way – Tony Chivers Jan 21 '19 at 19:20
  • function newpdf = lognormpdf(data,sigma) newpdf = -1/2*sum(log(2*pi)+log(sigma^2)+((u1)^2)/sigma^2); phat = mle(u1,'logpdf', newpdf, 'start', 0.05); end – Tony Chivers Jan 21 '19 at 19:30
  • is this something that you mean – Tony Chivers Jan 21 '19 at 19:30
  • i realise you're trying to help, but imagine a novice phd student, trying to study with a tough question from their supervisor. And your answers are great, helpful for people who know what they're doing, but for me I am clueless. I just need some direction on this. Could I copy and paste your answer and use it? – Tony Chivers Jan 21 '19 at 20:02
  • Yeah, of course you can use it, that's the point of SO :) It's just that this only helps with the first stage of your problem (some more effort might be required to get `mle` to work correctly). – Dev-iL Jan 21 '19 at 20:05
  • thats the part I am struggling with, how can I actually get this to work. – Tony Chivers Jan 21 '19 at 20:28