loglikelihood.m 917 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. % [L,bic]=loglikelihood(M,y,np)
  2. %
  3. % Input
  4. % M: a vector of probabilities (model predictions)
  5. % y: a vector of binomial outcomes
  6. % np: the number of parameters in the model
  7. %
  8. % Output
  9. % L: the log likelihood
  10. % bic: the bayesian information criteria
  11. function [L,bic]=loglikelihood(M,y,np)
  12. % if isnumeric(M)
  13. N = nan+M;
  14. N(y==1)=M(y==1);
  15. N(y==0)=1-M(y==0);
  16. N(isnan(N) | isnan(M) | isnan(y))=[];
  17. L=sum(log(N));
  18. bic=2*-L+np*log(numel(N));
  19. % not implemented yet
  20. % else
  21. % modeltype = class(M);
  22. % nobs = M.NumObservations;
  23. % resp = M.Variables.(M.ResponseName);
  24. % fit = fitted(M);
  25. % llpt = log(fitted(M)*resp + (1-fitted(M))*(1-resp));
  26. % llpt(isnan(resp))=[];
  27. % switch modeltype
  28. % case 'GeneralizedLinearMixedModel'
  29. % case {'GeneralizedLinearModel','NonLinearModel'}
  30. % nparams = M.NumPredictors;
  31. % end