-
Notifications
You must be signed in to change notification settings - Fork 1
/
GraphicalModelsMLE.m2
2108 lines (1878 loc) · 73.1 KB
/
GraphicalModelsMLE.m2
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
-- -*- coding: utf-8-unix -*-
-*
Copyright 2020 Carlos Amendola, Luis David Garcia Puente, Roser Homs Pons,
Olga Kuznetsova, Harshit J Motwani, Elina Robeva and David Swinarski.
You may redistribute this file under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 2 of
the License, or any later version.
*-
newPackage(
"GraphicalModelsMLE",
Version => "1.0",
Date => "April 19, 2022",
Authors => {
{Name=> "Carlos Amendola",
Email=> "carlos.amendola@mis.mpg.de",
HomePage=>"http://www.carlos-amendola.com/"},
{Name => "Luis David Garcia Puente",
Email => "lgarciapuente@coloradocollege.edu",
HomePage => "https://luisgarciapuente.github.io"},
{Name=> "Roser Homs Pons",
Email=> "roser.homs@tum.de",
HomePage=>"https://personal-homepages.mis.mpg.de/homspons/index.html"},
{Name=> "Olga Kuznetsova",
Email=> "kuznetsova.olga@gmail.com",
HomePage=>"https://okuznetsova.com"},
{Name=> "Harshit J Motwani",
Email=> "harshitmotwani2015@gmail.com",
HomePage=>"https://sites.google.com/view/harshitjmotwani/home"},
{Name=> "Elina Robeva",
Email=> "erobeva@gmail.com",
HomePage=>"https://www.math.ubc.ca/~erobeva/"},
{Name=> "David Swinarski",
Email=> "dswinarski@fordham.edu",
HomePage=>"http://faculty.fordham.edu/dswinarski"}
},
Headline => "maximum likelihood estimates for graphical statistical models",
Keywords => {"Algebraic Statistics"},
DebuggingMode => true,
PackageExports => {"GraphicalModels","Graphs","EigenSolver","NumericalAlgebraicGeometry","StatGraphs"}
)
export {
"checkPD",
"checkPSD",
"Solver",--optional argument in solverMLE
"ConcentrationMatrix",-- optional argument in solverMLE
"CovarianceMatrix", -- optional argument in scoreEquations
"Saturate",-- optional argument in scoreEquations and solverMLE
"jacobianMatrixOfRationalFunction",
"MLdegree",
"OptionsEigenSolver",--optional argument in solverMLE
"OptionsNAG4M2",--optional argument in solverMLE
"RealPrecision",--optional argument in solverMLE and scoreEquations
"sampleCovarianceMatrix",
"SampleData",-- optional argument in scoreEquations and solverMLE
"SaturateOptions", -- optional argument in scoreEquations and solverMLE
"scoreEquations",
"solverMLE",
"ZeroTolerance"
}
--**************************--
-- INTERNAL ROUTINES --
--**************************--
--*************************************--
-- Functions (local) used throughout --
--*************************************--
------------------------------------------------------
-- Substitues a list of points on a list of matrices
-- input - list of points from sols
-- matrix whose entries are variables
-- (expect it to be an inverse of a covariance matrix, Sin)
-- output - list of matrices after substituting these values
------------------------------------------------------
genListMatrix = (L,A) ->
(
T:= for l in L list coordinates(l);
M:= for t in T list substitute(A,matrix{t});
return M
);
----------------------------------------------
-- Selects all argmax for log det K- trace(S*K),
-- where K is an element of the list L.
-- We assume that L is the intersection of the
-- variety of the ideal generated by the Jacobian
-- of critical equations and the cone of PD matrices.
-- input - list L of candidate Sinv matrices (Sinv is Sigma^{-1}) and
-- sample covariance matrix V. Notation in line with scoreEquationsFromCovarianceMatrix
-- output -list of argmax
----------------------------------------------
maxMLE=(L,V)->(
if #L==0 then error("No critical points to evaluate");
if #L==1 then (E:=L_0; maxPt:=log det L_0- trace (V*L_0))
else
(eval:=for Sinv in L list log det Sinv- trace (V*Sinv);
evalReal:=for pt in eval when isReal pt list pt;
if #evalReal==0 then error("No critical point evaluates to a real solution");
maxPt=max evalReal;
indexOptimal:=positions(eval, i ->i== maxPt);
E= for i in indexOptimal list L_i;);
return (maxPt, E)
);
-------------------------------------------
-- scoreEquationsInternal - function that returns
-- both the ideal and the corresponding SInv matrix.
-- The user-facing scoreEquations method returns only
-- the ideal, whereas SInv is used in solverMLE
-------------------------------------------
scoreEquationsInternal={Saturate => true, SaturateOptions => options saturate, SampleData=>true, RealPrecision=>53, CovarianceMatrix => false}>>opts->(R,U)->(
----------------------------------------------------
-- Extract information about the graph
----------------------------------------------------
-- Lambda
L := directedEdgesMatrix R;
-- Psi
P := bidirectedEdgesMatrix R;
-- If the mixedGraph only has undirected part, call specific function for undirected.
if L==0 and P==0 then
return scoreEquationsInternalUndir(R,U,opts);
-- K
K := undirectedEdgesMatrix R;
----------------------------------------------------
-- Create an auxiliary ring and its fraction field
-- which do not have the s variables
----------------------------------------------------
-- create a new ring, lpR, which does not have the s variables
lpR:=coefficientRing(R)[gens R-set support covarianceMatrix R];
-- create its fraction field
FR := frac(lpR);
-----------------------------------------------------
-- Compute Sinv
-----------------------------------------------------
-- Kinv
K=sub(K, FR);
Kinv:=inverse K;
P=sub(P,FR);
--Omega
if K==0 then W:=P else (if P==0 then W=Kinv else W = directSum(Kinv,P));
-- move to FR, the fraction field of lpR
L= sub(L,FR);
-- Sigma
d:=numcols L;
if L==0 then S:=W else (
IdL := inverse (id_(FR^d)-L);
S = (transpose IdL) * W * IdL
);
Sinv := inverse S;
-----------------------------------------------------
-- Compute score equations ideal
----------------------------------------------------
-- Sample covariance matrix
if opts.SampleData then V:= sampleCovarianceMatrix(U) else V=U;
if ring V===RR_53 then V = matrix apply(entries V,r->r/(v->lift(numeric(opts.RealPrecision,v),QQ)));
-- Jacobian of log-likelihood function
C1 := trace(Sinv * V);
C1derivative := jacobianMatrixOfRationalFunction(C1);
LL :=jacobianMatrixOfRationalFunction (det Sinv)*matrix{{1/det(Sinv)}} - C1derivative;
LL=flatten entries(LL);
denoms := apply(#LL, i -> lift(denominator(LL_i), lpR));
J:=ideal apply(#LL, i -> lift(numerator(LL_i),lpR));
--Saturate
if opts.Saturate then (
argSaturate:=opts.SaturateOptions >>newOpts-> args ->(args, newOpts);
for i from 0 to (#denoms-1) do (
if degree denoms_i =={0} then J=J else
J=saturate(argSaturate(J,denoms_i))
);
);
return (J,Sinv);
);
----------------------------------------------------
--scoreEquationsInternalUndir for undirected graphs
----------------------------------------------------
scoreEquationsInternalUndir={Saturate => true, SaturateOptions => options saturate, SampleData=>true, RealPrecision=> 53, CovarianceMatrix => false}>>opts->(R,U)->(
-- Sample covariance matrix
if opts.SampleData then V := sampleCovarianceMatrix(U) else V=U;
if ring V===RR_53 then V = matrix apply(entries V,r->r/(v->lift(numeric(opts.RealPrecision,v),QQ)));
-- Concentration matrix K
K:=undirectedEdgesMatrix R;
-- move to a new ring, lpR, which does not have the s variables
lpR:=coefficientRing(R)[gens R - set support covarianceMatrix R];
K=sub(K,lpR);
J:=ideal{jacobian ideal{determinant(K)}-determinant(K)*jacobian(ideal{trace(K*V)})};
if opts.Saturate then
( argSaturate:=opts.SaturateOptions >>newOpts-> args ->(args, newOpts);
J=saturate(argSaturate(J,ideal{determinant(K)}));
);
return (J,K);
);
-------------------------------------------
-- Method copied from package DeterminantalRepresentations
-------------------------------------------
-----------------------------------------------
-- Method for retriving the real part of a matrix.
--The code of this function is directly taken from DeterminantalRepresentations package in M2.
realPartMatrix = A -> matrix apply(entries A, r -> r/realPart)
--**************************--
-- METHODS --
--**************************--
sampleCovarianceMatrix = method(TypicalValue =>Matrix);
sampleCovarianceMatrix(Matrix) := (U) -> (
n := numRows U;
--Convert from integers to rationals if needed
if ring U===ZZ then U=sub(U,QQ);
--Convert matrix into list of row matrices
U = for i to n-1 list U^{i};
--Compute the mean vector
Ubar := matrix{{(1/n)}} * sum(U);
--Compute sample covariance matrix
return ((1/n)*(sum apply(n, i -> (transpose (U#i-Ubar))*(U#i-Ubar))));
);
sampleCovarianceMatrix(List) := (U) -> (
return sampleCovarianceMatrix(matrix U);
);
jacobianMatrixOfRationalFunction = method(TypicalValue =>Matrix);
jacobianMatrixOfRationalFunction(RingElement) := (F) -> (
if not instance(ring F,FractionField) then error "Expected element in a field of fractions";
f:=numerator(F);
g:=denominator(F);
R:=ring(f);
answer:=diff(vars(R), f) * g - diff(vars(R), g)*f;
answer=substitute(answer, ring(F));
return transpose(matrix({{(1/g)^2}})*answer)
);
scoreEquations = method(TypicalValue =>Sequence, Options =>{SampleData => true, Saturate => true, SaturateOptions => options saturate, RealPrecision => 53, CovarianceMatrix => false});
scoreEquations(Ring,Matrix) := opts -> (R, U) -> (
----------------------------------------------------
--Check input
----------------------------------------------------
if not R.?graph then error "Expected a ring created with gaussianRing of a Graph, Bigraph, Digraph or MixedGraph";
if not numRows U==#vertices R.graph then error "Size of sample data does not match the graph.";
if not opts.SampleData then (if not U==transpose U then error "The sample covariance matrix must be symmetric.");
---------------------------------------------------
-- Apply appropriate scoreEquations routine
---------------------------------------------------
if R.graphType===Graph
then (J,Sinv):=scoreEquationsInternalUndir(R,U,opts)
else (J,Sinv)=scoreEquationsInternal(R,U,opts);
if not opts.CovarianceMatrix
then return J
else return (J, inverse Sinv)
;
);
scoreEquations(Matrix,Ring) := opts ->(U,R) -> (
return scoreEquations(R,U,opts);
);
scoreEquations(Ring,List) := opts ->(R, U) -> (
----------------------------------------------------
--Check input
----------------------------------------------------
if not opts.SampleData then error "The sample covariance matrix must be a matrix.";
---------------------------------------------------
-- Call scoreEquations routine with a matrix
---------------------------------------------------
return scoreEquations(R,matrix U,opts);
);
scoreEquations(List,Ring) := opts ->(U,R) -> (
return scoreEquations(R,U,opts);
);
--Allow for graphs as input
scoreEquations(MixedGraph, Matrix) := opts ->(G,U) -> (
return scoreEquations(gaussianRing(G),U,opts);
);
scoreEquations(MixedGraph,List) := opts ->(G,U) -> (
return scoreEquations(gaussianRing(G),U,opts);
);
--All permutations of input for MixedGraphs and other type of graphs
scoreEquations(Graph,List) := opts -> (G, U) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(Digraph,List) := opts -> (G, U) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(Bigraph,List) := opts -> (G, U) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(Graph,Digraph,List) := opts -> (G,D,U) -> (
return scoreEquations(mixedGraph (G,D),U, opts);
);
scoreEquations(Digraph,Graph,List) := opts -> (D,G,U) -> (
return scoreEquations(mixedGraph (D,G),U, opts);
);
scoreEquations(Digraph,Bigraph,List) := opts -> (D,B,U) -> (
return scoreEquations(mixedGraph (D,B),U, opts);
);
scoreEquations(Bigraph,Digraph,List) := opts -> (B,D,U) -> (
return scoreEquations(mixedGraph (B,D),U, opts);
);
scoreEquations(Graph, Bigraph,List) := opts -> (G,B,U) -> (
return scoreEquations(mixedGraph (G,B),U, opts);
);
scoreEquations(Bigraph,Graph,List) := opts -> (B,G,U) -> (
return scoreEquations(mixedGraph (B,G),U, opts);
);
scoreEquations(Graph, Digraph, Bigraph, List) := opts -> (G,D,B,U) -> (
return scoreEquations(mixedGraph (G,D,B),U, opts);
);
scoreEquations(Digraph, Bigraph, Graph, List) := opts -> (D,B,G,U) -> (
return scoreEquations(mixedGraph (D,B,G),U, opts);
);
scoreEquations(Bigraph, Graph, Digraph, List) := opts -> (B,G,D,U) -> (
return scoreEquations(mixedGraph (B,G,D),U, opts);
);
scoreEquations(Graph,Bigraph, Digraph, List) := opts -> (G,B,D,U) -> (
return scoreEquations(mixedGraph (G,B,D),U, opts);
);
scoreEquations(Bigraph, Digraph,Graph, List) := opts -> (B,D,G,U) -> (
return scoreEquations(mixedGraph (B,D,G),U, opts);
);
scoreEquations(Digraph, Graph, Bigraph, List) := opts -> (D,G,B,U) -> (
return scoreEquations(mixedGraph (D,G,B),U, opts);
);
scoreEquations(List,MixedGraph) := opts -> (U,G) -> (
return scoreEquations(G,U, opts);
);
scoreEquations(List,Graph) := opts -> (U,G) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(List,Digraph) := opts -> (U,G) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(List,Bigraph) := opts -> (U,G) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(List,Graph,Digraph) := opts -> (U,G,D) -> (
return scoreEquations(mixedGraph (G,D),U, opts);
);
scoreEquations(List,Digraph,Graph) := opts -> (U,D,G) -> (
return scoreEquations(mixedGraph (D,G),U, opts);
);
scoreEquations(List,Digraph,Bigraph) := opts -> (U,D,B) -> (
return scoreEquations(mixedGraph (D,B),U, opts);
);
scoreEquations(List,Bigraph,Digraph) := opts -> (U,B,D) -> (
return scoreEquations(mixedGraph (B,D),U, opts);
);
scoreEquations(List,Graph, Bigraph) := opts -> (U,G,B) -> (
return scoreEquations(mixedGraph (G,B),U, opts);
);
scoreEquations(List,Bigraph,Graph) := opts -> (U,B,G) -> (
return scoreEquations(mixedGraph (B,G),U, opts);
);
scoreEquations(List,Graph, Digraph, Bigraph) := opts -> (U,G,D,B) -> (
return scoreEquations(mixedGraph (G,D,B),U, opts);
);
scoreEquations(List,Digraph, Bigraph, Graph) := opts -> (U,D,B,G) -> (
return scoreEquations(mixedGraph (D,B,G),U, opts);
);
scoreEquations(List,Bigraph, Graph, Digraph) := opts -> (U,B,G,D) -> (
return scoreEquations(mixedGraph (B,G,D),U, opts);
);
scoreEquations(List,Graph,Bigraph, Digraph) := opts -> (U,G,B,D) -> (
return scoreEquations(mixedGraph (G,B,D),U, opts);
);
scoreEquations(List,Bigraph, Digraph,Graph) := opts -> (U,B,D,G) -> (
return scoreEquations(mixedGraph (B,D,G),U, opts);
);
scoreEquations(List,Digraph, Graph, Bigraph) := opts -> (U,D,G,B) -> (
return scoreEquations(mixedGraph (D,G,B),U, opts);
);
scoreEquations(Graph,Matrix) := opts -> (G, U) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(Digraph,Matrix) := opts -> (G, U) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(Bigraph,Matrix) := opts -> (G, U) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(Graph,Digraph,Matrix) := opts -> (G,D,U) -> (
return scoreEquations(mixedGraph (G,D),U, opts);
);
scoreEquations(Digraph,Graph,Matrix) := opts -> (D,G,U) -> (
return scoreEquations(mixedGraph (D,G),U, opts);
);
scoreEquations(Digraph,Bigraph,Matrix) := opts -> (D,B,U) -> (
return scoreEquations(mixedGraph (D,B),U, opts);
);
scoreEquations(Bigraph,Digraph,Matrix) := opts -> (B,D,U) -> (
return scoreEquations(mixedGraph (B,D),U, opts);
);
scoreEquations(Graph, Bigraph,Matrix) := opts -> (G,B,U) -> (
return scoreEquations(mixedGraph (G,B),U, opts);
);
scoreEquations(Bigraph,Graph,Matrix) := opts -> (B,G,U) -> (
return scoreEquations(mixedGraph (B,G),U, opts);
);
scoreEquations(Graph, Digraph, Bigraph, Matrix) := opts -> (G,D,B,U) -> (
return scoreEquations(mixedGraph (G,D,B),U, opts);
);
scoreEquations(Digraph, Bigraph, Graph, Matrix) := opts -> (D,B,G,U) -> (
return scoreEquations(mixedGraph (D,B,G),U, opts);
);
scoreEquations(Bigraph, Graph, Digraph, Matrix) := opts -> (B,G,D,U) -> (
return scoreEquations(mixedGraph (B,G,D),U, opts);
);
scoreEquations(Graph,Bigraph, Digraph, Matrix) := opts -> (G,B,D,U) -> (
return scoreEquations(mixedGraph (G,B,D),U, opts);
);
scoreEquations(Bigraph, Digraph,Graph, Matrix) := opts -> (B,D,G,U) -> (
return scoreEquations(mixedGraph (B,D,G),U, opts);
);
scoreEquations(Digraph, Graph, Bigraph, Matrix) := opts -> (D,G,B,U) -> (
return scoreEquations(mixedGraph (D,G,B),U, opts);
);
scoreEquations(Matrix,MixedGraph) := opts -> (U,G) -> (
return scoreEquations(G,U, opts);
);
scoreEquations(Matrix,Graph) := opts -> (U,G) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(Matrix,Digraph) := opts -> (U,G) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(Matrix,Bigraph) := opts -> (U,G) -> (
return scoreEquations(mixedGraph (G),U, opts);
);
scoreEquations(Matrix,Graph,Digraph) := opts -> (U,G,D) -> (
return scoreEquations(mixedGraph (G,D),U, opts);
);
scoreEquations(Matrix,Digraph,Graph) := opts -> (U,D,G) -> (
return scoreEquations(mixedGraph (D,G),U, opts);
);
scoreEquations(Matrix,Digraph,Bigraph) := opts -> (U,D,B) -> (
return scoreEquations(mixedGraph (D,B),U, opts);
);
scoreEquations(Matrix,Bigraph,Digraph) := opts -> (U,B,D) -> (
return scoreEquations(mixedGraph (B,D),U, opts);
);
scoreEquations(Matrix,Graph, Bigraph) := opts -> (U,G,B) -> (
return scoreEquations(mixedGraph (G,B),U, opts);
);
scoreEquations(Matrix,Bigraph,Graph) := opts -> (U,B,G) -> (
return scoreEquations(mixedGraph (B,G),U, opts);
);
scoreEquations(Matrix,Graph, Digraph, Bigraph) := opts -> (U,G,D,B) -> (
return scoreEquations(mixedGraph (G,D,B),U, opts);
);
scoreEquations(Matrix,Digraph, Bigraph, Graph) := opts -> (U,D,B,G) -> (
return scoreEquations(mixedGraph (D,B,G),U, opts);
);
scoreEquations(Matrix,Bigraph, Graph, Digraph) := opts -> (U,B,G,D) -> (
return scoreEquations(mixedGraph (B,G,D),U, opts);
);
scoreEquations(Matrix,Graph,Bigraph, Digraph) := opts -> (U,G,B,D) -> (
return scoreEquations(mixedGraph (G,B,D),U, opts);
);
scoreEquations(Matrix,Bigraph, Digraph,Graph) := opts -> (U,B,D,G) -> (
return scoreEquations(mixedGraph (B,D,G),U, opts);
);
scoreEquations(Matrix,Digraph, Graph, Bigraph) := opts -> (U,D,G,B) -> (
return scoreEquations(mixedGraph (D,G,B),U, opts);
);
checkPD = method(TypicalValue =>List, Options =>{ZeroTolerance=>1e-10});
checkPD(List) := opts -> (L) -> (
for l in L
list (
if not length (select(eigenvalues l, i->realPart i<= opts.ZeroTolerance
or abs(imaginaryPart i )>opts.ZeroTolerance))==0 then continue;
realPartMatrix l)
);
checkPD(Matrix):= opts -> (L)->{
return checkPD({L},opts);
};
checkPSD = method(TypicalValue =>List, Options =>{ZeroTolerance=>1e-10});
checkPSD(List) := opts -> (L) -> (
for l in L
list (
if not length (select(eigenvalues l, i->realPart i< -opts.ZeroTolerance
or abs(imaginaryPart i )>opts.ZeroTolerance))==0 then continue;
realPartMatrix l)
);
checkPSD(Matrix):= opts -> (L)->{
return checkPSD({L},opts);
};
MLdegree = method(TypicalValue =>ZZ);
MLdegree(Ring):= (R) -> (
if not R.?graph then error "Expected gaussianRing created from a graph, digraph, bigraph or mixedGraph";
n:=# vertices R.graph;
J:=scoreEquations(R,random(RR^n,RR^n));
dimJ := dim J;
if dimJ > 0 then error concatenate("the ideal of score equations has dimension ",toString dimJ, " > 0,
so ML degree is not well-defined. The degree of this ideal is ", toString degree J,".");
return degree J;
);
solverMLE = method(TypicalValue =>Sequence, Options =>{SampleData=>true, ConcentrationMatrix=> false, Saturate => true, SaturateOptions => options saturate, Solver=>"EigenSolver", OptionsEigenSolver => options zeroDimSolve, OptionsNAG4M2=> options solveSystem, RealPrecision => 53, ZeroTolerance=>1e-10});
solverMLE(MixedGraph,Matrix) := opts -> (G, U) -> (
return solverMLE(gaussianRing G,U,opts);
);
-- Allow list instead of matrix
solverMLE(MixedGraph,List):= opts ->(G,U) -> (
return solverMLE(gaussianRing G,U,opts);
);
--Allow ring instead of graph
solverMLE(Ring,Matrix):= opts ->(R,U) -> (
-- check input
if not numgens source U ==R.gaussianRingData#nn then error "Size of sample data does not match the graph.";
-- sample covariance matrix
if opts.SampleData then V := sampleCovarianceMatrix(U)
else (V=U;
if not V==transpose V then error "The sample covariance matrix must be symmetric.");
-- generate the ideal of the score equations
if opts.Saturate then (
argSaturate:=opts.SaturateOptions >>newOpts-> args ->(args, SaturateOptions=>newOpts,SampleData=>false);
(J,SInv):=scoreEquationsInternal(argSaturate(R,V));)
else (J,SInv)= scoreEquationsInternal(R,V,Saturate=>false, SampleData=>false);
-- check that the system has finitely many solutions
if dim J =!= 0 then return J
else (
ML:=degree J;
-- solve system
if opts.Solver=="EigenSolver" then(
argES:=opts.OptionsEigenSolver >>newOpts-> args ->(args, newOpts);
sols:=zeroDimSolve(argES(J));
) else (
if opts.Solver=="NAG4M2" then (
sys:= flatten entries gens J;
argNAG4M2:=opts.OptionsNAG4M2 >>newOpts-> args ->(args, newOpts);
sols=solveSystem(argNAG4M2(sys));
)
else error "Accepted solver options are EigenSolver
(which uses function zeroDimSolve) or NAG4M2 (which uses solveSystem). Options should
be given as strings.";
);
--evaluate matrices on solutions
M:=genListMatrix(sols,SInv);
--consider only PD matrices
L:=checkPD (M, ZeroTolerance=>opts.ZeroTolerance);
--find the optimal points
(maxPt, E):=maxMLE(L,V);
if not opts.ConcentrationMatrix then (
if instance(E,List) then E=(for e in E list e=inverse e) else E=inverse E
);
return (maxPt,E,ML));
);
-- Allow ring instead of graph and list instead of matrix
solverMLE(Ring,List):= opts ->(R,U) -> (
-- check input
if not opts.SampleData then error "The sample covariance matrix must be a matrix.";
-- call solverMLE for a matrix
return solverMLE(R,matrix U,opts);
);
-- Permutations of input
solverMLE(Matrix,Ring) := opts -> (U, R) -> (
return solverMLE(R,U, opts);
);
solverMLE(List,Ring) := opts -> (U, R) -> (
return solverMLE(R,U, opts);
);
solverMLE(Graph,List) := opts -> (G, U) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(Digraph,List) := opts -> (G, U) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(Bigraph,List) := opts -> (G, U) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(Graph,Digraph,List) := opts -> (G,D,U) -> (
return solverMLE(mixedGraph (G,D),U, opts);
);
solverMLE(Digraph,Graph,List) := opts -> (D,G,U) -> (
return solverMLE(mixedGraph (D,G),U, opts);
);
solverMLE(Digraph,Bigraph,List) := opts -> (D,B,U) -> (
return solverMLE(mixedGraph (D,B),U, opts);
);
solverMLE(Bigraph,Digraph,List) := opts -> (B,D,U) -> (
return solverMLE(mixedGraph (B,D),U, opts);
);
solverMLE(Graph, Bigraph,List) := opts -> (G,B,U) -> (
return solverMLE(mixedGraph (G,B),U, opts);
);
solverMLE(Bigraph,Graph,List) := opts -> (B,G,U) -> (
return solverMLE(mixedGraph (B,G),U, opts);
);
solverMLE(Graph, Digraph, Bigraph, List) := opts -> (G,D,B,U) -> (
return solverMLE(mixedGraph (G,D,B),U, opts);
);
solverMLE(Digraph, Bigraph, Graph, List) := opts -> (D,B,G,U) -> (
return solverMLE(mixedGraph (D,B,G),U, opts);
);
solverMLE(Bigraph, Graph, Digraph, List) := opts -> (B,G,D,U) -> (
return solverMLE(mixedGraph (B,G,D),U, opts);
);
solverMLE(Graph,Bigraph, Digraph, List) := opts -> (G,B,D,U) -> (
return solverMLE(mixedGraph (G,B,D),U, opts);
);
solverMLE(Bigraph, Digraph,Graph, List) := opts -> (B,D,G,U) -> (
return solverMLE(mixedGraph (B,D,G),U, opts);
);
solverMLE(Digraph, Graph, Bigraph, List) := opts -> (D,G,B,U) -> (
return solverMLE(mixedGraph (D,G,B),U, opts);
);
solverMLE(List,MixedGraph) := opts -> (U,G) -> (
return solverMLE(G,U, opts);
);
solverMLE(List,Graph) := opts -> (U,G) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(List,Digraph) := opts -> (U,G) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(List,Bigraph) := opts -> (U,G) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(List,Graph,Digraph) := opts -> (U,G,D) -> (
return solverMLE(mixedGraph (G,D),U, opts);
);
solverMLE(List,Digraph,Graph) := opts -> (U,D,G) -> (
return solverMLE(mixedGraph (D,G),U, opts);
);
solverMLE(List,Digraph,Bigraph) := opts -> (U,D,B) -> (
return solverMLE(mixedGraph (D,B),U, opts);
);
solverMLE(List,Bigraph,Digraph) := opts -> (U,B,D) -> (
return solverMLE(mixedGraph (B,D),U, opts);
);
solverMLE(List,Graph, Bigraph) := opts -> (U,G,B) -> (
return solverMLE(mixedGraph (G,B),U, opts);
);
solverMLE(List,Bigraph,Graph) := opts -> (U,B,G) -> (
return solverMLE(mixedGraph (B,G),U, opts);
);
solverMLE(List,Graph, Digraph, Bigraph) := opts -> (U,G,D,B) -> (
return solverMLE(mixedGraph (G,D,B),U, opts);
);
solverMLE(List,Digraph, Bigraph, Graph) := opts -> (U,D,B,G) -> (
return solverMLE(mixedGraph (D,B,G),U, opts);
);
solverMLE(List,Bigraph, Graph, Digraph) := opts -> (U,B,G,D) -> (
return solverMLE(mixedGraph (B,G,D),U, opts);
);
solverMLE(List,Graph,Bigraph, Digraph) := opts -> (U,G,B,D) -> (
return solverMLE(mixedGraph (G,B,D),U, opts);
);
solverMLE(List,Bigraph, Digraph,Graph) := opts -> (U,B,D,G) -> (
return solverMLE(mixedGraph (B,D,G),U, opts);
);
solverMLE(List,Digraph, Graph, Bigraph) := opts -> (U,D,G,B) -> (
return solverMLE(mixedGraph (D,G,B),U, opts);
);
solverMLE(Graph,Matrix) := opts -> (G, U) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(Digraph,Matrix) := opts -> (G, U) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(Bigraph,Matrix) := opts -> (G, U) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(Graph,Digraph,Matrix) := opts -> (G,D,U) -> (
return solverMLE(mixedGraph (G,D),U, opts);
);
solverMLE(Digraph,Graph,Matrix) := opts -> (D,G,U) -> (
return solverMLE(mixedGraph (D,G),U, opts);
);
solverMLE(Digraph,Bigraph,Matrix) := opts -> (D,B,U) -> (
return solverMLE(mixedGraph (D,B),U, opts);
);
solverMLE(Bigraph,Digraph,Matrix) := opts -> (B,D,U) -> (
return solverMLE(mixedGraph (B,D),U, opts);
);
solverMLE(Graph, Bigraph,Matrix) := opts -> (G,B,U) -> (
return solverMLE(mixedGraph (G,B),U, opts);
);
solverMLE(Bigraph,Graph,Matrix) := opts -> (B,G,U) -> (
return solverMLE(mixedGraph (B,G),U, opts);
);
solverMLE(Graph, Digraph, Bigraph, Matrix) := opts -> (G,D,B,U) -> (
return solverMLE(mixedGraph (G,D,B),U, opts);
);
solverMLE(Digraph, Bigraph, Graph, Matrix) := opts -> (D,B,G,U) -> (
return solverMLE(mixedGraph (D,B,G),U, opts);
);
solverMLE(Bigraph, Graph, Digraph, Matrix) := opts -> (B,G,D,U) -> (
return solverMLE(mixedGraph (B,G,D),U, opts);
);
solverMLE(Graph,Bigraph, Digraph, Matrix) := opts -> (G,B,D,U) -> (
return solverMLE(mixedGraph (G,B,D),U, opts);
);
solverMLE(Bigraph, Digraph,Graph, Matrix) := opts -> (B,D,G,U) -> (
return solverMLE(mixedGraph (B,D,G),U, opts);
);
solverMLE(Digraph, Graph, Bigraph, Matrix) := opts -> (D,G,B,U) -> (
return solverMLE(mixedGraph (D,G,B),U, opts);
);
solverMLE(Matrix,MixedGraph) := opts -> (U,G) -> (
return solverMLE(G,U, opts);
);
solverMLE(Matrix,Graph) := opts -> (U,G) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(Matrix,Digraph) := opts -> (U,G) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(Matrix,Bigraph) := opts -> (U,G) -> (
return solverMLE(mixedGraph (G),U, opts);
);
solverMLE(Matrix,Graph,Digraph) := opts -> (U,G,D) -> (
return solverMLE(mixedGraph (G,D),U, opts);
);
solverMLE(Matrix,Digraph,Graph) := opts -> (U,D,G) -> (
return solverMLE(mixedGraph (D,G),U, opts);
);
solverMLE(Matrix,Digraph,Bigraph) := opts -> (U,D,B) -> (
return solverMLE(mixedGraph (D,B),U, opts);
);
solverMLE(Matrix,Bigraph,Digraph) := opts -> (U,B,D) -> (
return solverMLE(mixedGraph (B,D),U, opts);
);
solverMLE(Matrix,Graph, Bigraph) := opts -> (U,G,B) -> (
return solverMLE(mixedGraph (G,B),U, opts);
);
solverMLE(Matrix,Bigraph,Graph) := opts -> (U,B,G) -> (
return solverMLE(mixedGraph (B,G),U, opts);
);
solverMLE(Matrix,Graph, Digraph, Bigraph) := opts -> (U,G,D,B) -> (
return solverMLE(mixedGraph (G,D,B),U, opts);
);
solverMLE(Matrix,Digraph, Bigraph, Graph) := opts -> (U,D,B,G) -> (
return solverMLE(mixedGraph (D,B,G),U, opts);
);
solverMLE(Matrix,Bigraph, Graph, Digraph) := opts -> (U,B,G,D) -> (
return solverMLE(mixedGraph (B,G,D),U, opts);
);
solverMLE(Matrix,Graph,Bigraph, Digraph) := opts -> (U,G,B,D) -> (
return solverMLE(mixedGraph (G,B,D),U, opts);
);
solverMLE(Matrix,Bigraph, Digraph,Graph) := opts -> (U,B,D,G) -> (
return solverMLE(mixedGraph (B,D,G),U, opts);
);
solverMLE(Matrix,Digraph, Graph, Bigraph) := opts -> (U,D,G,B) -> (
return solverMLE(mixedGraph (D,G,B),U, opts);
);
--******************************************--
-- DOCUMENTATION --
--******************************************--
beginDocumentation()
doc ///
Key
GraphicalModelsMLE
Headline
a package for MLE of parameters for Gaussian graphical models
Description
Text
{\bf Graphical Models MLE} is a package for algebraic statistics that broadens the functionalities of @TO GraphicalModels@.
It computes the maximum likelihood estimates (MLE) of the covariance matrix of Gaussian graphical models associated to loopless mixed graphs(LMG).
The main features of the package are the computation of the @TO sampleCovarianceMatrix@ of sample data,
the ideal generated by @TO scoreEquations@ of log-likelihood functions of Gaussian graphical model,
the @TO MLdegree@ of such models and the MLE for the covariance or concentration matrix via @TO solverMLE@.
For more details on the type of graphical models that are accepted see @TO gaussianRing@.
In particular, for further information about LMG with undirected, directed and bidirected edges, check @TO partitionLMG@.
{\bf References:}
An introduction to key notions such as MLE and ML-degree can be found in the books:
Seth Sullivant, {\em Algebraic statistics}, American Mathematical Society, Vol 194, 2018.
Mathias Drton, Bernd Sturmfels and Seth Sullivant, {\em Lectures on Algebraic Statistics}, Oberwolfach Seminars, Vol 40, Birkhauser, Basel, 2009.
The definition and classification of loopless mixed graphs (LMG) can be found in the paper:
Kayvan Sadeghi and Steffen Lauritzen, {\em Markov properties for mixed graphs}, Bernoulli, 20 (2014), no 2, 676-696.
{\bf Examples:}
Computation of a sample covariance matrix from sample data:
Example
U= matrix{{1,2,1,-1},{2,1,3,0},{-1, 0, 1, 1},{-5, 3, 4, -6}};
sampleCovarianceMatrix(U)
Text
The ideal generated by the score equations of the log-likelihood function of the graphical model associated to the
graph $1\rightarrow 2,1\rightarrow 3,2\rightarrow 3,3\rightarrow 4,3<-> 4$ is computed as follows:
Example
G = mixedGraph(digraph {{1,2},{1,3},{2,3},{3,4}},bigraph{{3,4}});
R = gaussianRing(G);
U = matrix{{6, 10, 1/3, 1}, {3/5, 3, 1/2, 1}, {4/5, 3/2, 9/8, 3/10}, {10/7, 2/3,1, 8/3}};
scoreEquations(R,U)
Text
Computation of the ML-degree of the 4-cycle:
Example
G=graph{{1,2},{2,3},{3,4},{4,1}};
MLdegree(gaussianRing G)
Text
Next compute the MLE for the covariance matrix of the graphical model associated
to the graph $1\rightarrow 3,2\rightarrow 4,3<-> 4,1 - 2$.
The input is the sample covariance instead of the sample data.
Example
G = mixedGraph(digraph {{1,3},{2,4}},bigraph {{3,4}},graph {{1,2}});
V = matrix {{7/20, 13/50, -3/50, -19/100}, {13/50, 73/100, -7/100, -9/100},{-3/50, -7/100, 2/5, 3/50}, {-19/100, -9/100, 3/50, 59/100}};
solverMLE(G,V,SampleData=>false)
Text
As an application of @TO solverMLE@: positive definite matrix completion
Text
Consider the following symmetric matrix with some unknown entries:
Example
R=QQ[x,y];
M=matrix{{115,-13,x,47},{-13,5,7,y},{x,7,27,-21},{47,y,-21,29}}
Text
Unknown entries correspond to the non-edges of the 4-cycle. A positive definite completion of this matrix
is obtained by giving values to x and y and computing the MLE for the covariance matrix in the Gaussian graphical model
given by the 4-cycle. Check @TO solverMLE@ for more details.
Example
G=graph{{1,2},{2,3},{3,4},{1,4}};
V=matrix{{115,-13,-29,47},{-13,5,7,-11},{-29,7,27,-21},{47,-11,-21,29}};
(mx,MLE,ML)=solverMLE(G,V,SampleData=>false)
Caveat
GraphicalModelsMLE requires @TO Graphs@, @TO StatGraphs@ and @TO GraphicalModels@.
In order to use the default numerical solver, it also requires @TO EigenSolver@.
@TO Graphs@ allows the user to create graphs whose vertices are labeled arbitrarily.
However, several functions in GraphicalModels sort the vertices of the graph. Hence, graphs used as input to methods
in GraphicalModelsMLE must have sortable vertex labels, e.g., all numbers or all letters.
@TO StatGraphs@ allows the user to work with objects such as bigraphs and mixedGraphs.
@TO GraphicalModels@ is used to generate @TO gaussianRing@, i.e. rings encoding graph properties.
///
--------------------------------
-- Documentation
--------------------------------
doc ///
Key
sampleCovarianceMatrix
(sampleCovarianceMatrix, List)
(sampleCovarianceMatrix, Matrix)
Headline
sample covariance matrix of observation vectors
Usage
sampleCovarianceMatrix U
Inputs
U:Matrix
or @TO List@ of sample data
Outputs
:Matrix
sample covariance matrix of the sample data
Description
Text
The sample covariance matrix is $S = \frac{1}{n} \sum_{i=1}^{n} (X^{(i)}-\bar{X}) (X^{(i)}-\bar{X})^T$.
Note that for normally distributed random variables, $S$ is the maximum likelihood estimator (MLE) for the
covariance matrix. This is different from the unbiased estimator, which uses a denominator of $n-1$ instead of $n$.
Sample data is input as a matrix or a list.
The rows of the matrix or the elements of the list are observation vectors.
Example
L= {{1,2,1,-1},{2,1,3,0},{-1, 0, 1, 1},{-5, 3, 4, -6}};
sampleCovarianceMatrix(L)
U= matrix{{1,2,1,-1},{2,1,3,0},{-1, 0, 1, 1},{-5, 3, 4, -6}};
sampleCovarianceMatrix(U)
///
doc ///
Key
jacobianMatrixOfRationalFunction
(jacobianMatrixOfRationalFunction,RingElement)
Headline
Jacobian matrix of a rational function
Usage
jacobianMatrixOfRationalFunction(F)
Inputs
F:RingElement
in @TO frac@
Outputs
:Matrix
the Jacobian matrix of a rational function
Description
Text
This function computes the Jacobian matrix of a rational function.
The input is an element in a fraction field.
Example
R=QQ[x,y];
FR=frac R;
F=1/(x^2+y^2);
jacobianMatrixOfRationalFunction(F)
Example
R=QQ[t_1,t_2,t_3];
FR=frac R;
jacobianMatrixOfRationalFunction( (t_1^2*t_2)/(t_1+t_2^2+t_3^3) )
///
-------------------------------------------------------
-- Documentation scoreEquations -----------------------
-------------------------------------------------------
doc ///
Key
scoreEquations
(scoreEquations, Ring, List)
(scoreEquations, Ring, Matrix)
(scoreEquations, List, Ring)
(scoreEquations, Matrix, Ring)
(scoreEquations, MixedGraph, Matrix)
(scoreEquations, MixedGraph, List)
(scoreEquations, Graph, List)
(scoreEquations, Digraph, List)
(scoreEquations, Bigraph, List)
(scoreEquations, Graph, Digraph,List)
(scoreEquations, Digraph, Graph,List)
(scoreEquations, Digraph, Bigraph, List)
(scoreEquations, Bigraph, Digraph, List)
(scoreEquations, Graph, Bigraph, List)
(scoreEquations, Bigraph, Graph, List)
(scoreEquations, Graph, Digraph, Bigraph, List)
(scoreEquations, Digraph, Bigraph, Graph, List)
(scoreEquations, Bigraph, Graph, Digraph, List)
(scoreEquations, Graph, Bigraph, Digraph, List)
(scoreEquations, Bigraph, Digraph, Graph, List)
(scoreEquations, Digraph, Graph, Bigraph, List)
(scoreEquations, List, MixedGraph)
(scoreEquations, List, Graph)