Commit 2afed31
Refactor cross-section simulation: reverse loop structure for better performance (#734)
This commit refactors the cross-sectional agent simulation in both McCall
model lectures to use a more efficient loop structure.
Changes:
- Replaced old approach (loop over time, vectorize over agents at each step)
with new approach (vectorize over agents, loop over time per agent)
- Added sim_agent() function that uses lax.fori_loop to simulate a single
agent forward T time steps
- Added sim_agents_vmap to vectorize sim_agent across multiple agents
- Updated simulate_cross_section() to use the new implementation
- Updated plot_cross_sectional_unemployment() to use sim_agents_vmap
- Added explanatory text clarifying differences between
simulate_employment_path() and sim_agent()
Performance: The new approach has comparable or slightly better performance
while being more modular and conceptually cleaner.
Files modified:
- mccall_model_with_sep_markov.md (discrete wage case)
- mccall_fitted_vfi.md (continuous wage case)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude <[email protected]>1 parent ed8e89a commit 2afed31
File tree
2 files changed
+115
-97
lines changed- lectures
2 files changed
+115
-97
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
611 | 611 | | |
612 | 612 | | |
613 | 613 | | |
614 | | - | |
| 614 | + | |
615 | 615 | | |
616 | | - | |
617 | | - | |
618 | | - | |
619 | | - | |
620 | | - | |
621 | | - | |
| 616 | + | |
622 | 617 | | |
623 | | - | |
624 | | - | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | | - | |
629 | | - | |
630 | | - | |
631 | | - | |
632 | | - | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
637 | 621 | | |
638 | | - | |
639 | | - | |
640 | | - | |
641 | | - | |
| 622 | + | |
642 | 623 | | |
643 | | - | |
644 | | - | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
645 | 629 | | |
646 | | - | |
647 | | - | |
648 | | - | |
| 630 | + | |
649 | 631 | | |
650 | | - | |
651 | | - | |
652 | | - | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
653 | 639 | | |
654 | | - | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
655 | 649 | | |
656 | | - | |
657 | | - | |
| 650 | + | |
658 | 651 | | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
659 | 655 | | |
660 | | - | |
661 | | - | |
662 | | - | |
| 656 | + | |
| 657 | + | |
663 | 658 | | |
664 | 659 | | |
665 | 660 | | |
| |||
669 | 664 | | |
670 | 665 | | |
671 | 666 | | |
672 | | - | |
| 667 | + | |
673 | 668 | | |
674 | | - | |
675 | | - | |
676 | | - | |
677 | | - | |
678 | | - | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
679 | 673 | | |
680 | | - | |
681 | | - | |
| 674 | + | |
682 | 675 | | |
| 676 | + | |
| 677 | + | |
683 | 678 | | |
684 | 679 | | |
685 | 680 | | |
686 | 681 | | |
687 | 682 | | |
688 | | - | |
689 | | - | |
690 | | - | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
691 | 694 | | |
692 | 695 | | |
693 | | - | |
694 | 696 | | |
695 | | - | |
696 | 697 | | |
697 | 698 | | |
698 | 699 | | |
| |||
743 | 744 | | |
744 | 745 | | |
745 | 746 | | |
| 747 | + | |
746 | 748 | | |
747 | 749 | | |
748 | 750 | | |
749 | 751 | | |
750 | | - | |
751 | | - | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
752 | 764 | | |
753 | 765 | | |
754 | 766 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
748 | 748 | | |
749 | 749 | | |
750 | 750 | | |
751 | | - | |
| 751 | + | |
752 | 752 | | |
753 | | - | |
754 | | - | |
755 | | - | |
756 | | - | |
757 | | - | |
758 | | - | |
759 | | - | |
| 753 | + | |
760 | 754 | | |
761 | | - | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
762 | 760 | | |
763 | 761 | | |
764 | 762 | | |
765 | | - | |
766 | | - | |
767 | | - | |
768 | | - | |
769 | | - | |
770 | | - | |
771 | | - | |
772 | | - | |
| 763 | + | |
773 | 764 | | |
774 | | - | |
775 | | - | |
776 | | - | |
| 765 | + | |
777 | 766 | | |
778 | | - | |
779 | | - | |
780 | | - | |
| 767 | + | |
781 | 768 | | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
782 | 776 | | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
783 | 781 | | |
784 | | - | |
785 | | - | |
| 782 | + | |
786 | 783 | | |
787 | | - | |
788 | | - | |
789 | | - | |
790 | | - | |
791 | | - | |
| 784 | + | |
| 785 | + | |
792 | 786 | | |
793 | | - | |
794 | | - | |
| 787 | + | |
795 | 788 | | |
| 789 | + | |
| 790 | + | |
796 | 791 | | |
797 | | - | |
798 | | - | |
799 | | - | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
800 | 795 | | |
801 | 796 | | |
802 | 797 | | |
| |||
806 | 801 | | |
807 | 802 | | |
808 | 803 | | |
809 | | - | |
| 804 | + | |
810 | 805 | | |
811 | | - | |
812 | | - | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
813 | 810 | | |
| 811 | + | |
814 | 812 | | |
815 | 813 | | |
816 | 814 | | |
| |||
822 | 820 | | |
823 | 821 | | |
824 | 822 | | |
825 | | - | |
826 | | - | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
827 | 829 | | |
828 | 830 | | |
829 | 831 | | |
| |||
834 | 836 | | |
835 | 837 | | |
836 | 838 | | |
837 | | - | |
| 839 | + | |
838 | 840 | | |
839 | 841 | | |
840 | 842 | | |
| |||
851 | 853 | | |
852 | 854 | | |
853 | 855 | | |
854 | | - | |
855 | | - | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
856 | 862 | | |
857 | 863 | | |
858 | 864 | | |
| |||
0 commit comments