Mercurial > hg > ltpda
comparison m-toolbox/classes/+utils/@math/chi2.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
2 % | |
3 % DESCRIPTION: Compute chi2 and its gradient. | |
4 % | |
5 % VERSION: $Id: chi2.m,v 1.1 2011/03/10 16:55:34 congedo Exp $ | |
6 % | |
7 % HISTORY: 10-03-2011 G. Congedo | |
8 % | |
9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
10 | |
11 function [chi2,g] = chi2(p,data,models,Dmodels,lb,ub) | |
12 | |
13 weights = 1; | |
14 Nfreeparams = numel(p); | |
15 Nmdls = numel(models); | |
16 Ndata = numel(data(:,1)); | |
17 Np = numel(p); | |
18 | |
19 mdldata = zeros(Ndata,Nmdls); | |
20 for kk=1:Nmdls | |
21 mdldata(:,kk) = models{kk}(p); | |
22 end | |
23 res = (mdldata-data).*weights; | |
24 if all(p>=lb & p<=ub) | |
25 chi2 = res'*res; | |
26 chi2 = sum(diag(chi2)); | |
27 else | |
28 chi2 = 10e50; | |
29 end | |
30 | |
31 if nargout > 1 % gradient required | |
32 grad = cell(Nmdls,1); | |
33 g = zeros(Nmdls,Nfreeparams); | |
34 for kk=1:Nmdls | |
35 grad{kk} = zeros(Ndata,Np); | |
36 for ii=1:Np | |
37 grad{kk}(:,ii) = Dmodels{kk}{ii}(p); | |
38 g(kk,ii) = 2.*res(:,kk)'*grad{kk}(:,ii); | |
39 end | |
40 end | |
41 if Nmdls>1 | |
42 g = sum(g); | |
43 end | |
44 end | |
45 | |
46 end |