-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspm_COVID.m
134 lines (116 loc) · 5.24 KB
/
spm_COVID.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function [F,Ep,Cp,pE,pC,Eh] = spm_COVID(Y,pE,pC,hC)
% Variational inversion of COVID model
% FORMAT [F,Ep,Cp,pE,pC,Eh] = spm_COVID(Y,pE,pC,hC)
% Y - timeseries data
% pE - prior expectation of parameters
% pC - prior covariances of parameters
% hC - prior covariances of precisions
%
% F - log evidence (negative variational free energy)
% Ep - posterior expectation of parameters
% Cp - posterior covariances of parameters
% pE - prior expectation of parameters
% pC - prior covariances of parameters
%
% This routine inverts a generative model of some timeseries data (Y),
% returning a variational (free energy) bound on log model evidence, and
% posterior densities of the model parameters (in terms of posterior
% expectations and covariances). This inversion uses standard variational
% Laplace; i.e., a (natural) gradient ascent on variational free energy
% under the Laplace assumption (i.e.,Gaussian priors and likelihood
% model).
%
% Model inversion entails specifying the generative model in terms of a log
% likelihood function and priors. These priors cover the model parameters
% and precision parameters that determine the likelihood of any given data.
% The precision priors (sometimes referred to as hyper priors) are
% specified in terms of the expectation and covariance of the log precision
% of random fluctuations about the predicted outcome variable. In this
% instance, the outcome variables are campus. This means that a square root
% transform allows a Gaussian approximation to the implicit (Poisson)
% likelihood distribution over observations.
%
% The log likelihood function is provided as a subroutine in the (Matlab)
% code (spm_COVID_LL) below. However, because of Gaussian assumptions about
% the likelihood, we can use a simpler scheme, using the predicted outcomes
% from spm_COVID_gen, following a square root transform. The square root
% transform is treated as a feature selection or link function; please see
% the subroutine spm_COVID_FS.
%__________________________________________________________________________
% Copyright (C) 2020 Wellcome Centre for Human Neuroimaging
% Karl Friston
% $Id: spm_COVID.m 7866 2020-05-30 09:57:38Z karl $
# SPDX-License-Identifier: GPL-2.0
% Gaussian priors over model parameters
%--------------------------------------------------------------------------
if nargin < 3
[pE,pC] = spm_COVID_priors;
end
if nargin < 4
hC = 1/256;
end
% complete model specification
%--------------------------------------------------------------------------
M.G = @spm_COVID_gen; % generative function
M.L = @spm_COVID_LL; % log-likelihood function
M.FS = @spm_COVID_FS; % feature selection (link function)
M.pE = pE; % prior expectations (parameters)
M.pC = pC; % prior covariances (parameters)
M.hE = 0; % prior expectation (log-precision)
M.hC = hC; % prior covariances (log-precision)
M.T = size(Y,1); % number of samples
U = 1:size(Y,2); % number of response variables
% model inversion with Variational Laplace (Gauss Newton)
%--------------------------------------------------------------------------
[Ep,Cp,Eh,F] = spm_nlsi_GN(M,U,Y);
% alternative inversion with Variational Laplace (curvature of log likelihood)
%--------------------------------------------------------------------------
% [Ep,Cp,F] = spm_nlsi_Newton(M,U,Y); Eh = 1;
return
% likelihood model
%__________________________________________________________________________
function L = spm_COVID_LL(P,M,U,Y)
% log-likelihood function
% FORMAT L = spm_COVID_LL(P,M,U,Y)
% P - model parameters
% M - model structure
% U - inputs or control variables
% Y - outputs or response variables
%
% This auxiliary function evaluates the log likelihood of a sequence of
% count data under Poisson assumptions
%__________________________________________________________________________
% generate prediction
%--------------------------------------------------------------------------
[T,N] = size(Y);
M.T = T;
y = M.G(P,M,U);
y = y(1:T,1:N);
% ensure all counts are greater than zero
%--------------------------------------------------------------------------
i = logical(Y < 1); Y(i) = 1;
i = logical(y < 1); y(i) = 1;
% place MDP in trial structure
%--------------------------------------------------------------------------
p = spm_Npdf(y(:),Y(:),Y(:));
L = sum(log(p + 1e-6));
% link function for feature selection (square root transform)
%__________________________________________________________________________
function Y = spm_COVID_FS(Y)
% feature selection for COVID model
% FORMAT Y = spm_COVID_FS(Y)
% P - model parameters
% M - model structurel
% U - inputs or control variables
% Y - outputs or response variables
%
% This auxiliary function takes appropriate gradients and performs a square
% root transform
%__________________________________________________________________________
% ensure all counts are greater than zero
%--------------------------------------------------------------------------
i = logical(Y < 1); Y(i) = 1;
% square root transform
%--------------------------------------------------------------------------
Y = sqrt(Y);
return