/
RandomMonomialIdeals.m2
2624 lines (2460 loc) · 93.7 KB
/
RandomMonomialIdeals.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 -*-
newPackage(
"RandomMonomialIdeals",
Version => "1.0",
Date => "January 28, 2019",
Authors => {
{
Name => "Sonja Petrovic",
Email => "sonja.petrovic@iit.edu",
HomePage => "http://math.iit.edu/~spetrov1/"
},
{
Name => "Despina Stasi",
Email => "stasdes@iit.edu",
HomePage => "http://math.iit.edu/~stasdes/"
},
{
Name => "Dane Wilburne",
Email => "dwilburn@hawk.iit.edu",
HomePage => "http://mypages.iit.edu/~dwilburn/"
},
{
Name => "Tanner Zielinski",
Email => "tzielin1@hawk.iit.edu",
HomePage => "https://www.linkedin.com/in/tannerzielinski/"
},
{
Name => "Daniel Kosmas",
Email => "dkosmas@hawk.iit.edu",
HomePage => "https://www.linkedin.com/in/daniel-kosmas-03160988/"
},
{
Name => "Parker Joncus",
Email => "pjoncus@hawk.iit.edu"
},
{
Name => "Richard Osborn",
Email => "rosborn@hawk.iit.edu"
},
{
Name => "Monica Yun",
Email => "myun1@hawk.iit.edu"
},
{
Name => "Genevieve Hummel",
Email => "ghummel1@hawk.iit.edu"
}
},
Headline => "Erdos-Renyi-type random monomial ideals",
Keywords => {"Examples and Random Objects"},
PackageImports => { "Depth", "BoijSoederberg", "Serialization" },
DebuggingMode => false,
Certification => {
"journal name" => "The Journal of Software for Algebra and Geometry",
"journal URI" => "http://j-sag.org/",
"article title" => "Random Monomial Ideals: a Macaulay2 package",
"acceptance date" => "11 April 2019",
"published article URI" => "https://msp.org/jsag/2019/9-1/p08.xhtml",
"published article DOI" => "10.2140/jsag.2019.9.65",
"published code URI" => "https://msp.org/jsag/2019/9-1/jsag-v9-n1-x08-RandomMonomialIdeals.m2",
"repository code URI" => "http://github.com/Macaulay2/M2/blob/master/M2/Macaulay2/packages/RandomMonomialIdeals.m2",
"release at publication" => "902570b14480e7590b6960b1352331acce3ef817", -- git commit number in hex
"version at publication" => "1.0",
"volume number" => "9",
"volume URI" => "https://msp.org/jsag/2019/9-1/"
}
)
export {
"randomMonomialSets",
"randomMonomialSet",
"idealsFromGeneratingSets",
"randomMonomialIdeals",
"Coefficients",
"VariableName",
"mingenStats",
"IncludeZeroIdeals",
"dimStats",
"regStats",
"CMStats",
"borelFixedStats",
"ShowTally",
"degStats",
"bettiStats",
"SaveBettis",
"CountPure",
"pdimStats",
"Sample",
"sample",
"ModelName", "Parameters", "Generate", "SampleSize", "getData",
"writeSample",
"Model",
"model",
"ER",
"statistics",
"Mean", "StdDev", "Histogram"
}
--***************************************--
-- Exported methods --
--***************************************--
Sample = new Type of MutableHashTable
Model = new Type of HashTable
Data = local Data
model = method(TypicalValue => Model)
model(List,FunctionClosure,String):=(p,f,name)->(
-- p = parameter list
-- f = random generator from the model (predefined fn!)
new Model from {
Name => name,
Parameters => p,
Generate => () -> f toSequence p
}
)
ER = method(TypicalValue => Model)
ER (ZZ,ZZ,RR) := (n,D,p) -> (
if n<1 then error "n expected to be a positive integer";
if p<0.0 or 1.0<p then error "p expected to be a real number between 0.0 and 1.0";
x := symbol x;
R := QQ[x_1..x_n];
new Model from {
Name => "Erdos-Renyi",
Parameters => (n,D,p),
Generate => ()->randomMonomialSet(R,D,p)
}
)
ER (PolynomialRing,ZZ,RR) := (R,D,p) -> (
if p<0.0 or 1.0<p then error "p expected to be a real number between 0.0 and 1.0";
new Model from {
Name => "Erdos-Renyi",
Parameters => (R,D,p),
Generate => ()->randomMonomialSet(R,D,p)
}
)
ER (ZZ,ZZ,ZZ) := (n,D,M) -> (
if n<1 then error "n expected to be a positive integer";
x := symbol x;
R := QQ[x_1..x_n];
new Model from {
Name => "Erdos-Renyi",
Parameters => (n,D,M),
Generate => ()->randomMonomialSet(R,D,M)
}
)
ER (PolynomialRing,ZZ,ZZ) := (R,D,M) -> (
new Model from {
Name => "Erdos-Renyi",
Parameters => (R,D,M),
Generate => ()->randomMonomialSet(R,D,M)
}
)
ER (ZZ,ZZ,List) := (n,D,pOrM) -> (
if n<1 then error "n expected to be a positive integer";
if #pOrM != D then error "pOrM expected to be a list of length D";
if not all(pOrM, q->instance(q, ZZ)) and not all(pOrM, q->instance(q,RR))
then error "pOrM must be a list of all integers or all real numbers";
x := symbol x;
R := QQ[x_1..x_n];
new Model from {
Name => "Erdos-Renyi",
Parameters => (n,D,pOrM),
Generate => ()->randomMonomialSet(R,D,pOrM)
}
)
ER (PolynomialRing,ZZ,List) := (R,D,pOrM) -> (
if #pOrM != D then error "pOrM expected to be a list of length D";
if not all(pOrM, q->instance(q, ZZ)) and not all(pOrM, q->instance(q,RR))
then error "pOrM must be a list of all integers or all real numbers";
if all(pOrM, q->instance(q,RR)) and any(pOrM,q-> q<0.0 or 1.0<q)
then error "pOrM expected to be a list of real numbers between 0.0 and 1.0";
new Model from {
Name => "Erdos-Renyi",
Parameters => (R,D,pOrM),
Generate => ()->randomMonomialSet(R,D,pOrM)
}
)
sample = method(TypicalValue => Sample)
sample (Model, ZZ) := (M,N) -> (
if N<1 then stderr << "warning: N expected to be a positive integer" << endl;
s:=new Sample;
s.ModelName = M.Name;
s.Parameters = M.Parameters;
s.SampleSize = N;
s.Data = apply(N,i->M.Generate());
s
)
sample String := filename -> (
if not isDirectory filename then error "expected a directory";
modelFile := realpath filename | "Model.txt";
model := lines read openIn modelFile;
s := new Sample;
s.ModelName = model#1;
s.Parameters = value toString stack drop(model,{0,1});
s.SampleSize = value model#0;
dataFile := realpath filename | "Data.txt";
s.Data = value read openIn dataFile;
s
)
getData = method()
getData Sample := s -> (s.Data)
writeSample = method()
writeSample (Sample, String) := (s, dirname) -> (
if fileExists dirname then (
stderr << "warning: directory or file with this name already exists." << endl;
if not isDirectory dirname then (
stderr << "warning: overwrting file." << endl;
removeFile dirname;
mkdir dirname;
);
if isDirectory dirname then (
stderr << "warning: updating name of directory:" << endl;
dirname = dirname |"New";
stderr << dirname << endl;
mkdir dirname;
);
)
else mkdir dirname;
realpath dirname | "Model.txt" << s.SampleSize << endl << s.ModelName << endl << serialize s.Parameters << close;
realpath dirname | "Data.txt" << serialize s.Data << close; -- Write other data
)
statistics = method(TypicalValue => HashTable)
statistics (Sample, Function) := HashTable => (s,f) -> (
fData := apply(getData s,f);
histogram := tally fData;
if not(class fData_0 === ZZ) then (
-- in case the data is actually a BettiTally type, then we are able to get the mean and stddev of the tables:
if (class fData_0 === BettiTally) then (
-- compute the average (entry-wise) tally table:
dataSum := sum histogram;
dataMean := mat2betti(1/s.SampleSize *(sub(matrix(dataSum), RR)));
-- compute the standard deviation (entry-wise) of the Betti tables:
dataMeanMtx := matrix dataMean;
dataVariance := 1/s.SampleSize * sum apply(fData, currentTally -> (
mtemp := new MutableMatrix from dataMeanMtx;
currentTallyMatrix := matrix currentTally;
apply(numrows currentTallyMatrix, i->
apply(numcols currentTallyMatrix, j->
(
--compute mtemp_(i,j) := (bMean_(i,j) - bCurrent_(i,j)):
mtemp_(i,j) = mtemp_(i,j) - currentTallyMatrix_j_i
)
)
);
--square entries of mtemp, to get (bMean_(i,j) - bCurrent_(i,j))^2:
mtemp = matrix pack(apply( flatten entries mtemp,i->i^2), numcols mtemp)
)
);
-- dataStdDev := dataVariance^(1/2); -- <--need to compute entry-wise for the matrix(BettyTally)
dataStdDev := mat2betti matrix pack(apply( flatten entries dataVariance,i->sqrt i), numcols dataVariance);
new HashTable from {
Mean=>mat2betti dataMeanMtx,
StdDev=>dataStdDev,
Histogram=>histogram
}
) else (
stderr << "Warning: the statistics method is returning only the Tally of the outputs of
your function applied to the sample data. If you want more information, such as mean and
standard deviation, then ensure you use a function with numerical (ZZ) or BettiTally output." <<endl;
histogram
)
)
else (
mean := (sum fData)/s.SampleSize;
new HashTable from {
Mean=>mean,
StdDev=>sqrt(sum apply(fData, x-> (mean-x)^2)/s.SampleSize),
Histogram=>histogram
}
)
)
randomMonomialSets = method(TypicalValue => List, Options => {
Coefficients => QQ,
VariableName => "x",
Strategy => "ER"
})
randomMonomialSets (ZZ,ZZ,RR,ZZ) := List => o -> (n,D,p,N) -> (
if p<0.0 or 1.0<p then error "p expected to be a real number between 0.0 and 1.0";
randomMonomialSets(n,D,toList(D:p),N,o)
)
randomMonomialSets (PolynomialRing,ZZ,RR,ZZ) := List => o -> (R,D,p,N) -> (
if p<0.0 or 1.0<p then error "p expected to be a real number between 0.0 and 1.0";
randomMonomialSets(R,D,toList(D:p),N,o)
)
randomMonomialSets (ZZ,ZZ,ZZ,ZZ) := List => o -> (n,D,M,N) -> (
if N<1 then stderr << "warning: N expected to be a positive integer" << endl;
if not (instance(o.VariableName,Symbol) or instance(o.VariableName,String) or instance(o.VariableName,IndexedVariableTable)) then error "expected VariableName to be a string or symbol";
x := toSymbol o.VariableName;
R := o.Coefficients[x_1..x_n];
apply(N,i-> randomMonomialSet(R,D,M,o))
)
randomMonomialSets (PolynomialRing,ZZ,ZZ,ZZ) := List => o -> (R,D,M,N) -> (
if N<1 then stderr << "warning: N expected to be a positive integer" << endl;
apply(N,i-> randomMonomialSet(R,D,M,o))
)
randomMonomialSets (ZZ,ZZ,List,ZZ) := List => o -> (n,D,pOrM,N) -> (
if n<1 then error "n expected to be a positive integer";
if N<1 then stderr << "warning: N expected to be a positive integer" << endl;
if not (instance(o.VariableName,Symbol) or instance(o.VariableName,String) or instance(o.VariableName,IndexedVariableTable)) then error "expected VariableName to be a string or symbol";
x := toSymbol o.VariableName;
R := o.Coefficients[x_1..x_n];
apply(N,i-> randomMonomialSet(R,D,pOrM,o))
)
randomMonomialSets (PolynomialRing,ZZ,List,ZZ) := List => o -> (R,D,pOrM,N) -> (
if N<1 then stderr << "warning: N expected to be a positive integer" << endl;
apply(N,i-> randomMonomialSet(R,D,pOrM,o))
)
randomMonomialSet = method(TypicalValue => List, Options => {
Coefficients => QQ,
VariableName => "x",
Strategy => "ER"
})
randomMonomialSet (ZZ,ZZ,RR) := List => o -> (n,D,p) -> (
if p<0.0 or 1.0<p then error "p expected to be a real number between 0.0 and 1.0";
randomMonomialSet(n,D,toList(D:p),o)
)
randomMonomialSet (PolynomialRing,ZZ,RR) := List => o -> (R,D,p) -> (
if p<0.0 or 1.0<p then error "p expected to be a real number between 0.0 and 1.0";
randomMonomialSet(R,D,toList(D:p),o)
)
randomMonomialSet (ZZ,ZZ,ZZ) := List => o -> (n,D,M) -> (
if n<1 then error "n expected to be a positive integer";
if not (instance(o.VariableName,Symbol) or instance(o.VariableName,String) or instance(o.VariableName,IndexedVariableTable)) then error "expected VariableName to be a string or symbol";
x := toSymbol o.VariableName;
R := o.Coefficients[x_1..x_n];
randomMonomialSet(R,D,M)
)
randomMonomialSet (PolynomialRing,ZZ,ZZ) := List => o -> (R,D,M) -> (
if M<0 then stderr << "warning: M expected to be a nonnegative integer" << endl;
if o.Strategy === "Minimal" then error "Minimal not implemented for fixed size ER model";
allMonomials := flatten flatten apply(toList(1..D),d->entries basis(d,R));
C := take(random(allMonomials), M);
if C==={} then {0_R} else C
)
randomMonomialSet (ZZ,ZZ,List) := List => o -> (n,D,pOrM) -> (
if n<1 then error "n expected to be a positive integer";
if not (instance(o.VariableName,Symbol) or instance(o.VariableName,String) or instance(o.VariableName,IndexedVariableTable)) then error "expected VariableName to be a string or symbol";
x := toSymbol o.VariableName;
R := o.Coefficients[x_1..x_n];
randomMonomialSet(R,D,pOrM,o)
)
randomMonomialSet (PolynomialRing,ZZ,List) := List => o -> (R,D,pOrM) -> (
if #pOrM != D then error "pOrM expected to be a list of length D";
if not all(pOrM, q->instance(q, ZZ)) and not all(pOrM, q->instance(q,RR))
then error "pOrM must be a list of all integers or all real numbers";
B := {};
if all(pOrM,q->instance(q,ZZ)) then (
if o.Strategy === "Minimal" then (
currentRingM := R;
apply(D, d->(
chosen := take(random(flatten entries basis(d+1, currentRingM)), pOrM_d);
B = flatten append(B, chosen/(i->sub(i, R)));
currentRingM = currentRingM/promote(ideal(chosen), currentRingM)
)
)
) else B = flatten apply(toList(1..D), d->take(random(flatten entries basis(d,R)), pOrM_(d-1)));
) else if all(pOrM,q->instance(q,RR)) then (
if any(pOrM,q-> q<0.0 or 1.0<q) then error "pOrM expected to be a list of real numbers between 0.0 and 1.0";
if o.Strategy === "Minimal" then (
currentRing := R;
apply(D, d->(
chosen := select(flatten entries basis(d+1, currentRing), m->random(0.0,1.0)<=pOrM_d);
B = flatten append(B, chosen/(i->sub(i, R)));
currentRing = currentRing/promote(ideal(chosen), currentRing)
)
)
) else B = flatten apply(toList(1..D),d-> select(flatten entries basis(d,R),m-> random(0.0,1.0)<=pOrM_(d-1)));
);
B = apply(B,m->sub(m,R));
if B==={} then {0_R} else B
)
bettiStats = method(TypicalValue =>Sequence, Options =>{IncludeZeroIdeals=>true, SaveBettis => "", CountPure => false, Verbose => false})
bettiStats List := o-> (ideals) -> (
N := #ideals; Z:=0;
if o.SaveBettis != "" then (
if fileExists o.SaveBettis then (
stderr << "warning: filename already exists. Overwriting." << endl;
removeFile o.SaveBettis;
);
);
if not o.IncludeZeroIdeals then (
(ideals,Z) = extractNonzeroIdeals(ideals);
if o.Verbose then stdio << "There are "<<N<<" ideals in this sample. Of those, "<<Z<<" are the zero ideal." << endl;
if (Z>0 and not o.IncludeZeroIdeals) then stdio <<"The Betti statistics do not include those for the zero ideals."<< endl
);
if (o.Verbose and o.IncludeZeroIdeals) then (
Z = (extractNonzeroIdeals(ideals))_1;
stdio << "There are "<<N<<" ideals in this sample. Of those, "<<Z<<" are the zero ideal." << endl;
if Z>0 then stdio <<"The Betti statistics do include those for the zero ideals."<< endl
);
-- sum of the betti tables and betti shapes:
betaShapes := new BettiTally;
bettisHistogram := {};
pure := 0; -- count pure Betti tables
-- add up all the betti tables:
apply(#ideals,i->(
resi := betti res ideals_i;
if o.CountPure then if isPure resi then pure = pure +1;
if o.SaveBettis != "" then o.SaveBettis << resi << endl;
bettisHistogram = append(bettisHistogram, resi);
-- let's only keep a 1 in all spots where there was a non-zero Betti number:
beta1mtx := matrix(resi);
Rtemp := (ring ideals_i)^1/ideals_i;
beta1shape := new BettiTally from mat2betti matrix pack(1+pdim(Rtemp), apply(flatten entries beta1mtx, i-> if i>0 then i=1 else i=0));
betaShapes = betaShapes + beta1shape
)
);
if o.SaveBettis != "" then o.SaveBettis << close;
-- compute the average Betti table shape:
bShapeMean := mat2betti(1/#ideals*(sub(matrix(betaShapes), RR)));
-- compute the average (entry-wise) Betti table:
betaSum := sum bettisHistogram;
bMean := mat2betti(1/#ideals*(sub(matrix(betaSum), RR)));
-- compute the standard deviation (entry-wise) of the Betti tables:
bMeanMtx := matrix bMean;
betaVariance := 1/#ideals * sum apply(bettisHistogram, currentBetti -> (
mtemp := new MutableMatrix from bMeanMtx;
currentBettiMatrix := matrix currentBetti;
apply(numrows currentBettiMatrix, i->
apply(numcols currentBettiMatrix, j->(
--compute mtemp_(i,j) := (bMean_(i,j) - bCurrent_(i,j)):
mtemp_(i,j) = mtemp_(i,j) - currentBettiMatrix_j_i
)
)
);
--square entries of mtemp, to get (bMean_(i,j) - bCurrent_(i,j))^2:
mtemp = matrix pack(apply( flatten entries mtemp,i->i^2), numcols mtemp)
)
);
-- betaStdDev := betaVariance^(1/2); -- <--need to compute entry-wise for the matrix(BettyTally)
bStdDev := mat2betti matrix pack(apply( flatten entries betaVariance,i->sqrt i), numcols betaVariance);
if o.CountPure then return (bShapeMean,bMean,bStdDev,pure);
(bShapeMean,bMean,bStdDev)
)
degStats = method(TypicalValue =>Sequence, Options =>{ShowTally => false, Verbose => false})
degStats List := o-> (ideals) -> (
N := #ideals;
deg := 0;
degHistogram := apply(ideals, i-> degree i);
ret:=();
avg:=1./N*(sum degHistogram);
Ex2:=1./N*(sum apply(elements(tally degHistogram), i->i^2));
var:= Ex2 - avg^2;
stdDev:= var^(1/2);
if o.ShowTally then return (avg, stdDev, tally degHistogram);
if o.Verbose then (
numberOfZeroIdeals := (extractNonzeroIdeals(ideals))_1;
stdio << "There are "<<N<<" ideals in this sample. Of those, "<< numberOfZeroIdeals <<" are the zero ideal." << endl;
if numberOfZeroIdeals>0 then stdio <<"The degree statistics do include those for the zero ideals."<< endl
);
(avg, stdDev)
)
--creates a list of monomialIdeal objects from a list of monomial generating sets
idealsFromGeneratingSets = method(TypicalValue => List, Options => {IncludeZeroIdeals => true, Verbose => false})
idealsFromGeneratingSets(List):= o -> (B) -> (
N := # B;
n := numgens ring ideal B#0; -- ring of the first monomial in the first gen set
ideals := B / (b-> monomialIdeal b);
(nonzeroIdeals,numberOfZeroIdeals) := extractNonzeroIdeals(ideals);
if o.Verbose then stdio <<"There are "<<#B<<" ideals in this sample. Of those, "<<numberOfZeroIdeals<<" are the zero ideal."<< endl;
if o.IncludeZeroIdeals then return ideals else return (nonzeroIdeals,numberOfZeroIdeals);
)
dimStats = method(TypicalValue => Sequence, Options => {ShowTally => false, Verbose =>false})
dimStats List := o-> (ideals) -> (
N := #ideals;
dims:=0;
dimsHistogram := apply(ideals, i-> dim i);
ret:= ();
avg:=1./N*(sum dimsHistogram);
Ex2:=1./N*(sum apply(elements(tally dimsHistogram), i->i^2));
var:= Ex2 - avg^2;
stdDev:= var^(1/2);
if o.ShowTally then return (avg, stdDev, tally dimsHistogram);
if o.Verbose then (
numberOfZeroIdeals := (extractNonzeroIdeals(ideals))_1;
stdio << "There are "<<N<<" ideals in this sample. Of those, "<< numberOfZeroIdeals <<" are the zero ideal." << endl;
if numberOfZeroIdeals>0 then stdio <<"The Krull dimension statistics do include those for the zero ideals."<< endl
);
(avg, stdDev)
)
regStats = method(TypicalValue => Sequence, Options => {ShowTally => false, Verbose => false})
regStats List := o-> (ideals) -> (
N:=#ideals;
ideals = extractNonzeroIdeals(ideals);
ideals = ideals_0;
reg := 0;
regHistogram:={};
if set {} === set ideals then (
regHistogram = N:-infinity;
stdDev := 0;
if o.ShowTally then return (-infinity, 0, tally regHistogram);
if o.Verbose then stdio <<"All ideals in this list are the zero ideal." << endl;
(-infinity, 0)
)
else (
regHistogram = apply(ideals, i-> regularity i);
avg := 1./#ideals*(sum regHistogram);
Ex2 := (1./(#ideals))*(sum apply(elements(tally regHistogram), i->i^2));
var := Ex2-avg^2;
stdDev = var^(1/2);
if o.ShowTally then return (avg, stdDev,tally regHistogram);
if o.Verbose then (
stdio << "There are "<<N<<" ideals in this sample. Of those, "<< toString(N-#ideals) <<" are the zero ideal." << endl;
stdio << "The zero ideals were extracted from the sample before reporting the regularity statistics."<< endl;
);
(avg, stdDev)
)
)
randomMonomialIdeals = method(TypicalValue => List, Options => {Coefficients => QQ, VariableName => "x", IncludeZeroIdeals => true, Strategy => "ER"})
randomMonomialIdeals (ZZ,ZZ,List,ZZ) := List => o -> (n,D,pOrM,N) -> (
B:={};
if all(pOrM,q->instance(q,RR)) then B=randomMonomialSets(n,D,pOrM,N,Coefficients=>o.Coefficients,VariableName=>o.VariableName,Strategy=>"Minimal")
else if all(pOrM,q->instance(q,ZZ)) then B=randomMonomialSets(n,D,pOrM,N,Coefficients=>o.Coefficients,VariableName=>o.VariableName, Strategy=>o.Strategy);
idealsFromGeneratingSets(B,IncludeZeroIdeals=>o.IncludeZeroIdeals)
)
randomMonomialIdeals (ZZ,ZZ,RR,ZZ) := List => o -> (n,D,p,N) -> (
B:=randomMonomialSets(n,D,p,N,Coefficients=>o.Coefficients,VariableName=>o.VariableName,Strategy=>"Minimal");
idealsFromGeneratingSets(B,IncludeZeroIdeals=>o.IncludeZeroIdeals)
)
randomMonomialIdeals (ZZ,ZZ,ZZ,ZZ) := List => o -> (n,D,M,N) -> (
B:=randomMonomialSets(n,D,M,N,Coefficients=>o.Coefficients,VariableName=>o.VariableName);
idealsFromGeneratingSets(B,IncludeZeroIdeals=>o.IncludeZeroIdeals)
)
CMStats = method(TypicalValue => QQ, Options =>{Verbose => false})
CMStats (List) := QQ => o -> (ideals) -> (
cm := 0;
N := #ideals;
R := ring(ideals#0);
for i from 0 to #ideals-1 do (
if isCM(R/ideals_i) == true then cm = cm + 1 else cm = cm
);
if o.Verbose then (
numberOfZeroIdeals := (extractNonzeroIdeals(ideals))_1;
stdio <<"There are "<<N<<" ideals in this sample. Of those, " << numberOfZeroIdeals << " are the zero ideal." << endl;
if numberOfZeroIdeals>0 then stdio <<"The zero ideals are included in the reported count of Cohen-Macaulay quotient rings."<< endl;
stdio << cm << " out of " << N << " ideals in the given sample are Cohen-Macaulay." << endl;
);
cm/N
)
borelFixedStats = method(TypicalValue =>QQ, Options =>{Verbose => false})
borelFixedStats (List) := QQ => o -> (ideals) -> (
bor := 0;
N:=#ideals;
for i from 0 to #ideals-1 do (
if isBorel((ideals_i)) == true then bor = bor + 1 else bor = bor
);
if o.Verbose then (
numberOfZeroIdeals := (extractNonzeroIdeals(ideals))_1;
stdio <<"There are "<<N<<" ideals in this sample. Of those, " << numberOfZeroIdeals << " are the zero ideal." << endl;
if numberOfZeroIdeals>0 then stdio <<"The zero ideals are included in the reported count of Borel-fixed monomial ideals."<< endl;
stdio << bor << " out of " << N << " monomial ideals in the given sample are Borel-fixed." << endl;
);
bor/N
)
mingenStats = method(TypicalValue => Sequence, Options => {ShowTally => false, Verbose =>false})
mingenStats (List) := Sequence => o -> (ideals) -> (
N:=#ideals;
ideals = extractNonzeroIdeals(ideals);
numberOfZeroIdeals := ideals_1;
ideals = ideals_0;
num := 0;
numgensHist := {};
m := 0;
complexityHist := {};
ret:=();
if set {} === set ideals then (
numgensHist = N:-infinity;
complexityHist = N:-infinity;
numStdDev := 0;
comStdDev := 0;
if o.ShowTally then return (-infinity, 0, tally numgensHist, -infinity, 0, tally complexityHist);
if o.Verbose then stdio <<"This sample included only zero ideals." << endl;
(-infinity, 0, -infinity, 0)
)
else (
apply(#ideals,i->(
mingensi := gens gb ideals_i;
numgensi := numgens source mingensi;
mi := max({degrees(mingensi)}#0#1);
numgensHist = append(numgensHist, numgensi);
complexityHist = append(complexityHist, mi#0)
)
);
numAvg:=sub((1/(#ideals))*(sum numgensHist), RR);
comAvg:=sub((1/(#ideals))*(sum complexityHist), RR);
numEx2:=sub((1/(#ideals))*(sum apply(elements(tally numgensHist), i->i^2)), RR);
comEx2:=sub((1/(#ideals))*(sum apply(elements(tally complexityHist), i->i^2)), RR);
numVar:= numEx2 - numAvg^2;
comVar:= comEx2 - comAvg^2;
numStdDev= numVar^(1/2);
comStdDev= comVar^(1/2);
if o.ShowTally then return (numAvg, numStdDev, tally numgensHist, comAvg, comStdDev, tally complexityHist);
if o.Verbose then (
stdio <<"There are "<<N<<" ideals in this sample. Of those, " << numberOfZeroIdeals << " are the zero ideal." << endl;
if numberOfZeroIdeals>0 then stdio <<"The statistics returned (mean and standard deviation of # of min gens and mean and standard deviation of degree complexity) do NOT include those for the zero ideals."<< endl
);
(numAvg, numStdDev, comAvg, comStdDev)
)
)
pdimStats = method(TypicalValue=>Sequence, Options => {ShowTally => false, Verbose => false})
pdimStats (List) := o-> (ideals) -> (
N:=#ideals;
R:=ring(ideals_0);
pdHist := apply(ideals, i-> pdim(R^1/i));
ret:=();
avg:=sub(((1/N)*(sum pdHist)),RR);
Ex2:=sub(((1/N)*(sum apply(elements(tally pdHist), i->i^2))), RR);
var:= Ex2 - avg^2;
stdDev:= var^(1/2);
if o.ShowTally then return (avg, stdDev, tally pdHist);
if o.Verbose then (
numberOfZeroIdeals := (extractNonzeroIdeals(ideals))_1;
stdio <<"There are "<<N<<" ideals in this sample. Of those, " << numberOfZeroIdeals << " are the zero ideal." << endl;
if numberOfZeroIdeals>0 then stdio <<"The projective dimension statistics do include those for the zero ideals."<< endl
);
(avg, stdDev)
)
--**********************************--
-- Internal methods --
--**********************************--
toSymbol = method()
toSymbol Symbol := p -> p
toSymbol String := p -> getSymbol p
toSymbol IndexedVariableTable := p -> p
-- Internal method that takes as input list of ideals and splits out the zero ideals, counting them:
-- input list of ideals
-- output a sequence (list of non-zero ideals from the list , the number of zero ideals in the list)
-- (not exported, therefore no need to document)
extractNonzeroIdeals = ( ideals ) -> (
nonzeroIdeals := select(ideals,i->i != 0);
numberOfZeroIdeals := # ideals - # nonzeroIdeals;
-- numberOfZeroIdeals = # positions(B,b-> b#0==0); -- since 0 is only included if the ideal = ideal{}, this is safe too
return(nonzeroIdeals,numberOfZeroIdeals)
)
-- Internal method that takes as input list of generating sets and splits out the zero ideals, counting them:
-- input list of generating sets
-- output a sequence (list of non-zero ideals from the list , the number of zero ideals in the list)
-- (not exported, therefore no need to document)
extractNonzeroIdealsFromGens = ( generatingSets ) -> (
nonzeroIdeals := select(generatingSets,i-> i#0 != 0_(ring i#0)); --ideal(0)*ring(i));
numberOfZeroIdeals := # generatingSets - # nonzeroIdeals;
-- numberOfZeroIdeals = # positions(B,b-> b#0==0); -- since 0 is only included if the ideal = ideal{}, this is safe too
return(nonzeroIdeals,numberOfZeroIdeals)
)
-- the following function is needed to fix the Boij-Soederberg "matrix BettiTally" method
-- that we can't use directly for StdDev computation, because we're working over RR not over ZZ:
matrix(BettiTally, ZZ, ZZ) := opts -> (B,lowestDegree, highestDegree) -> (
c := pdim B + 1;
r := highestDegree - lowestDegree + 1;
--M := mutableMatrix(ZZ,r,c);
M := mutableMatrix(RR,r,c);
scan(pairs B, (i,v) -> (
if v != 0 then
M_(i_2-i_0-lowestDegree, i_0) = v;
)
);
matrix M
)
--******************************************--
-- DOCUMENTATION --
--******************************************--
beginDocumentation()
doc ///
Key
RandomMonomialIdeals
Headline
A package for generating Erdos-Renyi-type random monomial ideals and variations
Description
Text
{\em RandomMonomialIdeals} is a package for sampling random monomial ideals from an Erdos-Renyi-type distribution, the graded version of it, and some extensions.
It also introduces new types of objects, @TO Sample@ and @TO Model@, to allow for streamlined handling of random objects and their statistics in {\em Macaulay2}.
Most of the models implemented are drawn from the paper {\em Random Monomial Ideals} by Jesus A. De Loera, Sonja Petrovic, Lily Silverstein, Despina Stasi, and Dane Wilburne
(@HREF"https://arxiv.org/abs/1701.07130"@).
The main method, @TO randomMonomialSets@, generates a sample of size $N$ from the distribution $\mathcal B(n, D, p)$ of
sets of monomials of degree at most $D$ on $n$ variables, where $p$ is the probability of selecting any given monomial:
Example
n=3; D=2; p=0.5; N=4;
L = randomMonomialSets(n,D,p,N)
Text
For a formal definition of the distribution, see Section 1 of @HREF"https://arxiv.org/abs/1701.07130"@.
As is customary, we use the word `model' when referring to a distribution of the random objects at hand.
The model defined by $\mathcal B(n, D, p)$ was inspired by the Erdos-Renyi random graph model denoted by $G(n,p)$:
it is a natural generalization, as squarefree monomials of degree tww can be encoded by edges of a graph whose vertices are the variables.
The random graph model can be introduced in two ways: one can either fix the probability of an edge or fix the total number of edges.
Thus the package also includes the model variant that generates a fixed {\em number} of monomials:
Example
n=3; D=2; M=3; N=4;
L = randomMonomialSets(n,D,M,N)
Text
To sample from the {\em graded} model from Section 6 of @HREF"https://arxiv.org/abs/1701.07130"@,
simply replace $p$ by a list of $D$ probabilities, one for each degree.
In the example below, monomials of degree 1 are not selected since their probability = 0,
while each monomial of degree 2 is selected with probability 1.
Example
n=3; D=2; N=4;
randomMonomialSets(n,D,{0.0,1.0},N)
Text
The package also allows for sampling from the {\em graded} version of the fixed number of monomials model,
where we specify the requested number of monomials of each degree.
In the example below, we sample random sets of monomials with one monomial of degree 1, zero of degree 2 and three monomials of degree 3.
Example
n=3; D=3; N=4;
randomMonomialSets(n,D,{1,0,3},N)
Text
Finally, we can request the monomial sets generated by the {\em graded} model with a fixed number of monomials to be minimal generating sets. We can also employ the minimal strategy for a couple of other versions of the randomMonomialSets method.
Example
n=3; D=3; N=4;
randomMonomialSets(n,D,{1,0,3},N, Strategy=>"Minimal")
randomMonomialSets(n,D,{0.0,0.3,0.5},N, Strategy=>"Minimal")
randomMonomialSets(n,D,0.1,N, Strategy=>"Minimal")
Text
Once a sample (that is, a set of random objects) is generated, one can compute various statistics of algebraic properties of the sample.
The methods in the package offer a way to compute and summarize statistics of some of the common properties, such as
degree, dimension, projective dimension, Castelnuovo-Mumford regularity, etc.
For example, we can use the method @TO dimStats@ to get the Krull dimension statistics:
Example
ideals=idealsFromGeneratingSets(L)
dimStats(ideals,ShowTally=>true)
Text
The first entry in the output of the method @TO dimStats@ is the mean Krull dimension of the sample.
The second entry is the standard deviation.
Similarly, one can obtain the mean and standard deviations of the number of minimal generators and degree complexity via @TO mingenStats@,
and the average Betti table shape, mean Betti table, and its standard deviation via @TO bettiStats@:
Example
mingenStats ideals
bettiStats ideals
Text
For developing other models and computing statistics on objects other than monomial ideals, the package also
defines two new types of object, @TO Model@ and @TO Sample@, which allow for convenient storage of statistics
from a sample of algebraic objects and streamlines writing sample data into files.
For example, below we create a sample of size 10 from the Erdos-Renyi distribution $\mathcal B(n, D, p)$ on monomials
in $Q[y,w]$ with $D=4$, and $p=0.5$, and then a sample of size 15 from the graded version of this distribution
on monomials in $Z/101[z_1..z_8]$ with $D=2$, and $p={0.25,0.5}$:
Example
sample1 = sample(ER(QQ[y,w],4,0.5),10)
sample2 = sample(ER(ZZ/101[z_1..z_8],2,{0.25,0.75}),15)
Text
The output is a hash table with 4 entries. To obtain the random sets of monomials that were generated (that is, the actual data we are interested in),
use the command @TO getData@:
Example
keys sample1
sample2.Parameters
myData = getData(sample1);
myData_0
Text
We can also use the object of type @TO Sample@ to calculate the mean, standard deviation, and tally of the dimension of the ideals in the sample:
Example
statistics(sample(ER(CC[z_1..z_8],5,0.1),100), degree@@ideal)
Text
Most of the methods in this package offer various options, such as selecting a specific ring with which to work,
or specifying variable names, coefficients, etc. Here is a simple example:
Example
R=ZZ/101[a..e];
randomMonomialSets(R,D,p,N)
randomMonomialSets(n,D,p,N,VariableName=>"t")
Text
In some cases, we may want to work directly with the sets of randomly chosen monomials, while at other times it may be
more convenient to pass directly to the corresponding random monomial ideals.
Both options induce the same distribution on monomial ideals:
Example
randomMonomialSets(3,4,1.0,1)
monomialIdeal flatten oo
randomMonomialIdeals(3,4,1.0,1)
SeeAlso
randomMonomialSet
Verbose
sample
model
///
doc ///
Key
randomMonomialSets
(randomMonomialSets,ZZ,ZZ,RR,ZZ)
(randomMonomialSets,PolynomialRing,ZZ,RR,ZZ)
(randomMonomialSets,ZZ,ZZ,ZZ,ZZ)
(randomMonomialSets,PolynomialRing,ZZ,ZZ,ZZ)
(randomMonomialSets,ZZ,ZZ,List,ZZ)
(randomMonomialSets,PolynomialRing,ZZ,List,ZZ)
Headline
randomly generates lists of monomials in fixed number of variables up to a given degree
Usage
randomMonomialSets(ZZ,ZZ,RR,ZZ)
randomMonomialSets(PolynomialRing,ZZ,RR,ZZ)
randomMonomialSets(ZZ,ZZ,ZZ,ZZ)
randomMonomialSets(PolynomialRing,ZZ,ZZ,ZZ)
randomMonomialSets(ZZ,ZZ,List,ZZ)
randomMonomialSets(PolynomialRing,ZZ,List,ZZ)
Inputs
n: ZZ
number of variables, OR
: PolynomialRing
the ring in which the monomials are to live if $n$ is not specified
D: ZZ
maximum degree
p: RR
the probability of selecting a monomial, OR
M: ZZ
number of monomials in the set, up to the maximum number of monomials in $n$ variables of degree at most $D$ OR
: List
of real numbers whose $i$-th entry is the probability of selecting a monomial of degree $i$, OR
: List
of integers whose $i$-th entry is the number of monomials of degree $i$ in each set, up to the maximum number of monomials in $n$ variables of degree exactly $i$
N: ZZ
number of sets to be generated
Outputs
: List
random generating sets of monomials
Description
Text
This function creates $N$ random sets of monomials of degree $d$, $1\leq d\leq D$, in $n$ variables.
It does so by calling @TO randomMonomialSet@ $N$ times.
SeeAlso
randomMonomialSet
///
doc ///
Key
bettiStats
(bettiStats,List)
Headline
statistics on Betti tables of a sample of monomial ideals or list of objects
Usage
bettiStats(List)
Inputs
L: List
of objects of type @TO MonomialIdeal@, or any objects to which @TO betti@ @TO res@ can be applied.
Outputs
: Sequence
of objects of type @TO BettiTally@, representing the mean Betti table shape and the mean Betti table of the elements in the list {\tt L}.
Description
Text
For a sample of ideals stored as a list, this function computes some basic Betti table statistics of the sample.
Namely, it computes the average shape of the Betti tables (where 1 is recorded in entry (ij) for each element if $beta_{ij}$ is not zero),
and it also computes the average Betti table (that is, the table whose (ij) entry is the mean value of $beta_{ij}$ for all ideals in the sample).
Example
R = ZZ/101[a..e];
L={monomialIdeal"a2b,bc", monomialIdeal"ab,bc3",monomialIdeal"ab,ac,bd,be,ae,cd,ce,a3,b3,c3,d3,e3"}
(meanBettiShape,meanBetti,stdDevBetti) = bettiStats L;
meanBettiShape
meanBetti
stdDevBetti
Text
For sample size $N$, the average Betti table {\em shape} considers nonzero Betti numbers. It is to be interpreted as follows:
entry (i,j) encodes the following sum of indicators:
$\sum_{all ideals} 1_{beta_{ij}>0} / N$; that is,
the proportion of ideals with a nonzero $beta_{ij}$.
Thus an entry of 0.33 means 33% of ideals have a non-zero Betti number there.
Example
apply(L,i->betti res i)
meanBettiShape
Text
For sample size $N$, the average Betti table is to be interpreted as follows:
entry $(i,j)$ encodes $\sum_{I\in ideals}beta_{ij}(R/I) / N$:
Example
apply(L,i->betti res i)
meanBetti
///
doc ///
Key
SaveBettis
[bettiStats, SaveBettis]
Headline
optional input to store all Betti tables computed
Description
Text
The function that computes statistics on Betti tables has an option to save all of the Betti tables to a file.
This may be useful if the computation from the resolution, which is what is called from @TO bettiStats@, takes too long.
Example
ZZ/101[a..e];
L={monomialIdeal"a2b,bc", monomialIdeal"ab,bc3",monomialIdeal"ab,ac,bd,be,ae,cd,ce,a3,b3,c3,d3,e3"}
bettiStats (L,SaveBettis=>"myBettiDiagrams")
SeeAlso
bettiStats
CountPure
Verbose
IncludeZeroIdeals
///
doc ///
Key
CountPure
[bettiStats, CountPure]
Headline
optional input to show the number of objects in the list whose Betti tables are pure
Description
Text
Putting the option {\tt CountPure => true} in function @TO bettiStats@ adds the number of pure Betti tables to the Betti table statistics.
In the following example, exactly one of the ideals has a pure Betti table:
Example
ZZ/101[a..c];
L={monomialIdeal"ab,bc", monomialIdeal"ab,bc3"}
(meanShape,meanBetti,stdevBetti,pure) = bettiStats (L,CountPure=>true);
pure
SeeAlso
bettiStats
SaveBettis
Verbose
IncludeZeroIdeals
///
doc ///
Key
degStats
(degStats,List)
Headline
statistics on the degrees of a list of objects
Usage
degStats(List)
Inputs
ideals: List
of objects of type @TO MonomialIdeal@, or any objects to which @TO degree@ can be applied.
Outputs
: Sequence
whose first entry is the average degree of a list of monomial ideals, second entry is the standard deviation of the degree, and third entry (if option turned on) is the degree tally
Description
Text
This function computes the degree of $R/I$ for each ideal $I$ in the list and computes the mean and standard deviation of the degrees.
Example
R=ZZ/101[a,b,c];
ideals = {monomialIdeal"a3,b,c2", monomialIdeal"a3,b,ac"}
degStats(ideals)
Text
The following examples use the existing functions @TO randomMonomialSets@ and @TO idealsFromGeneratingSets@ or
@TO randomMonomialIdeals@ to automatically generate a list of ideals, rather than creating the list manually:
Example
ideals = idealsFromGeneratingSets(randomMonomialSets(4,3,1.0,3))
degStats(ideals)
Example
ideals = randomMonomialIdeals(4,3,1.0,3)
degStats(ideals)
Text
Note that this function can be run with a list of any objects to which @TO degree@ can be applied.
///
doc ///
Key
randomMonomialIdeals
(randomMonomialIdeals,ZZ,ZZ,RR,ZZ)
(randomMonomialIdeals,ZZ,ZZ,ZZ,ZZ)