attachment:GLM1_script.m of SignalAnalysisMatlabSchedule - Methods
location: attachment:GLM1_script.m of SignalAnalysisMatlabSchedule

Attachment 'GLM1_script.m'

Download

   1 %% Basis functions and matrix inversion
   2 
   3 1*[1 0 0] + 2*[0 1 0] + 3*[0 0 1]
   4 
   5 1*[1 1 1] + 1*[0 1 0] + 2*[0 0 1]
   6 
   7 -1*[1 -1 0] + -1*[0 1 -1] + 2*[1 1 1]
   8 
   9 % creating a linear system
  10 bf1 = [1 -1 0];
  11 bf2 = [0 1 -1];
  12 bf3 = [1 1 1];
  13 
  14 % You may call this the "design matrix"
  15 BF = [bf1', bf2', bf3'];
  16 
  17 data = [1 2 3]';
  18 
  19 % solve equation
  20 sol = inv(BF)*data
  21 
  22 % check if it's really a solution
  23 BF*sol
  24 
  25 % check if inverse does what it's supposed to do
  26 inv(BF)*BF
  27 BF*inv(BF)
  28 
  29 % note: matrix inversion is not element-wise division
  30 inv(BF)
  31 BF.^-1
  32 
  33 % you can also use
  34 y = BF \ data
  35 y = pinv(BF)*data
  36 
  37 % let's introduce collinearity and see what happens
  38 BF(:,1) = [1 1 1]'
  39 
  40 % let's "fix" the collinearity
  41 BF(1,3) = 1 + 1e-12
  42 
  43 inv(BF)
  44 % the inverse matrix has huge values - this may amplify errors in the data
  45 
  46 %% parameter estimation - forward model
  47 x = 0:0.1:8*pi; % abscissa values
  48 
  49 figure;
  50 y1 = x';         % straight line (note transpose)
  51 plot(x, y1);
  52 
  53 figure;
  54 y2 = sin(x)';    % sine curve
  55 plot(x, y2);
  56 
  57 figure;
  58 y3 = exp(-x/2)'; % exponential function
  59 plot(x, y3);
  60 
  61 % Generate data with some noise
  62 figure;
  63 y = y1/5 + y2 + 5*y3 + 0.5*randn(length(y1),1);
  64 plot(x, y);
  65 
  66 whos
  67 %% Estimating the parameters using minimum least-squares (MNLS)
  68 % creating the design matrix
  69 M = [y1 y2 y3];
  70 whos
  71 % note that in this case there are many more data points than parameters
  72 % i.e. the problem is over-determined
  73 
  74 % check correlation among basis functions
  75 cM = corr(M)
  76 imagesc(cM)
  77 colorbar
  78 
  79 % (pseudo)inverting the design matrix
  80 Minv = pinv(M);
  81 whos
  82 
  83 % getting the solution
  84 b = Minv*y
  85 
  86 % checking how solution predicts the data
  87 figure;
  88 ypred = b(1)*y1 + b(2)*y2 + b(3)*y3;
  89 plot(x, ypred)
  90 
  91 % compare with "ground truth"
  92 hold on;    % don't overwrite figure
  93 yreal = y1/5 + y2 + 5*y3;
  94 plot(x, yreal, 'r');
  95 
  96 % plot the difference between measured and predicted data
  97 figure;
  98 ydiff = y - ypred; 
  99 plot(x, ydiff);
 100 
 101 % compute your very own "pseudoinverse"
 102 mypinv = inv(M'*M)*M';
 103 
 104 % check if the results is the same
 105 b2 = mypinv*y;
 106 
 107 % it could have been so easy:
 108 b3 = regress(y, M)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2018-11-15 14:50:04, 2556.1 KB) [[attachment:ArcherBoyd+Guerit_vocoder_Nov18.zip]]
  • [get | view] (2019-11-21 17:21:33, 1661.8 KB) [[attachment:CBU_FunctionsVocoder_22Nov19.zip]]
  • [get | view] (2019-11-19 16:53:57, 4.6 KB) [[attachment:Code_Optimisation_20Nov19.zip]]
  • [get | view] (2016-03-09 10:59:16, 259.0 KB) [[attachment:DP_Optimisation_2016.zip]]
  • [get | view] (2015-01-20 17:22:38, 614.9 KB) [[attachment:EMEGdata.mat]]
  • [get | view] (2016-02-03 12:32:14, 26.8 KB) [[attachment:ExampleScripts.zip]]
  • [get | view] (2018-01-12 13:39:53, 3150.4 KB) [[attachment:Filtering&Oscillations_ForUpload_TallieJan18.pdf]]
  • [get | view] (2018-07-19 14:49:35, 2.5 KB) [[attachment:Functions and Calculus.m]]
  • [get | view] (2018-07-19 14:49:30, 433.8 KB) [[attachment:Functions and Calculus.pdf]]
  • [get | view] (2015-02-02 09:27:07, 522.0 KB) [[attachment:GLM1.pdf]]
  • [get | view] (2019-11-19 13:06:58, 790.4 KB) [[attachment:GLM1_19Nov19.pdf]]
  • [get | view] (2019-11-19 13:07:11, 2.5 KB) [[attachment:GLM1_19Nov19.zip]]
  • [get | view] (2018-01-12 13:00:28, 9.0 KB) [[attachment:GLM1_Jan2018.m]]
  • [get | view] (2018-01-12 13:02:14, 354.1 KB) [[attachment:GLM1_Jan2018.pdf]]
  • [get | view] (2018-11-13 11:34:38, 11.5 KB) [[attachment:GLM1_Nov2018.m]]
  • [get | view] (2018-11-13 11:34:31, 1417.1 KB) [[attachment:GLM1_Nov2018.pptx]]
  • [get | view] (2018-11-13 12:07:58, 11.5 KB) [[attachment:GLM1_Nov2018_ES18.m]]
  • [get | view] (2018-11-13 12:07:52, 271.1 KB) [[attachment:GLM1_Nov2018_ES18.pdf]]
  • [get | view] (2015-02-02 09:27:27, 2.1 KB) [[attachment:GLM1_script.m]]
  • [get | view] (2016-02-11 18:26:35, 560.4 KB) [[attachment:GLM_part1.pdf]]
  • [get | view] (2018-01-08 09:49:09, 8.5 KB) [[attachment:GLM_workshop.m]]
  • [get | view] (2015-02-18 12:51:09, 580.2 KB) [[attachment:IntroCalculus 18Feb15.pdf]]
  • [get | view] (2015-03-02 18:21:14, 4.3 KB) [[attachment:IntroFilteringOscillations_25Feb15.zip]]
  • [get | view] (2016-02-18 15:52:26, 237.9 KB) [[attachment:IntroGLM2_17Feb16.pdf]]
  • [get | view] (2015-02-04 11:58:10, 125.6 KB) [[attachment:IntroGLM2_4Feb15.pdf]]
  • [get | view] (2016-02-04 17:19:34, 445.3 KB) [[attachment:IntroMatrixAlgebra.pdf]]
  • [get | view] (2015-03-04 12:00:10, 1.3 KB) [[attachment:IntroOptimisation_4Mar15.zip]]
  • [get | view] (2018-01-09 12:44:37, 19.3 KB) [[attachment:Matlab_Files.zip]]
  • [get | view] (2015-01-20 17:29:01, 6.1 KB) [[attachment:MatrixAlgebra.m]]
  • [get | view] (2018-11-13 12:08:14, 917.4 KB) [[attachment:Matrix_Algebra_AT18.pdf]]
  • [get | view] (2015-03-02 18:19:54, 260.9 KB) [[attachment:Olaf FilteringOscillations 25Feb15.pdf]]
  • [get | view] (2015-02-18 12:51:00, 1.8 KB) [[attachment:Olaf IntroCalculus 18Feb15 Matlab.zip]]
  • [get | view] (2016-02-18 15:52:39, 2.9 KB) [[attachment:Olaf IntroGLM2 17Feb2016.m]]
  • [get | view] (2015-02-04 11:58:19, 2.9 KB) [[attachment:Olaf IntroGLM2 4Feb15.m]]
  • [get | view] (2015-03-04 12:00:04, 113.0 KB) [[attachment:Olaf Optimisation 4Mar15.pdf]]
  • [get | view] (2015-01-16 13:56:32, 3.2 KB) [[attachment:Sampling.m]]
  • [get | view] (2015-01-16 13:56:14, 754.0 KB) [[attachment:Sampling.pdf]]
  • [get | view] (2016-02-04 17:19:23, 1107.2 KB) [[attachment:SignalSamplingNoise.pdf]]
  • [get | view] (2018-11-13 12:08:19, 1479.3 KB) [[attachment:Signal_sampling_noise_AT18.pdf]]
  • [get | view] (2018-01-11 09:31:52, 259.2 KB) [[attachment:optimisation.zip]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.