-
Notifications
You must be signed in to change notification settings - Fork 16
/
CIF.m
1031 lines (891 loc) · 46.4 KB
/
CIF.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef CIF < handle
%CIF - Conditional Intensity function.
%<a href="matlab: methods('CIF')">methods</a>
%
%Reference page in Help browser
%<a href="matlab: doc('CIF')">doc CIF</a>
%
% nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology
% Cajigas, I, Malik, WQ, Brown, EN
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License as published
% by the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software Foundation,
% Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
properties
b %Regression Coefficients
varIn %The labels for the coefficients in b
stimVars %The subset of labels that correspond to the stimulus
indepVars %
stats
fitType % binomial or poisson - determines how lambda is related to the regression coefficients
lambdaDelta % symbolic expression for the product of lambda and delta
lambdaDeltaGamma
LogLambdaDeltaGamma
spikeTrain
gradientLambdaDelta
gradientLogLambdaDelta %symbolic expression for first partial w.r.t. to stimulus variables
gradientLambdaDeltaGamma %symbolic expression for first partial w.r.t. to history coefficient variables
gradientLogLambdaDeltaGamma %symbolic expression for first partial w.r.t. to history coefficient variables
jacobianLambdaDelta
jacobianLogLambdaDelta %symbolic expression for second partial w.r.t. to stimulus variables
jacobianLambdaDeltaGamma %symbolic expression for second partial w.r.t. to history variables
jacobianLogLambdaDeltaGamma %symbolic expression for second partial w.r.t. to history variables
history
histCoeffs
histCoeffVars %Defined in case we want to take derivatives with respect to the history params (as in the M-step of EM)
histVars
historyMat
% end
% properties (Hidden)
lambdaDeltaFunction % function handle to evaluate lambda*delta
lambdaDeltaGammaFunction
LogLambdaDeltaGammaFunction
gradientFunction % partial derivative of log(lambda*delta) w.r.t stimulus variables
gradientLogFunction
gradientFunctionGamma % partial derivative of lambda*delta w.r.t stimulus variables
gradientLogFunctionGamma % partial derivative of log(lambda*delta) w.r.t stimulus variables
jacobianFunction % second partial derivative of (lambda*delta) w.r.t. to stimulus variables
jacobianLogFunction
jacobianFunctionGamma % second partial derivative of lambda*delta w.r.t. to stimulus variables
jacobianLogFunctionGamma % second partial derivative of log(lambda*delta) w.r.t. to stimulus variables
argstr % parse out stimulus variables by each element since the above functions dont take vector inputs
argstrLDGamma
end
methods
function cifObj = CIF(beta,Xnames,stimNames,fitType,histCoeffs,historyObj,nst)
% cifObj = CIF(beta,Xnames,stimNames,fitType)
% beta: regression coefficients
%
% Xnames: names of the variables in the order they are
% specified by beta.
%
% stimNames: names of the subset of variables that are define
% the stimulus.
%
% fitType: poisson or binomial - defines how the parameters are
% related to the CIF. For poisson, lamda*delta =
% exp(X*beta). For binomial, lambda*delta=
% exp(X*beta)/(1+exp(X*beta));
%
% histCoeffs: coefficients for each of the history windows
% defined in historyObj
%
% historyObj: an object of class History that defines the how
% the spiking activity is being windowed. This
% input could also be a vector of windowTimes to be
% used in creating the historyObj.
if(nargin<7)
cifObj.spikeTrain = [];
else
cifObj.spikeTrain = nst.nstCopy;
end
if(nargin<6)
cifObj.history=[];
else
cifObj.setHistory(historyObj);
end
if(nargin<5)
cifObj.histCoeffs = [];
else
[r,c] = size(histCoeffs);
if(r==1)
cifObj.histCoeffs = histCoeffs;
elseif(c==1)
cifObj.histCoeffs = histCoeffs';
else
error('History Coefficient vector must have one dimension equal to 1');
end
end
if(nargin<4)
fitType = 'poisson';
end
if(isa(Xnames,'sym'))
XnamesTemp=cell(length(Xnames),1);
for i=1:length(beta)
XnamesTemp{i} = char(Xnames(i));
end
Xnames=XnamesTemp;
end
% Define input variables as a vector;
[r,c] = size(Xnames);
if(r==1)
Xnames = Xnames';
cifObj.varIn = sym(Xnames);
elseif(c==1)
cifObj.varIn = sym(Xnames);
else
error(' Must have one dimension equal to 1');
end
% Define stimulus variables as a vector
[r,c] = size(stimNames);
if(r==1)
cifObj.stimVars = sym(stimNames');
elseif(c==1)
cifObj.stimVars = sym(stimNames);
else
error(' Must have one dimension equal to 1');
end
% Define beta as a row vector
if(isnumeric(beta))
[r,c] = size(beta);
if(r==1)
cifObj.b = beta;
elseif(c==1)
cifObj.b = beta';
elseif(isempty(beta))
% error('Coefficient vector must have one dimension equal to 1');
%define beta as symbolic
betaLabel=cell(1,length(cifObj.varIn));
for i=1:length(cifObj.varIn)
betaLabel{i} = strcat('b',num2str(i));
end
display('Beta is being treated as symbolic! Must provide an input vector length(beta)+length(Xnames) to evaluate');
cifObj.b = sym(betaLabel);
beta = cifObj.b;
allVarNames = cell(length(Xnames)+length(betaLabel),1);
allVarNames(1:length(betaLabel)) = betaLabel;
allVarNames((length(betaLabel)+1):(length(betaLabel)+length(Xnames)))=Xnames;
cifObj.varIn = sym(allVarNames);
end
elseif(isa(beta,'cell'))
[r,c] = size(beta);
if(r==1)
betaLabel = beta;
elseif(c==1)
betaLabel = beta';
else
error(' Beta Must have one dimension equal to 1');
end
cifObj.b = sym(betaLabel);
beta = cifObj.b;
allVarNames = cell(length(Xnames)+length(betaLabel),1);
allVarNames(1:length(betaLabel)) = betaLabel;
allVarNames((length(betaLabel)+1):(length(betaLabel)+length(Xnames)))=Xnames;
cifObj.varIn = sym(allVarNames);
elseif(isa(beta,'sym'))
betaLabel=cell(1,length(beta));
for i=1:length(beta)
betaLabel{i} = char(beta(i));
end
cifObj.b = sym(betaLabel);
beta = cifObj.b;
allVarNames = cell(length(Xnames)+length(betaLabel),1);
allVarNames(1:length(betaLabel)) = betaLabel;
allVarNames((length(betaLabel)+1):(length(betaLabel)+length(Xnames)))=Xnames;
cifObj.varIn = sym(allVarNames);
end
%Define History variables if they were passed in
if(and(~isempty(cifObj.histCoeffs),~isempty(cifObj.history)))
for i=1:length(cifObj.histCoeffs)
histNames{i} = strcat('dN',num2str(i));
histCoeffVars{i} = strcat('gamma',num2str(i));
end
cifObj.histVars = sym(histNames');
cifObj.histCoeffVars = sym(histCoeffVars);
histCoeffsVarsTrans = sym(histCoeffVars');
else
cifObj.histVars = {};
cifObj.histCoeffVars = {};
histCoeffsVarsTrans = {};
end
% Define the functional form of the Conditonal Intensity
% Function based on how the data was fit.
cifObj.fitType = fitType;
if(isempty(cifObj.histVars))
if(strcmp(fitType,'poisson'))
cifObj.lambdaDelta = simplify(exp(beta*cifObj.varIn));
cifObj.lambdaDeltaFunction = matlabFunction(cifObj.lambdaDelta,'vars',cifObj.varIn);
elseif(strcmp(fitType,'binomial'))
cifObj.lambdaDelta = simplify(exp(beta*cifObj.varIn)./(1+exp(beta*cifObj.varIn)));
cifObj.lambdaDeltaFunction = matlabFunction(cifObj.lambdaDelta,'vars',symvar(cifObj.varIn));
end
else
if(strcmp(fitType,'poisson'))
cifObj.lambdaDelta = simplify(exp(beta*cifObj.varIn + cifObj.histCoeffs*cifObj.histVars));
cifObj.lambdaDeltaGamma = simplify(exp(beta*cifObj.varIn + cifObj.histCoeffVars*cifObj.histVars));
cifObj.lambdaDeltaFunction = matlabFunction(cifObj.lambdaDelta,'vars',[cifObj.varIn; cifObj.histVars]);
cifObj.lambdaDeltaGammaFunction = matlabFunction(cifObj.lambdaDeltaGamma,'vars',[cifObj.varIn; cifObj.histVars; histCoeffsVarsTrans]);
elseif(strcmp(fitType,'binomial'))
cifObj.lambdaDelta = simplify(exp(beta*cifObj.varIn + cifObj.histCoeffs*cifObj.histVars)./(1+exp(beta*cifObj.varIn + cifObj.histCoeffs*cifObj.histVars)));
cifObj.lambdaDeltaGamma = simplify(exp(beta*cifObj.varIn + cifObj.histCoeffVars*cifObj.histVars)./(1+exp(beta*cifObj.varIn + cifObj.histCoeffVars*cifObj.histVars)));
cifObj.lambdaDeltaFunction = matlabFunction(cifObj.lambdaDelta,'vars',symvar([cifObj.varIn; cifObj.histVars]));
cifObj.lambdaDeltaGammaFunction = matlabFunction(cifObj.lambdaDeltaGamma,'vars',symvar([cifObj.varIn; cifObj.histVars; histCoeffsVarsTrans]));
end
end
% Additional Functions needed for decoding
% The gradient of log(lambda*delta) and the jacobian of
% log(lambda*delta)
cifObj.gradientLambdaDelta = simplify(jacobian(cifObj.lambdaDelta,cifObj.stimVars));
cifObj.gradientLogLambdaDelta=simplify(jacobian(log(cifObj.lambdaDelta),cifObj.stimVars));
cifObj.gradientFunction = matlabFunction(cifObj.gradientLambdaDelta,'vars',[symvar(cifObj.varIn); cifObj.histVars]);
cifObj.gradientLogFunction = matlabFunction(cifObj.gradientLogLambdaDelta,'vars',[symvar(cifObj.varIn); cifObj.histVars]);
cifObj.jacobianLambdaDelta=simplify(jacobian(cifObj.gradientLambdaDelta,cifObj.stimVars));
cifObj.jacobianFunction = matlabFunction(cifObj.jacobianLambdaDelta,'vars',[symvar(cifObj.varIn); cifObj.histVars]);
cifObj.jacobianLogLambdaDelta=simplify(jacobian(cifObj.gradientLogLambdaDelta,cifObj.stimVars));
cifObj.jacobianLogFunction = matlabFunction(cifObj.jacobianLogLambdaDelta,'vars',[symvar(cifObj.varIn); cifObj.histVars]);
if(and(~isempty(cifObj.histCoeffs),~isempty(cifObj.history)))
cifObj.LogLambdaDeltaGamma=simplify(log(cifObj.lambdaDeltaGamma));
cifObj.LogLambdaDeltaGammaFunction = matlabFunction(cifObj.LogLambdaDeltaGamma,'vars',[symvar(cifObj.varIn); cifObj.histVars;histCoeffsVarsTrans]);
cifObj.gradientLogLambdaDeltaGamma=simplify(jacobian(log(cifObj.lambdaDeltaGamma),cifObj.histCoeffVars));
cifObj.gradientLambdaDeltaGamma=simplify(jacobian((cifObj.lambdaDeltaGamma),cifObj.histCoeffVars));
cifObj.gradientLogFunctionGamma = matlabFunction(cifObj.gradientLogLambdaDeltaGamma,'vars',[symvar(cifObj.varIn); cifObj.histVars;histCoeffsVarsTrans]);
cifObj.gradientFunctionGamma = matlabFunction(cifObj.gradientLambdaDeltaGamma,'vars',[symvar(cifObj.varIn); cifObj.histVars;histCoeffsVarsTrans]);
cifObj.jacobianLogLambdaDeltaGamma=simplify(jacobian(cifObj.gradientLogLambdaDeltaGamma,cifObj.histCoeffVars));
cifObj.jacobianLambdaDeltaGamma=simplify(jacobian(cifObj.gradientLambdaDeltaGamma,cifObj.histCoeffVars));
cifObj.jacobianLogFunctionGamma = matlabFunction(cifObj.jacobianLogLambdaDeltaGamma,'vars',[symvar(cifObj.varIn); cifObj.histVars;histCoeffsVarsTrans]);
cifObj.jacobianFunctionGamma = matlabFunction(cifObj.jacobianLambdaDeltaGamma,'vars',[symvar(cifObj.varIn); cifObj.histVars;histCoeffsVarsTrans]);
else
cifObj.LogLambdaDeltaGamma=[];
cifObj.LogLambdaDeltaGammaFunction = [];
cifObj.gradientLogLambdaDeltaGamma=[];
cifObj.gradientLambdaDeltaGamma=[];
cifObj.gradientLogFunctionGamma = [];
cifObj.gradientFunctionGamma = [];
cifObj.jacobianLogLambdaDeltaGamma=[];
cifObj.jacobianLambdaDeltaGamma=[];
cifObj.jacobianLogFunctionGamma = [];
cifObj.jacobianFunctionGamma = [];
end
cifObj.indepVars = symvar(cifObj.lambdaDelta);
% Determine the number of variables and make a default string
% that will be used to evaluate the above functions
% This is required since functions defined by using the
% matlabFunction command do not take vector inputs and so each
% value needs to be passed separatedly. Defining this string
% now simplifies how we evaluate these functions
argstr='';
if(length([symvar(cifObj.varIn); cifObj.histVars])==1)
argstr = 'val';
else
for i=1:(length(symvar(cifObj.varIn))+length(cifObj.histVars))
if(i==1)
argstr = 'val(1)';
else
argstr = strcat(argstr,[',val(' num2str(i) ')']);
end
end
end
cifObj.argstr = argstr;
argstrVarHist='';
if(length([symvar(cifObj.varIn); cifObj.histVars; histCoeffsVarsTrans])==1)
argstrVarHist = 'val';
else
for i=1:(length(symvar(cifObj.varIn))+length(cifObj.histVars)+length(histCoeffsVarsTrans))
if(i==1)
argstrVarHist = 'val(1)';
else
argstrVarHist = strcat(argstrVarHist,[',val(' num2str(i) ')']);
end
end
end
cifObj.argstrLDGamma = argstrVarHist;
if(~isempty(cifObj.spikeTrain) && ~isempty(cifObj.history))
cifObj.historyMat = cifObj.history.computeHistory(cifObj.spikeTrain).dataToMatrix;
else
cifObj.historyMat = [];
end
end
function cifObjNew = CIFCopy(cifObj)
% pause;
% cifObjNew = CIF(cifObj.b,cifObj.stimVars,cifObj.stimVars,cifObj.fitType);
%make a new CIF thats super simple
cifObjNew = CIF([1],['x'],['x'],cifObj.fitType);
%copy parameters from the old cifObj to the new one
fnames = fields(cifObj);
for i=1:length(fnames)
cifObjNew.(fnames{i}) = cifObj.(fnames{i});
end
end
function setSpikeTrain(cifObj, spikeTrain)
cifObj.spikeTrain = spikeTrain.nstCopy;
if(~isempty(cifObj.history))
cifObj.historyMat = cifObj.history.computeHistory(cifObj.spikeTrain).dataToMatrix;
else
cifObj.historyMat = [];
end
end
function setHistory(cifObj,histObj)
%Sets the input history object to be the history object that
%corresponds to this CIF.
% histObj: can be of class History or a vector of doubles to be
% used in creating a History object
if(isa(histObj,'History'))
cifObj.history = History(histObj.windowTimes);
elseif(isa(histObj,'double'));
cifObj.history = History(histObj);
else
error('History can only be set by passing in a History Object or a vector of windowTimes');
end
end
function outVal = evalLambdaDelta(cifObj,stimVal,time_index,nst)
% outVal = evalLambdaDelta(cifObj,stimVal,nst)
% scalar value of lambda*delta where lambda is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4)
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal];
evalString = strcat('outVal = cifObj.lambdaDeltaFunction(',cifObj.argstr,');');
eval(evalString);
end
function outVal = evalGradient(cifObj,stimVal,time_index,nst)
% outVal = evalGradient(cifObj,stimVal,nst)
% row vector of the gradient of log(lambda*delta) with respect
% to the stimulus variables.
% The gradient is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4)
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal];
evalString = strcat('outVal = cifObj.gradientFunction(',cifObj.argstr,');');
eval(evalString);
end
function outVal = evalGradientLog(cifObj,stimVal,time_index,nst)
% outVal = evalGradient(cifObj,stimVal,nst)
% row vector of the gradient of log(lambda*delta) with respect
% to the stimulus variables.
% The gradient is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4)
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal];
evalString = strcat('outVal = cifObj.gradientLogFunction(',cifObj.argstr,');');
eval(evalString);
end
function outVal = evalJacobian(cifObj,stimVal,time_index,nst)
% outVal = evalJacobian(cifObj,stimVal,nst)
% matrix vector of the jacobian of log(lambda*delta) with
% to the stimulus variables. The gradient is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4)
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal];
evalString = strcat('outVal = cifObj.jacobianFunction(',cifObj.argstr,');');
eval(evalString);
end
function outVal = evalJacobianLog(cifObj,stimVal,time_index,nst)
% outVal = evalJacobian(cifObj,stimVal,nst)
% matrix vector of the jacobian of log(lambda*delta) with
% to the stimulus variables. The gradient is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4)
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal];
evalString = strcat('outVal = cifObj.jacobianLogFunction(',cifObj.argstr,');');
eval(evalString);
end
%%For history parameters
function outVal = evalLDGamma(cifObj,stimVal,time_index,nst,gamma)
% outVal = evalLambdaDelta(cifObj,stimVal,nst)
% scalar value of lambda*delta where lambda is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4 || isempty(nst))
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal;gamma];
evalString = strcat('outVal = cifObj.lambdaDeltaGammaFunction(',cifObj.argstrLDGamma,');');
eval(evalString);
end
function outVal = evalLogLDGamma(cifObj,stimVal,time_index,nst,gamma)
% outVal = evalLambdaDelta(cifObj,stimVal,nst)
% scalar value of lambda*delta where lambda is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4 || isempty(nst))
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal;gamma];
evalString = strcat('outVal = cifObj.LogLambdaDeltaGammaFunction(',cifObj.argstrLDGamma,');');
eval(evalString);
end
function outVal = evalGradientLDGamma(cifObj,stimVal,time_index,nst,gamma)
% outVal = evalGradient(cifObj,stimVal,nst)
% row vector of the gradient of log(lambda*delta) with respect
% to the stimulus variables.
% The gradient is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4 || isempty(nst))
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal;gamma];
evalString = strcat('outVal = cifObj.gradientFunctionGamma(',cifObj.argstrLDGamma,');');
eval(evalString);
end
function outVal = evalGradientLogLDGamma(cifObj,stimVal,time_index,nst,gamma)
% outVal = evalGradient(cifObj,stimVal,nst)
% row vector of the gradient of log(lambda*delta) with respect
% to the stimulus variables.
% The gradient is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4 || isempty(nst))
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal;gamma];
evalString = strcat('outVal = cifObj.gradientLogFunctionGamma(',cifObj.argstrLDGamma,');');
eval(evalString);
end
function outVal = evalJacobianLogLDGamma(cifObj,stimVal,time_index,nst,gamma)
% outVal = evalJacobian(cifObj,stimVal,nst)
% matrix vector of the jacobian of log(lambda*delta) with
% to the stimulus variables. The gradient is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4 || isempty(nst))
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal;gamma];
evalString = strcat('outVal = cifObj.jacobianLogFunctionGamma(',cifObj.argstrLDGamma,');');
eval(evalString);
end
function outVal = evalJacobianLDGamma(cifObj,stimVal,time_index,nst,gamma)
% outVal = evalJacobian(cifObj,stimVal,nst)
% matrix vector of the jacobian of log(lambda*delta) with
% to the stimulus variables. The gradient is evaluated at the
% values in stimVal. If there this CIF has history dependence
% the nspikeTrain nst is used to compute the history effect
if(nargin<3)
time_index=[];
histVal = [];
end
if(nargin<4 || isempty(nst))
if(~isempty(time_index) && ~isempty(cifObj.historyMat))
histVal=cifObj.historyMat(time_index,:)';
end
else
if(isa(nst,'nspikeTrain'))
if(~isempty(cifObj.history))
histData=cifObj.history.computeHistory(nst).dataToMatrix;
histVal = histData(end,:)';
else
histVal = [];
end
else
error('Second Input must be of class nspikeTrain');
end
end
val = [stimVal;histVal;gamma];
evalString = strcat('outVal = cifObj.jacobianFunctionGamma(',cifObj.argstrLDGamma,');');
eval(evalString);
end
function ans = isSymBeta(cifObj)
if(isa(cifObj.b,'sym'))
ans=1;
else
ans=0;
end
end
end
methods (Static)
function spikeTrainColl=simulateCIFByThinningFromLambda(lambda,numRealizations,maxTimeRes)%,histCoeffs,histObj)
% spikeTrainColl=simulateCIFByThinning(lambda,numRealizations,maxTimeRes)
% Returns a nstColl with numRealization distinct nspikeTrains
% corresponding to realizations of the point process specified
% by the conditional intensity function lambda.
%
% lambda: a SignalObj or Covariate that is the CIF time series.
% numRealizations: number of realizations to return of the
% point process specified by lambda.
% maxTimeRes: makes sure that only there is only one spike
% occurs within the time maxTimeRes.
%
% Note: Currently assumes no history dependence. Needs to be
% modified so that a new lambda is determined at each
% time step which includes the current spiking activity
% in a given realization.
% if(nargin<5)
% histObj = [];
% end
%
% if(nargin<4)
% histCoeffs = [];
% end
if(nargin<3)
maxTimeRes =[];
end
Tmax = lambda.maxTime;
lambdaBound = max(lambda);
N=ceil(lambdaBound*(1.5*Tmax)); %Expected number of arrivals in interval 1.5*Tmax
nst=cell(1,numRealizations);
for i=1:numRealizations
u = rand(1,N); %N samples U(0,1)
w = -log(u)./(lambdaBound); %Exponential rate lambdaBound
tSpikes = cumsum(w); %time of the spikes
tSpikes = tSpikes(tSpikes<=Tmax); %keep only in interval of interest
% Thinning
% if(and(~isempty(histObj),~isempty(histCoeffs)))
% tempnst = nspikeTrain(tSpikes); tempnst.setMinTime(lambda.minTime);
% tempnst.setMaxTime(lambda.maxTime);
% tempnst.resample(lambda.sampleRate);
% histData = histObj.computeHistory(tempnst);
% lambdaHist = SignalObj(lambda.time,exp(histData.dataToMatrix * histCoeffs)); % Assumes poisson lambda
% lambdaProd = lambda.*lambdaHist;
% lambdaBound = max(lambdaProd);
% lambdaRatio = lambdaProd.getValueAt(tSpikes)./lambdaBound;
% else
lambdaRatio = lambda.getValueAt(tSpikes)./lambdaBound;
% end
u2 = rand(length(lambdaRatio),1);
%If lambdaRatio is greater than u2 keep spike otherwise throw
%away
if(~isempty(lambdaRatio))
tSpikesThin = tSpikes(lambdaRatio>=u2);
else
tSpikesThin =[];
end
if(isempty(maxTimeRes))
nst{i} = nspikeTrain(tSpikesThin);
nst{i}.setName(num2str(1));
else
tSpikesThin = unique(ceil(tSpikesThin./maxTimeRes)*maxTimeRes);
nst{i} = nspikeTrain(tSpikesThin);
nst{i}.setName(num2str(1));
end
end
spikeTrainColl=nstColl(nst);
spikeTrainColl.setMinTime(lambda.minTime);
spikeTrainColl.setMaxTime(lambda.maxTime);
end
function [spikeTrainColl, lambda]=simulateCIFByThinning(mu,hist,stim,ens,inputStimSignal,inputEnsSignal,numRealizations,simType)
% spikeTrainColl=simulateCIF(mu,hist,stim,inputStimSignal,inputEnsSignal,numRealizations)
% Returns a nstColl with numRealization different nspikeTrain
% objects. Each nspikeTrain object is one particular
% realization of the point process defined the input parameters
% in the following way:
% lambda*delta = exp(inputTerms)./(1+exp(inputTerms)
% where inputTerms = (mu + stim*inputStimSignal +
% hist*spikeTrain + ens*inputEnsSignal)
%
% mu: double the indicates the mean rate of the point process
% hist: a transfer function (tf) object that is convolved with
% process spiking activity to determine the history
% effect.
% stim: a transfer function (tf) object that is convolved with
% inputStimSignal to determine the stimulus effect
%
% ens : a transfer function (tf) object that is convolved with
% the inputEnsSignal to determine the ensemble effect
%
% inputStimSignal: a SignalObj specifying the stimulation time
% series.
% inputEnsSignal: a SignalObj specifying the ensemble activity
%
% numRealizations: number of nspikeTrains to return. The
% the conditional intensity function will be
% simulated this number of times to generated
% distinct realizations of the point process.
% <a href="matlab:web('PPSimExample.html', '-helpbrowser')">Example use of simulateCIF</a>
%
if(nargin<8 || isempty(simType))
simType='binomial';
end
if(nargin<7)
numRealizations =1;
end
Ts=hist.Ts;
if(1/inputStimSignal.sampleRate == hist.Ts && 1/inputStimSignal.sampleRate ==stim.Ts)
assignin('base','S',stim);
assignin('base','H',hist);
assignin('base','E',ens);
assignin('base','mu',mu);
assignin('base','Ts',hist.Ts/100);
assignin('base','TsInt',hist.Ts);
if(strcmp(simType,'poisson'))
simTypeSelect = 1;
elseif(strcmp(simType,'binomial'))
simTypeSelect = 0;
else
error('simType must be either poisson or binomial');
end
assignin('base','simTypeSelect',simTypeSelect);
options = simget;
lambdaData = zeros(length(inputStimSignal.time),numRealizations);
t=inputStimSignal.time;
u=[inputStimSignal.data, inputEnsSignal.data];
assignin('base','t',t);
assignin('base','u',u);
% options.T
for i=1:numRealizations
simOut = sim('PointProcessSimulationThinning','SimulationMode','normal','AbsTol','1e-5',...
'SaveState','on','StateSaveName','xout',...
'SaveOutput','on','OutputSaveName','yout',...
'SaveTime','on','TimeSaveName','tout',...
'StopTime', num2str(inputStimSignal.maxTime),...
'StartTime', num2str(inputStimSignal.minTime));
simOutVars = simOut.who;
yout = simOut.get('yout');
tout = yout.time;
% [tout,~,yout] = sim('PointProcessSimulationThinning',[inputStimSignal.minTime inputStimSignal.maxTime],options,inputStimSignal.dataToStructure, inputEnsSignal.dataToStructure);
spikeTimes = tout(yout.signals(1).values>.5);
nst{i} = nspikeTrain(spikeTimes);
nst{i}.setName(num2str(1));
lambdaData(:,i) = interp1(tout, yout.signals(2).values./Ts,inputStimSignal.time);
end
spikeTrainColl=nstColl(nst);
spikeTrainColl.setMinTime(inputStimSignal.minTime);
spikeTrainColl.setMaxTime(inputStimSignal.maxTime);
lambda = Covariate(inputStimSignal.time,lambdaData,'\lambda(t|H_t)','time','s','Hz');
else
error('History and Stimulus Transfer functions be discrete and have ''Ts'' equal to 1/inputStimSignal.sampleRate');
end
end
function [spikeTrainColl, lambda]=simulateCIF(mu,hist,stim,ens,inputStimSignal,inputEnsSignal,numRealizations,simType)
% spikeTrainColl=simulateCIF(mu,hist,stim,inputStimSignal,inputEnsSignal,numRealizations)
% Returns a nstColl with numRealization different nspikeTrain
% objects. Each nspikeTrain object is one particular
% realization of the point process defined the input parameters
% in the following way:
% lambda*delta = exp(inputTerms)./(1+exp(inputTerms)
% where inputTerms = (mu + stim*inputStimSignal +
% hist*spikeTrain + ens*inputEnsSignal)
%
% mu: double the indicates the mean rate of the point process
% hist: a transfer function (tf) object that is convolved with
% process spiking activity to determine the history
% effect.
% stim: a transfer function (tf) object that is convolved with
% inputStimSignal to determine the stimulus effect
%
% ens : a transfer function (tf) object that is convolved with
% the inputEnsSignal to determine the ensemble effect
%
% inputStimSignal: a SignalObj specifying the stimulation time
% series.
% inputEnsSignal: a SignalObj specifying the ensemble activity
%
% numRealizations: number of nspikeTrains to return. The
% the conditional intensity function will be
% simulated this number of times to generated
% distinct realizations of the point process.
% <a href="matlab:web('PPSimExample.html', '-helpbrowser')">Example use of simulateCIF</a>
%
if(nargin<8 || isempty(simType))
simType='binomial';
end
if(nargin<7)
numRealizations =1;
end
if(1/inputStimSignal.sampleRate == hist.Ts && 1/inputStimSignal.sampleRate ==stim.Ts)
assignin('base','S',stim);
assignin('base','H',hist);
assignin('base','E',ens);
assignin('base','mu',mu);
assignin('base','Ts',stim.Ts);