tpavlic

example_num_opt_SHS: Numerical optimization example for mean

Sep 10th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.25 KB | None | 0 0
  1. function [estlambda, lambda] = example_num_opt_SHS
  2. %
  3. % Numerical optimization example for mean-field model of SHS
  4. %
  5.  
  6. %% For testing, generate some random trajectories
  7.  
  8. % Sample once every dt seconds
  9. dt = 5;
  10. t = 0:dt:120;
  11.  
  12. % Noise scale
  13. noisesd = 0.01;
  14.  
  15. % Arbitrary rates that we hopefully can fit later
  16. % (scale to accomodate sampling interval)
  17. lambda = [ 0 2 3 ; 5 0 9 ; 2 7 0 ];
  18. lambda = lambda/max(max(lambda)) * 1/(2*dt);
  19.  
  20. % Graph laplacian (to generate fake data)
  21. laplacian = lambda.*(1-eye(size(lambda))) - (1-eye(size(lambda)))*lambda.*eye(size(lambda));
  22.  
  23. % Fake data, with random initial condition and noise added at every step
  24.  
  25. % Initial condition for mean-field trajectory is randomized
  26. meandata = zeros(length(lambda), length(t));
  27. meandata(:, 1) = randperm(length(lambda))' + noisesd*randn(length(lambda),1);
  28.  
  29. % Generate mean-field trajectory based on Laplacian and some noise
  30. for i = 2:length(t)
  31.     meandata(:, i) = expm(laplacian*t(i))*meandata(:, 1) + noisesd*randn(length(lambda),1);
  32. end
  33.  
  34. %% Use a least-squares fit to get lambda back out from meandata
  35.  
  36. % fmincon (non-linear optimization with constraints) arguments:
  37. %    1. Function handle defining the metric to minimize
  38. %    2. Initial condition (all of the off-diagonal elements of lambda matrix)
  39. %    3. Matrix "A" in the inequality constraint A*x <= b
  40. %    4. Vector "b" in the inequality constraint A*x <= b
  41. % In the linear inequality constraint, A*x <= b, "x" is a feasible solution
  42. % to the optimization problem.
  43.  
  44. % The output vector is the optimal off-diagonal elements of the rate matrix
  45. estlv = fmincon( @(lv)( lsmetric(lv, meandata, t) ), ...
  46.             randperm(length(lambda)*(length(lambda) - 1)), ...
  47.             [ -eye(length(lambda)*(length(lambda) - 1)) ; ...
  48.               eye(length(lambda)*(length(lambda) - 1)) ], ...
  49.             [ zeros(length(lambda)*(length(lambda) - 1), 1) ;
  50.               (1/(2*5))*ones(length(lambda)*(length(lambda) - 1), 1) ] );
  51.  
  52. % These lines turn the estlv vector into an estimated rate matrix estlambda
  53. estlambda = zeros( length(lambda) );
  54. offdiagonals = setdiff(1:(length(lambda))^2, ...
  55.                        (1:length(lambda))+length(lambda)*((1:length(lambda))-1));
  56. estlambda(offdiagonals) = estlv;
  57.  
  58. end
  59.  
  60. %% Metric for least-squares fit
  61.  
  62. % This metric sums up the squared differences between the data (i.e.,
  63. % meandata at times t) and a trajectory defined by "lambdavec"
  64.  
  65. function lssum = lsmetric( lambdavec, meandata, t )
  66.     % This maps the number of elements in lambdavec to the number of states
  67.     num_states = max( roots( [1 -1 -length(lambdavec)] ) );
  68.  
  69.     % This generates a rate matrix whose diagonal entries are zero and all
  70.     % off-diagonal entires come from lambdavec
  71.     lambda = zeros( num_states );
  72.     offdiagonals = setdiff(1:(num_states^2), ...
  73.                            (1:num_states)+num_states*((1:num_states)-1));
  74.     lambda(offdiagonals) = lambdavec;
  75.  
  76.     % This generates the Laplacian from the rate matrix
  77.     laplacian = lambda.*(1-eye(size(lambda))) - (1-eye(size(lambda)))*lambda.*eye(size(lambda));
  78.  
  79.     % This sums up the squared differences over time
  80.     lssum = 0;
  81.     for i = 2:length(t)
  82.         lssum = lssum + norm( expm(laplacian*t(i))*meandata(:,1) - meandata(:,i) )^2;
  83.     end
  84.  
  85. end
Advertisement
Add Comment
Please, Sign In to add comment