File size: 46,516 Bytes
cdec422 | 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 | import Mathlib
import OAI.LinearAlgebra.MatrixFields.Entropy.InitialLeafRates
import OAI.LinearAlgebra.MatrixFields.Extraction.ComplexLabelHierarchySeparation
import OAI.LinearAlgebra.MatrixFields.Extraction.ComplexRationalTypes
namespace OAI
namespace MatrixAllFields
open scoped BigOperators Topology Polynomial
section
namespace MatrixMultiplication.Foundation
open Filter
open scoped Topology
noncomputable def factorialLogRemainder (n : ℕ) : ℝ :=
Real.log (Nat.factorial n : ℝ) - ((n : ℝ) * Real.log n - n)
theorem factorialLogRemainder_eq (n : ℕ) :
factorialLogRemainder n =
Real.log (Nat.factorial n : ℝ) - ((n : ℝ) * Real.log n - n) := rfl
@[simp] theorem factorialLogRemainder_zero : factorialLogRemainder 0 = 0 := by
simp [factorialLogRemainder]
theorem factorialLogRemainder_stirling (n : ℕ) :
factorialLogRemainder n =
Real.log (Stirling.stirlingSeq n) + (1 / 2 : ℝ) * Real.log (2 * n) := by
cases n with
| zero => simp
| succ n =>
have h := Stirling.log_stirlingSeq_formula (n + 1)
rw [Real.log_div (by positivity) (Real.exp_ne_zero _), Real.log_exp] at h
unfold factorialLogRemainder
linarith
theorem tendsto_factorialLogRemainder_div :
Tendsto (fun n : ℕ => factorialLogRemainder n / (n : ℝ)) atTop (𝓝 0) := by
have hnat : Tendsto (fun n : ℕ => (n : ℝ)) atTop atTop :=
tendsto_natCast_atTop_atTop
have hstirling :
Tendsto (fun n : ℕ => Real.log (Stirling.stirlingSeq n) / (n : ℝ))
atTop (𝓝 0) :=
(Stirling.tendsto_stirlingSeq_sqrt_pi.log (by positivity)).div_atTop hnat
have hlog : Tendsto (fun n : ℕ => Real.log (n : ℝ) / (n : ℝ))
atTop (𝓝 0) := by
simpa only [Function.comp_def, id_eq] using
Real.isLittleO_log_id_atTop.tendsto_div_nhds_zero.comp hnat
have hconstant : Tendsto (fun n : ℕ => Real.log 2 / (n : ℝ))
atTop (𝓝 0) := tendsto_const_nhds.div_atTop hnat
have hsum : Tendsto
(fun n : ℕ => Real.log (Stirling.stirlingSeq n) / (n : ℝ) +
(1 / 2 : ℝ) * (Real.log 2 / (n : ℝ) + Real.log (n : ℝ) / (n : ℝ)))
atTop (𝓝 0) := by
simpa using hstirling.add ((hconstant.add hlog).const_mul (1 / 2 : ℝ))
apply hsum.congr'
filter_upwards [eventually_ne_atTop (0 : ℕ)] with n hn
rw [factorialLogRemainder_stirling,
Real.log_mul (by norm_num) (Nat.cast_ne_zero.mpr hn)]
ring
theorem tendsto_factorialLogRemainder_mul_div (c : ℕ) :
Tendsto (fun t : ℕ => factorialLogRemainder (t * c) / (t : ℝ))
atTop (𝓝 0) := by
by_cases hc : c = 0
· subst c
simp
have hmul : Tendsto (fun t : ℕ => t * c) atTop atTop :=
tendsto_id.atTop_mul_const' (Nat.pos_of_ne_zero hc)
have hscaled : Tendsto
(fun t : ℕ => factorialLogRemainder (t * c) / (t * c : ℕ) * (c : ℝ))
atTop (𝓝 0) := by
simpa using (tendsto_factorialLogRemainder_div.comp hmul).mul_const (c : ℝ)
apply hscaled.congr
intro t
by_cases ht : t = 0
· simp [ht]
rw [Nat.cast_mul]
field_simp [Nat.cast_ne_zero.mpr hc, Nat.cast_ne_zero.mpr ht]
theorem factorialLogRemainder_succ (n : ℕ) :
factorialLogRemainder (n + 1) = factorialLogRemainder n + 1 -
(n : ℝ) * (Real.log ((n : ℝ) + 1) - Real.log n) := by
unfold factorialLogRemainder
rw [Nat.factorial_succ, Nat.cast_mul,
Real.log_mul (by positivity) (by positivity)]
push_cast
ring
private theorem nat_mul_log_increment_le_one (n : ℕ) :
(n : ℝ) * (Real.log ((n : ℝ) + 1) - Real.log n) ≤ 1 := by
by_cases hn : n = 0
· simp [hn]
have hpos : 0 < (n : ℝ) := Nat.cast_pos.mpr (Nat.pos_of_ne_zero hn)
have hsucc : 0 < (n : ℝ) + 1 := by positivity
have hlog := Real.log_le_sub_one_of_pos (div_pos hsucc hpos)
rw [Real.log_div hsucc.ne' hpos.ne'] at hlog
have hmul := mul_le_mul_of_nonneg_left hlog hpos.le
have heq : (n : ℝ) * (((n : ℝ) + 1) / (n : ℝ) - 1) = 1 := by
field_simp [hpos.ne']
ring
rwa [heq] at hmul
private theorem one_le_succ_mul_log_increment (n : ℕ) (hn : 0 < n) :
1 ≤ ((n : ℝ) + 1) * (Real.log ((n : ℝ) + 1) - Real.log n) := by
have hpos : 0 < (n : ℝ) := Nat.cast_pos.mpr hn
have hsucc : 0 < (n : ℝ) + 1 := by positivity
have hlog := Real.one_sub_inv_le_log_of_pos (div_pos hsucc hpos)
rw [Real.log_div hsucc.ne' hpos.ne'] at hlog
have hmul := mul_le_mul_of_nonneg_left hlog hsucc.le
have heq : ((n : ℝ) + 1) * (1 - (((n : ℝ) + 1) / (n : ℝ))⁻¹) = 1 := by
rw [inv_div]
field_simp [hsucc.ne']
ring
rwa [heq] at hmul
theorem factorialLogRemainder_nonneg (n : ℕ) : 0 ≤ factorialLogRemainder n := by
induction n with
| zero => simp
| succ n ih =>
have hlog := nat_mul_log_increment_le_one n
rw [factorialLogRemainder_succ]
linarith
theorem factorialLogRemainder_le_one_add_log_pos (n : ℕ) :
factorialLogRemainder (n + 1) ≤ 1 + Real.log (n + 1 : ℕ) := by
induction n with
| zero => norm_num [factorialLogRemainder]
| succ n ih =>
have hlog := one_le_succ_mul_log_increment (n + 1) (Nat.succ_pos n)
have hstep := factorialLogRemainder_succ (n + 1)
push_cast at ih hlog hstep ⊢
nlinarith
theorem factorialLogRemainder_le_one_add_log (n : ℕ) :
factorialLogRemainder n ≤ 1 + Real.log (n + 1 : ℕ) := by
cases n with
| zero => norm_num
| succ n =>
refine (factorialLogRemainder_le_one_add_log_pos n).trans ?_
apply add_le_add le_rfl
apply Real.log_le_log
· positivity
· exact_mod_cast Nat.le_succ (n + 1)
theorem abs_factorialLogRemainder_le (n : ℕ) :
|factorialLogRemainder n| ≤ 1 + Real.log (n + 1 : ℕ) := by
rw [abs_of_nonneg (factorialLogRemainder_nonneg n)]
exact factorialLogRemainder_le_one_add_log n
end MatrixMultiplication.Foundation
namespace MatrixMultiplication.Foundation
open Filter
open scoped BigOperators Topology
variable {A : Type*} [Fintype A]
theorem log_multinomial (counts : A → ℕ) :
Real.log (Nat.multinomial Finset.univ counts : ℝ) =
Real.log (Nat.factorial (∑ a, counts a) : ℝ) -
∑ a, Real.log (Nat.factorial (counts a) : ℝ) := by
have hspec :
(∏ a, (Nat.factorial (counts a) : ℝ)) *
(Nat.multinomial Finset.univ counts : ℝ) =
(Nat.factorial (∑ a, counts a) : ℝ) := by
exact_mod_cast Nat.multinomial_spec Finset.univ counts
have hprod : (∏ a, (Nat.factorial (counts a) : ℝ)) ≠ 0 := by
exact Finset.prod_ne_zero_iff.mpr (fun a _ => by positivity)
have hmulti : (Nat.multinomial Finset.univ counts : ℝ) ≠ 0 := by
exact_mod_cast (Nat.ne_of_gt (Nat.multinomial_pos Finset.univ counts))
have hlog := congrArg Real.log hspec
rw [Real.log_mul hprod hmulti,
Real.log_prod (fun a _ => by positivity)] at hlog
linarith
private theorem entropyTerm_div_scalar (x y : ℝ) :
entropyTerm (x / y) = entropyTerm x / y + (x / y) * Real.log y := by
by_cases hx : x = 0
· simp [hx]
by_cases hy : y = 0
· simp [hy]
rw [entropyTerm, Real.log_div hx hy]
unfold entropyTerm
ring
theorem finiteEntropy_nat_normalize (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) :
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) =
Real.log (∑ a, counts a : ℕ) -
(∑ a, (counts a : ℝ) * Real.log (counts a : ℝ)) /
(∑ a, counts a : ℕ) := by
have hD' : ((∑ a, counts a : ℕ) : ℝ) ≠ 0 := by
exact_mod_cast (Nat.ne_of_gt hD)
unfold finiteEntropy
simp only [entropyTerm_div_scalar]
simp only [entropyTerm, neg_mul,
Finset.sum_add_distrib, ← Finset.sum_div, ← Finset.sum_mul,
Finset.sum_neg_distrib]
rw [← Nat.cast_sum, div_self hD']
ring
theorem log_multinomial_eq_main_add_remainder (counts : A → ℕ) :
Real.log (Nat.multinomial Finset.univ counts : ℝ) =
(∑ a, counts a : ℕ) * Real.log (∑ a, counts a : ℕ) -
(∑ a, (counts a : ℝ) * Real.log (counts a : ℝ)) +
factorialLogRemainder (∑ a, counts a) -
∑ a, factorialLogRemainder (counts a) := by
rw [log_multinomial]
unfold factorialLogRemainder
simp only [Finset.sum_sub_distrib, ← Nat.cast_sum]
ring
theorem log_multinomial_eq_entropy_add_remainder (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) :
Real.log (Nat.multinomial Finset.univ counts : ℝ) =
(∑ a, counts a : ℕ) *
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) +
factorialLogRemainder (∑ a, counts a) -
∑ a, factorialLogRemainder (counts a) := by
have hD' : ((∑ a, counts a : ℕ) : ℝ) ≠ 0 := by
exact_mod_cast (Nat.ne_of_gt hD)
rw [log_multinomial_eq_main_add_remainder, finiteEntropy_nat_normalize counts hD,
mul_sub, mul_div_cancel₀ _ hD']
theorem log_multinomial_sub_entropy_eq_remainder (counts : A → ℕ) :
Real.log (Nat.multinomial Finset.univ counts : ℝ) -
(∑ a, counts a : ℕ) *
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) =
factorialLogRemainder (∑ a, counts a) -
∑ a, factorialLogRemainder (counts a) := by
by_cases hD : 0 < ∑ a, counts a
· rw [log_multinomial_eq_entropy_add_remainder counts hD]
ring
· have hzero : (∑ a, counts a) = 0 := Nat.eq_zero_of_not_pos hD
have hc : ∀ a, counts a = 0 := by
intro a
have hle : counts a ≤ ∑ b, counts b :=
Finset.single_le_sum (fun b _ => Nat.zero_le (counts b)) (Finset.mem_univ a)
rw [hzero] at hle
exact Nat.eq_zero_of_le_zero hle
simp [hc, Nat.multinomial]
theorem abs_log_multinomial_sub_entropy_le (counts : A → ℕ) :
|Real.log (Nat.multinomial Finset.univ counts : ℝ) -
(∑ a, counts a : ℕ) *
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ))| ≤
((Fintype.card A : ℝ) + 1) *
(1 + Real.log ((∑ a, counts a) + 1 : ℕ)) := by
rw [log_multinomial_sub_entropy_eq_remainder]
have hsum_nonneg : 0 ≤ ∑ a, factorialLogRemainder (counts a) :=
Finset.sum_nonneg (fun a _ => factorialLogRemainder_nonneg (counts a))
have heach (a : A) : factorialLogRemainder (counts a) ≤
1 + Real.log ((∑ b, counts b) + 1 : ℕ) := by
refine (factorialLogRemainder_le_one_add_log (counts a)).trans ?_
apply add_le_add le_rfl
apply Real.log_le_log
· positivity
· have hle : counts a ≤ ∑ b, counts b :=
Finset.single_le_sum (fun b _ => Nat.zero_le (counts b)) (Finset.mem_univ a)
exact_mod_cast Nat.add_le_add_right hle 1
calc
|factorialLogRemainder (∑ a, counts a) - ∑ a, factorialLogRemainder (counts a)| ≤
|factorialLogRemainder (∑ a, counts a)| +
|∑ a, factorialLogRemainder (counts a)| := by
simpa only [sub_eq_add_neg, abs_neg] using
abs_add_le (factorialLogRemainder (∑ a, counts a))
(-(∑ a, factorialLogRemainder (counts a)))
_ = factorialLogRemainder (∑ a, counts a) +
∑ a, factorialLogRemainder (counts a) := by
rw [abs_of_nonneg (factorialLogRemainder_nonneg _), abs_of_nonneg hsum_nonneg]
_ ≤ (1 + Real.log ((∑ a, counts a) + 1 : ℕ)) +
∑ _a : A, (1 + Real.log ((∑ a, counts a) + 1 : ℕ)) :=
add_le_add (factorialLogRemainder_le_one_add_log _)
(Finset.sum_le_sum (fun a _ => heach a))
_ = ((Fintype.card A : ℝ) + 1) *
(1 + Real.log ((∑ a, counts a) + 1 : ℕ)) := by
simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul]
ring
theorem abs_log_multinomial_div_sub_entropy_le (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) :
|Real.log (Nat.multinomial Finset.univ counts : ℝ) / (∑ a, counts a : ℕ) -
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ))| ≤
((Fintype.card A : ℝ) + 1) *
((1 + Real.log ((∑ a, counts a) + 1 : ℕ)) / (∑ a, counts a : ℕ)) := by
have hD' : (0 : ℝ) < (∑ a, counts a : ℕ) := Nat.cast_pos.mpr hD
calc
|Real.log (Nat.multinomial Finset.univ counts : ℝ) / (∑ a, counts a : ℕ) -
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ))| =
|(Real.log (Nat.multinomial Finset.univ counts : ℝ) -
(∑ a, counts a : ℕ) *
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ))) /
(∑ a, counts a : ℕ)| := by
rw [sub_div, mul_div_cancel_left₀ _ hD'.ne']
_ = |Real.log (Nat.multinomial Finset.univ counts : ℝ) -
(∑ a, counts a : ℕ) *
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ))| /
(∑ a, counts a : ℕ) := by
rw [abs_div, abs_of_pos hD']
_ ≤ ((Fintype.card A : ℝ) + 1) *
((1 + Real.log ((∑ a, counts a) + 1 : ℕ)) / (∑ a, counts a : ℕ)) := by
simpa only [mul_div_assoc] using
div_le_div_of_nonneg_right (abs_log_multinomial_sub_entropy_le counts) hD'.le
private theorem tendsto_one_add_log_succ_div :
Tendsto (fun n : ℕ => (1 + Real.log (n + 1 : ℕ)) / (n : ℝ))
atTop (𝓝 0) := by
have hnat : Tendsto (fun n : ℕ => (n : ℝ)) atTop atTop :=
tendsto_natCast_atTop_atTop
have hconstant : Tendsto (fun n : ℕ => (1 : ℝ) / n) atTop (𝓝 0) :=
tendsto_const_nhds.div_atTop hnat
have hlog : Tendsto (fun n : ℕ => Real.log (n : ℝ) / n) atTop (𝓝 0) := by
simpa only [Function.comp_def, id_eq] using
Real.isLittleO_log_id_atTop.tendsto_div_nhds_zero.comp hnat
have hshift := Real.tendsto_log_nat_add_one_sub_log.div_atTop hnat
have hsum : Tendsto
(fun n : ℕ => (1 : ℝ) / n +
((Real.log ((n : ℝ) + 1) - Real.log n) / n + Real.log n / n))
atTop (𝓝 0) := by
simpa using hconstant.add (hshift.add hlog)
apply hsum.congr
intro n
push_cast
ring
theorem tendsto_log_multinomial_div_sub_entropy {I : Type*} {l : Filter I}
(counts : I → A → ℕ)
(htotal : Tendsto (fun i => ∑ a, counts i a) l atTop) :
Tendsto
(fun i => Real.log (Nat.multinomial Finset.univ (counts i) : ℝ) /
(∑ a, counts i a : ℕ) -
finiteEntropy (fun a => (counts i a : ℝ) / (∑ a, counts i a : ℕ)))
l (𝓝 0) := by
have hbound : Tendsto
(fun i => ((Fintype.card A : ℝ) + 1) *
((1 + Real.log ((∑ a, counts i a) + 1 : ℕ)) / (∑ a, counts i a : ℕ)))
l (𝓝 0) := by
simpa using (tendsto_one_add_log_succ_div.comp htotal).const_mul
((Fintype.card A : ℝ) + 1)
refine squeeze_zero_norm' ?_ hbound
filter_upwards [htotal.eventually (eventually_gt_atTop (0 : ℕ))] with i hi
simpa only [Real.norm_eq_abs] using
abs_log_multinomial_div_sub_entropy_le (counts i) hi
theorem tendsto_log_multinomial_of_empirical_tendsto {I : Type*} {l : Filter I}
(counts : I → A → ℕ) (p : A → ℝ)
(htotal : Tendsto (fun i => ∑ a, counts i a) l atTop)
(hmass : ∀ a, Tendsto
(fun i => (counts i a : ℝ) / (∑ b, counts i b : ℕ)) l (𝓝 (p a))) :
Tendsto
(fun i => Real.log (Nat.multinomial Finset.univ (counts i) : ℝ) /
(∑ a, counts i a : ℕ))
l (𝓝 (finiteEntropy p)) := by
have herror := tendsto_log_multinomial_div_sub_entropy counts htotal
have hentropy := tendsto_finiteEntropy_of_tendsto hmass
simpa only [zero_add, sub_add_cancel] using herror.add hentropy
private theorem mul_log_mul (x y : ℝ) :
(x * y) * Real.log (x * y) =
(x * Real.log x) * y + x * (y * Real.log y) := by
calc
(x * y) * Real.log (x * y) = -entropyTerm (x * y) := by
simp [entropyTerm]
_ = -(y * entropyTerm x + x * entropyTerm y) := by
rw [entropyTerm_mul]
_ = (x * Real.log x) * y + x * (y * Real.log y) := by
unfold entropyTerm
ring
theorem log_multinomial_mul_eq (counts : A → ℕ) (t : ℕ) :
Real.log (Nat.multinomial Finset.univ (fun a => t * counts a) : ℝ) =
(t : ℝ) *
((∑ a, counts a : ℕ) * Real.log (∑ a, counts a : ℕ) -
∑ a, (counts a : ℝ) * Real.log (counts a : ℝ)) +
factorialLogRemainder (t * ∑ a, counts a) -
∑ a, factorialLogRemainder (t * counts a) := by
have hsum : (∑ a, t * counts a) = t * ∑ a, counts a :=
(Finset.mul_sum Finset.univ counts t).symm
have hlogs :
(∑ a, ((t : ℝ) * counts a) * Real.log ((t : ℝ) * counts a)) =
((t : ℝ) * Real.log t) * (∑ a, counts a : ℕ) +
(t : ℝ) * ∑ a, (counts a : ℝ) * Real.log (counts a : ℝ) := by
simp only [mul_log_mul, Finset.sum_add_distrib, ← Finset.mul_sum,
← Nat.cast_sum]
rw [log_multinomial_eq_main_add_remainder, hsum]
simp only [Nat.cast_mul]
rw [mul_log_mul, hlogs]
ring
theorem tendsto_log_multinomial_mul (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) :
Tendsto
(fun t : ℕ =>
Real.log (Nat.multinomial Finset.univ (fun a => t * counts a) : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop
(𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)))) := by
have hD' : ((∑ a, counts a : ℕ) : ℝ) ≠ 0 := by
exact_mod_cast (Nat.ne_of_gt hD)
have hsum :
Tendsto (fun t : ℕ => ∑ a, factorialLogRemainder (t * counts a) / (t : ℝ))
atTop (𝓝 0) := by
simpa using tendsto_finsetSum Finset.univ
(fun a _ => tendsto_factorialLogRemainder_mul_div (counts a))
have herror : Tendsto
(fun t : ℕ =>
(factorialLogRemainder (t * ∑ a, counts a) / (t : ℝ) -
∑ a, factorialLogRemainder (t * counts a) / (t : ℝ)) /
(∑ a, counts a : ℕ)) atTop (𝓝 0) := by
simpa using ((tendsto_factorialLogRemainder_mul_div (∑ a, counts a)).sub
hsum).div_const (∑ a, counts a : ℕ)
have hlimit : Tendsto
(fun t : ℕ =>
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) +
(factorialLogRemainder (t * ∑ a, counts a) / (t : ℝ) -
∑ a, factorialLogRemainder (t * counts a) / (t : ℝ)) /
(∑ a, counts a : ℕ))
atTop
(𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)))) := by
simpa only [add_zero] using tendsto_const_nhds.add herror
apply hlimit.congr'
filter_upwards [eventually_ne_atTop (0 : ℕ)] with t ht
rw [log_multinomial_mul_eq, finiteEntropy_nat_normalize counts hD,
← Finset.sum_div]
have ht' : (t : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr ht
field_simp [ht', hD']
ring
theorem tendsto_log_multinomial_repetitions {I : Type*} {l : Filter I}
(counts : A → ℕ) (hD : 0 < ∑ a, counts a)
(repetitions : I → ℕ) (hrep : Tendsto repetitions l atTop) :
Tendsto
(fun i =>
Real.log (Nat.multinomial Finset.univ
(fun a => repetitions i * counts a) : ℝ) /
((repetitions i : ℝ) * (∑ a, counts a : ℕ)))
l (𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)))) := by
exact (tendsto_log_multinomial_mul counts hD).comp hrep
theorem tendsto_log_exactWords_card_mul [DecidableEq A] (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) :
Tendsto
(fun t : ℕ => Real.log (Fintype.card (ExactWords (fun a => t * counts a)) : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop
(𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)))) := by
simpa only [exactWords_card] using tendsto_log_multinomial_mul counts hD
theorem abs_log_exactWords_card_sub_entropy_le [DecidableEq A] (counts : A → ℕ) :
|Real.log (Fintype.card (ExactWords counts) : ℝ) -
(∑ a, counts a : ℕ) *
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ))| ≤
((Fintype.card A : ℝ) + 1) *
(1 + Real.log ((∑ a, counts a) + 1 : ℕ)) := by
simpa only [exactWords_card] using abs_log_multinomial_sub_entropy_le counts
theorem log_card_le_entropy_of_injective_exactWords [DecidableEq A]
{O : Type*} [Fintype O] [Nonempty O] (counts : A → ℕ)
(label : O → ExactWords counts) (hinj : Function.Injective label) :
Real.log (Fintype.card O : ℝ) ≤
(∑ a, counts a : ℕ) *
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) +
((Fintype.card A : ℝ) + 1) *
(1 + Real.log ((∑ a, counts a) + 1 : ℕ)) := by
have hcard := Fintype.card_le_of_injective label hinj
have hlog : Real.log (Fintype.card O : ℝ) ≤
Real.log (Fintype.card (ExactWords counts) : ℝ) :=
Real.log_le_log (Nat.cast_pos.mpr Fintype.card_pos) (by exact_mod_cast hcard)
have herror := (abs_le.mp (abs_log_exactWords_card_sub_entropy_le counts)).2
linarith
theorem log_card_div_le_entropy_of_injective_exactWords [DecidableEq A]
{O : Type*} [Fintype O] [Nonempty O] (counts : A → ℕ)
(hD : 0 < ∑ a, counts a)
(label : O → ExactWords counts) (hinj : Function.Injective label) :
Real.log (Fintype.card O : ℝ) / (∑ a, counts a : ℕ) ≤
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) +
((Fintype.card A : ℝ) + 1) *
((1 + Real.log ((∑ a, counts a) + 1 : ℕ)) / (∑ a, counts a : ℕ)) := by
have hD' : (0 : ℝ) < (∑ a, counts a : ℕ) := Nat.cast_pos.mpr hD
have h := div_le_div_of_nonneg_right
(log_card_le_entropy_of_injective_exactWords counts label hinj) hD'.le
rw [add_div, mul_div_cancel_left₀ _ hD'.ne', mul_div_assoc] at h
exact h
theorem tendsto_log_exactWords_card_of_empirical_tendsto [DecidableEq A]
{I : Type*} {l : Filter I} (counts : I → A → ℕ) (p : A → ℝ)
(htotal : Tendsto (fun i => ∑ a, counts i a) l atTop)
(hmass : ∀ a, Tendsto
(fun i => (counts i a : ℝ) / (∑ b, counts i b : ℕ)) l (𝓝 (p a))) :
Tendsto
(fun i => Real.log (Fintype.card (ExactWords (counts i)) : ℝ) /
(∑ a, counts i a : ℕ))
l (𝓝 (finiteEntropy p)) := by
simpa only [exactWords_card] using
tendsto_log_multinomial_of_empirical_tendsto counts p htotal hmass
namespace RationalLaw
theorem exists_exactWords_entropy_limit [DecidableEq A] (p : RationalLaw A) :
∃ counts : A → ℕ, 0 < ∑ a, counts a ∧
(∀ a, (counts a : ℝ) / (∑ a, counts a : ℕ) = p.toFiniteLaw.mass a) ∧
Tendsto
(fun t : ℕ => Real.log (Fintype.card (ExactWords (fun a => t * counts a)) : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop (𝓝 (finiteEntropy p.toFiniteLaw.mass)) := by
obtain ⟨counts, hD, hmass⟩ := p.exists_type_representation
refine ⟨counts, hD, hmass, ?_⟩
have hfun : (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) =
p.toFiniteLaw.mass := funext hmass
rw [← hfun]
exact tendsto_log_exactWords_card_mul counts hD
end RationalLaw
end MatrixMultiplication.Foundation
noncomputable section
namespace MatrixMultiplication.Foundation
open Filter
open scoped BigOperators Topology
namespace LabelHierarchySeparation
theorem poolAuxiliaryCost_pos (k : ℕ) : 0 < poolAuxiliaryCost k := by
unfold poolAuxiliaryCost
split_ifs
· exact Nat.zero_lt_one
· exact Separation.gridGroupOrder_pos k
theorem population_le_poolAuxiliaryCost (k : ℕ) : k ≤ poolAuxiliaryCost k := by
unfold poolAuxiliaryCost
split_ifs with hk
· simp [hk]
· exact Separation.population_le_gridGroupOrder k
theorem poolAuxiliaryCost_le_gridGroupOrder (k : ℕ) :
poolAuxiliaryCost k ≤ Separation.gridGroupOrder k := by
unfold poolAuxiliaryCost
split_ifs with hk
· simpa only [hk] using Separation.population_le_gridGroupOrder k
· exact le_rfl
theorem tendsto_log_poolAuxiliaryCost_div_nat {K : ℕ → ℕ} {H : ℝ}
(hK : ∀ n, 0 < K n) (hH : 0 ≤ H)
(hpop : Tendsto (fun n => Real.log (K n : ℝ) / (n : ℝ)) atTop (𝓝 H)) :
Tendsto (fun n => Real.log (poolAuxiliaryCost (K n) : ℝ) / (n : ℝ))
atTop (𝓝 H) := by
refine tendsto_of_tendsto_of_tendsto_of_le_of_le hpop
(Separation.tendsto_log_gridGroupOrder_div_nat hK hH hpop) ?_ ?_
· intro n
apply div_le_div_of_nonneg_right _ (Nat.cast_nonneg n)
exact Real.log_le_log (Nat.cast_pos.mpr (hK n))
(by exact_mod_cast population_le_poolAuxiliaryCost (K n))
· intro n
apply div_le_div_of_nonneg_right _ (Nat.cast_nonneg n)
exact Real.log_le_log (Nat.cast_pos.mpr (poolAuxiliaryCost_pos (K n)))
(by exact_mod_cast poolAuxiliaryCost_le_gridGroupOrder (K n))
end LabelHierarchySeparation
namespace HierarchySeparationRates
universe u v w
def populationEntropyRate {B : Type*} [Fintype B] (counts : B → ℕ) : ℝ :=
(∑ b, counts b : ℕ) *
finiteEntropy (fun b => (counts b : ℝ) / (∑ b, counts b : ℕ))
theorem tendsto_log_multinomial_mul_div_repetition {B : Type*} [Fintype B]
(counts : B → ℕ) :
Tendsto
(fun t : ℕ =>
Real.log (Nat.multinomial Finset.univ (fun b => t * counts b) : ℝ) / (t : ℝ))
atTop (𝓝 (populationEntropyRate counts)) := by
by_cases hD : 0 < ∑ b, counts b
· have hD' : ((∑ b, counts b : ℕ) : ℝ) ≠ 0 :=
Nat.cast_ne_zero.mpr (Nat.ne_of_gt hD)
have h := (tendsto_log_multinomial_mul counts hD).const_mul
((∑ b, counts b : ℕ) : ℝ)
apply h.congr
intro t
by_cases ht : t = 0
· simp [ht]
have ht' : (t : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr ht
change ((∑ b, counts b : ℕ) : ℝ) *
(Real.log (Nat.multinomial Finset.univ (fun b => t * counts b) : ℝ) /
((t : ℝ) * (∑ b, counts b : ℕ))) = _
field_simp [hD', ht']
· have hz : (∑ b, counts b) = 0 := Nat.eq_zero_of_not_pos hD
have hc (b : B) : counts b = 0 := by
apply Nat.eq_zero_of_le_zero
calc
counts b ≤ ∑ c, counts c :=
Finset.single_le_sum (fun c _ => Nat.zero_le _) (Finset.mem_univ b)
_ = 0 := hz
simp only [populationEntropyRate, hz, Nat.cast_zero, zero_mul]
simp [Nat.multinomial, hc]
theorem populationEntropyRate_nonneg {B : Type*} [Fintype B] (counts : B → ℕ) :
0 ≤ populationEntropyRate counts := by
apply ge_of_tendsto' (tendsto_log_multinomial_mul_div_repetition counts)
intro t
apply div_nonneg _ (Nat.cast_nonneg t)
apply Real.log_nonneg
exact_mod_cast Nat.one_le_iff_ne_zero.mpr
(Nat.ne_of_gt (Nat.multinomial_pos Finset.univ (fun b => t * counts b)))
section FinitePools
variable {I : Type u} [Fintype I] {Alphabet : I → Type v}
[∀ i, Fintype (Alphabet i)]
def poolTypeCount (counts : ∀ i, Alphabet i → ℕ) (i : I) (t : ℕ) : ℕ :=
Nat.multinomial Finset.univ (fun b => t * counts i b)
def poolEntropyTotal (counts : ∀ i, Alphabet i → ℕ) : ℝ :=
∑ i, populationEntropyRate (counts i)
def pairingProduct (counts : ∀ i, Alphabet i → ℕ) (t : ℕ) : ℕ :=
∏ i, poolTypeCount counts i t
def gridProduct (counts : ∀ i, Alphabet i → ℕ) (t : ℕ) : ℕ :=
∏ i, Separation.gridGroupOrder (poolTypeCount counts i t)
def auxiliaryProduct (counts : ∀ i, Alphabet i → ℕ) (t : ℕ) : ℕ :=
∏ i, LabelHierarchySeparation.poolAuxiliaryCost (poolTypeCount counts i t)
omit [Fintype I] in
theorem poolTypeCount_pos (counts : ∀ i, Alphabet i → ℕ) (i : I) (t : ℕ) :
0 < poolTypeCount counts i t := Nat.multinomial_pos _ _
theorem pairingProduct_le_auxiliaryProduct (counts : ∀ i, Alphabet i → ℕ) (t : ℕ) :
pairingProduct counts t ≤ auxiliaryProduct counts t := by
exact Finset.prod_le_prod (fun i _ =>
LabelHierarchySeparation.population_le_poolAuxiliaryCost (poolTypeCount counts i t))
theorem auxiliaryProduct_le_gridProduct (counts : ∀ i, Alphabet i → ℕ) (t : ℕ) :
auxiliaryProduct counts t ≤ gridProduct counts t := by
exact Finset.prod_le_prod (fun i _ =>
LabelHierarchySeparation.poolAuxiliaryCost_le_gridGroupOrder (poolTypeCount counts i t))
omit [Fintype I] in
theorem tendsto_log_poolTypeCount_div (counts : ∀ i, Alphabet i → ℕ) (i : I) :
Tendsto (fun t : ℕ => Real.log (poolTypeCount counts i t : ℝ) / (t : ℝ))
atTop (𝓝 (populationEntropyRate (counts i))) :=
tendsto_log_multinomial_mul_div_repetition (counts i)
theorem tendsto_log_pairingProduct_div (counts : ∀ i, Alphabet i → ℕ) :
Tendsto (fun t : ℕ => Real.log (pairingProduct counts t : ℝ) / (t : ℝ))
atTop (𝓝 (poolEntropyTotal counts)) := by
exact Separation.tendsto_log_nat_prod_div_nat Finset.univ
(fun i _ t => poolTypeCount_pos counts i t)
(fun i _ => tendsto_log_poolTypeCount_div counts i)
theorem tendsto_log_gridProduct_div (counts : ∀ i, Alphabet i → ℕ) :
Tendsto (fun t : ℕ => Real.log (gridProduct counts t : ℝ) / (t : ℝ))
atTop (𝓝 (poolEntropyTotal counts)) := by
exact Separation.tendsto_log_gridGroupOrder_prod_div_nat Finset.univ
(fun i _ t => poolTypeCount_pos counts i t)
(fun i _ => populationEntropyRate_nonneg (counts i))
(fun i _ => tendsto_log_poolTypeCount_div counts i)
theorem tendsto_log_auxiliaryProduct_div (counts : ∀ i, Alphabet i → ℕ) :
Tendsto (fun t : ℕ => Real.log (auxiliaryProduct counts t : ℝ) / (t : ℝ))
atTop (𝓝 (poolEntropyTotal counts)) := by
exact Separation.tendsto_log_nat_prod_div_nat Finset.univ
(fun i _ t => LabelHierarchySeparation.poolAuxiliaryCost_pos (poolTypeCount counts i t))
(fun i _ => LabelHierarchySeparation.tendsto_log_poolAuxiliaryCost_div_nat
(poolTypeCount_pos counts i) (populationEntropyRate_nonneg (counts i))
(tendsto_log_poolTypeCount_div counts i))
end FinitePools
section SourceHierarchy
variable {A : Type u} [Fintype A] {Label : ℕ → Type v}
[∀ n, Fintype (Label n)]
theorem pushforwardCounts_mul {B : Type*} (counts : A → ℕ) (label : A → B)
(t : ℕ) (b : B) :
pushforwardCounts (fun a => t * counts a) label b =
t * pushforwardCounts counts label b := by
classical
unfold pushforwardCounts
rw [Finset.mul_sum]
apply Finset.sum_congr rfl
intro a _
by_cases h : label a = b <;> simp [h]
omit [∀ n, Fintype (Label n)] in
theorem sourcePrefixCounts_mul (counts : A → ℕ) (labels : ∀ n, A → Label n)
(t n : ℕ) (r : LabelRecord Label n) :
LabelHierarchyCounts.sourcePrefixCounts (fun a => t * counts a) labels n r =
t * LabelHierarchyCounts.sourcePrefixCounts counts labels n r :=
pushforwardCounts_mul counts (labelRecordOf labels n) t r
abbrev PrefixPool (Label : ℕ → Type v) (depth : ℕ) :=
Σ n : Fin depth, LabelRecord Label n.val
def sourcePoolCounts (counts : A → ℕ) (labels : ∀ n, A → Label n) (depth : ℕ)
(j : PrefixPool Label depth) (s : Label j.1.val) : ℕ :=
LabelHierarchyCounts.sourcePrefixCounts counts labels (j.1.val + 1) (j.2, s)
theorem pairingProduct_source_eq (counts : A → ℕ) (labels : ∀ n, A → Label n)
(depth : ℕ) (hinjective : Function.Injective (labelRecordOf labels depth)) (t : ℕ) :
pairingProduct (sourcePoolCounts counts labels depth) t =
Nat.multinomial Finset.univ (fun a => t * counts a) := by
classical
unfold pairingProduct
rw [Fintype.prod_sigma]
have hpool (n : Fin depth) :
(∏ r : LabelRecord Label n.val,
poolTypeCount (sourcePoolCounts counts labels depth) ⟨n, r⟩ t) =
LabelHierarchyCounts.refinementCount
(LabelHierarchyCounts.sourcePrefixCounts (fun a => t * counts a) labels) n.val := by
apply Finset.prod_congr rfl
intro r _
change Nat.multinomial Finset.univ
(fun s : Label n.val => t *
LabelHierarchyCounts.sourcePrefixCounts counts labels (n.val + 1) (r, s)) =
Nat.multinomial Finset.univ
(fun s : Label n.val =>
LabelHierarchyCounts.sourcePrefixCounts (fun a => t * counts a)
labels (n.val + 1) (r, s))
apply Nat.multinomial_congr
intro s _
exact (sourcePrefixCounts_mul counts labels t (n.val + 1) (r, s)).symm
simp_rw [hpool]
rw [Fin.prod_univ_eq_prod_range]
exact LabelHierarchyCounts.source_refinementCount_product
(fun a => t * counts a) labels depth hinjective
theorem pairingProduct_source_eq_exactWords_card [DecidableEq A]
(counts : A → ℕ) (labels : ∀ n, A → Label n) (depth : ℕ)
(hinjective : Function.Injective (labelRecordOf labels depth)) (t : ℕ) :
pairingProduct (sourcePoolCounts counts labels depth) t =
Fintype.card (ExactWords (fun a => t * counts a)) := by
rw [pairingProduct_source_eq counts labels depth hinjective, exactWords_card]
theorem source_poolEntropyTotal_eq (counts : A → ℕ) (labels : ∀ n, A → Label n)
(depth : ℕ) (hinjective : Function.Injective (labelRecordOf labels depth)) :
poolEntropyTotal (sourcePoolCounts counts labels depth) = populationEntropyRate counts := by
have hsource : Tendsto
(fun t : ℕ => Real.log (pairingProduct (sourcePoolCounts counts labels depth) t : ℝ) /
(t : ℝ)) atTop (𝓝 (populationEntropyRate counts)) := by
simpa only [pairingProduct_source_eq counts labels depth hinjective] using
tendsto_log_multinomial_mul_div_repetition counts
exact tendsto_nhds_unique
(tendsto_log_pairingProduct_div (sourcePoolCounts counts labels depth)) hsource
private theorem normalized_of_per_repetition (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) (K : ℕ → ℕ)
(h : Tendsto (fun t : ℕ => Real.log (K t : ℝ) / (t : ℝ))
atTop (𝓝 (populationEntropyRate counts))) :
Tendsto (fun t : ℕ => Real.log (K t : ℝ) / ((t : ℝ) * (∑ a, counts a : ℕ)))
atTop (𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)))) := by
have hD' : ((∑ a, counts a : ℕ) : ℝ) ≠ 0 :=
Nat.cast_ne_zero.mpr (Nat.ne_of_gt hD)
have hmain : populationEntropyRate counts / (∑ a, counts a : ℕ) =
finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) := by
unfold populationEntropyRate
exact mul_div_cancel_left₀ _ hD'
simpa only [div_div, hmain] using h.div_const ((∑ a, counts a : ℕ) : ℝ)
theorem tendsto_log_source_pairingProduct (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) (labels : ∀ n, A → Label n) (depth : ℕ)
(hinjective : Function.Injective (labelRecordOf labels depth)) :
Tendsto
(fun t : ℕ => Real.log (pairingProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop (𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)))) := by
simpa only [pairingProduct_source_eq counts labels depth hinjective] using
tendsto_log_multinomial_mul counts hD
theorem tendsto_log_source_gridProduct (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) (labels : ∀ n, A → Label n) (depth : ℕ)
(hinjective : Function.Injective (labelRecordOf labels depth)) :
Tendsto
(fun t : ℕ => Real.log (gridProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop (𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)))) := by
apply normalized_of_per_repetition counts hD
simpa only [source_poolEntropyTotal_eq counts labels depth hinjective] using
tendsto_log_gridProduct_div (sourcePoolCounts counts labels depth)
theorem tendsto_log_source_auxiliaryProduct (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) (labels : ∀ n, A → Label n) (depth : ℕ)
(hinjective : Function.Injective (labelRecordOf labels depth)) :
Tendsto
(fun t : ℕ => Real.log (auxiliaryProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop (𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)))) := by
apply normalized_of_per_repetition counts hD
simpa only [source_poolEntropyTotal_eq counts labels depth hinjective] using
tendsto_log_auxiliaryProduct_div (sourcePoolCounts counts labels depth)
theorem rationalLaw_exists_source_rates [DecidableEq A] (p : RationalLaw A)
(labels : ∀ n, A → Label n) (depth : ℕ)
(hinjective : Function.Injective (labelRecordOf labels depth)) :
∃ counts : A → ℕ, 0 < ∑ a, counts a ∧
(∀ a, (counts a : ℝ) / (∑ a, counts a : ℕ) = p.toFiniteLaw.mass a) ∧
Tendsto
(fun t : ℕ => Real.log (Fintype.card (ExactWords (fun a => t * counts a)) : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop (𝓝 (finiteEntropy p.toFiniteLaw.mass)) ∧
Tendsto
(fun t : ℕ => Real.log (pairingProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop (𝓝 (finiteEntropy p.toFiniteLaw.mass)) ∧
Tendsto
(fun t : ℕ => Real.log (gridProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop (𝓝 (finiteEntropy p.toFiniteLaw.mass)) ∧
Tendsto
(fun t : ℕ => Real.log (auxiliaryProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ)))
atTop (𝓝 (finiteEntropy p.toFiniteLaw.mass)) := by
obtain ⟨counts, hD, hmass⟩ := p.exists_type_representation
have hfun : (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) =
p.toFiniteLaw.mass := funext hmass
refine ⟨counts, hD, hmass, ?_, ?_, ?_, ?_⟩
· simpa only [hfun] using tendsto_log_exactWords_card_mul counts hD
· simpa only [hfun] using tendsto_log_source_pairingProduct counts hD labels depth hinjective
· simpa only [hfun] using tendsto_log_source_gridProduct counts hD labels depth hinjective
· simpa only [hfun] using tendsto_log_source_auxiliaryProduct counts hD labels depth hinjective
end SourceHierarchy
end HierarchySeparationRates
end MatrixMultiplication.Foundation
namespace MatrixMultiplication.LeafRates
open MatrixMultiplication.Foundation Filter
open scoped BigOperators Topology
variable {A U : Type*} [Fintype A] [DecidableEq A] [Fintype U] [DecidableEq U]
omit [DecidableEq U] in
theorem log_card_supported (statistic : U → A) (counts b : A → ℕ)
(hb : ∀ a, 0 < counts a → Fintype.card {u : U // statistic u = a} = b a)
(hbpos : ∀ a, 0 < b a) :
Real.log (Fintype.card (ExactStatisticWords.Words statistic counts) : ℝ) =
Real.log (Nat.multinomial Finset.univ counts : ℝ) +
∑ a, (counts a : ℝ) * Real.log (b a : ℝ) := by
rw [ExactStatisticWords.card_of_supported statistic counts b hb, Nat.cast_mul, Nat.cast_prod]
have hm : (Nat.multinomial Finset.univ counts : ℝ) ≠ 0 := by
exact_mod_cast (Nat.ne_of_gt (Nat.multinomial_pos Finset.univ counts))
have hp : (∏ a, (b a ^ counts a : ℕ) : ℝ) ≠ 0 := by
apply Finset.prod_ne_zero_iff.mpr
intro a _
exact_mod_cast (Nat.ne_of_gt (pow_pos (hbpos a) _))
rw [Real.log_mul hm hp, Real.log_prod
(fun a _ => by exact_mod_cast (Nat.ne_of_gt (pow_pos (hbpos a) (counts a))))]
simp only [Nat.cast_pow, Real.log_pow]
omit [DecidableEq U] in
theorem log_card (statistic : U → A) (counts b : A → ℕ)
(hb : ∀ a, Fintype.card {u : U // statistic u = a} = b a)
(hbpos : ∀ a, 0 < b a) :
Real.log (Fintype.card (ExactStatisticWords.Words statistic counts) : ℝ) =
Real.log (Nat.multinomial Finset.univ counts : ℝ) +
∑ a, (counts a : ℝ) * Real.log (b a : ℝ) :=
log_card_supported statistic counts b (fun a _ => hb a) hbpos
omit [DecidableEq U] in
theorem tendsto_log_card_supported (statistic : U → A) (counts b : A → ℕ)
(hb : ∀ a, 0 < counts a → Fintype.card {u : U // statistic u = a} = b a)
(hbpos : ∀ a, 0 < b a) (hD : 0 < ∑ a, counts a) :
Tendsto (fun t : ℕ =>
Real.log (Fintype.card (ExactStatisticWords.Words statistic (fun a => t * counts a)) : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ))) atTop
(𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) +
∑ a, ((counts a : ℝ) / (∑ a, counts a : ℕ)) * Real.log (b a : ℝ))) := by
have hl := (tendsto_log_multinomial_mul counts hD).add_const
(∑ a, ((counts a : ℝ) / (∑ a, counts a : ℕ)) * Real.log (b a : ℝ))
apply hl.congr'
filter_upwards [eventually_ne_atTop (0 : ℕ)] with t ht
have hbt : ∀ a, 0 < t * counts a → Fintype.card {u : U // statistic u = a} = b a :=
fun a ha => hb a (by nlinarith)
rw [log_card_supported statistic (fun a => t * counts a) b hbt hbpos, add_div]
congr 1
rw [Finset.sum_div]
apply Finset.sum_congr rfl
intro a _
have ht' : (t : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr ht
have hD' : ((∑ a, counts a : ℕ) : ℝ) ≠ 0 := by exact_mod_cast (Nat.ne_of_gt hD)
have hDsum : (∑ a, (counts a : ℝ)) ≠ 0 := by
simpa only [Nat.cast_sum] using hD'
simp only [Nat.cast_mul]
field_simp [ht', hD', hDsum]
omit [DecidableEq U] in
theorem tendsto_log_card (statistic : U → A) (counts b : A → ℕ)
(hb : ∀ a, Fintype.card {u : U // statistic u = a} = b a)
(hbpos : ∀ a, 0 < b a) (hD : 0 < ∑ a, counts a) :
Tendsto (fun t : ℕ =>
Real.log (Fintype.card (ExactStatisticWords.Words statistic (fun a => t * counts a)) : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ))) atTop
(𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (∑ a, counts a : ℕ)) +
∑ a, ((counts a : ℝ) / (∑ a, counts a : ℕ)) * Real.log (b a : ℝ))) :=
tendsto_log_card_supported statistic counts b (fun a _ => hb a) hbpos hD
end MatrixMultiplication.LeafRates
namespace MatrixMultiplication.AllFieldZeroLeafRates
open MatrixMultiplication.Foundation Filter AllFieldParameters CWWindowedLeaves CWCompleteStatistics
open scoped BigOperators Topology
attribute [local instance] Classical.propDecidable
abbrev fourWords (t : Shape) (counts : PairSlot → ℕ) (m : ℕ) :=
ExactStatisticWords.Words
(fun w : Alphabet 4 (shapeMax t) => fourStatistic w.val) (fun s => m * counts s)
def twoWords (k : Fin 5) (counts : Fin 6 → ℕ) (m : ℕ) :=
ExactStatisticWords.Words
(fun w : Alphabet 2 k.val => twoStatistic w.val) (fun s => m * counts s)
instance twoWordsFintype (k : Fin 5) (counts : Fin 6 → ℕ) (m : ℕ) :
Fintype (twoWords k counts m) := by
unfold twoWords
infer_instance
theorem multiplicity_pos (s : Fin 6) : 0 < AllFieldParameters.multiplicity s := by
fin_cases s <;> decide
theorem four_supported (t : Shape) (counts : PairSlot → ℕ)
(hD : 0 < ∑ s, counts s)
(hlaw : ∀ s, (counts s : ℝ) / (∑ s, counts s : ℕ) =
(zeroPairLaw t s.1 s.2 : ℝ)) (s : PairSlot) (hs : 0 < counts s) :
Fintype.card {w : Alphabet 4 (shapeMax t) // fourStatistic w.val = s} =
AllFieldParameters.multiplicity s.1 * AllFieldParameters.multiplicity s.2 := by
apply four_fixedWeight_multiplicity
by_contra hweight
have hzero := zeroPairLaw_outside_support t s.1 s.2 hweight
have hpos : (0 : ℝ) < (counts s : ℝ) / (∑ s, counts s : ℕ) :=
div_pos (by exact_mod_cast hs) (by exact_mod_cast hD)
rw [hlaw s, hzero, Rat.cast_zero] at hpos
exact (lt_irrefl 0) hpos
theorem two_supported (d : ℚ) (k : Fin 5) (counts : Fin 6 → ℕ)
(hD : 0 < ∑ s, counts s)
(hlaw : ∀ s, (counts s : ℝ) / (∑ s, counts s : ℕ) =
(statisticLaw d k s : ℝ)) (s : Fin 6) (hs : 0 < counts s) :
Fintype.card {w : Alphabet 2 k.val // twoStatistic w.val = s} =
AllFieldParameters.multiplicity s := by
apply two_fixedWeight_multiplicity
by_contra hweight
have hzero := statisticLaw_outside_support d k s hweight
have hpos : (0 : ℝ) < (counts s : ℝ) / (∑ s, counts s : ℕ) :=
div_pos (by exact_mod_cast hs) (by exact_mod_cast hD)
rw [hlaw s, hzero, Rat.cast_zero] at hpos
exact (lt_irrefl 0) hpos
theorem four_card_pos (t : Shape) (counts : PairSlot → ℕ)
(hD : 0 < ∑ s, counts s)
(hlaw : ∀ s, (counts s : ℝ) / (∑ s, counts s : ℕ) =
(zeroPairLaw t s.1 s.2 : ℝ)) (m : ℕ) :
0 < Fintype.card (fourWords t counts m) := by
apply ExactStatisticWords.card_pos_of_supported
(fun w : Alphabet 4 (shapeMax t) => fourStatistic w.val) (fun s => m * counts s)
(fun s => AllFieldParameters.multiplicity s.1 * AllFieldParameters.multiplicity s.2)
· intro s hs
exact four_supported t counts hD hlaw s (Nat.pos_of_mul_pos_left hs)
· intro s _
exact Nat.mul_pos (multiplicity_pos s.1) (multiplicity_pos s.2)
theorem two_card_pos (d : ℚ) (k : Fin 5) (counts : Fin 6 → ℕ)
(hD : 0 < ∑ s, counts s)
(hlaw : ∀ s, (counts s : ℝ) / (∑ s, counts s : ℕ) =
(statisticLaw d k s : ℝ)) (m : ℕ) :
0 < Fintype.card (twoWords k counts m) := by
have h := ExactStatisticWords.card_pos_of_supported
(fun w : Alphabet 2 k.val => twoStatistic w.val) (fun s => m * counts s)
AllFieldParameters.multiplicity
(fun s hs => two_supported d k counts hD hlaw s (Nat.pos_of_mul_pos_left hs))
(fun s _ => multiplicity_pos s)
rw [Fintype.card_eq_nat_card] at h ⊢
exact h
theorem four_tendsto_log_card (t : Shape) (counts : PairSlot → ℕ)
(hD : 0 < ∑ s, counts s)
(hlaw : ∀ s, (counts s : ℝ) / (∑ s, counts s : ℕ) =
(zeroPairLaw t s.1 s.2 : ℝ)) :
Tendsto (fun m : ℕ => Real.log (Fintype.card (fourWords t counts m) : ℝ) /
((m : ℝ) * (∑ s, counts s : ℕ))) atTop (𝓝 (fourRate t)) := by
have h := LeafRates.tendsto_log_card_supported
(fun w : Alphabet 4 (shapeMax t) => fourStatistic w.val) counts
(fun s => AllFieldParameters.multiplicity s.1 * AllFieldParameters.multiplicity s.2)
(four_supported t counts hD hlaw)
(fun s => Nat.mul_pos (multiplicity_pos s.1) (multiplicity_pos s.2)) hD
simpa only [fourWords, hlaw, fourRate] using h
theorem two_tendsto_log_card (d : ℚ) (k : Fin 5) (counts : Fin 6 → ℕ)
(hD : 0 < ∑ s, counts s)
(hlaw : ∀ s, (counts s : ℝ) / (∑ s, counts s : ℕ) =
(statisticLaw d k s : ℝ)) :
Tendsto (fun m : ℕ => Real.log (Fintype.card (twoWords k counts m) : ℝ) /
((m : ℝ) * (∑ s, counts s : ℕ))) atTop (𝓝 (statisticRate d k)) := by
have h := LeafRates.tendsto_log_card_supported
(fun w : Alphabet 2 k.val => twoStatistic w.val) counts AllFieldParameters.multiplicity
(two_supported d k counts hD hlaw) multiplicity_pos hD
simp only [Fintype.card_eq_nat_card] at h ⊢
simpa only [twoWords, hlaw, statisticRate] using h
end MatrixMultiplication.AllFieldZeroLeafRates
end
end
end MatrixAllFields
end OAI
|