diff --git a/Fall24/cheatsheets/index.html b/Fall24/cheatsheets/index.html index a893273c..fceb016d 100644 --- a/Fall24/cheatsheets/index.html +++ b/Fall24/cheatsheets/index.html @@ -95,7 +95,7 @@

Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • diff --git a/Fall24/climate_science/2d_advection_diffusion/index.html b/Fall24/climate_science/2d_advection_diffusion/index.html index f0b96dff..003f1d70 100644 --- a/Fall24/climate_science/2d_advection_diffusion/index.html +++ b/Fall24/climate_science/2d_advection_diffusion/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/advection_and_diffusion/index.html b/Fall24/climate_science/advection_and_diffusion/index.html index 9b44ab3f..96b9ef4a 100644 --- a/Fall24/climate_science/advection_and_diffusion/index.html +++ b/Fall24/climate_science/advection_and_diffusion/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/climate2_snowball_earth/index.html b/Fall24/climate_science/climate2_snowball_earth/index.html index aaf76d05..0c878eaf 100644 --- a/Fall24/climate_science/climate2_snowball_earth/index.html +++ b/Fall24/climate_science/climate2_snowball_earth/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/how_to_collaborate_on_software/index.html b/Fall24/climate_science/how_to_collaborate_on_software/index.html index c4cc7c47..607c20f2 100644 --- a/Fall24/climate_science/how_to_collaborate_on_software/index.html +++ b/Fall24/climate_science/how_to_collaborate_on_software/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/inverse_climate_model/index.html b/Fall24/climate_science/inverse_climate_model/index.html index f9285642..b4b63afc 100644 --- a/Fall24/climate_science/inverse_climate_model/index.html +++ b/Fall24/climate_science/inverse_climate_model/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/odes_and_parameterized_types/index.html b/Fall24/climate_science/odes_and_parameterized_types/index.html index d0b96be3..b3157931 100644 --- a/Fall24/climate_science/odes_and_parameterized_types/index.html +++ b/Fall24/climate_science/odes_and_parameterized_types/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/optimization_with_JuMP/index.html b/Fall24/climate_science/optimization_with_JuMP/index.html index f3f0e1e7..bcb51ccc 100644 --- a/Fall24/climate_science/optimization_with_JuMP/index.html +++ b/Fall24/climate_science/optimization_with_JuMP/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/our_first_climate_model/index.html b/Fall24/climate_science/our_first_climate_model/index.html index 2a3146ae..761cf129 100644 --- a/Fall24/climate_science/our_first_climate_model/index.html +++ b/Fall24/climate_science/our_first_climate_model/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/predicting_the_weather/index.html b/Fall24/climate_science/predicting_the_weather/index.html index 35e91516..44c0d10f 100644 --- a/Fall24/climate_science/predicting_the_weather/index.html +++ b/Fall24/climate_science/predicting_the_weather/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/resistors_and_stencils/index.html b/Fall24/climate_science/resistors_and_stencils/index.html index e3cb9da3..017f858a 100644 --- a/Fall24/climate_science/resistors_and_stencils/index.html +++ b/Fall24/climate_science/resistors_and_stencils/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/climate_science/time_stepping/index.html b/Fall24/climate_science/time_stepping/index.html index 10521f9d..40bf9d7d 100644 --- a/Fall24/climate_science/time_stepping/index.html +++ b/Fall24/climate_science/time_stepping/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/data_science/discrete_and_continuous/index.html b/Fall24/data_science/discrete_and_continuous/index.html index 19a02e9a..6f524262 100644 --- a/Fall24/data_science/discrete_and_continuous/index.html +++ b/Fall24/data_science/discrete_and_continuous/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/data_science/linearmodel_datascience/index.html b/Fall24/data_science/linearmodel_datascience/index.html index ae017b2a..0d03acf9 100644 --- a/Fall24/data_science/linearmodel_datascience/index.html +++ b/Fall24/data_science/linearmodel_datascience/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/data_science/optimization/index.html b/Fall24/data_science/optimization/index.html index 5ab93b1f..791e5d0e 100644 --- a/Fall24/data_science/optimization/index.html +++ b/Fall24/data_science/optimization/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/data_science/pca/index.html b/Fall24/data_science/pca/index.html index e85c3769..179ce5ea 100644 --- a/Fall24/data_science/pca/index.html +++ b/Fall24/data_science/pca/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/data_science/random_variables_as_types/index.html b/Fall24/data_science/random_variables_as_types/index.html index 7adb0b85..2077e6fd 100644 --- a/Fall24/data_science/random_variables_as_types/index.html +++ b/Fall24/data_science/random_variables_as_types/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/data_science/random_vars/index.html b/Fall24/data_science/random_vars/index.html index 418620f5..88e5e479 100644 --- a/Fall24/data_science/random_vars/index.html +++ b/Fall24/data_science/random_vars/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/data_science/random_walks backup 1/index.html b/Fall24/data_science/random_walks backup 1/index.html deleted file mode 100644 index a220a31c..00000000 --- a/Fall24/data_science/random_walks backup 1/index.html +++ /dev/null @@ -1,146 +0,0 @@ - - - -Random Walks — Interactive Computational Thinking — MIT - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - -
    -
    -

    Section 2.5

    -

    Random Walks

    - -
    - -

    Lecture Video

    -
    - -
    -
    -
    - - - -
    -
    - - diff --git a/Fall24/data_science/random_walks/index.html b/Fall24/data_science/random_walks/index.html index 639c54ca..3e4f979f 100644 --- a/Fall24/data_science/random_walks/index.html +++ b/Fall24/data_science/random_walks/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/data_science/random_walks_II/index.html b/Fall24/data_science/random_walks_II/index.html index 37c0ec43..f542b85e 100644 --- a/Fall24/data_science/random_walks_II/index.html +++ b/Fall24/data_science/random_walks_II/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/data_science/simulating_component_failure/index.html b/Fall24/data_science/simulating_component_failure/index.html index 165f6496..9e77c63a 100644 --- a/Fall24/data_science/simulating_component_failure/index.html +++ b/Fall24/data_science/simulating_component_failure/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/generated_assets/2d_advection_diffusion_ef1fe4d1.plutostate b/Fall24/generated_assets/2d_advection_diffusion_59b732fc.plutostate similarity index 86% rename from Fall24/generated_assets/2d_advection_diffusion_ef1fe4d1.plutostate rename to Fall24/generated_assets/2d_advection_diffusion_59b732fc.plutostate index 14281df0..74e28c1f 100644 Binary files a/Fall24/generated_assets/2d_advection_diffusion_ef1fe4d1.plutostate and b/Fall24/generated_assets/2d_advection_diffusion_59b732fc.plutostate differ diff --git a/Fall24/generated_assets/abstraction_14381616.plutostate b/Fall24/generated_assets/abstraction_f5f39a66.plutostate similarity index 70% rename from Fall24/generated_assets/abstraction_14381616.plutostate rename to Fall24/generated_assets/abstraction_f5f39a66.plutostate index 249fcb12..c5ebb39c 100644 Binary files a/Fall24/generated_assets/abstraction_14381616.plutostate and b/Fall24/generated_assets/abstraction_f5f39a66.plutostate differ diff --git a/Fall24/generated_assets/advection_and_diffusion_eb02c8e0.plutostate b/Fall24/generated_assets/advection_and_diffusion_5789d346.plutostate similarity index 94% rename from Fall24/generated_assets/advection_and_diffusion_eb02c8e0.plutostate rename to Fall24/generated_assets/advection_and_diffusion_5789d346.plutostate index 505c4a0d..2d2d704f 100644 Binary files a/Fall24/generated_assets/advection_and_diffusion_eb02c8e0.plutostate and b/Fall24/generated_assets/advection_and_diffusion_5789d346.plutostate differ diff --git a/Fall24/generated_assets/basic_syntax_7a86302a.plutostate b/Fall24/generated_assets/basic_syntax_7a86302a.plutostate deleted file mode 100644 index 8b77f049..00000000 Binary files a/Fall24/generated_assets/basic_syntax_7a86302a.plutostate and /dev/null differ diff --git a/Fall24/generated_assets/basic_syntax_a8a4d9ec.plutostate b/Fall24/generated_assets/basic_syntax_a8a4d9ec.plutostate new file mode 100644 index 00000000..e9b5df02 Binary files /dev/null and b/Fall24/generated_assets/basic_syntax_a8a4d9ec.plutostate differ diff --git a/Fall24/generated_assets/climate2_snowball_earth_43383815.plutostate b/Fall24/generated_assets/climate2_snowball_earth_82d9339c.plutostate similarity index 97% rename from Fall24/generated_assets/climate2_snowball_earth_43383815.plutostate rename to Fall24/generated_assets/climate2_snowball_earth_82d9339c.plutostate index 7327edb2..1972837d 100644 Binary files a/Fall24/generated_assets/climate2_snowball_earth_43383815.plutostate and b/Fall24/generated_assets/climate2_snowball_earth_82d9339c.plutostate differ diff --git a/Fall24/generated_assets/discrete_and_continuous_6ee5c54a.plutostate b/Fall24/generated_assets/discrete_and_continuous_8593a7a9.plutostate similarity index 95% rename from Fall24/generated_assets/discrete_and_continuous_6ee5c54a.plutostate rename to Fall24/generated_assets/discrete_and_continuous_8593a7a9.plutostate index d33e06fc..79e57285 100644 Binary files a/Fall24/generated_assets/discrete_and_continuous_6ee5c54a.plutostate and b/Fall24/generated_assets/discrete_and_continuous_8593a7a9.plutostate differ diff --git a/Fall24/generated_assets/dynamicprograms_181ecd05.plutostate b/Fall24/generated_assets/dynamicprograms_b7a33393.plutostate similarity index 86% rename from Fall24/generated_assets/dynamicprograms_181ecd05.plutostate rename to Fall24/generated_assets/dynamicprograms_b7a33393.plutostate index 8ad8c5dd..871703bc 100644 Binary files a/Fall24/generated_assets/dynamicprograms_181ecd05.plutostate and b/Fall24/generated_assets/dynamicprograms_b7a33393.plutostate differ diff --git a/Fall24/generated_assets/how_to_collaborate_on_software_18d2a1d9.plutostate b/Fall24/generated_assets/how_to_collaborate_on_software_16ca937d.plutostate similarity index 63% rename from Fall24/generated_assets/how_to_collaborate_on_software_18d2a1d9.plutostate rename to Fall24/generated_assets/how_to_collaborate_on_software_16ca937d.plutostate index 9db2a516..c4d04595 100644 Binary files a/Fall24/generated_assets/how_to_collaborate_on_software_18d2a1d9.plutostate and b/Fall24/generated_assets/how_to_collaborate_on_software_16ca937d.plutostate differ diff --git a/Fall24/generated_assets/hw0_29912ad8.plutostate b/Fall24/generated_assets/hw0_393d9f59.plutostate similarity index 96% rename from Fall24/generated_assets/hw0_29912ad8.plutostate rename to Fall24/generated_assets/hw0_393d9f59.plutostate index 7b3ec9ff..1f825133 100644 Binary files a/Fall24/generated_assets/hw0_29912ad8.plutostate and b/Fall24/generated_assets/hw0_393d9f59.plutostate differ diff --git a/Fall24/generated_assets/hw10_95fe5bbc.plutostate b/Fall24/generated_assets/hw10_850beff4.plutostate similarity index 83% rename from Fall24/generated_assets/hw10_95fe5bbc.plutostate rename to Fall24/generated_assets/hw10_850beff4.plutostate index e49af74a..b852e81f 100644 Binary files a/Fall24/generated_assets/hw10_95fe5bbc.plutostate and b/Fall24/generated_assets/hw10_850beff4.plutostate differ diff --git a/Fall24/generated_assets/hw1_299c4e25.plutostate b/Fall24/generated_assets/hw1_1ee526ec.plutostate similarity index 90% rename from Fall24/generated_assets/hw1_299c4e25.plutostate rename to Fall24/generated_assets/hw1_1ee526ec.plutostate index b9fdbbff..eb3eea0a 100644 Binary files a/Fall24/generated_assets/hw1_299c4e25.plutostate and b/Fall24/generated_assets/hw1_1ee526ec.plutostate differ diff --git a/Fall24/generated_assets/hw2_3a94055e.plutostate b/Fall24/generated_assets/hw2_3a94055e.plutostate deleted file mode 100644 index 2c79a7f6..00000000 Binary files a/Fall24/generated_assets/hw2_3a94055e.plutostate and /dev/null differ diff --git a/Fall24/generated_assets/hw2_825e3864.plutostate b/Fall24/generated_assets/hw2_825e3864.plutostate new file mode 100644 index 00000000..99d239bd Binary files /dev/null and b/Fall24/generated_assets/hw2_825e3864.plutostate differ diff --git a/Fall24/generated_assets/hw3_e3d49892.plutostate b/Fall24/generated_assets/hw3_e3d49892.plutostate new file mode 100644 index 00000000..cbb6741e Binary files /dev/null and b/Fall24/generated_assets/hw3_e3d49892.plutostate differ diff --git a/Fall24/generated_assets/hw3_fc9a1563.plutostate b/Fall24/generated_assets/hw3_fc9a1563.plutostate deleted file mode 100644 index 51df038d..00000000 Binary files a/Fall24/generated_assets/hw3_fc9a1563.plutostate and /dev/null differ diff --git a/Fall24/generated_assets/hw4_77566d9e.plutostate b/Fall24/generated_assets/hw4_77566d9e.plutostate new file mode 100644 index 00000000..2d9b7435 Binary files /dev/null and b/Fall24/generated_assets/hw4_77566d9e.plutostate differ diff --git a/Fall24/generated_assets/hw4_8d138b97.plutostate b/Fall24/generated_assets/hw4_8d138b97.plutostate deleted file mode 100644 index 37a4ca40..00000000 Binary files a/Fall24/generated_assets/hw4_8d138b97.plutostate and /dev/null differ diff --git a/Fall24/generated_assets/hw5_3f2df81a.plutostate b/Fall24/generated_assets/hw5_3f2df81a.plutostate new file mode 100644 index 00000000..c8ef11a6 Binary files /dev/null and b/Fall24/generated_assets/hw5_3f2df81a.plutostate differ diff --git a/Fall24/generated_assets/hw5_89d78b8d.plutostate b/Fall24/generated_assets/hw5_89d78b8d.plutostate deleted file mode 100644 index 7f0265c6..00000000 Binary files a/Fall24/generated_assets/hw5_89d78b8d.plutostate and /dev/null differ diff --git a/Fall24/generated_assets/hw6_61150391.plutostate b/Fall24/generated_assets/hw6_61150391.plutostate deleted file mode 100644 index f17125ce..00000000 Binary files a/Fall24/generated_assets/hw6_61150391.plutostate and /dev/null differ diff --git a/Fall24/generated_assets/hw6_a7413284.plutostate b/Fall24/generated_assets/hw6_a7413284.plutostate new file mode 100644 index 00000000..6ec62100 Binary files /dev/null and b/Fall24/generated_assets/hw6_a7413284.plutostate differ diff --git a/Fall24/generated_assets/hw7_89e9b9d3.plutostate b/Fall24/generated_assets/hw7_89e9b9d3.plutostate deleted file mode 100644 index c990cee4..00000000 Binary files a/Fall24/generated_assets/hw7_89e9b9d3.plutostate and /dev/null differ diff --git a/Fall24/generated_assets/hw7_e0e8657b.plutostate b/Fall24/generated_assets/hw7_e0e8657b.plutostate new file mode 100644 index 00000000..fbd8b980 Binary files /dev/null and b/Fall24/generated_assets/hw7_e0e8657b.plutostate differ diff --git a/Fall24/generated_assets/hw8_670e2ad5.plutostate b/Fall24/generated_assets/hw8_07274dcd.plutostate similarity index 74% rename from Fall24/generated_assets/hw8_670e2ad5.plutostate rename to Fall24/generated_assets/hw8_07274dcd.plutostate index c38db196..0b176069 100644 Binary files a/Fall24/generated_assets/hw8_670e2ad5.plutostate and b/Fall24/generated_assets/hw8_07274dcd.plutostate differ diff --git a/Fall24/generated_assets/hw9_0da6b17c.plutostate b/Fall24/generated_assets/hw9_0da6b17c.plutostate new file mode 100644 index 00000000..168a7ca5 Binary files /dev/null and b/Fall24/generated_assets/hw9_0da6b17c.plutostate differ diff --git a/Fall24/generated_assets/hw9_7fd9c16e.plutostate b/Fall24/generated_assets/hw9_7fd9c16e.plutostate deleted file mode 100644 index e3e64a2f..00000000 Binary files a/Fall24/generated_assets/hw9_7fd9c16e.plutostate and /dev/null differ diff --git a/Fall24/generated_assets/images_1222a1fc.plutostate b/Fall24/generated_assets/images_e0cd734c.plutostate similarity index 97% rename from Fall24/generated_assets/images_1222a1fc.plutostate rename to Fall24/generated_assets/images_e0cd734c.plutostate index 1e17bfdb..febf1181 100644 Binary files a/Fall24/generated_assets/images_1222a1fc.plutostate and b/Fall24/generated_assets/images_e0cd734c.plutostate differ diff --git a/Fall24/generated_assets/inverse_climate_model_bcb5a166.plutostate b/Fall24/generated_assets/inverse_climate_model_6adc7ddd.plutostate similarity index 89% rename from Fall24/generated_assets/inverse_climate_model_bcb5a166.plutostate rename to Fall24/generated_assets/inverse_climate_model_6adc7ddd.plutostate index a97d5f8a..79297033 100644 Binary files a/Fall24/generated_assets/inverse_climate_model_bcb5a166.plutostate and b/Fall24/generated_assets/inverse_climate_model_6adc7ddd.plutostate differ diff --git a/Fall24/generated_assets/linear_transformation_3de70dd8.plutostate b/Fall24/generated_assets/linear_transformation_1658a552.plutostate similarity index 83% rename from Fall24/generated_assets/linear_transformation_3de70dd8.plutostate rename to Fall24/generated_assets/linear_transformation_1658a552.plutostate index ee5a0813..b0550fe6 100644 Binary files a/Fall24/generated_assets/linear_transformation_3de70dd8.plutostate and b/Fall24/generated_assets/linear_transformation_1658a552.plutostate differ diff --git a/Fall24/generated_assets/linearmodel_datascience_3006b0ba.plutostate b/Fall24/generated_assets/linearmodel_datascience_001cd42b.plutostate similarity index 89% rename from Fall24/generated_assets/linearmodel_datascience_3006b0ba.plutostate rename to Fall24/generated_assets/linearmodel_datascience_001cd42b.plutostate index 388388d0..1d940166 100644 Binary files a/Fall24/generated_assets/linearmodel_datascience_3006b0ba.plutostate and b/Fall24/generated_assets/linearmodel_datascience_001cd42b.plutostate differ diff --git a/Fall24/generated_assets/newton_method_325e10e8.plutostate b/Fall24/generated_assets/newton_method_b17f4123.plutostate similarity index 71% rename from Fall24/generated_assets/newton_method_325e10e8.plutostate rename to Fall24/generated_assets/newton_method_b17f4123.plutostate index 3d647fc7..279ead84 100644 Binary files a/Fall24/generated_assets/newton_method_325e10e8.plutostate and b/Fall24/generated_assets/newton_method_b17f4123.plutostate differ diff --git a/Fall24/generated_assets/odes_and_parameterized_types_d59077d7.plutostate b/Fall24/generated_assets/odes_and_parameterized_types_1138d0b5.plutostate similarity index 88% rename from Fall24/generated_assets/odes_and_parameterized_types_d59077d7.plutostate rename to Fall24/generated_assets/odes_and_parameterized_types_1138d0b5.plutostate index cbaeca0d..cee2401c 100644 Binary files a/Fall24/generated_assets/odes_and_parameterized_types_d59077d7.plutostate and b/Fall24/generated_assets/odes_and_parameterized_types_1138d0b5.plutostate differ diff --git a/Fall24/generated_assets/optimization_a85837d5.plutostate b/Fall24/generated_assets/optimization_43fc4488.plutostate similarity index 69% rename from Fall24/generated_assets/optimization_a85837d5.plutostate rename to Fall24/generated_assets/optimization_43fc4488.plutostate index 06a0a7cd..a1f685bf 100644 Binary files a/Fall24/generated_assets/optimization_a85837d5.plutostate and b/Fall24/generated_assets/optimization_43fc4488.plutostate differ diff --git a/Fall24/generated_assets/optimization_with_JuMP_68e41f28.plutostate b/Fall24/generated_assets/optimization_with_JuMP_243709d9.plutostate similarity index 97% rename from Fall24/generated_assets/optimization_with_JuMP_68e41f28.plutostate rename to Fall24/generated_assets/optimization_with_JuMP_243709d9.plutostate index ee901524..b6d01de9 100644 Binary files a/Fall24/generated_assets/optimization_with_JuMP_68e41f28.plutostate and b/Fall24/generated_assets/optimization_with_JuMP_243709d9.plutostate differ diff --git a/Fall24/generated_assets/our_first_climate_model_b543d6db.plutostate b/Fall24/generated_assets/our_first_climate_model_f02b92e6.plutostate similarity index 93% rename from Fall24/generated_assets/our_first_climate_model_b543d6db.plutostate rename to Fall24/generated_assets/our_first_climate_model_f02b92e6.plutostate index a3d49b6c..6e2b0bf0 100644 Binary files a/Fall24/generated_assets/our_first_climate_model_b543d6db.plutostate and b/Fall24/generated_assets/our_first_climate_model_f02b92e6.plutostate differ diff --git a/Fall24/generated_assets/pca_a0b039e1.plutostate b/Fall24/generated_assets/pca_ed1ecfd7.plutostate similarity index 97% rename from Fall24/generated_assets/pca_a0b039e1.plutostate rename to Fall24/generated_assets/pca_ed1ecfd7.plutostate index 6694e198..ea9b2c08 100644 Binary files a/Fall24/generated_assets/pca_a0b039e1.plutostate and b/Fall24/generated_assets/pca_ed1ecfd7.plutostate differ diff --git a/Fall24/generated_assets/plots_tutorial_e3393a36.plutostate b/Fall24/generated_assets/plots_tutorial_f74bd6fd.plutostate similarity index 99% rename from Fall24/generated_assets/plots_tutorial_e3393a36.plutostate rename to Fall24/generated_assets/plots_tutorial_f74bd6fd.plutostate index 3882a962..b8972a0e 100644 Binary files a/Fall24/generated_assets/plots_tutorial_e3393a36.plutostate and b/Fall24/generated_assets/plots_tutorial_f74bd6fd.plutostate differ diff --git a/Fall24/generated_assets/predicting_the_weather_350d3047.plutostate b/Fall24/generated_assets/predicting_the_weather_3a88dd28.plutostate similarity index 96% rename from Fall24/generated_assets/predicting_the_weather_350d3047.plutostate rename to Fall24/generated_assets/predicting_the_weather_3a88dd28.plutostate index 78da21c3..390824c2 100644 Binary files a/Fall24/generated_assets/predicting_the_weather_350d3047.plutostate and b/Fall24/generated_assets/predicting_the_weather_3a88dd28.plutostate differ diff --git a/Fall24/generated_assets/random_variables_as_types_7745c02e.plutostate b/Fall24/generated_assets/random_variables_as_types_b61e2219.plutostate similarity index 92% rename from Fall24/generated_assets/random_variables_as_types_7745c02e.plutostate rename to Fall24/generated_assets/random_variables_as_types_b61e2219.plutostate index 260e226c..e15e1435 100644 Binary files a/Fall24/generated_assets/random_variables_as_types_7745c02e.plutostate and b/Fall24/generated_assets/random_variables_as_types_b61e2219.plutostate differ diff --git a/Fall24/generated_assets/random_vars_eba6df04.plutostate b/Fall24/generated_assets/random_vars_e57a0e4f.plutostate similarity index 96% rename from Fall24/generated_assets/random_vars_eba6df04.plutostate rename to Fall24/generated_assets/random_vars_e57a0e4f.plutostate index 01be195f..b20628e1 100644 Binary files a/Fall24/generated_assets/random_vars_eba6df04.plutostate and b/Fall24/generated_assets/random_vars_e57a0e4f.plutostate differ diff --git a/Fall24/generated_assets/random_walks_f14e2c21.plutostate b/Fall24/generated_assets/random_walks_81b0ac1d.plutostate similarity index 78% rename from Fall24/generated_assets/random_walks_f14e2c21.plutostate rename to Fall24/generated_assets/random_walks_81b0ac1d.plutostate index b7840dcd..c4f509f9 100644 Binary files a/Fall24/generated_assets/random_walks_f14e2c21.plutostate and b/Fall24/generated_assets/random_walks_81b0ac1d.plutostate differ diff --git a/Fall24/generated_assets/random_walks_II_04386990.plutostate b/Fall24/generated_assets/random_walks_II_4be42e3a.plutostate similarity index 94% rename from Fall24/generated_assets/random_walks_II_04386990.plutostate rename to Fall24/generated_assets/random_walks_II_4be42e3a.plutostate index 49dd727b..e954a7af 100644 Binary files a/Fall24/generated_assets/random_walks_II_04386990.plutostate and b/Fall24/generated_assets/random_walks_II_4be42e3a.plutostate differ diff --git a/Fall24/generated_assets/random_walks_backup_1_5eac959a.plutostate b/Fall24/generated_assets/random_walks_backup_1_5eac959a.plutostate deleted file mode 100644 index 588c55e2..00000000 Binary files a/Fall24/generated_assets/random_walks_backup_1_5eac959a.plutostate and /dev/null differ diff --git a/Fall24/generated_assets/random_walks_backup_1_fbb5ae65.jl b/Fall24/generated_assets/random_walks_backup_1_fbb5ae65.jl deleted file mode 100644 index f3818bec..00000000 --- a/Fall24/generated_assets/random_walks_backup_1_fbb5ae65.jl +++ /dev/null @@ -1,1573 +0,0 @@ -### A Pluto.jl notebook ### -# v0.19.45 - -#> [frontmatter] -#> chapter = 2 -#> video = "https://www.youtube.com/watch?v=14hHtGJ4s-g" -#> image = "https://user-images.githubusercontent.com/6933510/136196563-f4b5b44c-5252-4e67-8c82-c550de891c55.png" -#> section = 5 -#> order = 5 -#> title = "Random Walks" -#> layout = "layout.jlhtml" -#> youtube_id = "14hHtGJ4s-g" -#> description = "" -#> tags = ["lecture", "module2", "programming", "track_julia", "plotting", "structure", "type", "interactive", "random", "statistics", "track_math", "track_data"] - -using Markdown -using InteractiveUtils - -# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error). -macro bind(def, element) - quote - local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end - local el = $(esc(element)) - global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el) - el - end -end - -# ╔═╡ 97e807b2-9237-11eb-31ef-6fe0d4cc94d3 -using Plots, PlutoUI, BenchmarkTools - -# ╔═╡ 5f0d7a44-91e0-11eb-10ae-d73156f965e6 -TableOfContents(aside=true) - -# ╔═╡ 9647147a-91ab-11eb-066f-9bc190368fb2 -md""" -# Julia concepts - -- Benchmarking: BenchmarkTools.jl -- Plotting in a loop -- Generic programming -- Mutable vs immutable structs -- Vectors of vectors -- Aliasing of memory -- `cumsum` -""" - -# ╔═╡ ff1aca1e-91e7-11eb-343e-0f89d9570b06 -md""" -# Motivation: Dynamics of hard discs -""" - -# ╔═╡ 66a2f510-9232-11eb-3be9-131febc0039f -md""" -Brown observed **Brownian motion** in 1827: Large particles like sand or pollen in water move around seemingly at random. Einstein explained this in 1905 as repeated impacts with water molecules. - -We can visualise that with a simulation of hard discs bouncing off one another. Even though the dynamics is not random -- each disc follows Newton's laws -- if we just look at a single one of them it *looks* random. -""" - -# ╔═╡ bd3170e6-91ae-11eb-06f8-ebb6b2e7869f -md""" -## Visualising random walks -""" - -# ╔═╡ a304c842-91df-11eb-3fac-6dd63087f6de -md""" -A **random walk** models random motion in time and space. At each time step an object moves in a random direction. - -Let's visualise the result in 2 dimensions. -""" - -# ╔═╡ 798507d6-91db-11eb-2e4a-3ba02f12ba65 -md""" -N = $(@bind N Slider(1:6, show_value=true, default=1)) -""" - -# ╔═╡ 3504168a-91de-11eb-181d-1d580d5dc071 -md""" -t = $(@bind t Slider(1:10^N, show_value=true, default=1)) -""" - -# ╔═╡ b62c4af8-9232-11eb-2f66-dd27dcb87d20 -md""" -We see that the dynamics closely resembles, at least qualitatively, that of the hard disc. -""" - -# ╔═╡ 905379ce-91ad-11eb-295d-8354ecf5c5b1 -md""" -# Why use random walks? - -#### Why should we model with random processes? - -- Either we need to get a lot of data to precisely characterise a system, or we assume it's random - -- It may well be that *can* have all the details, but by making a simplifying assumption we actually gain more *understanding*. - - -#### Examples: - - -- Stock price going up and down - - -- Pollutants getting dispersed in the air - - -- Neutral genes moving through a population - - -""" - -# ╔═╡ 5c4f0f26-91ad-11eb-033b-2bd221f0bdba -md""" -# Simple random walk - -The simplest version is called **simple random walk**: one object jumps on the integers: at each time step it jumps left or right. - -Each such step is a new random variable, taking values $\pm 1$ at each step, each with probability $1/2$ (in the simplest case). -We can think of this as a scaling of a Bernoulli random variable. - -In order to simulate this, we need to know how to generate the jumps. -""" - -# ╔═╡ da98b676-91e0-11eb-0d97-57b8a8aadf2a -md""" -## Julia: Benchmarking -""" - -# ╔═╡ e0b607c0-91e0-11eb-10aa-53ec33570e59 -md""" -There are various ways we could generate random values $\pm 1$. Let's use this as an opportunity to learn about **benchmarking** in Julia, i.e. measuring run time to compare performance. In this case we will do "micro-benchmarks", which try to compare tiny pieces of code which we intend to run many millions of times, so that small differences in run time can be significant. - -The `BenchmarkTools.jl` package provides relatively easy-to-use tools for measuring this, by running the code many times and calculating statistics. The `@btime` macro is a simple way to estimate actual running time. Each option should be enclosed in a function. -""" - -# ╔═╡ fa1635d4-91e3-11eb-31bd-cf61c502ad35 -md""" -Here are a few different ways we could generate random steps: -""" - -# ╔═╡ f7f9e4c6-91e3-11eb-1a56-8b98f0b09b46 -begin - step1() = rand( (-1, +1) ) - - step2() = 2 * (rand() < 0.5) - 1 - - step3() = 2 * rand(Bool) - 1 - - step4() = sign(randn()) -end - -# ╔═╡ 5da7b076-91b4-11eb-3eba-b3f5849efabb -with_terminal() do - @btime step1() - @btime step2() - @btime step3() - @btime step4() -end - -# ╔═╡ ea9e77e2-91b1-11eb-185d-cd006db11f60 -md""" -## Trajectory of a random walk -""" - -# ╔═╡ 12b4d528-9239-11eb-2824-8ddb5e2ba892 -md""" -We can now calculate the **trajectory** of a 1D random walk as it takes several steps. -It starts at an initial position, for example, 0, and takes consecutive steps: -""" - -# ╔═╡ 2f525796-9239-11eb-1865-9b01eadcf548 -function walk1D(N) - x = 0 - xs = [x] - - for i in 1:N - x += step1() - push!(xs, x) - end - - return xs -end - -# ╔═╡ 51abfe6e-9239-11eb-362a-259570250663 -begin - plot() - - for i in 1:10 - plot!(walk1D(100), leg=false, size=(500, 300), lw=2, alpha=0.5) - end - - plot!() -end - -# ╔═╡ b847b5ca-9239-11eb-02fe-db4d9625bc5f -md""" -# Making it more general: Random walks using types -""" - -# ╔═╡ c2deb090-9239-11eb-0739-a74379c15ce6 -md""" -Now suppose we want to think about more general random walks, for example moving around in 2D. Then we need to *generalise* the above function. - -Based on our experience from last time, you should suspect that a good way to do this is with *types*. We will define -""" - -# ╔═╡ d420d492-91d9-11eb-056d-33cc8f0aed74 -abstract type Walker end - -# ╔═╡ ad2d4dd8-91d5-11eb-27af-6f0c6e61a86a -struct Walker1D <: Walker - pos::Int -end - -# ╔═╡ d0f81f28-91d9-11eb-2e79-61461ef5b132 -position(w::Walker) = w.pos - -# ╔═╡ b8f2c508-91d5-11eb-31b5-61810f171270 -step(w::Walker1D) = rand( (-1, +1) ) - -# ╔═╡ 23b84ce2-91da-11eb-01f8-c308ac4d1c7a -struct Walker2D <: Walker - x::Int - y::Int -end - -# ╔═╡ 537f952a-91da-11eb-33cf-6be2fd3bc45c -position(w::Walker2D) = (w.x, w.y) - -# ╔═╡ 3c3971e2-91da-11eb-384c-01c627318bdc -update(w::W, step) where {W <: Walker} = W(position(w) + step) - -# ╔═╡ 5b972296-91da-11eb-29b1-074f3926181e -step(w::Walker2D) = rand( [ [1, 0], [0, 1], [-1, 0], [0, -1] ] ) - -# ╔═╡ 3ad5a93c-91db-11eb-3227-c96bf8fd2206 -update(w::Walker2D, step::Vector) = Walker2D(w.x + step[1], w.y + step[2]) - -# ╔═╡ cb0ef266-91d5-11eb-314b-0545c0c817d0 -function trajectory(w::W, N) where {W} # W is a type parameter - ws = [position(w)] - - for i in 1:N - pos = position(w) - w = update(w, step(w)) - - push!(ws, position(w)) - end - - return ws -end - -# ╔═╡ 048fac02-91da-11eb-0d26-4f258b4cd043 -trajectory(Walker1D(0), 10) - -# ╔═╡ 74182fe0-91da-11eb-219a-01f13b86406d -traj = trajectory(Walker2D(0, 0), 10^N) - -# ╔═╡ 4c8d8294-91db-11eb-353d-c3696c615b3d -begin - plot(traj[1:t], ratio=1, leg=false, alpha=0.5, lw=2) - scatter!([ traj[1], traj[t] ], c=[:red, :green]) - - xlims!(minimum(first.(traj)) - 1, maximum(first.(traj)) + 1) - ylims!(minimum(last.(traj)) - 1, maximum(last.(traj)) + 1) - -end - -# ╔═╡ 57972a32-91e5-11eb-1d62-fbc22c494db9 -md""" -## Random walks as sum of random variables -""" - -# ╔═╡ 63122dd0-91e5-11eb-34c3-b1b5c87809b8 -md""" -We can connect up with the discussion from last lecture about the sum of random variables: The position $S_n$ of a random walk at time $n$ is a random variable that is the sum of $n$ random variables (which are often independent and id - -$$S_n = X_1 + \cdots + X_n$$ - -where $X_i$ is the random variable modelling the $i$th step of the walk. - -Often each $X_i$ will be independent random variables of the same type, in which case we talk about an **independent and identically distributed** (IID) collection. - -In principle we could use the method from last lecture to model this, but there is a difficulty since we are interested in all intermediate steps: The values $S_{n-1}$ and $S_n$, for example, are *not* independent (even though the $X_i$ are). For example, if $S_{n-1}$ is large, then $S_n$ is also about as large, so they are highly correlated. -""" - -# ╔═╡ bd013582-91e7-11eb-2d79-e18e45f6d639 -md""" -## Cumulative sum -""" - -# ╔═╡ c092ab72-91e7-11eb-2bd1-13c8c8bb30e4 -md""" -Suppose we generate steps $X_i$, e.g. -""" - -# ╔═╡ cfb0f9ba-91e7-11eb-26b5-5f0f59d03cff -steps = rand( (-1, +1), 10 ) - -# ╔═╡ d6edb326-91e7-11eb-03b1-d93e3bc83ca6 -md""" -The trajectory, or sample path, of the random walk, is the collection of positions over time. These are the **partial sums** - -$$\begin{align} -S_1 & = X_1 \\ -S_2 &= X_1 + X_2 \\ -S_3 &= X_1 + X_2 + X_3 -\end{align}$$ - -etc. -""" - -# ╔═╡ 4ef42cec-91e8-11eb-2976-0950ffe5de6c -md""" -We can calculate all of these values using the `cumsum` ("cumulative sum", also called "prefix sum" or "scan") function: -""" - -# ╔═╡ ace8658e-91e8-11eb-0b9d-4b759635e417 -cumsum(steps) - -# ╔═╡ b049ff58-91e8-11eb-203b-4f4b5ee5f01f -md""" -Let's plot this: -""" - -# ╔═╡ b6775d3a-91e8-11eb-0187-618cb538d142 -plot(cumsum(steps), m=:o, leg=false, size=(500, 300)) - -# ╔═╡ 8b1441b6-91e4-11eb-16b2-d7eadd3fd69c -md""" -# Trajectories vs evolving probability distributions -""" - -# ╔═╡ d1315f94-91e4-11eb-1076-81156e24d2f1 -md""" -So far we have looked at single trajectories of random walks. We can think of this as the equivalent of sampling using `rand`. Suppose that we were to (conceptually) run millions or billions of trajectories of our random walk. At each *time* step, we can ask for the probability distribution in *space*, averaged over all those walks. -Note that we now have *two* variables. We could do this by literally running many walks and computing histograms. Instead we can look at how probabilities move around. - -Let's call $p_i^t$ the probability of being at site $i$ at time $t$. We can then ask what the probability of being at site $i$ at time $t+1$. It must have been in one of the neighbouring sites at time $t$, so - -$$p_i^{t+1} = \textstyle \frac{1}{2} (p_{i-1}^{t} + p_{i+1}^{t}).$$ - -This is sometimes called the **master equation** (even though that is rather a useless, non-descriptive name). It describes how the probability distribution -""" - -# ╔═╡ 97c958c2-91eb-11eb-17cb-410acc7f3e49 -md""" -At time $t$ we have a whole probability distribution, which we can think of as a vector $\mathbf{p}^t$. - -Let's write a function to **evolve** the vector to the next time step. -""" - -# ╔═╡ b7a98dba-91eb-11eb-3c78-074aa835b5fb -function evolve(p) - p′ = similar(p) # make a vector of the same length and type - # to store the probability vector at the next time step - - for i in 2:length(p)-1 - p′[i] = 0.5 * (p[i-1] + p[i+1]) - end - - p′[1] = 0 - p′[end] = 0 - - return p′ - -end - -# ╔═╡ f7f17c0c-91eb-11eb-3fa5-3bd90bb7044e -md""" -Wait... Do you recognise this? - -We have seen this before! This is just a **convolution**, like the blurring kernel from image processing (except that in our particular model `p[i]` itself does not contribute to the value of `new_p[i]`.) -""" - -# ╔═╡ f0aed302-91eb-11eb-13fb-d9418ef327a8 -md""" -Note that just as we did with images, we have a problem at the edges of the system. We need to specify **boundary conditions**. For now we have put 0s at the boundary. This corresponds to probability *escaping* at the boundary: any probability that arrives at the boundary (i.e. the first or last cell) at a given time step does not stay in the system. We can think of the probability as analogous to a chemical that disappears -""" - -# ╔═╡ c1062e00-922b-11eb-1f31-ddbd03f8f986 -md""" -We also need to specify an *initial* condition $\mathbf{p}_0$. This tells us where our walker is at time $0$. Suppose that all walkers start at $0$. We will place this in the middle of our vector. Then the probability at that location is 1, while the probability elsewhere is 0: -""" - -# ╔═╡ 547188ea-9233-11eb-1a89-5ff9468b31f7 -function initial_condition(n) - - p0 = zeros(n) - p0[n ÷ 2 + 1] = 1 - - return p0 -end - -# ╔═╡ 2920abfe-91ec-11eb-19bc-935fa1ba0a96 -md""" -Now let's try and visualise the time evolution. -""" - -# ╔═╡ 3fadf88c-9236-11eb-19fa-d191ac5a6191 -function time_evolution(p0, N) - ps = [p0] - p = p0 - - for i in 1:N - p = evolve(p) - push!(ps, copy(p)) - end - - return ps -end - -# ╔═╡ 58653a70-9236-11eb-3dae-47adc2a77cb4 -p0 = initial_condition(101) - -# ╔═╡ 5d02f21e-9236-11eb-26ea-6593aa80a2eb -ps = time_evolution(p0, 100) - -# ╔═╡ 863caf0a-9236-11eb-1013-ab7d83f3fc0c -ps[2] - -# ╔═╡ b803406e-9236-11eb-3aad-056b7f2c9b4b -md""" -t = $(@bind tt Slider(1:length(ps), show_value=true, default=1)) -""" - -# ╔═╡ cc7aaeea-9236-11eb-3fad-2b5ad3962ec1 -plot(ps[tt], ylim=(0, 1), leg=false, size=(500, 300)) - -# ╔═╡ dabb5766-9236-11eb-3be9-9b33ba5af68a -ps[tt] - -# ╔═╡ 6cde6ef4-9236-11eb-219a-4d20adaf9988 -M = reduce(hcat, ps)' - -# ╔═╡ 7e8c1a2a-9236-11eb-20e9-57f6601f5472 -heatmap(M, yflip=true) - -# ╔═╡ 00000000-0000-0000-0000-000000000001 -PLUTO_PROJECT_TOML_CONTENTS = """ -[deps] -BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" -Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" -PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8" - -[compat] -BenchmarkTools = "~1.3.1" -Plots = "~1.29.1" -PlutoUI = "~0.7.48" -""" - -# ╔═╡ 00000000-0000-0000-0000-000000000002 -PLUTO_MANIFEST_TOML_CONTENTS = """ -# This file is machine-generated - editing it directly is not advised - -julia_version = "1.10.4" -manifest_format = "2.0" -project_hash = "7f7f421624990bcbfe26a0a6a938b5b21ea8e293" - -[[deps.AbstractPlutoDingetjes]] -deps = ["Pkg"] -git-tree-sha1 = "6e1d2a35f2f90a4bc7c2ed98079b2ba09c35b83a" -uuid = "6e696c72-6542-2067-7265-42206c756150" -version = "1.3.2" - -[[deps.ArgTools]] -uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f" -version = "1.1.1" - -[[deps.Artifacts]] -uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33" - -[[deps.Base64]] -uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" - -[[deps.BenchmarkTools]] -deps = ["JSON", "Logging", "Printf", "Profile", "Statistics", "UUIDs"] -git-tree-sha1 = "d9a9701b899b30332bbcb3e1679c41cce81fb0e8" -uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" -version = "1.3.2" - -[[deps.Bzip2_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "9e2a6b69137e6969bab0152632dcb3bc108c8bdd" -uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0" -version = "1.0.8+1" - -[[deps.Cairo_jll]] -deps = ["Artifacts", "Bzip2_jll", "CompilerSupportLibraries_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"] -git-tree-sha1 = "a2f1c8c668c8e3cb4cca4e57a8efdb09067bb3fd" -uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a" -version = "1.18.0+2" - -[[deps.ColorSchemes]] -deps = ["ColorTypes", "ColorVectorSpace", "Colors", "FixedPointNumbers", "PrecompileTools", "Random"] -git-tree-sha1 = "4b270d6465eb21ae89b732182c20dc165f8bf9f2" -uuid = "35d6a980-a343-548e-a6ea-1d62b119f2f4" -version = "3.25.0" - -[[deps.ColorTypes]] -deps = ["FixedPointNumbers", "Random"] -git-tree-sha1 = "b10d0b65641d57b8b4d5e234446582de5047050d" -uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f" -version = "0.11.5" - -[[deps.ColorVectorSpace]] -deps = ["ColorTypes", "FixedPointNumbers", "LinearAlgebra", "Requires", "Statistics", "TensorCore"] -git-tree-sha1 = "a1f44953f2382ebb937d60dafbe2deea4bd23249" -uuid = "c3611d14-8923-5661-9e6a-0046d554d3a4" -version = "0.10.0" - - [deps.ColorVectorSpace.extensions] - SpecialFunctionsExt = "SpecialFunctions" - - [deps.ColorVectorSpace.weakdeps] - SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" - -[[deps.Colors]] -deps = ["ColorTypes", "FixedPointNumbers", "Reexport"] -git-tree-sha1 = "362a287c3aa50601b0bc359053d5c2468f0e7ce0" -uuid = "5ae59095-9a9b-59fe-a467-6f913c188581" -version = "0.12.11" - -[[deps.Compat]] -deps = ["TOML", "UUIDs"] -git-tree-sha1 = "b1c55339b7c6c350ee89f2c1604299660525b248" -uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "4.15.0" -weakdeps = ["Dates", "LinearAlgebra"] - - [deps.Compat.extensions] - CompatLinearAlgebraExt = "LinearAlgebra" - -[[deps.CompilerSupportLibraries_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" -version = "1.1.1+0" - -[[deps.ConstructionBase]] -deps = ["LinearAlgebra"] -git-tree-sha1 = "260fd2400ed2dab602a7c15cf10c1933c59930a2" -uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9" -version = "1.5.5" - - [deps.ConstructionBase.extensions] - ConstructionBaseIntervalSetsExt = "IntervalSets" - ConstructionBaseStaticArraysExt = "StaticArrays" - - [deps.ConstructionBase.weakdeps] - IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" - StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" - -[[deps.Contour]] -deps = ["StaticArrays"] -git-tree-sha1 = "9f02045d934dc030edad45944ea80dbd1f0ebea7" -uuid = "d38c429a-6771-53c6-b99e-75d170b6e991" -version = "0.5.7" - -[[deps.DataAPI]] -git-tree-sha1 = "abe83f3a2f1b857aac70ef8b269080af17764bbe" -uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" -version = "1.16.0" - -[[deps.DataStructures]] -deps = ["Compat", "InteractiveUtils", "OrderedCollections"] -git-tree-sha1 = "1d0a14036acb104d9e89698bd408f63ab58cdc82" -uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" -version = "0.18.20" - -[[deps.DataValueInterfaces]] -git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6" -uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464" -version = "1.0.0" - -[[deps.Dates]] -deps = ["Printf"] -uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" - -[[deps.DelimitedFiles]] -deps = ["Mmap"] -git-tree-sha1 = "9e2f36d3c96a820c678f2f1f1782582fcf685bae" -uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" -version = "1.9.1" - -[[deps.DocStringExtensions]] -deps = ["LibGit2"] -git-tree-sha1 = "2fb1e02f2b635d0845df5d7c167fec4dd739b00d" -uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" -version = "0.9.3" - -[[deps.Downloads]] -deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"] -uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" -version = "1.6.0" - -[[deps.EarCut_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "e3290f2d49e661fbd94046d7e3726ffcb2d41053" -uuid = "5ae413db-bbd1-5e63-b57d-d24a61df00f5" -version = "2.2.4+0" - -[[deps.EpollShim_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "8e9441ee83492030ace98f9789a654a6d0b1f643" -uuid = "2702e6a9-849d-5ed8-8c21-79e8b8f9ee43" -version = "0.0.20230411+0" - -[[deps.Expat_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "1c6317308b9dc757616f0b5cb379db10494443a7" -uuid = "2e619515-83b5-522b-bb60-26c02a35a201" -version = "2.6.2+0" - -[[deps.Extents]] -git-tree-sha1 = "94997910aca72897524d2237c41eb852153b0f65" -uuid = "411431e0-e8b7-467b-b5e0-f676ba4f2910" -version = "0.1.3" - -[[deps.FFMPEG]] -deps = ["FFMPEG_jll"] -git-tree-sha1 = "b57e3acbe22f8484b4b5ff66a7499717fe1a9cc8" -uuid = "c87230d0-a227-11e9-1b43-d7ebe4e7570a" -version = "0.4.1" - -[[deps.FFMPEG_jll]] -deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "JLLWrappers", "LAME_jll", "Libdl", "Ogg_jll", "OpenSSL_jll", "Opus_jll", "PCRE2_jll", "Pkg", "Zlib_jll", "libaom_jll", "libass_jll", "libfdk_aac_jll", "libvorbis_jll", "x264_jll", "x265_jll"] -git-tree-sha1 = "74faea50c1d007c85837327f6775bea60b5492dd" -uuid = "b22a6f82-2f65-5046-a5b2-351ab43fb4e5" -version = "4.4.2+2" - -[[deps.FileWatching]] -uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" - -[[deps.FixedPointNumbers]] -deps = ["Statistics"] -git-tree-sha1 = "05882d6995ae5c12bb5f36dd2ed3f61c98cbb172" -uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" -version = "0.8.5" - -[[deps.Fontconfig_jll]] -deps = ["Artifacts", "Bzip2_jll", "Expat_jll", "FreeType2_jll", "JLLWrappers", "Libdl", "Libuuid_jll", "Zlib_jll"] -git-tree-sha1 = "db16beca600632c95fc8aca29890d83788dd8b23" -uuid = "a3f928ae-7b40-5064-980b-68af3947d34b" -version = "2.13.96+0" - -[[deps.Formatting]] -deps = ["Logging", "Printf"] -git-tree-sha1 = "fb409abab2caf118986fc597ba84b50cbaf00b87" -uuid = "59287772-0a20-5a39-b81b-1366585eb4c0" -version = "0.4.3" - -[[deps.FreeType2_jll]] -deps = ["Artifacts", "Bzip2_jll", "JLLWrappers", "Libdl", "Zlib_jll"] -git-tree-sha1 = "5c1d8ae0efc6c2e7b1fc502cbe25def8f661b7bc" -uuid = "d7e528f0-a631-5988-bf34-fe36492bcfd7" -version = "2.13.2+0" - -[[deps.FriBidi_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "1ed150b39aebcc805c26b93a8d0122c940f64ce2" -uuid = "559328eb-81f9-559d-9380-de523a88c83c" -version = "1.0.14+0" - -[[deps.GLFW_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Libglvnd_jll", "Xorg_libXcursor_jll", "Xorg_libXi_jll", "Xorg_libXinerama_jll", "Xorg_libXrandr_jll", "xkbcommon_jll"] -git-tree-sha1 = "3f74912a156096bd8fdbef211eff66ab446e7297" -uuid = "0656b61e-2033-5cc2-a64a-77c0f6c09b89" -version = "3.4.0+0" - -[[deps.GR]] -deps = ["Base64", "DelimitedFiles", "GR_jll", "HTTP", "JSON", "Libdl", "LinearAlgebra", "Pkg", "Printf", "Random", "RelocatableFolders", "Serialization", "Sockets", "Test", "UUIDs"] -git-tree-sha1 = "c98aea696662d09e215ef7cda5296024a9646c75" -uuid = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71" -version = "0.64.4" - -[[deps.GR_jll]] -deps = ["Artifacts", "Bzip2_jll", "Cairo_jll", "FFMPEG_jll", "Fontconfig_jll", "GLFW_jll", "JLLWrappers", "JpegTurbo_jll", "Libdl", "Libtiff_jll", "Pixman_jll", "Pkg", "Qt5Base_jll", "Zlib_jll", "libpng_jll"] -git-tree-sha1 = "bc9f7725571ddb4ab2c4bc74fa397c1c5ad08943" -uuid = "d2c73de3-f751-5644-a686-071e5b155ba9" -version = "0.69.1+0" - -[[deps.GeoInterface]] -deps = ["Extents"] -git-tree-sha1 = "9fff8990361d5127b770e3454488360443019bb3" -uuid = "cf35fbd7-0cd7-5166-be24-54bfbe79505f" -version = "1.3.5" - -[[deps.GeometryBasics]] -deps = ["EarCut_jll", "Extents", "GeoInterface", "IterTools", "LinearAlgebra", "StaticArrays", "StructArrays", "Tables"] -git-tree-sha1 = "b62f2b2d76cee0d61a2ef2b3118cd2a3215d3134" -uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326" -version = "0.4.11" - -[[deps.Gettext_jll]] -deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "XML2_jll"] -git-tree-sha1 = "9b02998aba7bf074d14de89f9d37ca24a1a0b046" -uuid = "78b55507-aeef-58d4-861c-77aaff3498b1" -version = "0.21.0+0" - -[[deps.Glib_jll]] -deps = ["Artifacts", "Gettext_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Libiconv_jll", "Libmount_jll", "PCRE2_jll", "Zlib_jll"] -git-tree-sha1 = "7c82e6a6cd34e9d935e9aa4051b66c6ff3af59ba" -uuid = "7746bdde-850d-59dc-9ae8-88ece973131d" -version = "2.80.2+0" - -[[deps.Graphite2_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "344bf40dcab1073aca04aa0df4fb092f920e4011" -uuid = "3b182d85-2403-5c21-9c21-1e1f0cc25472" -version = "1.3.14+0" - -[[deps.Grisu]] -git-tree-sha1 = "53bb909d1151e57e2484c3d1b53e19552b887fb2" -uuid = "42e2da0e-8278-4e71-bc24-59509adca0fe" -version = "1.0.2" - -[[deps.HTTP]] -deps = ["Base64", "Dates", "IniFile", "Logging", "MbedTLS", "NetworkOptions", "Sockets", "URIs"] -git-tree-sha1 = "0fa77022fe4b511826b39c894c90daf5fce3334a" -uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3" -version = "0.9.17" - -[[deps.HarfBuzz_jll]] -deps = ["Artifacts", "Cairo_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "Graphite2_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Pkg"] -git-tree-sha1 = "129acf094d168394e80ee1dc4bc06ec835e510a3" -uuid = "2e76f6c2-a576-52d4-95c1-20adfe4de566" -version = "2.8.1+1" - -[[deps.Hyperscript]] -deps = ["Test"] -git-tree-sha1 = "179267cfa5e712760cd43dcae385d7ea90cc25a4" -uuid = "47d2ed2b-36de-50cf-bf87-49c2cf4b8b91" -version = "0.0.5" - -[[deps.HypertextLiteral]] -deps = ["Tricks"] -git-tree-sha1 = "7134810b1afce04bbc1045ca1985fbe81ce17653" -uuid = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2" -version = "0.9.5" - -[[deps.IOCapture]] -deps = ["Logging", "Random"] -git-tree-sha1 = "b6d6bfdd7ce25b0f9b2f6b3dd56b2673a66c8770" -uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89" -version = "0.2.5" - -[[deps.IniFile]] -git-tree-sha1 = "f550e6e32074c939295eb5ea6de31849ac2c9625" -uuid = "83e8ac13-25f8-5344-8a64-a9f2b223428f" -version = "0.5.1" - -[[deps.InteractiveUtils]] -deps = ["Markdown"] -uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" - -[[deps.IrrationalConstants]] -git-tree-sha1 = "630b497eafcc20001bba38a4651b327dcfc491d2" -uuid = "92d709cd-6900-40b7-9082-c6be49f344b6" -version = "0.2.2" - -[[deps.IterTools]] -git-tree-sha1 = "42d5f897009e7ff2cf88db414a389e5ed1bdd023" -uuid = "c8e1da08-722c-5040-9ed9-7db0dc04731e" -version = "1.10.0" - -[[deps.IteratorInterfaceExtensions]] -git-tree-sha1 = "a3f24677c21f5bbe9d2a714f95dcd58337fb2856" -uuid = "82899510-4779-5014-852e-03e436cf321d" -version = "1.0.0" - -[[deps.JLLWrappers]] -deps = ["Artifacts", "Preferences"] -git-tree-sha1 = "7e5d6779a1e09a36db2a7b6cff50942a0a7d0fca" -uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" -version = "1.5.0" - -[[deps.JSON]] -deps = ["Dates", "Mmap", "Parsers", "Unicode"] -git-tree-sha1 = "31e996f0a15c7b280ba9f76636b3ff9e2ae58c9a" -uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" -version = "0.21.4" - -[[deps.JpegTurbo_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "c84a835e1a09b289ffcd2271bf2a337bbdda6637" -uuid = "aacddb02-875f-59d6-b918-886e6ef4fbf8" -version = "3.0.3+0" - -[[deps.LAME_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "170b660facf5df5de098d866564877e119141cbd" -uuid = "c1c5ebd0-6772-5130-a774-d5fcae4a789d" -version = "3.100.2+0" - -[[deps.LERC_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "bf36f528eec6634efc60d7ec062008f171071434" -uuid = "88015f11-f218-50d7-93a8-a6af411a945d" -version = "3.0.0+1" - -[[deps.LLVMOpenMP_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "d986ce2d884d49126836ea94ed5bfb0f12679713" -uuid = "1d63c593-3942-5779-bab2-d838dc0a180e" -version = "15.0.7+0" - -[[deps.LZO_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "70c5da094887fd2cae843b8db33920bac4b6f07d" -uuid = "dd4b983a-f0e5-5f8d-a1b7-129d4a5fb1ac" -version = "2.10.2+0" - -[[deps.LaTeXStrings]] -git-tree-sha1 = "50901ebc375ed41dbf8058da26f9de442febbbec" -uuid = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" -version = "1.3.1" - -[[deps.Latexify]] -deps = ["Formatting", "InteractiveUtils", "LaTeXStrings", "MacroTools", "Markdown", "OrderedCollections", "Printf", "Requires"] -git-tree-sha1 = "8c57307b5d9bb3be1ff2da469063628631d4d51e" -uuid = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" -version = "0.15.21" - - [deps.Latexify.extensions] - DataFramesExt = "DataFrames" - DiffEqBiologicalExt = "DiffEqBiological" - ParameterizedFunctionsExt = "DiffEqBase" - SymEngineExt = "SymEngine" - - [deps.Latexify.weakdeps] - DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" - DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" - DiffEqBiological = "eb300fae-53e8-50a0-950c-e21f52c2b7e0" - SymEngine = "123dc426-2d89-5057-bbad-38513e3affd8" - -[[deps.LibCURL]] -deps = ["LibCURL_jll", "MozillaCACerts_jll"] -uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" -version = "0.6.4" - -[[deps.LibCURL_jll]] -deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] -uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" -version = "8.4.0+0" - -[[deps.LibGit2]] -deps = ["Base64", "LibGit2_jll", "NetworkOptions", "Printf", "SHA"] -uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" - -[[deps.LibGit2_jll]] -deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll"] -uuid = "e37daf67-58a4-590a-8e99-b0245dd2ffc5" -version = "1.6.4+0" - -[[deps.LibSSH2_jll]] -deps = ["Artifacts", "Libdl", "MbedTLS_jll"] -uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" -version = "1.11.0+1" - -[[deps.Libdl]] -uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" - -[[deps.Libffi_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "0b4a5d71f3e5200a7dff793393e09dfc2d874290" -uuid = "e9f186c6-92d2-5b65-8a66-fee21dc1b490" -version = "3.2.2+1" - -[[deps.Libgcrypt_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgpg_error_jll"] -git-tree-sha1 = "9fd170c4bbfd8b935fdc5f8b7aa33532c991a673" -uuid = "d4300ac3-e22c-5743-9152-c294e39db1e4" -version = "1.8.11+0" - -[[deps.Libglvnd_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll", "Xorg_libXext_jll"] -git-tree-sha1 = "6f73d1dd803986947b2c750138528a999a6c7733" -uuid = "7e76a0d4-f3c7-5321-8279-8d96eeed0f29" -version = "1.6.0+0" - -[[deps.Libgpg_error_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "fbb1f2bef882392312feb1ede3615ddc1e9b99ed" -uuid = "7add5ba3-2f88-524e-9cd5-f83b8a55f7b8" -version = "1.49.0+0" - -[[deps.Libiconv_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "f9557a255370125b405568f9767d6d195822a175" -uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531" -version = "1.17.0+0" - -[[deps.Libmount_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "0c4f9c4f1a50d8f35048fa0532dabbadf702f81e" -uuid = "4b2f31a3-9ecc-558c-b454-b3730dcb73e9" -version = "2.40.1+0" - -[[deps.Libtiff_jll]] -deps = ["Artifacts", "JLLWrappers", "JpegTurbo_jll", "LERC_jll", "Libdl", "Pkg", "Zlib_jll", "Zstd_jll"] -git-tree-sha1 = "3eb79b0ca5764d4799c06699573fd8f533259713" -uuid = "89763e89-9b03-5906-acba-b20f662cd828" -version = "4.4.0+0" - -[[deps.Libuuid_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "5ee6203157c120d79034c748a2acba45b82b8807" -uuid = "38a345b3-de98-5d2b-a5d3-14cd9215e700" -version = "2.40.1+0" - -[[deps.LinearAlgebra]] -deps = ["Libdl", "OpenBLAS_jll", "libblastrampoline_jll"] -uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" - -[[deps.LogExpFunctions]] -deps = ["DocStringExtensions", "IrrationalConstants", "LinearAlgebra"] -git-tree-sha1 = "a2d09619db4e765091ee5c6ffe8872849de0feea" -uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688" -version = "0.3.28" - - [deps.LogExpFunctions.extensions] - LogExpFunctionsChainRulesCoreExt = "ChainRulesCore" - LogExpFunctionsChangesOfVariablesExt = "ChangesOfVariables" - LogExpFunctionsInverseFunctionsExt = "InverseFunctions" - - [deps.LogExpFunctions.weakdeps] - ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" - ChangesOfVariables = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0" - InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" - -[[deps.Logging]] -uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" - -[[deps.MIMEs]] -git-tree-sha1 = "65f28ad4b594aebe22157d6fac869786a255b7eb" -uuid = "6c6e2e6c-3030-632d-7369-2d6c69616d65" -version = "0.1.4" - -[[deps.MacroTools]] -deps = ["Markdown", "Random"] -git-tree-sha1 = "2fa9ee3e63fd3a4f7a9a4f4744a52f4856de82df" -uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" -version = "0.5.13" - -[[deps.Markdown]] -deps = ["Base64"] -uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" - -[[deps.MbedTLS]] -deps = ["Dates", "MbedTLS_jll", "MozillaCACerts_jll", "NetworkOptions", "Random", "Sockets"] -git-tree-sha1 = "c067a280ddc25f196b5e7df3877c6b226d390aaf" -uuid = "739be429-bea8-5141-9913-cc70e7f3736d" -version = "1.1.9" - -[[deps.MbedTLS_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" -version = "2.28.2+1" - -[[deps.Measures]] -git-tree-sha1 = "c13304c81eec1ed3af7fc20e75fb6b26092a1102" -uuid = "442fdcdd-2543-5da2-b0f3-8c86c306513e" -version = "0.3.2" - -[[deps.Missings]] -deps = ["DataAPI"] -git-tree-sha1 = "ec4f7fbeab05d7747bdf98eb74d130a2a2ed298d" -uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28" -version = "1.2.0" - -[[deps.Mmap]] -uuid = "a63ad114-7e13-5084-954f-fe012c677804" - -[[deps.MozillaCACerts_jll]] -uuid = "14a3606d-f60d-562e-9121-12d972cd8159" -version = "2023.1.10" - -[[deps.NaNMath]] -deps = ["OpenLibm_jll"] -git-tree-sha1 = "0877504529a3e5c3343c6f8b4c0381e57e4387e4" -uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" -version = "1.0.2" - -[[deps.NetworkOptions]] -uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" -version = "1.2.0" - -[[deps.Ogg_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "887579a3eb005446d514ab7aeac5d1d027658b8f" -uuid = "e7412a2a-1a6e-54c0-be00-318e2571c051" -version = "1.3.5+1" - -[[deps.OpenBLAS_jll]] -deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] -uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" -version = "0.3.23+4" - -[[deps.OpenLibm_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "05823500-19ac-5b8b-9628-191a04bc5112" -version = "0.8.1+2" - -[[deps.OpenSSL_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "a12e56c72edee3ce6b96667745e6cbbe5498f200" -uuid = "458c3c95-2e84-50aa-8efc-19380b2a3a95" -version = "1.1.23+0" - -[[deps.Opus_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "51a08fb14ec28da2ec7a927c4337e4332c2a4720" -uuid = "91d4177d-7536-5919-b921-800302f37372" -version = "1.3.2+0" - -[[deps.OrderedCollections]] -git-tree-sha1 = "dfdf5519f235516220579f949664f1bf44e741c5" -uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" -version = "1.6.3" - -[[deps.PCRE2_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "efcefdf7-47ab-520b-bdef-62a2eaa19f15" -version = "10.42.0+1" - -[[deps.Parsers]] -deps = ["Dates", "PrecompileTools", "UUIDs"] -git-tree-sha1 = "8489905bcdbcfac64d1daa51ca07c0d8f0283821" -uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" -version = "2.8.1" - -[[deps.Pixman_jll]] -deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "LLVMOpenMP_jll", "Libdl"] -git-tree-sha1 = "35621f10a7531bc8fa58f74610b1bfb70a3cfc6b" -uuid = "30392449-352a-5448-841d-b1acce4e97dc" -version = "0.43.4+0" - -[[deps.Pkg]] -deps = ["Artifacts", "Dates", "Downloads", "FileWatching", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] -uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" -version = "1.10.0" - -[[deps.PlotThemes]] -deps = ["PlotUtils", "Statistics"] -git-tree-sha1 = "6e55c6841ce3411ccb3457ee52fc48cb698d6fb0" -uuid = "ccf2f8ad-2431-5c83-bf29-c5338b663b6a" -version = "3.2.0" - -[[deps.PlotUtils]] -deps = ["ColorSchemes", "Colors", "Dates", "PrecompileTools", "Printf", "Random", "Reexport", "Statistics"] -git-tree-sha1 = "7b1a9df27f072ac4c9c7cbe5efb198489258d1f5" -uuid = "995b91a9-d308-5afd-9ec6-746e21dbc043" -version = "1.4.1" - -[[deps.Plots]] -deps = ["Base64", "Contour", "Dates", "Downloads", "FFMPEG", "FixedPointNumbers", "GR", "GeometryBasics", "JSON", "Latexify", "LinearAlgebra", "Measures", "NaNMath", "Pkg", "PlotThemes", "PlotUtils", "Printf", "REPL", "Random", "RecipesBase", "RecipesPipeline", "Reexport", "Requires", "Scratch", "Showoff", "SparseArrays", "Statistics", "StatsBase", "UUIDs", "UnicodeFun", "Unzip"] -git-tree-sha1 = "9e42de869561d6bdf8602c57ec557d43538a92f0" -uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" -version = "1.29.1" - -[[deps.PlutoUI]] -deps = ["AbstractPlutoDingetjes", "Base64", "ColorTypes", "Dates", "FixedPointNumbers", "Hyperscript", "HypertextLiteral", "IOCapture", "InteractiveUtils", "JSON", "Logging", "MIMEs", "Markdown", "Random", "Reexport", "URIs", "UUIDs"] -git-tree-sha1 = "ab55ee1510ad2af0ff674dbcced5e94921f867a9" -uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8" -version = "0.7.59" - -[[deps.PrecompileTools]] -deps = ["Preferences"] -git-tree-sha1 = "5aa36f7049a63a1528fe8f7c3f2113413ffd4e1f" -uuid = "aea7be01-6a6a-4083-8856-8a6e6704d82a" -version = "1.2.1" - -[[deps.Preferences]] -deps = ["TOML"] -git-tree-sha1 = "9306f6085165d270f7e3db02af26a400d580f5c6" -uuid = "21216c6a-2e73-6563-6e65-726566657250" -version = "1.4.3" - -[[deps.Printf]] -deps = ["Unicode"] -uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" - -[[deps.Profile]] -deps = ["Printf"] -uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79" - -[[deps.Qt5Base_jll]] -deps = ["Artifacts", "CompilerSupportLibraries_jll", "Fontconfig_jll", "Glib_jll", "JLLWrappers", "Libdl", "Libglvnd_jll", "OpenSSL_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libxcb_jll", "Xorg_xcb_util_image_jll", "Xorg_xcb_util_keysyms_jll", "Xorg_xcb_util_renderutil_jll", "Xorg_xcb_util_wm_jll", "Zlib_jll", "xkbcommon_jll"] -git-tree-sha1 = "0c03844e2231e12fda4d0086fd7cbe4098ee8dc5" -uuid = "ea2cea3b-5b76-57ae-a6ef-0a8af62496e1" -version = "5.15.3+2" - -[[deps.REPL]] -deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] -uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" - -[[deps.Random]] -deps = ["SHA"] -uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" - -[[deps.RecipesBase]] -deps = ["PrecompileTools"] -git-tree-sha1 = "5c3d09cc4f31f5fc6af001c250bf1278733100ff" -uuid = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" -version = "1.3.4" - -[[deps.RecipesPipeline]] -deps = ["Dates", "NaNMath", "PlotUtils", "RecipesBase"] -git-tree-sha1 = "dc1e451e15d90347a7decc4221842a022b011714" -uuid = "01d81517-befc-4cb6-b9ec-a95719d0359c" -version = "0.5.2" - -[[deps.Reexport]] -git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b" -uuid = "189a3867-3050-52da-a836-e630ba90ab69" -version = "1.2.2" - -[[deps.RelocatableFolders]] -deps = ["SHA", "Scratch"] -git-tree-sha1 = "cdbd3b1338c72ce29d9584fdbe9e9b70eeb5adca" -uuid = "05181044-ff0b-4ac5-8273-598c1e38db00" -version = "0.1.3" - -[[deps.Requires]] -deps = ["UUIDs"] -git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7" -uuid = "ae029012-a4dd-5104-9daa-d747884805df" -version = "1.3.0" - -[[deps.SHA]] -uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" -version = "0.7.0" - -[[deps.Scratch]] -deps = ["Dates"] -git-tree-sha1 = "3bac05bc7e74a75fd9cba4295cde4045d9fe2386" -uuid = "6c6a2e73-6563-6170-7368-637461726353" -version = "1.2.1" - -[[deps.Serialization]] -uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" - -[[deps.Showoff]] -deps = ["Dates", "Grisu"] -git-tree-sha1 = "91eddf657aca81df9ae6ceb20b959ae5653ad1de" -uuid = "992d4aef-0814-514b-bc4d-f2e9a6c4116f" -version = "1.0.3" - -[[deps.Sockets]] -uuid = "6462fe0b-24de-5631-8697-dd941f90decc" - -[[deps.SortingAlgorithms]] -deps = ["DataStructures"] -git-tree-sha1 = "66e0a8e672a0bdfca2c3f5937efb8538b9ddc085" -uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c" -version = "1.2.1" - -[[deps.SparseArrays]] -deps = ["Libdl", "LinearAlgebra", "Random", "Serialization", "SuiteSparse_jll"] -uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" -version = "1.10.0" - -[[deps.StaticArrays]] -deps = ["LinearAlgebra", "PrecompileTools", "Random", "StaticArraysCore"] -git-tree-sha1 = "eeafab08ae20c62c44c8399ccb9354a04b80db50" -uuid = "90137ffa-7385-5640-81b9-e52037218182" -version = "1.9.7" - - [deps.StaticArrays.extensions] - StaticArraysChainRulesCoreExt = "ChainRulesCore" - StaticArraysStatisticsExt = "Statistics" - - [deps.StaticArrays.weakdeps] - ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" - Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" - -[[deps.StaticArraysCore]] -git-tree-sha1 = "192954ef1208c7019899fbf8049e717f92959682" -uuid = "1e83bf80-4336-4d27-bf5d-d5a4f845583c" -version = "1.4.3" - -[[deps.Statistics]] -deps = ["LinearAlgebra", "SparseArrays"] -uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" -version = "1.10.0" - -[[deps.StatsAPI]] -deps = ["LinearAlgebra"] -git-tree-sha1 = "1ff449ad350c9c4cbc756624d6f8a8c3ef56d3ed" -uuid = "82ae8749-77ed-4fe6-ae5f-f523153014b0" -version = "1.7.0" - -[[deps.StatsBase]] -deps = ["DataAPI", "DataStructures", "LinearAlgebra", "LogExpFunctions", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics", "StatsAPI"] -git-tree-sha1 = "d1bf48bfcc554a3761a133fe3a9bb01488e06916" -uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" -version = "0.33.21" - -[[deps.StructArrays]] -deps = ["ConstructionBase", "DataAPI", "Tables"] -git-tree-sha1 = "f4dc295e983502292c4c3f951dbb4e985e35b3be" -uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a" -version = "0.6.18" - - [deps.StructArrays.extensions] - StructArraysAdaptExt = "Adapt" - StructArraysGPUArraysCoreExt = "GPUArraysCore" - StructArraysSparseArraysExt = "SparseArrays" - StructArraysStaticArraysExt = "StaticArrays" - - [deps.StructArrays.weakdeps] - Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" - GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527" - SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" - StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" - -[[deps.SuiteSparse_jll]] -deps = ["Artifacts", "Libdl", "libblastrampoline_jll"] -uuid = "bea87d4a-7f5b-5778-9afe-8cc45184846c" -version = "7.2.1+1" - -[[deps.TOML]] -deps = ["Dates"] -uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" -version = "1.0.3" - -[[deps.TableTraits]] -deps = ["IteratorInterfaceExtensions"] -git-tree-sha1 = "c06b2f539df1c6efa794486abfb6ed2022561a39" -uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c" -version = "1.0.1" - -[[deps.Tables]] -deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "OrderedCollections", "TableTraits"] -git-tree-sha1 = "598cd7c1f68d1e205689b1c2fe65a9f85846f297" -uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" -version = "1.12.0" - -[[deps.Tar]] -deps = ["ArgTools", "SHA"] -uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" -version = "1.10.0" - -[[deps.TensorCore]] -deps = ["LinearAlgebra"] -git-tree-sha1 = "1feb45f88d133a655e001435632f019a9a1bcdb6" -uuid = "62fd8b95-f654-4bbd-a8a5-9c27f68ccd50" -version = "0.1.1" - -[[deps.Test]] -deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] -uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[[deps.Tricks]] -git-tree-sha1 = "eae1bb484cd63b36999ee58be2de6c178105112f" -uuid = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775" -version = "0.1.8" - -[[deps.URIs]] -git-tree-sha1 = "67db6cc7b3821e19ebe75791a9dd19c9b1188f2b" -uuid = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4" -version = "1.5.1" - -[[deps.UUIDs]] -deps = ["Random", "SHA"] -uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" - -[[deps.Unicode]] -uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" - -[[deps.UnicodeFun]] -deps = ["REPL"] -git-tree-sha1 = "53915e50200959667e78a92a418594b428dffddf" -uuid = "1cfade01-22cf-5700-b092-accc4b62d6e1" -version = "0.4.1" - -[[deps.Unzip]] -git-tree-sha1 = "34db80951901073501137bdbc3d5a8e7bbd06670" -uuid = "41fe7b60-77ed-43a1-b4f0-825fd5a5650d" -version = "0.1.2" - -[[deps.Wayland_jll]] -deps = ["Artifacts", "EpollShim_jll", "Expat_jll", "JLLWrappers", "Libdl", "Libffi_jll", "Pkg", "XML2_jll"] -git-tree-sha1 = "7558e29847e99bc3f04d6569e82d0f5c54460703" -uuid = "a2964d1f-97da-50d4-b82a-358c7fce9d89" -version = "1.21.0+1" - -[[deps.Wayland_protocols_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "93f43ab61b16ddfb2fd3bb13b3ce241cafb0e6c9" -uuid = "2381bf8a-dfd0-557d-9999-79630e7b1b91" -version = "1.31.0+0" - -[[deps.XML2_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Zlib_jll"] -git-tree-sha1 = "d9717ce3518dc68a99e6b96300813760d887a01d" -uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a" -version = "2.13.1+0" - -[[deps.XSLT_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Libgcrypt_jll", "Libgpg_error_jll", "Libiconv_jll", "XML2_jll", "Zlib_jll"] -git-tree-sha1 = "a54ee957f4c86b526460a720dbc882fa5edcbefc" -uuid = "aed1982a-8fda-507f-9586-7b0439959a61" -version = "1.1.41+0" - -[[deps.Xorg_libX11_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Xorg_libxcb_jll", "Xorg_xtrans_jll"] -git-tree-sha1 = "afead5aba5aa507ad5a3bf01f58f82c8d1403495" -uuid = "4f6342f7-b3d2-589e-9d20-edeb45f2b2bc" -version = "1.8.6+0" - -[[deps.Xorg_libXau_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "6035850dcc70518ca32f012e46015b9beeda49d8" -uuid = "0c0b7dd1-d40b-584c-a123-a41640f87eec" -version = "1.0.11+0" - -[[deps.Xorg_libXcursor_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXfixes_jll", "Xorg_libXrender_jll"] -git-tree-sha1 = "12e0eb3bc634fa2080c1c37fccf56f7c22989afd" -uuid = "935fb764-8cf2-53bf-bb30-45bb1f8bf724" -version = "1.2.0+4" - -[[deps.Xorg_libXdmcp_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "34d526d318358a859d7de23da945578e8e8727b7" -uuid = "a3789734-cfe1-5b06-b2d0-1dd0d9d62d05" -version = "1.1.4+0" - -[[deps.Xorg_libXext_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Xorg_libX11_jll"] -git-tree-sha1 = "d2d1a5c49fae4ba39983f63de6afcbea47194e85" -uuid = "1082639a-0dae-5f34-9b06-72781eeb8cb3" -version = "1.3.6+0" - -[[deps.Xorg_libXfixes_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libX11_jll"] -git-tree-sha1 = "0e0dc7431e7a0587559f9294aeec269471c991a4" -uuid = "d091e8ba-531a-589c-9de9-94069b037ed8" -version = "5.0.3+4" - -[[deps.Xorg_libXi_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXext_jll", "Xorg_libXfixes_jll"] -git-tree-sha1 = "89b52bc2160aadc84d707093930ef0bffa641246" -uuid = "a51aa0fd-4e3c-5386-b890-e753decda492" -version = "1.7.10+4" - -[[deps.Xorg_libXinerama_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXext_jll"] -git-tree-sha1 = "26be8b1c342929259317d8b9f7b53bf2bb73b123" -uuid = "d1454406-59df-5ea1-beac-c340f2130bc3" -version = "1.1.4+4" - -[[deps.Xorg_libXrandr_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll"] -git-tree-sha1 = "34cea83cb726fb58f325887bf0612c6b3fb17631" -uuid = "ec84b674-ba8e-5d96-8ba1-2a689ba10484" -version = "1.5.2+4" - -[[deps.Xorg_libXrender_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Xorg_libX11_jll"] -git-tree-sha1 = "47e45cd78224c53109495b3e324df0c37bb61fbe" -uuid = "ea2f1a96-1ddc-540d-b46f-429655e07cfa" -version = "0.9.11+0" - -[[deps.Xorg_libpthread_stubs_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "8fdda4c692503d44d04a0603d9ac0982054635f9" -uuid = "14d82f49-176c-5ed1-bb49-ad3f5cbd8c74" -version = "0.1.1+0" - -[[deps.Xorg_libxcb_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "XSLT_jll", "Xorg_libXau_jll", "Xorg_libXdmcp_jll", "Xorg_libpthread_stubs_jll"] -git-tree-sha1 = "bcd466676fef0878338c61e655629fa7bbc69d8e" -uuid = "c7cfdc94-dc32-55de-ac96-5a1b8d977c5b" -version = "1.17.0+0" - -[[deps.Xorg_libxkbfile_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Xorg_libX11_jll"] -git-tree-sha1 = "730eeca102434283c50ccf7d1ecdadf521a765a4" -uuid = "cc61e674-0454-545c-8b26-ed2c68acab7a" -version = "1.1.2+0" - -[[deps.Xorg_xcb_util_image_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"] -git-tree-sha1 = "0fab0a40349ba1cba2c1da699243396ff8e94b97" -uuid = "12413925-8142-5f55-bb0e-6d7ca50bb09b" -version = "0.4.0+1" - -[[deps.Xorg_xcb_util_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_libxcb_jll"] -git-tree-sha1 = "e7fd7b2881fa2eaa72717420894d3938177862d1" -uuid = "2def613f-5ad1-5310-b15b-b15d46f528f5" -version = "0.4.0+1" - -[[deps.Xorg_xcb_util_keysyms_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"] -git-tree-sha1 = "d1151e2c45a544f32441a567d1690e701ec89b00" -uuid = "975044d2-76e6-5fbe-bf08-97ce7c6574c7" -version = "0.4.0+1" - -[[deps.Xorg_xcb_util_renderutil_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"] -git-tree-sha1 = "dfd7a8f38d4613b6a575253b3174dd991ca6183e" -uuid = "0d47668e-0667-5a69-a72c-f761630bfb7e" -version = "0.3.9+1" - -[[deps.Xorg_xcb_util_wm_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Xorg_xcb_util_jll"] -git-tree-sha1 = "e78d10aab01a4a154142c5006ed44fd9e8e31b67" -uuid = "c22f9ab0-d5fe-5066-847c-f4bb1cd4e361" -version = "0.4.1+1" - -[[deps.Xorg_xkbcomp_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Xorg_libxkbfile_jll"] -git-tree-sha1 = "330f955bc41bb8f5270a369c473fc4a5a4e4d3cb" -uuid = "35661453-b289-5fab-8a00-3d9160c6a3a4" -version = "1.4.6+0" - -[[deps.Xorg_xkeyboard_config_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Xorg_xkbcomp_jll"] -git-tree-sha1 = "691634e5453ad362044e2ad653e79f3ee3bb98c3" -uuid = "33bec58e-1273-512f-9401-5d533626f822" -version = "2.39.0+0" - -[[deps.Xorg_xtrans_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "e92a1a012a10506618f10b7047e478403a046c77" -uuid = "c5fb5394-a638-5e4d-96e5-b29de1b5cf10" -version = "1.5.0+0" - -[[deps.Zlib_jll]] -deps = ["Libdl"] -uuid = "83775a58-1f1d-513f-b197-d71354ab007a" -version = "1.2.13+1" - -[[deps.Zstd_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "e678132f07ddb5bfa46857f0d7620fb9be675d3b" -uuid = "3161d3a3-bdf6-5164-811a-617609db77b4" -version = "1.5.6+0" - -[[deps.libaom_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl"] -git-tree-sha1 = "1827acba325fdcdf1d2647fc8d5301dd9ba43a9d" -uuid = "a4ae2306-e953-59d6-aa16-d00cac43593b" -version = "3.9.0+0" - -[[deps.libass_jll]] -deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "HarfBuzz_jll", "JLLWrappers", "Libdl", "Pkg", "Zlib_jll"] -git-tree-sha1 = "5982a94fcba20f02f42ace44b9894ee2b140fe47" -uuid = "0ac62f75-1d6f-5e53-bd7c-93b484bb37c0" -version = "0.15.1+0" - -[[deps.libblastrampoline_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" -version = "5.8.0+1" - -[[deps.libfdk_aac_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "daacc84a041563f965be61859a36e17c4e4fcd55" -uuid = "f638f0a6-7fb0-5443-88ba-1cc74229b280" -version = "2.0.2+0" - -[[deps.libpng_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Zlib_jll"] -git-tree-sha1 = "d7015d2e18a5fd9a4f47de711837e980519781a4" -uuid = "b53b4c65-9356-5827-b1ea-8c7a1a84506f" -version = "1.6.43+1" - -[[deps.libvorbis_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Ogg_jll", "Pkg"] -git-tree-sha1 = "b910cb81ef3fe6e78bf6acee440bda86fd6ae00c" -uuid = "f27f6e37-5d2b-51aa-960f-b287f2bc3b7a" -version = "1.3.7+1" - -[[deps.nghttp2_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" -version = "1.52.0+1" - -[[deps.p7zip_jll]] -deps = ["Artifacts", "Libdl"] -uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" -version = "17.4.0+2" - -[[deps.x264_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "4fea590b89e6ec504593146bf8b988b2c00922b2" -uuid = "1270edf5-f2f9-52d2-97e9-ab00b5d0237a" -version = "2021.5.5+0" - -[[deps.x265_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] -git-tree-sha1 = "ee567a171cce03570d77ad3a43e90218e38937a9" -uuid = "dfaa095f-4041-5dcd-9319-2fabd8486b76" -version = "3.5.0+0" - -[[deps.xkbcommon_jll]] -deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "Wayland_jll", "Wayland_protocols_jll", "Xorg_libxcb_jll", "Xorg_xkeyboard_config_jll"] -git-tree-sha1 = "9c304562909ab2bab0262639bd4f444d7bc2be37" -uuid = "d8fb68d0-12a3-5cfd-a85a-d49703b185fd" -version = "1.4.1+1" -""" - -# ╔═╡ Cell order: -# ╠═97e807b2-9237-11eb-31ef-6fe0d4cc94d3 -# ╠═5f0d7a44-91e0-11eb-10ae-d73156f965e6 -# ╟─9647147a-91ab-11eb-066f-9bc190368fb2 -# ╟─ff1aca1e-91e7-11eb-343e-0f89d9570b06 -# ╟─66a2f510-9232-11eb-3be9-131febc0039f -# ╟─bd3170e6-91ae-11eb-06f8-ebb6b2e7869f -# ╟─a304c842-91df-11eb-3fac-6dd63087f6de -# ╟─798507d6-91db-11eb-2e4a-3ba02f12ba65 -# ╟─3504168a-91de-11eb-181d-1d580d5dc071 -# ╟─4c8d8294-91db-11eb-353d-c3696c615b3d -# ╟─b62c4af8-9232-11eb-2f66-dd27dcb87d20 -# ╟─905379ce-91ad-11eb-295d-8354ecf5c5b1 -# ╟─5c4f0f26-91ad-11eb-033b-2bd221f0bdba -# ╟─da98b676-91e0-11eb-0d97-57b8a8aadf2a -# ╟─e0b607c0-91e0-11eb-10aa-53ec33570e59 -# ╟─fa1635d4-91e3-11eb-31bd-cf61c502ad35 -# ╠═f7f9e4c6-91e3-11eb-1a56-8b98f0b09b46 -# ╠═5da7b076-91b4-11eb-3eba-b3f5849efabb -# ╟─ea9e77e2-91b1-11eb-185d-cd006db11f60 -# ╟─12b4d528-9239-11eb-2824-8ddb5e2ba892 -# ╠═2f525796-9239-11eb-1865-9b01eadcf548 -# ╠═51abfe6e-9239-11eb-362a-259570250663 -# ╟─b847b5ca-9239-11eb-02fe-db4d9625bc5f -# ╟─c2deb090-9239-11eb-0739-a74379c15ce6 -# ╠═d420d492-91d9-11eb-056d-33cc8f0aed74 -# ╠═ad2d4dd8-91d5-11eb-27af-6f0c6e61a86a -# ╠═d0f81f28-91d9-11eb-2e79-61461ef5b132 -# ╠═b8f2c508-91d5-11eb-31b5-61810f171270 -# ╠═3c3971e2-91da-11eb-384c-01c627318bdc -# ╠═cb0ef266-91d5-11eb-314b-0545c0c817d0 -# ╠═048fac02-91da-11eb-0d26-4f258b4cd043 -# ╠═23b84ce2-91da-11eb-01f8-c308ac4d1c7a -# ╠═537f952a-91da-11eb-33cf-6be2fd3bc45c -# ╠═5b972296-91da-11eb-29b1-074f3926181e -# ╠═3ad5a93c-91db-11eb-3227-c96bf8fd2206 -# ╠═74182fe0-91da-11eb-219a-01f13b86406d -# ╟─57972a32-91e5-11eb-1d62-fbc22c494db9 -# ╟─63122dd0-91e5-11eb-34c3-b1b5c87809b8 -# ╟─bd013582-91e7-11eb-2d79-e18e45f6d639 -# ╟─c092ab72-91e7-11eb-2bd1-13c8c8bb30e4 -# ╠═cfb0f9ba-91e7-11eb-26b5-5f0f59d03cff -# ╟─d6edb326-91e7-11eb-03b1-d93e3bc83ca6 -# ╟─4ef42cec-91e8-11eb-2976-0950ffe5de6c -# ╠═ace8658e-91e8-11eb-0b9d-4b759635e417 -# ╟─b049ff58-91e8-11eb-203b-4f4b5ee5f01f -# ╠═b6775d3a-91e8-11eb-0187-618cb538d142 -# ╟─8b1441b6-91e4-11eb-16b2-d7eadd3fd69c -# ╟─d1315f94-91e4-11eb-1076-81156e24d2f1 -# ╟─97c958c2-91eb-11eb-17cb-410acc7f3e49 -# ╠═b7a98dba-91eb-11eb-3c78-074aa835b5fb -# ╟─f7f17c0c-91eb-11eb-3fa5-3bd90bb7044e -# ╟─f0aed302-91eb-11eb-13fb-d9418ef327a8 -# ╟─c1062e00-922b-11eb-1f31-ddbd03f8f986 -# ╠═547188ea-9233-11eb-1a89-5ff9468b31f7 -# ╟─2920abfe-91ec-11eb-19bc-935fa1ba0a96 -# ╠═3fadf88c-9236-11eb-19fa-d191ac5a6191 -# ╠═58653a70-9236-11eb-3dae-47adc2a77cb4 -# ╠═5d02f21e-9236-11eb-26ea-6593aa80a2eb -# ╠═863caf0a-9236-11eb-1013-ab7d83f3fc0c -# ╟─b803406e-9236-11eb-3aad-056b7f2c9b4b -# ╠═cc7aaeea-9236-11eb-3fad-2b5ad3962ec1 -# ╠═dabb5766-9236-11eb-3be9-9b33ba5af68a -# ╠═6cde6ef4-9236-11eb-219a-4d20adaf9988 -# ╠═7e8c1a2a-9236-11eb-20e9-57f6601f5472 -# ╟─00000000-0000-0000-0000-000000000001 -# ╟─00000000-0000-0000-0000-000000000002 diff --git a/Fall24/generated_assets/resistors_and_stencils_e788da9f.plutostate b/Fall24/generated_assets/resistors_and_stencils_4a5d83f8.plutostate similarity index 56% rename from Fall24/generated_assets/resistors_and_stencils_e788da9f.plutostate rename to Fall24/generated_assets/resistors_and_stencils_4a5d83f8.plutostate index ba4977b5..6862b095 100644 Binary files a/Fall24/generated_assets/resistors_and_stencils_e788da9f.plutostate and b/Fall24/generated_assets/resistors_and_stencils_4a5d83f8.plutostate differ diff --git a/Fall24/generated_assets/seamcarving_a93a0a52.plutostate b/Fall24/generated_assets/seamcarving_96c85dc0.plutostate similarity index 99% rename from Fall24/generated_assets/seamcarving_a93a0a52.plutostate rename to Fall24/generated_assets/seamcarving_96c85dc0.plutostate index bebb0bec..26b75c06 100644 Binary files a/Fall24/generated_assets/seamcarving_a93a0a52.plutostate and b/Fall24/generated_assets/seamcarving_96c85dc0.plutostate differ diff --git a/Fall24/generated_assets/simulating_component_failure_3c893b84.plutostate b/Fall24/generated_assets/simulating_component_failure_94051be7.plutostate similarity index 97% rename from Fall24/generated_assets/simulating_component_failure_3c893b84.plutostate rename to Fall24/generated_assets/simulating_component_failure_94051be7.plutostate index 311b911c..2744c395 100644 Binary files a/Fall24/generated_assets/simulating_component_failure_3c893b84.plutostate and b/Fall24/generated_assets/simulating_component_failure_94051be7.plutostate differ diff --git a/Fall24/generated_assets/structure_6638beac.plutostate b/Fall24/generated_assets/structure_07796d7c.plutostate similarity index 83% rename from Fall24/generated_assets/structure_6638beac.plutostate rename to Fall24/generated_assets/structure_07796d7c.plutostate index 46a4e6de..b7b3ffb9 100644 Binary files a/Fall24/generated_assets/structure_6638beac.plutostate and b/Fall24/generated_assets/structure_07796d7c.plutostate differ diff --git a/Fall24/generated_assets/time_stepping_2f3aee08.plutostate b/Fall24/generated_assets/time_stepping_c8f2a018.plutostate similarity index 94% rename from Fall24/generated_assets/time_stepping_2f3aee08.plutostate rename to Fall24/generated_assets/time_stepping_c8f2a018.plutostate index 55d11748..a811082d 100644 Binary files a/Fall24/generated_assets/time_stepping_2f3aee08.plutostate and b/Fall24/generated_assets/time_stepping_c8f2a018.plutostate differ diff --git a/Fall24/generated_assets/transformations2_d9045426.plutostate b/Fall24/generated_assets/transformations2_ee733e4b.plutostate similarity index 96% rename from Fall24/generated_assets/transformations2_d9045426.plutostate rename to Fall24/generated_assets/transformations2_ee733e4b.plutostate index 307d7117..384c7391 100644 Binary files a/Fall24/generated_assets/transformations2_d9045426.plutostate and b/Fall24/generated_assets/transformations2_ee733e4b.plutostate differ diff --git a/Fall24/generated_assets/transformations_and_autodiff_f24f5fff.plutostate b/Fall24/generated_assets/transformations_and_autodiff_92c3f85d.plutostate similarity index 85% rename from Fall24/generated_assets/transformations_and_autodiff_f24f5fff.plutostate rename to Fall24/generated_assets/transformations_and_autodiff_92c3f85d.plutostate index 6cc982a5..36c7c442 100644 Binary files a/Fall24/generated_assets/transformations_and_autodiff_f24f5fff.plutostate and b/Fall24/generated_assets/transformations_and_autodiff_92c3f85d.plutostate differ diff --git a/Fall24/generated_assets/transforming_images_da6cc29e.plutostate b/Fall24/generated_assets/transforming_images_5cd16c72.plutostate similarity index 98% rename from Fall24/generated_assets/transforming_images_da6cc29e.plutostate rename to Fall24/generated_assets/transforming_images_5cd16c72.plutostate index c543604f..186b8379 100644 Binary files a/Fall24/generated_assets/transforming_images_da6cc29e.plutostate and b/Fall24/generated_assets/transforming_images_5cd16c72.plutostate differ diff --git a/Fall24/homework/hw0/index.html b/Fall24/homework/hw0/index.html index 65972caf..7ad26837 100644 --- a/Fall24/homework/hw0/index.html +++ b/Fall24/homework/hw0/index.html @@ -1 +1 @@ - + diff --git a/Fall24/homework/hw1/index.html b/Fall24/homework/hw1/index.html index dfdc995a..e2efaed5 100644 --- a/Fall24/homework/hw1/index.html +++ b/Fall24/homework/hw1/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/homework/hw10/index.html b/Fall24/homework/hw10/index.html index 986b023c..8dc29617 100644 --- a/Fall24/homework/hw10/index.html +++ b/Fall24/homework/hw10/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/homework/hw2/index.html b/Fall24/homework/hw2/index.html index 3706d988..aeae3dea 100644 --- a/Fall24/homework/hw2/index.html +++ b/Fall24/homework/hw2/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/homework/hw3/index.html b/Fall24/homework/hw3/index.html index a6c6f6a4..729fbe8a 100644 --- a/Fall24/homework/hw3/index.html +++ b/Fall24/homework/hw3/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/homework/hw4/index.html b/Fall24/homework/hw4/index.html index 3bfee291..46fe5569 100644 --- a/Fall24/homework/hw4/index.html +++ b/Fall24/homework/hw4/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/homework/hw5/index.html b/Fall24/homework/hw5/index.html index f2ef01df..1948c8f1 100644 --- a/Fall24/homework/hw5/index.html +++ b/Fall24/homework/hw5/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/homework/hw6/index.html b/Fall24/homework/hw6/index.html index b29f5528..1d9b5bb4 100644 --- a/Fall24/homework/hw6/index.html +++ b/Fall24/homework/hw6/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/homework/hw7/index.html b/Fall24/homework/hw7/index.html index f98dbea1..7c8e76b8 100644 --- a/Fall24/homework/hw7/index.html +++ b/Fall24/homework/hw7/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/homework/hw8/index.html b/Fall24/homework/hw8/index.html index dcc35001..c59dd960 100644 --- a/Fall24/homework/hw8/index.html +++ b/Fall24/homework/hw8/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/homework/hw9/index.html b/Fall24/homework/hw9/index.html index 0ee23313..21e11711 100644 --- a/Fall24/homework/hw9/index.html +++ b/Fall24/homework/hw9/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -126,7 +126,7 @@

    Module 3: Climate Science

    - +
    diff --git a/Fall24/images_abstractions/abstraction/index.html b/Fall24/images_abstractions/abstraction/index.html index f1050466..a9c60e51 100644 --- a/Fall24/images_abstractions/abstraction/index.html +++ b/Fall24/images_abstractions/abstraction/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/images_abstractions/dynamicprograms/index.html b/Fall24/images_abstractions/dynamicprograms/index.html index 746de618..396bc65a 100644 --- a/Fall24/images_abstractions/dynamicprograms/index.html +++ b/Fall24/images_abstractions/dynamicprograms/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/images_abstractions/images/index.html b/Fall24/images_abstractions/images/index.html index 9b10e1f7..6d6d980c 100644 --- a/Fall24/images_abstractions/images/index.html +++ b/Fall24/images_abstractions/images/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/images_abstractions/linear_transformation/index.html b/Fall24/images_abstractions/linear_transformation/index.html index 253fd976..8b316541 100644 --- a/Fall24/images_abstractions/linear_transformation/index.html +++ b/Fall24/images_abstractions/linear_transformation/index.html @@ -1 +1 @@ - + diff --git a/Fall24/images_abstractions/newton_method/index.html b/Fall24/images_abstractions/newton_method/index.html index 0c740b26..21d5dc31 100644 --- a/Fall24/images_abstractions/newton_method/index.html +++ b/Fall24/images_abstractions/newton_method/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/images_abstractions/seamcarving/index.html b/Fall24/images_abstractions/seamcarving/index.html index eacafb73..ed643036 100644 --- a/Fall24/images_abstractions/seamcarving/index.html +++ b/Fall24/images_abstractions/seamcarving/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/images_abstractions/structure/index.html b/Fall24/images_abstractions/structure/index.html index 52b20894..9d25ab29 100644 --- a/Fall24/images_abstractions/structure/index.html +++ b/Fall24/images_abstractions/structure/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/images_abstractions/transformations2/index.html b/Fall24/images_abstractions/transformations2/index.html index ce8bde53..ef780d75 100644 --- a/Fall24/images_abstractions/transformations2/index.html +++ b/Fall24/images_abstractions/transformations2/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/images_abstractions/transformations_and_autodiff/index.html b/Fall24/images_abstractions/transformations_and_autodiff/index.html index 7a97b0a9..9b0c5fe5 100644 --- a/Fall24/images_abstractions/transformations_and_autodiff/index.html +++ b/Fall24/images_abstractions/transformations_and_autodiff/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/images_abstractions/transforming_images/index.html b/Fall24/images_abstractions/transforming_images/index.html index 215bab26..18b38a76 100644 --- a/Fall24/images_abstractions/transforming_images/index.html +++ b/Fall24/images_abstractions/transforming_images/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -138,7 +138,7 @@

    Lecture Video

    - + diff --git a/Fall24/index.html b/Fall24/index.html index 4bfa9bf6..4f9434ab 100644 --- a/Fall24/index.html +++ b/Fall24/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • @@ -249,9 +249,6 @@

    Modeling with Stochastic Simulation

    Random Variables as Types

    -
    -

    Random Walks

    -

    Random Walks

    diff --git a/Fall24/installation/index.html b/Fall24/installation/index.html index 12339178..82a8607b 100644 --- a/Fall24/installation/index.html +++ b/Fall24/installation/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • diff --git a/Fall24/logistics/index.html b/Fall24/logistics/index.html index eb30bbdc..3282b631 100644 --- a/Fall24/logistics/index.html +++ b/Fall24/logistics/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • diff --git a/Fall24/pluto_export.json b/Fall24/pluto_export.json index 3aad3927..bbf1f35a 100644 --- a/Fall24/pluto_export.json +++ b/Fall24/pluto_export.json @@ -1 +1 @@ -{"notebooks":{"images_abstractions/transformations2/":{"id":"images_abstractions/transformations2/","hash":"oTz1obPoguTrR2B1d5pFM_3CZ9lzKyaDi9K09KqXSiM","statefile_path":"./generated_assets/transformations2_d9045426.plutostate","notebookfile_path":"./generated_assets/transformations2_a1f53ca1.jl","html_path":"images_abstractions/transformations2/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196619-0750544f-cd6d-4ae3-ace7-60c24443d721.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/transformations2_d9045426.plutostate","youtube_id":"VDPf3RjoCpY","video":"https://www.youtube.com/watch?v=VDPf3RjoCpY","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/transformations2_a1f53ca1.jl","section":5,"order":5,"title":"Transformations II: Composability, Linearity and Nonlinearity","tags":["lecture","module1","transformation","track_math","track_julia","inverse","composition","matrix","linear algebra","nonlinear","linear"],"description":"Let's see what mathematical transformations, inverses, composition and (non-)linearity look like in practice. (i.e. applied to philip.jpg)"}},"data_science/random_walks_II/":{"id":"data_science/random_walks_II/","hash":"-EjENNgoD3DBc0qM-_QuMrKLPb3etsf-6nGcsR6BcBI","statefile_path":"./generated_assets/random_walks_II_04386990.plutostate","notebookfile_path":"./generated_assets/random_walks_II_34c448f8.jl","html_path":"data_science/random_walks_II/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/191981723-657fd4b8-c23d-4fb9-a3f2-496b4574fac1.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_walks_II_04386990.plutostate","youtube_id":"pIAFHyLmwbM","video":"https://www.youtube.com/watch?v=pIAFHyLmwbM","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_walks_II_34c448f8.jl","section":6,"order":6,"title":"Random Walks II","tags":["lecture","module2","track_julia","track_data","statistics","plotting","random","structure","type","programming","interactive"],"description":""}},"homework/hw1/":{"id":"homework/hw1/","hash":"2xpv7XVQYkuM1Odropb_rRsk7T8475T3TU6YZBU641U","statefile_path":"./generated_assets/hw1_299c4e25.plutostate","notebookfile_path":"./generated_assets/hw1_ed6f1adb.jl","html_path":"homework/hw1/index.html","frontmatter":{"chapter":1,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw1_ed6f1adb.jl","section":2.5,"order":2.5,"homework_number":1,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw1_299c4e25.plutostate","title":"Images and Arrays","tags":["homework","module1","image","track_julia","track_math","track_climate","track_data","programming","interactive","type","matrix"],"description":"Practice Julia basics by working with arrays of colors. At the end of this homework, you can see all of your filters applied to your webcam image!"}},"images_abstractions/linear_transformation/":{"id":"images_abstractions/linear_transformation/","hash":"G2NI2SE8o4FpwPmQIKrW7jEtqAfssVhZQHy8hthsBMI","statefile_path":"./generated_assets/linear_transformation_3de70dd8.plutostate","notebookfile_path":"./generated_assets/linear_transformation_d948631b.jl","html_path":"images_abstractions/linear_transformation/index.html","frontmatter":{"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/linear_transformation_d948631b.jl","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/linear_transformation_3de70dd8.plutostate"}},"data_science/random_variables_as_types/":{"id":"data_science/random_variables_as_types/","hash":"BUUGFSEvIrvjmEf8Fmub5KJBiaIErlTyBYggngwB_Jg","statefile_path":"./generated_assets/random_variables_as_types_7745c02e.plutostate","notebookfile_path":"./generated_assets/random_variables_as_types_15064505.jl","html_path":"data_science/random_variables_as_types/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196570-478bbb89-05fb-4799-99a0-0ede06354cb6.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_variables_as_types_7745c02e.plutostate","youtube_id":"xKAO38UsXo0","video":"https://www.youtube.com/watch?v=xKAO38UsXo0","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_variables_as_types_15064505.jl","section":4,"order":4,"title":"Random Variables as Types","tags":["lecture","module2","track_julia","type","programming","probability","interactive","random","track_math","Symbolics"],"description":""}},"images_abstractions/newton_method/":{"id":"images_abstractions/newton_method/","hash":"55JEGwCdCuzUncbgxb5Sq7MNUr3BxL_sKTGBqmmvfGU","statefile_path":"./generated_assets/newton_method_325e10e8.plutostate","notebookfile_path":"./generated_assets/newton_method_1b4492e7.jl","html_path":"images_abstractions/newton_method/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196605-b6119b9d-223c-44bc-97d5-ef7cfce66483.gif","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/newton_method_325e10e8.plutostate","youtube_id":"Wjcx9sNSLP8","video":"https://www.youtube.com/watch?v=Wjcx9sNSLP8","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/newton_method_1b4492e7.jl","section":6,"order":6,"title":"The Newton Method","tags":["lecture","module1","track_math","continuous","differentiation","automatic differentiation","ForwardDiff","interactive","Symbolics","transformation"],"description":"This lecture explains a method for finding the root of a function, but using code an illustrations instead of a chalkboard! We will illustrate the Newton method using the ForwardDiff.jl package to carry out automatic differentiation, but we will also try to understand what's going on \"under the hood\"."}},"images_abstractions/images/":{"id":"images_abstractions/images/","hash":"R5p_wAzsLsvZMtGHlnmXzF5j-gpGwkCNjSgBAzt3ZDU","statefile_path":"./generated_assets/images_1222a1fc.plutostate","notebookfile_path":"./generated_assets/images_c07f9a47.jl","html_path":"images_abstractions/images/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196634-2294d0a7-e79a-40d0-bbb8-81da70f4d398.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/images_1222a1fc.plutostate","youtube_id":"3zTO3LEY-cM","video":"https://www.youtube.com/watch?v=3zTO3LEY-cM","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/images_c07f9a47.jl","section":1,"order":1,"title":"Images as Data and Arrays","tags":["lecture","module1","philip","track_julia","matrix","image"],"description":""}},"week0/plots_tutorial/":{"id":"week0/plots_tutorial/","hash":"aY3IurGDDg6OclnVgj1vea-_jgn0qhuz5ljgV_0OCI0","statefile_path":"./generated_assets/plots_tutorial_e3393a36.plutostate","notebookfile_path":"./generated_assets/plots_tutorial_bac88d69.jl","html_path":"week0/plots_tutorial/index.html","frontmatter":{"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/plots_tutorial_bac88d69.jl","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/plots_tutorial_e3393a36.plutostate"}},"homework/hw3/":{"id":"homework/hw3/","hash":"m_nvRtlSqL4VSwom00U34puu835Nb_u5RZ0JiqkWh50","statefile_path":"./generated_assets/hw3_fc9a1563.plutostate","notebookfile_path":"./generated_assets/hw3_46eff99b.jl","html_path":"homework/hw3/index.html","frontmatter":{"chapter":1,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw3_46eff99b.jl","section":6.5,"order":6.5,"homework_number":3,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw3_fc9a1563.plutostate","title":"Structure and language","tags":["homework","module1","track_julia","track_math","track_climate","linguistics","programming","interactive","type","structure","data","artificial intelligence"],"description":"Automatically detect the language of a piece of text, and generate realistic-looking random text!"}},"images_abstractions/transforming_images/":{"id":"images_abstractions/transforming_images/","hash":"yIc0Tt496vFum4azzL93QLJWJJI6QX8TEwk1b1HLKDU","statefile_path":"./generated_assets/transforming_images_da6cc29e.plutostate","notebookfile_path":"./generated_assets/transforming_images_4e3487c8.jl","html_path":"images_abstractions/transforming_images/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196626-194e81c9-00f7-49f6-90c3-09945723b6a3.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/transforming_images_da6cc29e.plutostate","youtube_id":"uZYVjDDZW9A","video":"https://www.youtube.com/watch?v=uZYVjDDZW9A","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/transforming_images_4e3487c8.jl","section":4,"order":4,"title":"Transformations with Images","tags":["lecture","module1","image","convolution","track_math","matrix","philip"],"description":""}},"climate_science/climate2_snowball_earth/":{"id":"climate_science/climate2_snowball_earth/","hash":"Y5PUHX40JNkDw8uqTx_PadAYnKu52i-t_XI91mciSec","statefile_path":"./generated_assets/climate2_snowball_earth_43383815.plutostate","notebookfile_path":"./generated_assets/climate2_snowball_earth_1dd49363.jl","html_path":"climate_science/climate2_snowball_earth/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199703-5edb4791-f9c0-4872-b0a7-7c9b1b6502d5.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/climate2_snowball_earth_43383815.plutostate","youtube_id":"cdIgr_2nUvI","video":"https://www.youtube.com/watch?v=cdIgr_2nUvI","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/climate2_snowball_earth_1dd49363.jl","section":6,"order":6,"title":"Snowball Earth and hysteresis","tags":["lecture","module3","dynamics","track_math","track_climate","hysteresis","equilibrium","climate","modeling","differential equation","ODE","plotting","feedback","runaway","nonlinear","snowball earth"],"description":""}},"climate_science/our_first_climate_model/":{"id":"climate_science/our_first_climate_model/","hash":"rYZOsmmHNG_AcQAy5GmchTKaozY7HJCOuBkUL1scytM","statefile_path":"./generated_assets/our_first_climate_model_b543d6db.plutostate","notebookfile_path":"./generated_assets/our_first_climate_model_b24e86ad.jl","html_path":"climate_science/our_first_climate_model/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199705-7bdb6bb0-8698-43a1-87e6-c073ab102da5.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/our_first_climate_model_b543d6db.plutostate","youtube_id":"J1UsMa1cTeE","video":"https://www.youtube.com/watch?v=J1UsMa1cTeE","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/our_first_climate_model_b24e86ad.jl","section":4,"order":4,"title":"Our first climate model","tags":["lecture","module3","climate","modeling","track_climate","track_math","plotting","data","ODE","differential equation","CSV","dataframe","track_data"],"description":""}},"data_science/pca/":{"id":"data_science/pca/","hash":"N4mytXpnpzLHkbNMI-IepgFTI2e6UZeSnUS6VJZmTmk","statefile_path":"./generated_assets/pca_a0b039e1.plutostate","notebookfile_path":"./generated_assets/pca_b5b28937.jl","html_path":"data_science/pca/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196577-512cee99-aebf-48a9-97b8-358d5ca561ca.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/pca_a0b039e1.plutostate","youtube_id":"iuKrM_NzxCk","video":"https://www.youtube.com/watch?v=iuKrM_NzxCk","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/pca_b5b28937.jl","section":1,"order":1,"title":"Principal Component Analysis","tags":["lecture","module2","track_data","data","statistics","matrix","linear algebra","track_math"],"description":"In this notebook we will start looking at more general kinds of data, not only images, and we'll try to extract some information from the image using statistical methods, namely principal component analysis. This method tries to answer the questions \"which 'directions' are the most important in the data\" and \"can we reduce the dimensionality (number of useful variables) of the data\"?"}},"homework/hw7/":{"id":"homework/hw7/","hash":"BpA0MREy3vMDvo45fMIa-y_-1CwlP97nA9F7YUzlG9I","statefile_path":"./generated_assets/hw7_89e9b9d3.plutostate","notebookfile_path":"./generated_assets/hw7_31349006.jl","html_path":"homework/hw7/index.html","frontmatter":{"chapter":2,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw7_31349006.jl","section":7.5,"order":7.5,"homework_number":7,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw7_89e9b9d3.plutostate","title":"Epidemic modeling I","tags":["homework","module2","epidemiology","track_data","monte carlo","statistics","track_math","ODE","agent based model","differential equation","type","structure","plotting"],"description":"Simulate the spread of an epidemic by creating your own agent-based model from scratch, and find statistics using the Monte Carlo method."}},"climate_science/time_stepping/":{"id":"climate_science/time_stepping/","hash":"lT6WCBbBXeqCnTHpe_bYMC2bCfA2jsOeWPNYRrsp_Eo","statefile_path":"./generated_assets/time_stepping_2f3aee08.plutostate","notebookfile_path":"./generated_assets/time_stepping_08963e95.jl","html_path":"climate_science/time_stepping/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199718-ff811eb3-aad6-4d6b-99e0-f6bf922816b4.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/time_stepping_2f3aee08.plutostate","youtube_id":"3Y5gVyO8KcI","video":"https://www.youtube.com/watch?v=3Y5gVyO8KcI","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/time_stepping_08963e95.jl","section":1,"order":1,"title":"Time stepping","tags":["lecture","module3","epidemiology","track_math","track_climate","plotting","continuous","discrete","ODE","differential equation","modeling"],"description":""}},"homework/hw2/":{"id":"homework/hw2/","hash":"oJEV1A4TTLMXptj_8Pbp8c4XPWgKP5wdj3Trdc70ZNI","statefile_path":"./generated_assets/hw2_3a94055e.plutostate","notebookfile_path":"./generated_assets/hw2_d41591a0.jl","html_path":"homework/hw2/index.html","frontmatter":{"chapter":1,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw2_d41591a0.jl","section":4.5,"order":4.5,"homework_number":2,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw2_3a94055e.plutostate","title":"Convolutions","tags":["homework","module1","track_data","track_julia","track_climate","track_math","convolution","matrix","interactive","image","type","webcam","programming"],"description":"Create your own image filters using mathematical convolution!"}},"data_science/simulating_component_failure/":{"id":"data_science/simulating_component_failure/","hash":"pSDWGEc3XyK_Xz3PSuUSc5N5acs8am5O9dhevVwfv8Y","statefile_path":"./generated_assets/simulating_component_failure_3c893b84.plutostate","notebookfile_path":"./generated_assets/simulating_component_failure_18d620a5.jl","html_path":"data_science/simulating_component_failure/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196572-b11974d5-7335-4678-9092-630e034bbe8f.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/simulating_component_failure_3c893b84.plutostate","youtube_id":"d8BohH76C7E","video":"https://www.youtube.com/watch?v=d8BohH76C7E","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/simulating_component_failure_18d620a5.jl","section":3,"order":3,"title":"Modeling with Stochastic Simulation","tags":["lecture","module2","track_julia","probability","statistics","track_math","epidemiology","interactive","plotting","programming","type","discrete","continuous","ODE","differential equation","agent based model"],"description":""}},"climate_science/predicting_the_weather/":{"id":"climate_science/predicting_the_weather/","hash":"sOGNeLlg3nlUSWU3BzlXEBtpeA-v4y5Brw51hrGjhGY","statefile_path":"./generated_assets/predicting_the_weather_350d3047.plutostate","notebookfile_path":"./generated_assets/predicting_the_weather_788de1b0.jl","html_path":"climate_science/predicting_the_weather/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199708-af8acad2-4172-4fa7-911e-e30300efb5ee.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/predicting_the_weather_350d3047.plutostate","youtube_id":"M3udLzIHtsc","video":"https://www.youtube.com/watch?v=M3udLzIHtsc","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/predicting_the_weather_788de1b0.jl","section":3,"order":3,"title":"Why we can't predict the weather","tags":["lecture","module3","track_climate","track_math","bifurcation","nonlinear","ODE","differential equation","continuous","plotting","dynamics","climate","modeling","DifferentialEquations"],"description":""}},"data_science/optimization/":{"id":"data_science/optimization/","hash":"ILQWp5FDdFkzUsZriKHzy2fzB5HY_cEluT81XNaDxE4","statefile_path":"./generated_assets/optimization_a85837d5.plutostate","notebookfile_path":"./generated_assets/optimization_a716b420.jl","html_path":"data_science/optimization/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136199719-a56a217b-cd36-4da2-b407-7285dcec94df.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/optimization_a85837d5.plutostate","youtube_id":"44RA9fclTdA","video":"https://www.youtube.com/watch?v=44RA9fclTdA","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/optimization_a716b420.jl","section":9,"order":9,"title":"Optimization","tags":["lecture","module2","track_juli","track_climate","track_data","track_math","optimization"],"description":"We use Optim.jl and JuMP.jl to optimize a function: automatically finding the input that maximizes output."}},"climate_science/how_to_collaborate_on_software/":{"id":"climate_science/how_to_collaborate_on_software/","hash":"8pA388qD_8LSBy9H5agJ7EjeqlTFxXEiJ1i031Tbe50","statefile_path":"./generated_assets/how_to_collaborate_on_software_18d2a1d9.plutostate","notebookfile_path":"./generated_assets/how_to_collaborate_on_software_f33790f2.jl","html_path":"climate_science/how_to_collaborate_on_software/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199704-ba6d0586-34bf-490c-8fd0-6959ab42cd23.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/how_to_collaborate_on_software_18d2a1d9.plutostate","youtube_id":"7N9Vvc8amGM","video":"https://www.youtube.com/watch?v=7N9Vvc8amGM","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/how_to_collaborate_on_software_f33790f2.jl","section":5,"order":5,"title":"GitHub & Open Source Software","tags":["lecture","module3","track_julia","track_data","track_climate","github","open source","collaboration","pull request","fork","software development"],"description":""}},"homework/hw0/":{"id":"homework/hw0/","hash":"lT4I0lCJaNPSzz-F_ddJF0tB3X3Qxqe_j62dWO4bAgk","statefile_path":"./generated_assets/hw0_29912ad8.plutostate","notebookfile_path":"./generated_assets/hw0_d2083e95.jl","html_path":"homework/hw0/index.html","frontmatter":{"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw0_d2083e95.jl","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw0_29912ad8.plutostate"}},"climate_science/resistors_and_stencils/":{"id":"climate_science/resistors_and_stencils/","hash":"TIijo2GqgZEToIzIfxCB0xsblaSLmoRZfK07wyJYd6M","statefile_path":"./generated_assets/resistors_and_stencils_e788da9f.plutostate","notebookfile_path":"./generated_assets/resistors_and_stencils_a3a3884c.jl","html_path":"climate_science/resistors_and_stencils/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136200635-33c007ff-89f6-48dc-b1d3-eb56fd16003e.gif","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/resistors_and_stencils_e788da9f.plutostate","youtube_id":"DdTWgBlDgr0","video":"https://www.youtube.com/watch?v=DdTWgBlDgr0","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/resistors_and_stencils_a3a3884c.jl","section":8,"order":8,"title":"Resistors, stencils and climate models","tags":["lecture","module3","PDE","differential equation","ghost cell","boundary condition","climate","modeling","track_climate","track_math","stencil"],"description":""}},"data_science/random_walks/":{"id":"data_science/random_walks/","hash":"lrYNUC76PMQzHzV23tMIzY0ESzGIzclDGfHi0smsUuY","statefile_path":"./generated_assets/random_walks_f14e2c21.plutostate","notebookfile_path":"./generated_assets/random_walks_500db696.jl","html_path":"data_science/random_walks/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196563-f4b5b44c-5252-4e67-8c82-c550de891c55.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_walks_f14e2c21.plutostate","youtube_id":"14hHtGJ4s-g","video":"https://www.youtube.com/watch?v=14hHtGJ4s-g","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_walks_500db696.jl","section":5,"order":5,"title":"Random Walks","tags":["lecture","module2","programming","track_julia","plotting","structure","type","interactive","random","statistics","track_math","track_data"],"description":""}},"climate_science/inverse_climate_model/":{"id":"climate_science/inverse_climate_model/","hash":"n8fldF9ybi00HObWhLFf4fbVLlvPIOoizzinM8KPjgE","statefile_path":"./generated_assets/inverse_climate_model_bcb5a166.plutostate","notebookfile_path":"./generated_assets/inverse_climate_model_74e5c79f.jl","html_path":"climate_science/inverse_climate_model/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199660-315b045b-f144-4009-9282-7fe6d1f5d41b.gif","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/inverse_climate_model_bcb5a166.plutostate","youtube_id":"nm86_hDwYTU","video":"https://www.youtube.com/watch?v=nm86_hDwYTU","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/inverse_climate_model_74e5c79f.jl","section":10,"order":10,"title":"Climate Economics","tags":["lecture","module3","climate","modeling","track_climate","track_math","plotting","track_julia","economics","optimization","JuMP","simulation"],"description":""}},"data_science/linearmodel_datascience/":{"id":"data_science/linearmodel_datascience/","hash":"hx17fXJVRQeNbo7akEKkUyWloF3xb3esYgSvo-y6sx4","statefile_path":"./generated_assets/linearmodel_datascience_3006b0ba.plutostate","notebookfile_path":"./generated_assets/linearmodel_datascience_7d7b1d87.jl","html_path":"data_science/linearmodel_datascience/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136199721-8fd577cb-d6f3-492d-bbdc-37bc74664ca7.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/linearmodel_datascience_3006b0ba.plutostate","youtube_id":"O6NTKsR8TjQ","video":"https://www.youtube.com/watch?v=O6NTKsR8TjQ","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/linearmodel_datascience_7d7b1d87.jl","section":8,"order":8,"title":"Linear Model, Data Science, & Simulations","tags":["lecture","module2","track_julia","track_data","csv","dataframe","statistics","plotting","interactive"],"description":""}},"climate_science/odes_and_parameterized_types/":{"id":"climate_science/odes_and_parameterized_types/","hash":"vZ4Q-ajd1orQhwu2wx4R4KD9lMi1eaLHzIddzKGaCp8","statefile_path":"./generated_assets/odes_and_parameterized_types_d59077d7.plutostate","notebookfile_path":"./generated_assets/odes_and_parameterized_types_f9109ebd.jl","html_path":"climate_science/odes_and_parameterized_types/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199713-25eb2e90-c5cd-4e11-8463-6f6069a81a51.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/odes_and_parameterized_types_d59077d7.plutostate","youtube_id":"S71YIZ8e7MQ","video":"https://www.youtube.com/watch?v=S71YIZ8e7MQ","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/odes_and_parameterized_types_f9109ebd.jl","section":2,"order":2,"title":"ODEs and parameterized types","tags":["lecture","module3","epidemiology","plotting","ODE","differential equation","track_julia","track_math","programming","DifferentialEquations","type","structure","track_climate"],"description":""}},"data_science/random_walks backup 1/":{"id":"data_science/random_walks backup 1/","hash":"Za61-48upQJRvijFiVlJFC_2IyAmjM6u35EbIK2T0Ac","statefile_path":"./generated_assets/random_walks_backup_1_5eac959a.plutostate","notebookfile_path":"./generated_assets/random_walks_backup_1_fbb5ae65.jl","html_path":"data_science/random_walks backup 1/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196563-f4b5b44c-5252-4e67-8c82-c550de891c55.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_walks_backup_1_5eac959a.plutostate","youtube_id":"14hHtGJ4s-g","video":"https://www.youtube.com/watch?v=14hHtGJ4s-g","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_walks_backup_1_fbb5ae65.jl","section":5,"order":5,"title":"Random Walks","tags":["lecture","module2","programming","track_julia","plotting","structure","type","interactive","random","statistics","track_math","track_data"],"description":""}},"images_abstractions/dynamicprograms/":{"id":"images_abstractions/dynamicprograms/","hash":"qhxVGEYEtSkWaZ-9GyugN9MHj2xGBaB9XiAuK4OSbuI","statefile_path":"./generated_assets/dynamicprograms_181ecd05.plutostate","notebookfile_path":"./generated_assets/dynamicprograms_18551caa.jl","html_path":"images_abstractions/dynamicprograms/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196599-c6ae60f0-9269-4134-bb0d-5bcab928bd2b.gif","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/dynamicprograms_181ecd05.plutostate","youtube_id":"KyBXJV1zFlo","video":"https://www.youtube.com/watch?v=KyBXJV1zFlo","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/dynamicprograms_18551caa.jl","section":7,"order":7,"title":"Dynamic Programming","tags":["lecture","module1","optimization","programming","track_julia","track_math","interactive","discrete","lazy"],"description":""}},"images_abstractions/transformations_and_autodiff/":{"id":"images_abstractions/transformations_and_autodiff/","hash":"q4fGNpJ-5BX-2zqFimo-wYEKvpaCh3tqjksTlFkCFFQ","statefile_path":"./generated_assets/transformations_and_autodiff_f24f5fff.plutostate","notebookfile_path":"./generated_assets/transformations_and_autodiff_36c687ab.jl","html_path":"images_abstractions/transformations_and_autodiff/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196632-ad67cb84-a4c9-410e-ab72-f4fcfc26f69a.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/transformations_and_autodiff_f24f5fff.plutostate","youtube_id":"AAREeuaKCic","video":"https://www.youtube.com/watch?v=AAREeuaKCic","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/transformations_and_autodiff_36c687ab.jl","section":3,"order":3,"title":"Automatic Differentiation","tags":["lecture","module1","track_julia","track_math","programming","function","transformation","automatic differentiation","continuous","derivative"],"description":"We use the package ForwardDiff.jl to automatically differentiate functions, on scalar and vector domains."}},"data_science/discrete_and_continuous/":{"id":"data_science/discrete_and_continuous/","hash":"l2MX-ZnSC-k_oJTg7Cu7jzmsJaha8LuL9XU1McXCZZ8","statefile_path":"./generated_assets/discrete_and_continuous_6ee5c54a.plutostate","notebookfile_path":"./generated_assets/discrete_and_continuous_f9176397.jl","html_path":"data_science/discrete_and_continuous/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196552-ce16c06f-bd12-427f-80e5-aedb1fbc734a.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/discrete_and_continuous_6ee5c54a.plutostate","youtube_id":"H6Dcx3YeTkE","video":"https://www.youtube.com/watch?v=H6Dcx3YeTkE","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/discrete_and_continuous_f9176397.jl","section":7,"order":7,"title":"Discrete and Continuous","tags":["lecture","module2","track_math","discrete","continuous"],"description":""}},"climate_science/2d_advection_diffusion/":{"id":"climate_science/2d_advection_diffusion/","hash":"w4TnYP94mdBFWDnf26PhkTnllAJvZkOOM52mx3PoS84","statefile_path":"./generated_assets/2d_advection_diffusion_ef1fe4d1.plutostate","notebookfile_path":"./generated_assets/2d_advection_diffusion_60e784c3.jl","html_path":"climate_science/2d_advection_diffusion/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136200688-e3c6d6ee-808c-433f-8252-af6ad278fb4d.gif","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/2d_advection_diffusion_ef1fe4d1.plutostate","youtube_id":"DdTWgBlDgr0","video":"https://www.youtube.com/watch?v=DdTWgBlDgr0","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/2d_advection_diffusion_60e784c3.jl","section":9,"order":9,"title":"Advection and diffusion in 2D","tags":["lecture","module3","PDE","differential equation","track_math","track_climate","structure","modeling","climate","simulation","fluid simulation","continuous","advection–diffusion"],"description":""}},"climate_science/advection_and_diffusion/":{"id":"climate_science/advection_and_diffusion/","hash":"a4cnypcEiq96VPNW7wMVySHIPJRlMVXwbqy_l1we29E","statefile_path":"./generated_assets/advection_and_diffusion_eb02c8e0.plutostate","notebookfile_path":"./generated_assets/advection_and_diffusion_ca27876b.jl","html_path":"climate_science/advection_and_diffusion/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136200627-8211654f-7730-4f4a-8933-5b64164806c3.gif","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/advection_and_diffusion_eb02c8e0.plutostate","youtube_id":"Xb-iUwXI78A","video":"https://www.youtube.com/watch?v=Xb-iUwXI78A","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/advection_and_diffusion_ca27876b.jl","section":7,"order":7,"title":"Advection and diffusion in 1D","tags":["lecture","module3","advection–diffusion","climate","modelling","PDE","differential equation","track_math","track_climate","simulation","plotting","fluid simulation","continuous"],"description":""}},"homework/hw8/":{"id":"homework/hw8/","hash":"5n69_7rxqxnMTEv7vxQkLwE2rw25hBlUOxntQ3OQjUw","statefile_path":"./generated_assets/hw8_670e2ad5.plutostate","notebookfile_path":"./generated_assets/hw8_ffbd7ee6.jl","html_path":"homework/hw8/index.html","frontmatter":{"chapter":2,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw8_ffbd7ee6.jl","section":9.5,"order":9.5,"homework_number":8,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw8_670e2ad5.plutostate","title":"Epidemic modeling II","tags":["homework","module2","track_math","track_data","optimization","statistics","gradient","differentiation","automatic differentiation","continuous","probability","epidemiology","monte carlo","modeling","plotting"],"description":"Learn about optimisation and gradient descent with help from our visuals and automatic checks. We use these new skill to fit parameters of an epidemic model to match reality."}},"homework/hw5/":{"id":"homework/hw5/","hash":"7Msk5OBNM_0uXqpRAk1VehE69pnTFLgH1VGnBVHLk6U","statefile_path":"./generated_assets/hw5_89d78b8d.plutostate","notebookfile_path":"./generated_assets/hw5_e424cbec.jl","html_path":"homework/hw5/index.html","frontmatter":{"chapter":2,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw5_e424cbec.jl","section":2.5,"order":2.5,"homework_number":5,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw5_89d78b8d.plutostate","title":"Structure","tags":["homework","module2","track_julia","structure","track_math","type","matrix","linear algebra","track_data"],"description":"Create your own Julia structs and add new functionality to them, to create first-class mathematical objects."}},"climate_science/optimization_with_JuMP/":{"id":"climate_science/optimization_with_JuMP/","hash":"vPPV7sjZ4W6XIBgm_5vTL6OSUGzMqdjHoHzA8o73Wko","statefile_path":"./generated_assets/optimization_with_JuMP_68e41f28.plutostate","notebookfile_path":"./generated_assets/optimization_with_JuMP_eed5f3bc.jl","html_path":"climate_science/optimization_with_JuMP/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136200827-89647ae8-cb06-42ea-a18d-5f64e9cc2b25.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/optimization_with_JuMP_68e41f28.plutostate","youtube_id":"nm86_hDwYTU","video":"https://www.youtube.com/watch?v=nm86_hDwYTU","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/optimization_with_JuMP_eed5f3bc.jl","section":11,"order":11,"title":"Solving inverse problems","tags":["lecture","module3","optimization","JuMP","track_math","track_climate","track_julia","modeling","plotting"],"description":""}},"homework/hw4/":{"id":"homework/hw4/","hash":"PAZ2ByPsloxVE4bJ3T6P_x-SNMd1IWNOKEfkGOi0vNQ","statefile_path":"./generated_assets/hw4_8d138b97.plutostate","notebookfile_path":"./generated_assets/hw4_0776063c.jl","html_path":"homework/hw4/index.html","frontmatter":{"chapter":1,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw4_0776063c.jl","section":9.5,"order":9.5,"homework_number":4,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw4_8d138b97.plutostate","title":"Dynamic programming","tags":["homework","module1","track_julia","track_math","structure","programming","dynamic programming","matrix","recursion"],"description":""}},"images_abstractions/seamcarving/":{"id":"images_abstractions/seamcarving/","hash":"J5RXhtOWFK7cg5Ap2Fec_wOTYb8DKBqO_ciP9Xs4UA0","statefile_path":"./generated_assets/seamcarving_a93a0a52.plutostate","notebookfile_path":"./generated_assets/seamcarving_86579427.jl","html_path":"images_abstractions/seamcarving/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196584-b3c806a8-aa61-48d9-9e73-30583fcc38bf.gif","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/seamcarving_a93a0a52.plutostate","youtube_id":"KyBXJV1zFlo","video":"https://www.youtube.com/watch?v=KyBXJV1zFlo","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/seamcarving_86579427.jl","section":8,"order":8,"title":"Seam Carving","tags":["lecture","module1","image","matrix","track_julia","track_climate","track_data","optimization","interactive"],"description":""}},"homework/hw6/":{"id":"homework/hw6/","hash":"qxhAXOklNwWx1DiqpgFJk3w5NCmzYxZ3I13_fZnRLS8","statefile_path":"./generated_assets/hw6_61150391.plutostate","notebookfile_path":"./generated_assets/hw6_5c4018ab.jl","html_path":"homework/hw6/index.html","frontmatter":{"chapter":2,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw6_5c4018ab.jl","section":3.5,"order":3.5,"homework_number":6,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw6_61150391.plutostate","title":"Probability distributions","tags":["homework","module2","track_julia","track_math","track_data","structure","probability","statistics","plotting","interactive"],"description":"Calculate a probability distribution from a dataset, experiment with different statistical models, and learn how to plot your results."}},"images_abstractions/structure/":{"id":"images_abstractions/structure/","hash":"WNuhCtbzZuMrBgGEU-uPt2bU-8gjbXBhL-9ROhcTMYc","statefile_path":"./generated_assets/structure_6638beac.plutostate","notebookfile_path":"./generated_assets/structure_0aa1db58.jl","html_path":"images_abstractions/structure/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196581-ffdf4a3b-f35c-4eb8-85a7-f07560bd421b.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/structure_6638beac.plutostate","youtube_id":"wZrVxbmX218","video":"https://www.youtube.com/watch?v=wZrVxbmX218","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/structure_0aa1db58.jl","section":9,"order":9,"title":"Taking Advantage of Structure","tags":["lecture","module1","track_julia","type","programming","matrix","track_data","structure"],"description":""}},"homework/hw9/":{"id":"homework/hw9/","hash":"aibzOU750rT59qsBp8U_izGR3pVCFzREWg-84-73Rxk","statefile_path":"./generated_assets/hw9_7fd9c16e.plutostate","notebookfile_path":"./generated_assets/hw9_39f3266a.jl","html_path":"homework/hw9/index.html","frontmatter":{"chapter":3,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw9_39f3266a.jl","section":1.5,"order":1.5,"homework_number":9,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw9_7fd9c16e.plutostate","title":"Epidemic modeling III","tags":["homework","module3","epidemiology","track_julia","plotting","agent based model","statistics","random","modeling","monte carlo","track_climate"],"description":"In this problem set, we will look at a simple spatial agent-based epidemic model: agents can interact only with other agents that are nearby."}},"homework/hw10/":{"id":"homework/hw10/","hash":"3H2gBMoz2j_5wTp7wJkAfpqzm_lk4U7HSp_zkIzpNRo","statefile_path":"./generated_assets/hw10_95fe5bbc.plutostate","notebookfile_path":"./generated_assets/hw10_04a07ddc.jl","html_path":"homework/hw10/index.html","frontmatter":{"chapter":3,"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw10_04a07ddc.jl","section":7.5,"order":7.5,"homework_number":10,"plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/hw10_95fe5bbc.plutostate","title":"Climate modeling I","tags":["homework","module3","track_climate","track_julia","track_data","track_math","climate","plotting","interactive","modeling","climate model","economics","bifurcation","probability"],"description":"Play around with an energy balance model of the climate system, to explore the effect of doubling CO₂, and to examine the 'snowball earth' phenomenon."}},"week0/basic_syntax/":{"id":"week0/basic_syntax/","hash":"NvlmbLti-9kUoCRslIbBhzkSZHIkQcDGcDy9xoSBXW0","statefile_path":"./generated_assets/basic_syntax_7a86302a.plutostate","notebookfile_path":"./generated_assets/basic_syntax_6c66f936.jl","html_path":"week0/basic_syntax/index.html","frontmatter":{"plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/basic_syntax_6c66f936.jl","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/basic_syntax_7a86302a.plutostate"}},"images_abstractions/abstraction/":{"id":"images_abstractions/abstraction/","hash":"3lVHy2Snuw5-hgGJ2RFvTgNrupgWpyfJG-KOgg-EweQ","statefile_path":"./generated_assets/abstraction_14381616.plutostate","notebookfile_path":"./generated_assets/abstraction_cb4755de.jl","html_path":"images_abstractions/abstraction/index.html","frontmatter":{"chapter":1,"image":"","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/abstraction_14381616.plutostate","youtube_id":"3zTO3LEY-cM","video":"https://www.youtube.com/watch?v=3zTO3LEY-cM","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/abstraction_cb4755de.jl","section":2,"order":2,"title":"Abstraction","tags":["lecture","module1","track_julia","track_math"],"description":""}},"data_science/random_vars/":{"id":"data_science/random_vars/","hash":"RX72hvJGKXFBNU33qSlWb7iey8vvucqTviAfMDaLNDg","statefile_path":"./generated_assets/random_vars_eba6df04.plutostate","notebookfile_path":"./generated_assets/random_vars_86f67e45.jl","html_path":"data_science/random_vars/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196576-70e45c9d-ef0e-4498-bf61-58d9ae854c3e.png","plutopages_statefile_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_vars_eba6df04.plutostate","youtube_id":"7HrpoFZzITI","video":"https://www.youtube.com/watch?v=7HrpoFZzITI","plutopages_notebook_url":"++magic#root#url~6iM5uAAdYslu87Nld8HV8D++/generated_assets/random_vars_86f67e45.jl","section":2,"order":2,"title":"Sampling and Random Variables","tags":["lecture","module2","track_data","track_math","random","statistics","track_julia","image","probability","plotting","interactive"],"description":""}}},"pluto_version":"0.19.45","julia_version":"1.10.4","format_version":"1","title":null,"description":null,"collections":null} \ No newline at end of file +{"notebooks":{"images_abstractions/transformations2/":{"id":"images_abstractions/transformations2/","hash":"oTz1obPoguTrR2B1d5pFM_3CZ9lzKyaDi9K09KqXSiM","statefile_path":"./generated_assets/transformations2_ee733e4b.plutostate","notebookfile_path":"./generated_assets/transformations2_a1f53ca1.jl","html_path":"images_abstractions/transformations2/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196619-0750544f-cd6d-4ae3-ace7-60c24443d721.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/transformations2_ee733e4b.plutostate","youtube_id":"VDPf3RjoCpY","video":"https://www.youtube.com/watch?v=VDPf3RjoCpY","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/transformations2_a1f53ca1.jl","section":5,"order":5,"title":"Transformations II: Composability, Linearity and Nonlinearity","tags":["lecture","module1","transformation","track_math","track_julia","inverse","composition","matrix","linear algebra","nonlinear","linear"],"description":"Let's see what mathematical transformations, inverses, composition and (non-)linearity look like in practice. (i.e. applied to philip.jpg)"}},"data_science/random_walks_II/":{"id":"data_science/random_walks_II/","hash":"-EjENNgoD3DBc0qM-_QuMrKLPb3etsf-6nGcsR6BcBI","statefile_path":"./generated_assets/random_walks_II_4be42e3a.plutostate","notebookfile_path":"./generated_assets/random_walks_II_34c448f8.jl","html_path":"data_science/random_walks_II/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/191981723-657fd4b8-c23d-4fb9-a3f2-496b4574fac1.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/random_walks_II_4be42e3a.plutostate","youtube_id":"pIAFHyLmwbM","video":"https://www.youtube.com/watch?v=pIAFHyLmwbM","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/random_walks_II_34c448f8.jl","section":6,"order":6,"title":"Random Walks II","tags":["lecture","module2","track_julia","track_data","statistics","plotting","random","structure","type","programming","interactive"],"description":""}},"homework/hw1/":{"id":"homework/hw1/","hash":"2xpv7XVQYkuM1Odropb_rRsk7T8475T3TU6YZBU641U","statefile_path":"./generated_assets/hw1_1ee526ec.plutostate","notebookfile_path":"./generated_assets/hw1_ed6f1adb.jl","html_path":"homework/hw1/index.html","frontmatter":{"chapter":1,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw1_ed6f1adb.jl","section":2.5,"order":2.5,"homework_number":1,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw1_1ee526ec.plutostate","title":"Images and Arrays","tags":["homework","module1","image","track_julia","track_math","track_climate","track_data","programming","interactive","type","matrix"],"description":"Practice Julia basics by working with arrays of colors. At the end of this homework, you can see all of your filters applied to your webcam image!"}},"images_abstractions/linear_transformation/":{"id":"images_abstractions/linear_transformation/","hash":"G2NI2SE8o4FpwPmQIKrW7jEtqAfssVhZQHy8hthsBMI","statefile_path":"./generated_assets/linear_transformation_1658a552.plutostate","notebookfile_path":"./generated_assets/linear_transformation_d948631b.jl","html_path":"images_abstractions/linear_transformation/index.html","frontmatter":{"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/linear_transformation_d948631b.jl","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/linear_transformation_1658a552.plutostate"}},"data_science/random_variables_as_types/":{"id":"data_science/random_variables_as_types/","hash":"BUUGFSEvIrvjmEf8Fmub5KJBiaIErlTyBYggngwB_Jg","statefile_path":"./generated_assets/random_variables_as_types_b61e2219.plutostate","notebookfile_path":"./generated_assets/random_variables_as_types_15064505.jl","html_path":"data_science/random_variables_as_types/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196570-478bbb89-05fb-4799-99a0-0ede06354cb6.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/random_variables_as_types_b61e2219.plutostate","youtube_id":"xKAO38UsXo0","video":"https://www.youtube.com/watch?v=xKAO38UsXo0","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/random_variables_as_types_15064505.jl","section":4,"order":4,"title":"Random Variables as Types","tags":["lecture","module2","track_julia","type","programming","probability","interactive","random","track_math","Symbolics"],"description":""}},"images_abstractions/newton_method/":{"id":"images_abstractions/newton_method/","hash":"55JEGwCdCuzUncbgxb5Sq7MNUr3BxL_sKTGBqmmvfGU","statefile_path":"./generated_assets/newton_method_b17f4123.plutostate","notebookfile_path":"./generated_assets/newton_method_1b4492e7.jl","html_path":"images_abstractions/newton_method/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196605-b6119b9d-223c-44bc-97d5-ef7cfce66483.gif","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/newton_method_b17f4123.plutostate","youtube_id":"Wjcx9sNSLP8","video":"https://www.youtube.com/watch?v=Wjcx9sNSLP8","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/newton_method_1b4492e7.jl","section":6,"order":6,"title":"The Newton Method","tags":["lecture","module1","track_math","continuous","differentiation","automatic differentiation","ForwardDiff","interactive","Symbolics","transformation"],"description":"This lecture explains a method for finding the root of a function, but using code an illustrations instead of a chalkboard! We will illustrate the Newton method using the ForwardDiff.jl package to carry out automatic differentiation, but we will also try to understand what's going on \"under the hood\"."}},"images_abstractions/images/":{"id":"images_abstractions/images/","hash":"R5p_wAzsLsvZMtGHlnmXzF5j-gpGwkCNjSgBAzt3ZDU","statefile_path":"./generated_assets/images_e0cd734c.plutostate","notebookfile_path":"./generated_assets/images_c07f9a47.jl","html_path":"images_abstractions/images/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196634-2294d0a7-e79a-40d0-bbb8-81da70f4d398.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/images_e0cd734c.plutostate","youtube_id":"3zTO3LEY-cM","video":"https://www.youtube.com/watch?v=3zTO3LEY-cM","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/images_c07f9a47.jl","section":1,"order":1,"title":"Images as Data and Arrays","tags":["lecture","module1","philip","track_julia","matrix","image"],"description":""}},"week0/plots_tutorial/":{"id":"week0/plots_tutorial/","hash":"aY3IurGDDg6OclnVgj1vea-_jgn0qhuz5ljgV_0OCI0","statefile_path":"./generated_assets/plots_tutorial_f74bd6fd.plutostate","notebookfile_path":"./generated_assets/plots_tutorial_bac88d69.jl","html_path":"week0/plots_tutorial/index.html","frontmatter":{"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/plots_tutorial_bac88d69.jl","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/plots_tutorial_f74bd6fd.plutostate"}},"homework/hw3/":{"id":"homework/hw3/","hash":"m_nvRtlSqL4VSwom00U34puu835Nb_u5RZ0JiqkWh50","statefile_path":"./generated_assets/hw3_e3d49892.plutostate","notebookfile_path":"./generated_assets/hw3_46eff99b.jl","html_path":"homework/hw3/index.html","frontmatter":{"chapter":1,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw3_46eff99b.jl","section":6.5,"order":6.5,"homework_number":3,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw3_e3d49892.plutostate","title":"Structure and language","tags":["homework","module1","track_julia","track_math","track_climate","linguistics","programming","interactive","type","structure","data","artificial intelligence"],"description":"Automatically detect the language of a piece of text, and generate realistic-looking random text!"}},"images_abstractions/transforming_images/":{"id":"images_abstractions/transforming_images/","hash":"yIc0Tt496vFum4azzL93QLJWJJI6QX8TEwk1b1HLKDU","statefile_path":"./generated_assets/transforming_images_5cd16c72.plutostate","notebookfile_path":"./generated_assets/transforming_images_4e3487c8.jl","html_path":"images_abstractions/transforming_images/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196626-194e81c9-00f7-49f6-90c3-09945723b6a3.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/transforming_images_5cd16c72.plutostate","youtube_id":"uZYVjDDZW9A","video":"https://www.youtube.com/watch?v=uZYVjDDZW9A","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/transforming_images_4e3487c8.jl","section":4,"order":4,"title":"Transformations with Images","tags":["lecture","module1","image","convolution","track_math","matrix","philip"],"description":""}},"climate_science/climate2_snowball_earth/":{"id":"climate_science/climate2_snowball_earth/","hash":"Y5PUHX40JNkDw8uqTx_PadAYnKu52i-t_XI91mciSec","statefile_path":"./generated_assets/climate2_snowball_earth_82d9339c.plutostate","notebookfile_path":"./generated_assets/climate2_snowball_earth_1dd49363.jl","html_path":"climate_science/climate2_snowball_earth/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199703-5edb4791-f9c0-4872-b0a7-7c9b1b6502d5.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/climate2_snowball_earth_82d9339c.plutostate","youtube_id":"cdIgr_2nUvI","video":"https://www.youtube.com/watch?v=cdIgr_2nUvI","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/climate2_snowball_earth_1dd49363.jl","section":6,"order":6,"title":"Snowball Earth and hysteresis","tags":["lecture","module3","dynamics","track_math","track_climate","hysteresis","equilibrium","climate","modeling","differential equation","ODE","plotting","feedback","runaway","nonlinear","snowball earth"],"description":""}},"climate_science/our_first_climate_model/":{"id":"climate_science/our_first_climate_model/","hash":"rYZOsmmHNG_AcQAy5GmchTKaozY7HJCOuBkUL1scytM","statefile_path":"./generated_assets/our_first_climate_model_f02b92e6.plutostate","notebookfile_path":"./generated_assets/our_first_climate_model_b24e86ad.jl","html_path":"climate_science/our_first_climate_model/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199705-7bdb6bb0-8698-43a1-87e6-c073ab102da5.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/our_first_climate_model_f02b92e6.plutostate","youtube_id":"J1UsMa1cTeE","video":"https://www.youtube.com/watch?v=J1UsMa1cTeE","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/our_first_climate_model_b24e86ad.jl","section":4,"order":4,"title":"Our first climate model","tags":["lecture","module3","climate","modeling","track_climate","track_math","plotting","data","ODE","differential equation","CSV","dataframe","track_data"],"description":""}},"data_science/pca/":{"id":"data_science/pca/","hash":"N4mytXpnpzLHkbNMI-IepgFTI2e6UZeSnUS6VJZmTmk","statefile_path":"./generated_assets/pca_ed1ecfd7.plutostate","notebookfile_path":"./generated_assets/pca_b5b28937.jl","html_path":"data_science/pca/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196577-512cee99-aebf-48a9-97b8-358d5ca561ca.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/pca_ed1ecfd7.plutostate","youtube_id":"iuKrM_NzxCk","video":"https://www.youtube.com/watch?v=iuKrM_NzxCk","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/pca_b5b28937.jl","section":1,"order":1,"title":"Principal Component Analysis","tags":["lecture","module2","track_data","data","statistics","matrix","linear algebra","track_math"],"description":"In this notebook we will start looking at more general kinds of data, not only images, and we'll try to extract some information from the image using statistical methods, namely principal component analysis. This method tries to answer the questions \"which 'directions' are the most important in the data\" and \"can we reduce the dimensionality (number of useful variables) of the data\"?"}},"homework/hw7/":{"id":"homework/hw7/","hash":"BpA0MREy3vMDvo45fMIa-y_-1CwlP97nA9F7YUzlG9I","statefile_path":"./generated_assets/hw7_e0e8657b.plutostate","notebookfile_path":"./generated_assets/hw7_31349006.jl","html_path":"homework/hw7/index.html","frontmatter":{"chapter":2,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw7_31349006.jl","section":7.5,"order":7.5,"homework_number":7,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw7_e0e8657b.plutostate","title":"Epidemic modeling I","tags":["homework","module2","epidemiology","track_data","monte carlo","statistics","track_math","ODE","agent based model","differential equation","type","structure","plotting"],"description":"Simulate the spread of an epidemic by creating your own agent-based model from scratch, and find statistics using the Monte Carlo method."}},"climate_science/time_stepping/":{"id":"climate_science/time_stepping/","hash":"lT6WCBbBXeqCnTHpe_bYMC2bCfA2jsOeWPNYRrsp_Eo","statefile_path":"./generated_assets/time_stepping_c8f2a018.plutostate","notebookfile_path":"./generated_assets/time_stepping_08963e95.jl","html_path":"climate_science/time_stepping/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199718-ff811eb3-aad6-4d6b-99e0-f6bf922816b4.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/time_stepping_c8f2a018.plutostate","youtube_id":"3Y5gVyO8KcI","video":"https://www.youtube.com/watch?v=3Y5gVyO8KcI","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/time_stepping_08963e95.jl","section":1,"order":1,"title":"Time stepping","tags":["lecture","module3","epidemiology","track_math","track_climate","plotting","continuous","discrete","ODE","differential equation","modeling"],"description":""}},"homework/hw2/":{"id":"homework/hw2/","hash":"oJEV1A4TTLMXptj_8Pbp8c4XPWgKP5wdj3Trdc70ZNI","statefile_path":"./generated_assets/hw2_825e3864.plutostate","notebookfile_path":"./generated_assets/hw2_d41591a0.jl","html_path":"homework/hw2/index.html","frontmatter":{"chapter":1,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw2_d41591a0.jl","section":4.5,"order":4.5,"homework_number":2,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw2_825e3864.plutostate","title":"Convolutions","tags":["homework","module1","track_data","track_julia","track_climate","track_math","convolution","matrix","interactive","image","type","webcam","programming"],"description":"Create your own image filters using mathematical convolution!"}},"data_science/simulating_component_failure/":{"id":"data_science/simulating_component_failure/","hash":"pSDWGEc3XyK_Xz3PSuUSc5N5acs8am5O9dhevVwfv8Y","statefile_path":"./generated_assets/simulating_component_failure_94051be7.plutostate","notebookfile_path":"./generated_assets/simulating_component_failure_18d620a5.jl","html_path":"data_science/simulating_component_failure/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196572-b11974d5-7335-4678-9092-630e034bbe8f.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/simulating_component_failure_94051be7.plutostate","youtube_id":"d8BohH76C7E","video":"https://www.youtube.com/watch?v=d8BohH76C7E","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/simulating_component_failure_18d620a5.jl","section":3,"order":3,"title":"Modeling with Stochastic Simulation","tags":["lecture","module2","track_julia","probability","statistics","track_math","epidemiology","interactive","plotting","programming","type","discrete","continuous","ODE","differential equation","agent based model"],"description":""}},"climate_science/predicting_the_weather/":{"id":"climate_science/predicting_the_weather/","hash":"sOGNeLlg3nlUSWU3BzlXEBtpeA-v4y5Brw51hrGjhGY","statefile_path":"./generated_assets/predicting_the_weather_3a88dd28.plutostate","notebookfile_path":"./generated_assets/predicting_the_weather_788de1b0.jl","html_path":"climate_science/predicting_the_weather/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199708-af8acad2-4172-4fa7-911e-e30300efb5ee.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/predicting_the_weather_3a88dd28.plutostate","youtube_id":"M3udLzIHtsc","video":"https://www.youtube.com/watch?v=M3udLzIHtsc","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/predicting_the_weather_788de1b0.jl","section":3,"order":3,"title":"Why we can't predict the weather","tags":["lecture","module3","track_climate","track_math","bifurcation","nonlinear","ODE","differential equation","continuous","plotting","dynamics","climate","modeling","DifferentialEquations"],"description":""}},"data_science/optimization/":{"id":"data_science/optimization/","hash":"ILQWp5FDdFkzUsZriKHzy2fzB5HY_cEluT81XNaDxE4","statefile_path":"./generated_assets/optimization_43fc4488.plutostate","notebookfile_path":"./generated_assets/optimization_a716b420.jl","html_path":"data_science/optimization/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136199719-a56a217b-cd36-4da2-b407-7285dcec94df.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/optimization_43fc4488.plutostate","youtube_id":"44RA9fclTdA","video":"https://www.youtube.com/watch?v=44RA9fclTdA","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/optimization_a716b420.jl","section":9,"order":9,"title":"Optimization","tags":["lecture","module2","track_juli","track_climate","track_data","track_math","optimization"],"description":"We use Optim.jl and JuMP.jl to optimize a function: automatically finding the input that maximizes output."}},"climate_science/how_to_collaborate_on_software/":{"id":"climate_science/how_to_collaborate_on_software/","hash":"8pA388qD_8LSBy9H5agJ7EjeqlTFxXEiJ1i031Tbe50","statefile_path":"./generated_assets/how_to_collaborate_on_software_16ca937d.plutostate","notebookfile_path":"./generated_assets/how_to_collaborate_on_software_f33790f2.jl","html_path":"climate_science/how_to_collaborate_on_software/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199704-ba6d0586-34bf-490c-8fd0-6959ab42cd23.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/how_to_collaborate_on_software_16ca937d.plutostate","youtube_id":"7N9Vvc8amGM","video":"https://www.youtube.com/watch?v=7N9Vvc8amGM","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/how_to_collaborate_on_software_f33790f2.jl","section":5,"order":5,"title":"GitHub & Open Source Software","tags":["lecture","module3","track_julia","track_data","track_climate","github","open source","collaboration","pull request","fork","software development"],"description":""}},"homework/hw0/":{"id":"homework/hw0/","hash":"lT4I0lCJaNPSzz-F_ddJF0tB3X3Qxqe_j62dWO4bAgk","statefile_path":"./generated_assets/hw0_393d9f59.plutostate","notebookfile_path":"./generated_assets/hw0_d2083e95.jl","html_path":"homework/hw0/index.html","frontmatter":{"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw0_d2083e95.jl","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw0_393d9f59.plutostate"}},"climate_science/resistors_and_stencils/":{"id":"climate_science/resistors_and_stencils/","hash":"TIijo2GqgZEToIzIfxCB0xsblaSLmoRZfK07wyJYd6M","statefile_path":"./generated_assets/resistors_and_stencils_4a5d83f8.plutostate","notebookfile_path":"./generated_assets/resistors_and_stencils_a3a3884c.jl","html_path":"climate_science/resistors_and_stencils/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136200635-33c007ff-89f6-48dc-b1d3-eb56fd16003e.gif","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/resistors_and_stencils_4a5d83f8.plutostate","youtube_id":"DdTWgBlDgr0","video":"https://www.youtube.com/watch?v=DdTWgBlDgr0","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/resistors_and_stencils_a3a3884c.jl","section":8,"order":8,"title":"Resistors, stencils and climate models","tags":["lecture","module3","PDE","differential equation","ghost cell","boundary condition","climate","modeling","track_climate","track_math","stencil"],"description":""}},"data_science/random_walks/":{"id":"data_science/random_walks/","hash":"lrYNUC76PMQzHzV23tMIzY0ESzGIzclDGfHi0smsUuY","statefile_path":"./generated_assets/random_walks_81b0ac1d.plutostate","notebookfile_path":"./generated_assets/random_walks_500db696.jl","html_path":"data_science/random_walks/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196563-f4b5b44c-5252-4e67-8c82-c550de891c55.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/random_walks_81b0ac1d.plutostate","youtube_id":"14hHtGJ4s-g","video":"https://www.youtube.com/watch?v=14hHtGJ4s-g","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/random_walks_500db696.jl","section":5,"order":5,"title":"Random Walks","tags":["lecture","module2","programming","track_julia","plotting","structure","type","interactive","random","statistics","track_math","track_data"],"description":""}},"climate_science/inverse_climate_model/":{"id":"climate_science/inverse_climate_model/","hash":"n8fldF9ybi00HObWhLFf4fbVLlvPIOoizzinM8KPjgE","statefile_path":"./generated_assets/inverse_climate_model_6adc7ddd.plutostate","notebookfile_path":"./generated_assets/inverse_climate_model_74e5c79f.jl","html_path":"climate_science/inverse_climate_model/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199660-315b045b-f144-4009-9282-7fe6d1f5d41b.gif","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/inverse_climate_model_6adc7ddd.plutostate","youtube_id":"nm86_hDwYTU","video":"https://www.youtube.com/watch?v=nm86_hDwYTU","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/inverse_climate_model_74e5c79f.jl","section":10,"order":10,"title":"Climate Economics","tags":["lecture","module3","climate","modeling","track_climate","track_math","plotting","track_julia","economics","optimization","JuMP","simulation"],"description":""}},"data_science/linearmodel_datascience/":{"id":"data_science/linearmodel_datascience/","hash":"hx17fXJVRQeNbo7akEKkUyWloF3xb3esYgSvo-y6sx4","statefile_path":"./generated_assets/linearmodel_datascience_001cd42b.plutostate","notebookfile_path":"./generated_assets/linearmodel_datascience_7d7b1d87.jl","html_path":"data_science/linearmodel_datascience/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136199721-8fd577cb-d6f3-492d-bbdc-37bc74664ca7.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/linearmodel_datascience_001cd42b.plutostate","youtube_id":"O6NTKsR8TjQ","video":"https://www.youtube.com/watch?v=O6NTKsR8TjQ","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/linearmodel_datascience_7d7b1d87.jl","section":8,"order":8,"title":"Linear Model, Data Science, & Simulations","tags":["lecture","module2","track_julia","track_data","csv","dataframe","statistics","plotting","interactive"],"description":""}},"climate_science/odes_and_parameterized_types/":{"id":"climate_science/odes_and_parameterized_types/","hash":"vZ4Q-ajd1orQhwu2wx4R4KD9lMi1eaLHzIddzKGaCp8","statefile_path":"./generated_assets/odes_and_parameterized_types_1138d0b5.plutostate","notebookfile_path":"./generated_assets/odes_and_parameterized_types_f9109ebd.jl","html_path":"climate_science/odes_and_parameterized_types/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136199713-25eb2e90-c5cd-4e11-8463-6f6069a81a51.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/odes_and_parameterized_types_1138d0b5.plutostate","youtube_id":"S71YIZ8e7MQ","video":"https://www.youtube.com/watch?v=S71YIZ8e7MQ","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/odes_and_parameterized_types_f9109ebd.jl","section":2,"order":2,"title":"ODEs and parameterized types","tags":["lecture","module3","epidemiology","plotting","ODE","differential equation","track_julia","track_math","programming","DifferentialEquations","type","structure","track_climate"],"description":""}},"images_abstractions/dynamicprograms/":{"id":"images_abstractions/dynamicprograms/","hash":"qhxVGEYEtSkWaZ-9GyugN9MHj2xGBaB9XiAuK4OSbuI","statefile_path":"./generated_assets/dynamicprograms_b7a33393.plutostate","notebookfile_path":"./generated_assets/dynamicprograms_18551caa.jl","html_path":"images_abstractions/dynamicprograms/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196599-c6ae60f0-9269-4134-bb0d-5bcab928bd2b.gif","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/dynamicprograms_b7a33393.plutostate","youtube_id":"KyBXJV1zFlo","video":"https://www.youtube.com/watch?v=KyBXJV1zFlo","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/dynamicprograms_18551caa.jl","section":7,"order":7,"title":"Dynamic Programming","tags":["lecture","module1","optimization","programming","track_julia","track_math","interactive","discrete","lazy"],"description":""}},"images_abstractions/transformations_and_autodiff/":{"id":"images_abstractions/transformations_and_autodiff/","hash":"q4fGNpJ-5BX-2zqFimo-wYEKvpaCh3tqjksTlFkCFFQ","statefile_path":"./generated_assets/transformations_and_autodiff_92c3f85d.plutostate","notebookfile_path":"./generated_assets/transformations_and_autodiff_36c687ab.jl","html_path":"images_abstractions/transformations_and_autodiff/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196632-ad67cb84-a4c9-410e-ab72-f4fcfc26f69a.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/transformations_and_autodiff_92c3f85d.plutostate","youtube_id":"AAREeuaKCic","video":"https://www.youtube.com/watch?v=AAREeuaKCic","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/transformations_and_autodiff_36c687ab.jl","section":3,"order":3,"title":"Automatic Differentiation","tags":["lecture","module1","track_julia","track_math","programming","function","transformation","automatic differentiation","continuous","derivative"],"description":"We use the package ForwardDiff.jl to automatically differentiate functions, on scalar and vector domains."}},"data_science/discrete_and_continuous/":{"id":"data_science/discrete_and_continuous/","hash":"l2MX-ZnSC-k_oJTg7Cu7jzmsJaha8LuL9XU1McXCZZ8","statefile_path":"./generated_assets/discrete_and_continuous_8593a7a9.plutostate","notebookfile_path":"./generated_assets/discrete_and_continuous_f9176397.jl","html_path":"data_science/discrete_and_continuous/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196552-ce16c06f-bd12-427f-80e5-aedb1fbc734a.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/discrete_and_continuous_8593a7a9.plutostate","youtube_id":"H6Dcx3YeTkE","video":"https://www.youtube.com/watch?v=H6Dcx3YeTkE","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/discrete_and_continuous_f9176397.jl","section":7,"order":7,"title":"Discrete and Continuous","tags":["lecture","module2","track_math","discrete","continuous"],"description":""}},"climate_science/2d_advection_diffusion/":{"id":"climate_science/2d_advection_diffusion/","hash":"w4TnYP94mdBFWDnf26PhkTnllAJvZkOOM52mx3PoS84","statefile_path":"./generated_assets/2d_advection_diffusion_59b732fc.plutostate","notebookfile_path":"./generated_assets/2d_advection_diffusion_60e784c3.jl","html_path":"climate_science/2d_advection_diffusion/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136200688-e3c6d6ee-808c-433f-8252-af6ad278fb4d.gif","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/2d_advection_diffusion_59b732fc.plutostate","youtube_id":"DdTWgBlDgr0","video":"https://www.youtube.com/watch?v=DdTWgBlDgr0","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/2d_advection_diffusion_60e784c3.jl","section":9,"order":9,"title":"Advection and diffusion in 2D","tags":["lecture","module3","PDE","differential equation","track_math","track_climate","structure","modeling","climate","simulation","fluid simulation","continuous","advection–diffusion"],"description":""}},"climate_science/advection_and_diffusion/":{"id":"climate_science/advection_and_diffusion/","hash":"a4cnypcEiq96VPNW7wMVySHIPJRlMVXwbqy_l1we29E","statefile_path":"./generated_assets/advection_and_diffusion_5789d346.plutostate","notebookfile_path":"./generated_assets/advection_and_diffusion_ca27876b.jl","html_path":"climate_science/advection_and_diffusion/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136200627-8211654f-7730-4f4a-8933-5b64164806c3.gif","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/advection_and_diffusion_5789d346.plutostate","youtube_id":"Xb-iUwXI78A","video":"https://www.youtube.com/watch?v=Xb-iUwXI78A","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/advection_and_diffusion_ca27876b.jl","section":7,"order":7,"title":"Advection and diffusion in 1D","tags":["lecture","module3","advection–diffusion","climate","modelling","PDE","differential equation","track_math","track_climate","simulation","plotting","fluid simulation","continuous"],"description":""}},"homework/hw8/":{"id":"homework/hw8/","hash":"5n69_7rxqxnMTEv7vxQkLwE2rw25hBlUOxntQ3OQjUw","statefile_path":"./generated_assets/hw8_07274dcd.plutostate","notebookfile_path":"./generated_assets/hw8_ffbd7ee6.jl","html_path":"homework/hw8/index.html","frontmatter":{"chapter":2,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw8_ffbd7ee6.jl","section":9.5,"order":9.5,"homework_number":8,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw8_07274dcd.plutostate","title":"Epidemic modeling II","tags":["homework","module2","track_math","track_data","optimization","statistics","gradient","differentiation","automatic differentiation","continuous","probability","epidemiology","monte carlo","modeling","plotting"],"description":"Learn about optimisation and gradient descent with help from our visuals and automatic checks. We use these new skill to fit parameters of an epidemic model to match reality."}},"homework/hw5/":{"id":"homework/hw5/","hash":"7Msk5OBNM_0uXqpRAk1VehE69pnTFLgH1VGnBVHLk6U","statefile_path":"./generated_assets/hw5_3f2df81a.plutostate","notebookfile_path":"./generated_assets/hw5_e424cbec.jl","html_path":"homework/hw5/index.html","frontmatter":{"chapter":2,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw5_e424cbec.jl","section":2.5,"order":2.5,"homework_number":5,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw5_3f2df81a.plutostate","title":"Structure","tags":["homework","module2","track_julia","structure","track_math","type","matrix","linear algebra","track_data"],"description":"Create your own Julia structs and add new functionality to them, to create first-class mathematical objects."}},"climate_science/optimization_with_JuMP/":{"id":"climate_science/optimization_with_JuMP/","hash":"vPPV7sjZ4W6XIBgm_5vTL6OSUGzMqdjHoHzA8o73Wko","statefile_path":"./generated_assets/optimization_with_JuMP_243709d9.plutostate","notebookfile_path":"./generated_assets/optimization_with_JuMP_eed5f3bc.jl","html_path":"climate_science/optimization_with_JuMP/index.html","frontmatter":{"chapter":3,"image":"https://user-images.githubusercontent.com/6933510/136200827-89647ae8-cb06-42ea-a18d-5f64e9cc2b25.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/optimization_with_JuMP_243709d9.plutostate","youtube_id":"nm86_hDwYTU","video":"https://www.youtube.com/watch?v=nm86_hDwYTU","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/optimization_with_JuMP_eed5f3bc.jl","section":11,"order":11,"title":"Solving inverse problems","tags":["lecture","module3","optimization","JuMP","track_math","track_climate","track_julia","modeling","plotting"],"description":""}},"homework/hw4/":{"id":"homework/hw4/","hash":"PAZ2ByPsloxVE4bJ3T6P_x-SNMd1IWNOKEfkGOi0vNQ","statefile_path":"./generated_assets/hw4_77566d9e.plutostate","notebookfile_path":"./generated_assets/hw4_0776063c.jl","html_path":"homework/hw4/index.html","frontmatter":{"chapter":1,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw4_0776063c.jl","section":9.5,"order":9.5,"homework_number":4,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw4_77566d9e.plutostate","title":"Dynamic programming","tags":["homework","module1","track_julia","track_math","structure","programming","dynamic programming","matrix","recursion"],"description":""}},"images_abstractions/seamcarving/":{"id":"images_abstractions/seamcarving/","hash":"J5RXhtOWFK7cg5Ap2Fec_wOTYb8DKBqO_ciP9Xs4UA0","statefile_path":"./generated_assets/seamcarving_96c85dc0.plutostate","notebookfile_path":"./generated_assets/seamcarving_86579427.jl","html_path":"images_abstractions/seamcarving/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196584-b3c806a8-aa61-48d9-9e73-30583fcc38bf.gif","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/seamcarving_96c85dc0.plutostate","youtube_id":"KyBXJV1zFlo","video":"https://www.youtube.com/watch?v=KyBXJV1zFlo","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/seamcarving_86579427.jl","section":8,"order":8,"title":"Seam Carving","tags":["lecture","module1","image","matrix","track_julia","track_climate","track_data","optimization","interactive"],"description":""}},"homework/hw6/":{"id":"homework/hw6/","hash":"qxhAXOklNwWx1DiqpgFJk3w5NCmzYxZ3I13_fZnRLS8","statefile_path":"./generated_assets/hw6_a7413284.plutostate","notebookfile_path":"./generated_assets/hw6_5c4018ab.jl","html_path":"homework/hw6/index.html","frontmatter":{"chapter":2,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw6_5c4018ab.jl","section":3.5,"order":3.5,"homework_number":6,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw6_a7413284.plutostate","title":"Probability distributions","tags":["homework","module2","track_julia","track_math","track_data","structure","probability","statistics","plotting","interactive"],"description":"Calculate a probability distribution from a dataset, experiment with different statistical models, and learn how to plot your results."}},"images_abstractions/structure/":{"id":"images_abstractions/structure/","hash":"WNuhCtbzZuMrBgGEU-uPt2bU-8gjbXBhL-9ROhcTMYc","statefile_path":"./generated_assets/structure_07796d7c.plutostate","notebookfile_path":"./generated_assets/structure_0aa1db58.jl","html_path":"images_abstractions/structure/index.html","frontmatter":{"chapter":1,"image":"https://user-images.githubusercontent.com/6933510/136196581-ffdf4a3b-f35c-4eb8-85a7-f07560bd421b.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/structure_07796d7c.plutostate","youtube_id":"wZrVxbmX218","video":"https://www.youtube.com/watch?v=wZrVxbmX218","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/structure_0aa1db58.jl","section":9,"order":9,"title":"Taking Advantage of Structure","tags":["lecture","module1","track_julia","type","programming","matrix","track_data","structure"],"description":""}},"homework/hw9/":{"id":"homework/hw9/","hash":"aibzOU750rT59qsBp8U_izGR3pVCFzREWg-84-73Rxk","statefile_path":"./generated_assets/hw9_0da6b17c.plutostate","notebookfile_path":"./generated_assets/hw9_39f3266a.jl","html_path":"homework/hw9/index.html","frontmatter":{"chapter":3,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw9_39f3266a.jl","section":1.5,"order":1.5,"homework_number":9,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw9_0da6b17c.plutostate","title":"Epidemic modeling III","tags":["homework","module3","epidemiology","track_julia","plotting","agent based model","statistics","random","modeling","monte carlo","track_climate"],"description":"In this problem set, we will look at a simple spatial agent-based epidemic model: agents can interact only with other agents that are nearby."}},"homework/hw10/":{"id":"homework/hw10/","hash":"3H2gBMoz2j_5wTp7wJkAfpqzm_lk4U7HSp_zkIzpNRo","statefile_path":"./generated_assets/hw10_850beff4.plutostate","notebookfile_path":"./generated_assets/hw10_04a07ddc.jl","html_path":"homework/hw10/index.html","frontmatter":{"chapter":3,"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw10_04a07ddc.jl","section":7.5,"order":7.5,"homework_number":10,"plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/hw10_850beff4.plutostate","title":"Climate modeling I","tags":["homework","module3","track_climate","track_julia","track_data","track_math","climate","plotting","interactive","modeling","climate model","economics","bifurcation","probability"],"description":"Play around with an energy balance model of the climate system, to explore the effect of doubling CO₂, and to examine the 'snowball earth' phenomenon."}},"week0/basic_syntax/":{"id":"week0/basic_syntax/","hash":"NvlmbLti-9kUoCRslIbBhzkSZHIkQcDGcDy9xoSBXW0","statefile_path":"./generated_assets/basic_syntax_a8a4d9ec.plutostate","notebookfile_path":"./generated_assets/basic_syntax_6c66f936.jl","html_path":"week0/basic_syntax/index.html","frontmatter":{"plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/basic_syntax_6c66f936.jl","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/basic_syntax_a8a4d9ec.plutostate"}},"images_abstractions/abstraction/":{"id":"images_abstractions/abstraction/","hash":"3lVHy2Snuw5-hgGJ2RFvTgNrupgWpyfJG-KOgg-EweQ","statefile_path":"./generated_assets/abstraction_f5f39a66.plutostate","notebookfile_path":"./generated_assets/abstraction_cb4755de.jl","html_path":"images_abstractions/abstraction/index.html","frontmatter":{"chapter":1,"image":"","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/abstraction_f5f39a66.plutostate","youtube_id":"3zTO3LEY-cM","video":"https://www.youtube.com/watch?v=3zTO3LEY-cM","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/abstraction_cb4755de.jl","section":2,"order":2,"title":"Abstraction","tags":["lecture","module1","track_julia","track_math"],"description":""}},"data_science/random_vars/":{"id":"data_science/random_vars/","hash":"RX72hvJGKXFBNU33qSlWb7iey8vvucqTviAfMDaLNDg","statefile_path":"./generated_assets/random_vars_e57a0e4f.plutostate","notebookfile_path":"./generated_assets/random_vars_86f67e45.jl","html_path":"data_science/random_vars/index.html","frontmatter":{"chapter":2,"image":"https://user-images.githubusercontent.com/6933510/136196576-70e45c9d-ef0e-4498-bf61-58d9ae854c3e.png","plutopages_statefile_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/random_vars_e57a0e4f.plutostate","youtube_id":"7HrpoFZzITI","video":"https://www.youtube.com/watch?v=7HrpoFZzITI","plutopages_notebook_url":"++magic#root#url~ZlfmvEoYMZ0N9FNtNiV2a++/generated_assets/random_vars_86f67e45.jl","section":2,"order":2,"title":"Sampling and Random Variables","tags":["lecture","module2","track_data","track_math","random","statistics","track_julia","image","probability","plotting","interactive"],"description":""}}},"pluto_version":"0.19.45","julia_version":"1.10.4","format_version":"1","title":null,"description":null,"collections":null} \ No newline at end of file diff --git a/Fall24/pp_search_data.json b/Fall24/pp_search_data.json index 118fadde..6565b6d6 100644 --- a/Fall24/pp_search_data.json +++ b/Fall24/pp_search_data.json @@ -1 +1 @@ -[{"url":"cheatsheets/","title":"Cheatsheets","tags":["welcome"],"text":"Cheatsheets Getting Started with Julia - live . Fastrack to Julia cheatsheet. MATLAB-Julia-Python comparative cheatsheet by QuantEcon group Plots.jl cheatsheet"},{"url":"clips/","title":"clips","tags":[],"text":"Submit A Short Clip Did you have a favorite (or many favorite) 1-2 minute clips from one of the lectures in this course? Consider sending them to us and that way we can share them on Twitter so more people can get exposure to computational thinking. Use this form to upload clips."},{"url":".","title":"index","tags":["homepage"],"text":""},{"url":"installation/","title":"Software installation","tags":["welcome"],"text":"First-time setup: Install Julia & Pluto Video version: Text and pictures version: Step 1: Install Julia 1.10.4 Go to https://julialang.org/downloads and download the current stable release, Julia 1.10.4 , using the correct version for your operating system (Linux x86, Mac, Windows, etc). Step 2: Run Julia After installing, make sure that you can run Julia . On some systems, this means searching for the “Julia 1.10.4 ” program installed on your computer; in others, it means running the command julia in a terminal. Make sure that you can execute 1 + 1 : Make sure that you are able to launch Julia and calculate 1+1 before proceeding! Step 3: Install Pluto Next we will install the Pluto , the notebook environment that we will be using during the course. Pluto is a Julia programming environment designed for interactivity and quick experiments. Open the Julia REPL . This is the command-line interface to Julia, similar to the previous screenshot. Here you type Julia commands , and when you press ENTER, it runs, and you see the result. To install Pluto, we want to run a package manager command . To switch from Julia mode to Pkg mode, type ] (closing square bracket) at the julia> prompt: \njulia> ]\n\n(@v 1.10 ) pkg>\n The line turns blue and the prompt changes to pkg> , telling you that you are now in package manager mode . This mode allows you to do operations on packages (also called libraries). To install Pluto, run the following (case sensitive) command to add (install) the package to your system by downloading it from the internet.\nYou should only need to do this once for each installation of Julia: \n(@v 1.10 ) pkg> add Pluto\n This might take a couple of minutes, so you can go get yourself a cup of tea! You can now close the terminal. Step 4: Use a modern browser: Mozilla Firefox or Google Chrome We need a modern browser to view Pluto notebooks with. Firefox and Chrome work best. Second time: Running Pluto & opening a notebook Repeat the following steps whenever you want to work on a project or homework assignment. Step 1: Start Pluto Start the Julia REPL, like you did during the setup. In the REPL, type: julia> using Pluto\n\njulia> Pluto.run()\n The terminal tells us to go to http://localhost:1234/ (or a similar URL). Let’s open Firefox or Chrome and type that into the address bar. If you’re curious about what a Pluto notebook looks like, have a look at the Featured Notebooks . These notebooks are useful for learning some basics of Julia programming. If you want to hear the story behind Pluto, have a look a the JuliaCon presentation . If nothing happens in the browser the first time, close Julia and try again. And please let us know! Step 2a: Opening a notebook from the web This is the main menu - here you can create new notebooks, or open existing ones. Our homework assignments will always be based on a template notebook , available in this GitHub repository. To start from a template notebook on the web, you can paste the URL into the blue box and press ENTER. For example, homework 0 is available here . Go to this page, and on the top right, click on the button that says “Edit or run this notebook”. From these instructions, copy the notebook link, and paste it into the box. Press ENTER, and select OK in the confirmation box. The first thing we will want to do is to save the notebook somewhere on our own computer; see below. Step 2b: Opening an existing notebook file When you launch Pluto for the second time, your recent notebooks will appear in the main menu. You can click on them to continue where you left off. If you want to run a local notebook file that you have not opened before, then you need to enter its full path into the blue box in the main menu. More on finding full paths in step 3. Step 3: Saving a notebook We first need a folder to save our homework in. Open your file explorer and create one. Next, we need to know the absolute path of that folder. Here’s how you do that in Windows , MacOS and Ubuntu . For example, you might have: C:\\Users\\fons\\Documents\\18S191_assignments\\ on Windows /Users/fons/Documents/18S191_assignments/ on MacOS /home/fons/Documents/18S191_assignments/ on Ubuntu Now that we know the absolute path, go back to your Pluto notebook, and at the top of the page, click on “Save notebook…” . This is where you type the new path+filename for your notebook : Click Choose . Step 4: Sharing a notebook After working on your notebook (your code is autosaved when you run it), you will find your notebook file in the folder we created in step 3. This the file that you can share with others, or submit as your homework assignment to Canvas. \nconst run = f => f();\nrun(async () => {\nconst versions = await (await fetch(`https://julialang-s3.julialang.org/bin/versions.json`)).json()\nconst sortby = v => v.split(\"-\")[0].split(\".\").map(parseFloat).reduce((a,b) => a*10000 + b)\nconst version_names = Object.keys(versions).sort((a,b) => sortby(a) - sortby(b)).reverse()\nconst stable = version_names.find(v => versions[v].stable)\nconsole.log({stable})\nconst pkg_stable = /\\d+\\.\\d+/.exec(stable)[0]\ndocument.querySelectorAll(\"auto-julia-version\").forEach(el => {\n console.log(el)\n el.innerText = el.getAttribute(\"short\") == null ? stable : pkg_stable\n})\n});"},{"url":"logistics/","title":"Class logistics","tags":["welcome"],"text":"main a img {\n width: 5rem;\n margin: 1rem;\n}\n Course logistics MIT Students Only Suggestion : Bookmark this page for easy access to everything you need to follow this class! Counts as 6.0001 and 6.0002 credit, or can be taken in addition. Counts as a course\n6 elective. Course materials will be published on this website on Monday 1:00pm . Each week is a new chapter , which includes: Asynchronous video lectures (total 60 minutes) Interactive visualizations Exercises Live lectures On Monday 1:00pm - 2:30pm , after the material is published, there will also be: Q&A on Discord Live overview lecture (30 minutes) On Wednesday 1:00pm 2:30pm (MIT students only) , you will meet with fellow students and your TA to: Review the lecture Work on problem sets in small groups or individually, with the opportunity to ask questions to your TA Start date: February 16, 2021 Discussion forum and homework submission Discord : discussion (we encourage you to hang out here during class!) MIT Only: Canvas : Homework submissions, Piazza Link. If you’re a non-MIT student, don’t worry, the homework has built-in answers checks , or you can find a partner to cross-grade homeworks via Discord. Evaluation The final grade is 80% problem sets, and 20% MITx quick questions . Problem sets are released on Friday and due the following Friday by 11:59pm EST. They have equal weight; your lowest score will be dropped. MITx exercises ( quick questions ) are due before Wednesday (11:59pm), but are best done on Monday, during or right after the lectures."},{"url":"reviews/","title":"Class reviews","tags":["welcome"],"text":"Student feedback Applications Feedback\n Spring 2020\n The Introduction to Computational Thinking with Julia class is a welcome departure from the paradigm of teaching the canonical computer science examples such as sorting algorithms and graph search that are often overused in introductory curricula. The class delves into real-world applications from the very beginning in a way that gives students an opportunity to be excited about the possibilities of computer science and mathematical modelling all while learning how to harness the power and elegance of the modern language of Julia. Class Feedback\n Spring 2020\n This is one of the best classes I have ever taken. I like how the content is divided into four main real-world applications of computational thinking, which made learning very enjoyable and also made working with Julia easier for me. I also found it amazing that this class provided applications of topics in differential equations and linear algebra classes and added a new way to view them. Unfortunately, I haven't found a similar class on computational thinking for the spring as the skills I gain are very practical and needed in research (I was impressed by the number of MISTI research opportunities that asked for experience with agent-based modeling or modeling in general.) Website Feedback\n Spring 2020\n The class website made the class lectures and homework easy to find in addition to the GitHub page and canvas. - The Discord channel and Piazza also made it easier to ask questions and see other students' questions and comments. - The feedback for assignments was very clear, and the instructors were willing to explain my mistakes further by email. Lecture Feedback\n Spring 2020\n The synchronous lectures were very comfortable, and the instructors encouraged us to ask questions. What other people are saying MIT's Free Online Course to Learn Julia — The Rising Star — Review of 18.S191 by Towards AI Newsletter the course is exceptional🎇🎇🎇 — YT Cai (@Yitao_CAI) December 15, 2020 I cannot recommend this class enough, it’s awesome. https://t.co/4BsBEygeLd — James Doss-Gollin (@jdossgollin) December 16, 2020"},{"url":"search/","title":"Search results","tags":[],"text":"window.init_search(); Search Results \nLoading..."},{"url":"semesters/","title":"Previous semesters","tags":["welcome"],"text":"This is the Fall 2024 edition For previous versions of this class, see: Fall 2023 Fall 2022 Spring 2021 Fall 2020 Spring 2020 Fall 2019"},{"url":"sidebar data/","title":"sidebar data","tags":[],"text":"Dict \"main\" \"welcome\" collections \"welcome\" .pages, \"Module 1 Images, Transformations, Abstractions\" collections \"module1\" .pages, \"Module 2 Social Science & Data Science\" collections \"module2\" .pages, \"Module 3 Climate Science\" collections \"module3\" .pages, , "},{"url":"structure/","title":"structure","tags":[],"text":""},{"url":"climate_science/2d_advection_diffusion/","title":"Advection and diffusion in 2D","tags":["lecture","module3","PDE","differential equation","track_math","track_climate","structure","modeling","climate","simulation","fluid simulation","continuous","advection–diffusion"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v DdTWgBlDgr0\" image \"https user images.githubusercontent.com 6933510 136200688 e3c6d6ee 808c 433f 8252 af6ad278fb4d.gif\" section 9 order 9 title \"Advection and diffusion in 2D\" layout \"layout.jlhtml\" youtube id \"DdTWgBlDgr0\" description \"\" tags \"lecture\", \"module3\", \"PDE\", \"differential equation\", \"track math\", \"track climate\", \"structure\", \"modeling\", \"climate\", \"simulation\", \"fluid simulation\", \"continuous\", \"advection–diffusion\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Statistics using Plots using PlutoUI using OffsetArrays end TableOfContents md\"\"\" PDEs in 2D Heat transport by ocean currents \"\"\" html\"\"\" iframe width \"700\" height \"394\" src \"https www.youtube nocookie.com embed 6 GQuVopmUM?start 15\" frameborder \"0\" allow \"accelerometer autoplay clipboard write encrypted media gyroscope picture in picture\" allowfullscreen iframe \"\"\" md\"\"\" One dimensional advection diffusion Let's recall from the previous lecture that we derived the advection diffusion equation with one spatial dimension \\frac \\partial T x,t \\partial t U \\frac \\partial T \\partial x \\kappa \\frac \\partial^ 2 T \\partial x^ 2 , where T x, t is the temperature, U is a constant advective velocity and \\kappa is the heat diffusivity . Two dimensional advection diffusion The two dimensional advection diffusion equation adds advection and diffusion operators acting in a second spatial dimension y that is perpendicular to x . The temperature field T is then a function T T x, y, t , and the PDE becomes \\frac \\partial T x,y,t \\partial t u x,y \\frac \\partial T \\partial x v x,y \\frac \\partial T \\partial y \\kappa \\left \\frac \\partial^ 2 T \\partial x^ 2 \\frac \\partial^ 2 T \\partial y^ 2 \\right , where \\vec u x,y u, v u\\,\\mathbf \\hat x v\\,\\mathbf \\hat y is a velocity vector field, and each partial derivative is also a function of x , y and t . We will think of x as being the longitudinal direction positive from west to east and y as the latitudinal direction positive from south to north . \"\"\" begin reviewBox bind show review CheckBox default false md\"\"\" Review of multivariable calculus identities and notation Check the box for a review reviewBox \"\"\" end if show review md\"\"\" The two dimensional advection diffusion equation is often written more succinctly as \\frac \\partial T x,y,t \\partial t \\vec u \\cdot \\nabla T \\kappa \\nabla^ 2 T, using the following shorthand notation. The gradient operator is defined as \\nabla \\equiv \\left \\frac \\partial \\partial x , \\frac \\partial \\partial y \\right such that \\nabla T \\left \\frac \\partial T \\partial x , \\frac \\partial T \\partial y \\right and \\vec u \\cdot \\nabla T u, v \\cdot \\left \\frac \\partial T \\partial x , \\frac \\partial T \\partial y \\right u \\frac \\partial T \\partial x v\\frac \\partial T \\partial y . The Laplacian operator \\nabla^ 2 , sometimes denoted \\Delta , is defined as \\nabla^ 2 \\frac \\partial^ 2 \\partial x^ 2 \\frac \\partial^ 2 \\partial y^ 2 such that \\nabla^ 2 T \\frac \\partial^ 2 T \\partial x^ 2 \\frac \\partial^ 2 T \\partial y^ 2 . The divergence operator is defined as \\nabla \\cdot \\quad , such that \\nabla \\cdot \\vec u \\left \\frac \\partial \\partial x , \\frac \\partial \\partial x \\right \\cdot u,v \\frac \\partial u \\partial x \\frac \\partial v \\partial y . Note Since seawater is largely incompressible , we have \\nabla \\cdot \\vec u \\frac \\partial u \\partial x \\frac \\partial v \\partial y 0 , i.e. ocean currents are approximately a non divergent flow . Among other implications, this allows us to write \\begin align \\vec u \\cdot \\nabla T& u\\frac \\partial T x,y,t \\partial x v\\frac \\partial T x,y,t \\partial y \\newline & u\\frac \\partial T \\partial x v\\frac \\partial T \\partial y T\\left \\frac \\partial u \\partial x \\frac \\partial v \\partial y \\right \\newline & \\left u\\frac \\partial T \\partial x T\\frac \\partial u \\partial x \\right \\left v\\frac \\partial T \\partial y \\frac \\partial v \\partial y \\right \\newline & \\frac \\partial uT \\partial x \\frac \\partial vT \\partial x \\newline & \\nabla \\cdot \\vec u T \\end align using the product rule separately in both x and y . The flux form two dimensional advection diffusion equation This lets us finally re write the two dimensional advection diffusion equation as \\frac \\partial T \\partial t \\nabla \\cdot \\vec u T \\kappa \\nabla^ 2 T, which is the form we will use in our numerical algorithm below. \"\"\" end md\"\"\" Numerical solution of the 2D equation Discretizing advection in two dimensions In Lecture XX we saw that in one dimension we can discretize a first partial derivative in space using the centered finite difference \\frac \\partial T x i , t n \\partial x \\approx \\frac T i 1 ^ n T i 1 ^ n 2 \\Delta x . In two dimensions, we discretize the partial derivative the exact same way, except that we also need to keep track of the cell index j in the y dimension \\frac \\partial T x i , y j , t n \\partial x \\approx \\frac T i 1,\\, j ^ n T i 1,\\,j ^ n 2 \\Delta x . The x gradient kernel below, implemented using the `OffsetArray` type, is shown below, and is reminiscent of the edge detection or sharpening kernels used in image processing and machine learning \"\"\" md\"\"\" The first order partial derivative in y is similarly discretized as \\frac \\partial T x i , y j , t n \\partial y \\approx \\frac T i,\\, j 1 ^ n T i,\\,j 1 ^ n 2 \\Delta y . Its kernel is shown below. \"\"\" md\"\"\" Now that we have discretized the two derivate terms, we can write out the advective tendency for computing T i, j, n 1 as u\\frac \\partial T \\partial x v\\frac \\partial T \\partial y \\approx u i,\\, j ^ n \\frac T i,\\, j 1 ^ n T i,\\,j 1 ^ n 2 \\Delta y v i,\\, j ^ n \\frac T i,\\, j 1 ^ n T i,\\,j 1 ^ n 2 \\Delta y . We implement this in julia as a series of methods for the `advect` function. The first method computes the advective tendency as a single `Float64` type for the i,j grid cell while the second method returns an array of the tendencies for each grid cell using two nested for loops. \"\"\" md\"\"\" Discretizing diffusion in two dimensions Just as with advection, the process for discretizing the diffusion operators effectively consists of repeating the one dimensional process for x in a second dimension y , separately \\kappa \\left \\frac \\partial^ 2 T \\partial x^ 2 \\frac \\partial^ 2 T \\partial y^ 2 \\right \\kappa \\left \\frac T i 1,\\ j ^ n 2T i,\\ j ^ n T i 1,\\ j ^ n \\left \\Delta x \\right ^ 2 \\frac T i,\\ j 1 ^ n 2T i,\\ j ^ n T i,\\ j 1 ^ n \\left \\Delta y \\right ^ 2 \\right The corresponding x and y second derivative kernels are shown below \"\"\" md\"\"\" Just as we did with advection, we implement a `diffuse` function using multiple dispatch \"\"\" md\"\"\" No flux boundary conditions We want to impose the no flux boundary conditions, which states that u\\frac \\partial T \\partial x \\kappa \\frac \\partial T \\partial x 0 at x boundaries and v\\frac \\partial T \\partial y \\kappa \\frac \\partial T \\partial y 0 at the y boundaries. To impose this, we treat i 1 and i N x as ghost cells , which do not do anything expect help us impose these boundaries conditions. Discretely, the boundary fluxes between i 1 and i 2 vanish if \\dfrac T 2,\\,j ^ n T 1,\\,j ^ n \\Delta x 0 or T 1,\\,j ^ n T 2,\\,j ^ n . Thus, we can implement the boundary conditions by updating the temperature of the ghost cells to match their interior point neighbors \"\"\" function update ghostcells A Array Float64,2 option \"no flux\" Atmp view A , if option \"no flux\" A 1, Atmp 2, Atmp end, Atmp end 1, A , 1 Atmp , 2 Atmp , end Atmp , end 1 end end md\"Let's get a feel for what this is actually doing\" begin A rand Float64, 6,6 heatmap A, size 200, 200 end | as svg begin Acopy copy A update ghostcells Acopy heatmap Acopy, size 200, 200 end | as svg md\"\"\" Timestepping \"\"\" md\"\"\" Data structures \"\"\" begin struct Grid N Int64 L Float64 Δx Float64 Δy Float64 x Array Float64,2 y Array Float64,2 Nx Int64 Ny Int64 function Grid N, L Δx L N m Δy L N m x 0. Δx 2. Δx L Δx 2. x reshape x, 1, size x,1 y L Δy 2. Δy L Δy 2. y reshape y, size y,1 , 1 Nx, Ny size x, 2 , size y, 1 return new N, L, Δx, Δy, x, y, Nx, Ny end end Base.zeros G Grid zeros G.Ny, G.Nx end struct Parameters κ Float64 end abstract type ClimateModel end begin struct OceanModel ClimateModel G Grid P Parameters u Array Float64,2 v Array Float64,2 end OceanModel G, P OceanModel G, P, zeros G , zeros G OceanModel G OceanModel G, Parameters 1.e4 , zeros G , zeros G end begin mutable struct ClimateModelSimulation ModelType ClimateModel model ModelType T Array Float64,2 Δt Float64 iteration Int64 end ClimateModelSimulation C ModelType, T, Δt where ModelType ClimateModelSimulation ModelType C, T, Δt, 0 end md\"\"\" Simulating heat transport by advective & diffusive ocean currents \"\"\" md\" Simulation controls \" begin quiverBox bind show quiver CheckBox default false anomalyBox bind show anomaly CheckBox default false md\"\"\" Click to show the velocity field quiverBox or to show temperature anomalies instead of absolute values anomalyBox \"\"\" end begin U ex Slider bind U ex Slider 4 1 8, default 0, show value false md\"\"\" U ex Slider \"\"\" end begin md\" Vary the current speed U 2. ^U ex × reference \" end begin κ ex Slider bind κ ex Slider 0. 1.e3 1.e5, default 1.e4, show value true md\"\"\" Vary the diffusivity κ κ ex Slider m² s \"\"\" end bind go ex Clock 0.1 md\"\"\" Some unit tests for verification \"\"\" function CFL adv sim ClimateModelSimulation maximum sqrt. sim.model.u.^2 sim.model.v.^2 sim.Δt sim.model.G.Δx end md\"\"\" Appendix \"\"\" Nvec 1 25 function plot state sim ClimateModelSimulation clims 1.1, 1.1 , show quiver true, show anomaly false, IC nothing model sim.model grid sim.model.G p plot xlabel \"longitudinal distance km \", ylabel \"latitudinal distance km \", clabel \"Temperature\", yticks grid.L 1000e3 grid.L , Int64. 1e 3 grid.L 1000e3 grid.L , xticks 0 1000e3 grid.L , Int64. 1e 3 0 1000e3 grid.L , xlims 0., grid.L , ylims grid.L, grid.L , X repeat grid.x, grid.Ny, 1 Y repeat grid.y, 1, grid.Nx if show anomaly arrow col black maxdiff maximum abs. sim.T . IC heatmap p, grid.x , grid.y , sim.T . IC, clims 1.1, 1.1 , color balance, colorbar title \"Temperature anomaly °C \", linewidth 0., size 400, 530 else arrow col white heatmap p, grid.x , grid.y , sim.T, color thermal, levels clims 1 clims 2 clims 1 21. clims 2 , colorbar title \"Temperature °C \", clims clims, linewidth 0., size 400, 520 end annotate p, 50e3, 6170e3, text string \"t \", Int64 round sim.iteration sim.Δt 60 60 24 , \" days\" , color black, left, 9 if show quiver Nq grid.N ÷ 5 quiver p, X Nq 1 ÷2 Nq end , Y Nq 1 ÷2 Nq end , quiver grid.L 4 . model.u Nq 1 ÷2 Nq end , model.v Nq 1 ÷2 Nq end , color arrow col, alpha 0.7 end as png p end md\"\"\" Computing the velocity field for a single circular vortex \"\"\" begin ∂x ϕ, Δx ϕ ,2 end ϕ ,1 end 1 Δx ∂y ϕ, Δy ϕ 2 end, ϕ 1 end 1, Δy xpad ϕ hcat zeros size ϕ,1 , ϕ, zeros size ϕ,1 ypad ϕ vcat zeros size ϕ,2 ', ϕ, zeros size ϕ,2 ' xitp ϕ 0.5 ϕ ,2 end ϕ ,1 end 1 yitp ϕ 0.5 ϕ 2 end, ϕ 1 end 1, function diagnose velocities ψ, G u xitp ∂y ψ, G.Δy G.L v yitp ∂x ψ, G.Δx G.L return u, v end end function impose no flux u, v u 1, . 0. v 1, . 0. u end, . 0. v end, . 0. u ,1 . 0. v ,1 . 0. u ,end . 0. v ,end . 0. end function PointVortex G Ω 1., a 0.2, x0 0.5, y0 0. x reshape 0. G.Δx G.L G.Δx G.L 1. G.Δx G.L , 1, G.Nx 1 y reshape 1. G.Δy G.L G.Δy G.L 1. G.Δy G.L , G.Ny 1, 1 function ψ̂ x,y r sqrt. y . y0 .^2 . x . x0 .^2 stream Ω 4 r.^2 stream r . a Ω a^2 4 1. . 2 log. r r . a a return stream end u, v diagnose velocities ψ̂ x, y , G impose no flux u, v return u, v end md\"\"\" Computing a quasi realistic ocean velocity field \\vec u u, v Our velocity field is given by an analytical solution to the classic wind driven gyre problem, which is given by solving the fourth order partial differential equation \\epsilon M \\hat \\nabla ^ 4 \\hat \\Psi \\frac \\partial \\hat \\Psi \\partial \\hat x \\nabla \\times \\hat \\tau \\mathbf z , where the hats denote that all of the variables have been non dimensionalized and all of their constant coefficients have been bundles into the single parameter \\epsilon M \\equiv \\dfrac \\nu \\beta L^3 . The solution makes use of an advanced asymptotic method valid in the limit that \\epsilon \\ll 1 known as boundary layer analysis see MIT course 18.305 to learn more . \"\"\" function DoubleGyre G β 2e 11, τ₀ 0.1, ρ₀ 1.e3, ν 1.e5, κ 1.e5, H 1000. ϵM ν β G.L^3 ϵ ϵM^ 1 3. x reshape 0. G.Δx G.L G.Δx G.L 1. G.Δx G.L , 1, G.Nx 1 y reshape 1. G.Δy G.L G.Δy G.L 1. G.Δy G.L , G.Ny 1, 1 ψ̂ x,y π sin. π y 1 . x exp. x 2 ϵ . cos. √3 x 2 ϵ . 1. √3 sin. √3 x 2 ϵ . ϵ exp. x . 1. ϵ u, v τ₀ ρ₀ β G.L H . diagnose velocities ψ̂ x, y , G impose no flux u, v return u, v end md\"\"\" Some simple initial temperature fields \"\"\" linearT G 0.5 1. . y G.L for y in G.y , 1 , x in G.x 1, function InitBox G value 1., nx 2, ny 2, xspan false, yspan false T zeros G T G.Ny÷2 ny G.Ny÷2 ny, G.Nx÷2 nx G.Nx÷2 nx . value if xspan T G.Ny÷2 ny G.Ny÷2 ny, . value end if yspan T , G.Nx÷2 nx G.Nx÷2 nx . value end return T end begin G Grid 10, 6.e6 P Parameters κ ex u, v zeros G , zeros G u, v PointVortex G, Ω 0.5 u, v DoubleGyre G IC InitBox G IC InitBox G, xspan true IC linearT G model OceanModel G, P, u 2. ^U ex, v 2. ^U ex Δt 12 60 60 ocean sim ClimateModelSimulation model, copy IC , Δt end begin heat capacity 51. total heat content sum heat capacity ocean sim.T ocean sim.model.G.Δx ocean sim.model.G.Δy 1e 15 mean temp mean ocean sim.T end begin go ex md\"\"\" Let's make sure our model conserves energy. We have not added any energy to the system advection and diffusion just move the energy around. The total heat content is round total heat content, digits 3 peta Joules and the average temperature is round mean temp, digits 2 °C. \"\"\" end md\" The CFL condition The CFL condition is defined by \\text CFL \\dfrac \\max\\left \\sqrt u² v² \\right Δt Δx round CFL adv ocean sim , digits 2 \" plot kernel A heatmap collect A , color bluesreds, clims maximum abs. A , maximum abs. A , colorbar false, xticks false, yticks false, size 30 30 size A, 2 , 30 30 size A, 1 , xaxis false, yaxis false begin xgrad kernel OffsetArray reshape 1., 0, 1. , 1, 3 , 0 0, 1 1 plot kernel xgrad kernel end begin ygrad kernel OffsetArray reshape 1., 0, 1. , 3, 1 , 1 1, 0 0 plot kernel ygrad kernel end begin function advect T, u, v, Δy, Δx, j, i return . u j, i . sum xgrad kernel 0, 1 1 . T j, i 1 i 1 2Δx . v j, i . sum ygrad kernel 1 1, 0 . T j 1 j 1, i 2Δy end advect T, u, v, Δy, Δx advect T, u, v, Δy, Δx, j, i for j 2 size T, 1 1, i 2 size T, 2 1 advect T, O OceanModel advect T, O.u, O.v, O.G.Δy, O.G.Δx end begin xdiff kernel OffsetArray reshape 1., 2., 1. , 1, 3 , 0 0, 1 1 ydiff kernel OffsetArray reshape 1., 2., 1. , 3, 1 , 1 1, 0 0 plot kernel xdiff kernel , plot kernel ydiff kernel end begin function diffuse T, κ, Δy, Δx, j, i return κ. sum xdiff kernel 0, 1 1 . T j, i 1 i 1 Δx^2 sum ydiff kernel 1 1, 0 . T j 1 j 1, i Δy^2 end diffuse T, κ, Δy, Δx diffuse T, κ, Δy, Δx, j, i for j 2 size T, 1 1, i 2 size T, 2 1 diffuse T, O OceanModel diffuse T, O.P.κ, O.G.Δy, O.G.Δx end function timestep sim ClimateModelSimulation OceanModel update ghostcells sim.T tendencies advect sim.T, sim.model . diffuse sim.T, sim.model sim.T 2 end 1, 2 end 1 . sim.Δt tendencies sim.iteration 1 end for i in 1 50 timestep ocean sim end let go ex if ocean sim.iteration 0 timestep ocean sim else for i in 1 50 timestep ocean sim end end plot state ocean sim, clims 0.1, 1 , show quiver show quiver, show anomaly show anomaly, IC IC end tvec map Nvec do Npower G Grid 8 Npower, 6.e6 P Parameters κ ex u, v DoubleGyre G u, v PointVortex G, Ω 0.5 u, v zeros G , zeros G model OceanModel G, P, u, v IC InitBox G IC InitBox G, nx G.Nx÷2 1 IC linearT G Δt 6 60 60 S ClimateModelSimulation model, copy IC , Δt return elapsed timestep S end begin plot 8 Nvec, tvec, xlabel \"Number of Grid Cells in x direction \", ylabel \"elapsed time per timestep s \" end | as svg "},{"url":"climate_science/advection_and_diffusion/","title":"Advection and diffusion in 1D","tags":["lecture","module3","advection–diffusion","climate","modelling","PDE","differential equation","track_math","track_climate","simulation","plotting","fluid simulation","continuous"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v Xb iUwXI78A\" image \"https user images.githubusercontent.com 6933510 136200627 8211654f 7730 4f4a 8933 5b64164806c3.gif\" section 7 order 7 title \"Advection and diffusion in 1D\" layout \"layout.jlhtml\" youtube id \"Xb iUwXI78A\" description \"\" tags \"lecture\", \"module3\", \"advection–diffusion\", \"climate\", \"modelling\", \"PDE\", \"differential equation\", \"track math\", \"track climate\", \"simulation\", \"plotting\", \"fluid simulation\", \"continuous\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, LinearAlgebra TableOfContents depth 4 md\"\"\" Evolution in time and space Advection and diffusion in 1D \"\"\" md\"\"\" So far we have been looking at dynamics in time, for example how does the temperature of the Earth change over time. But the Earth does not have a single, uniform temperature rather, at a particular moment in time, different places on Earth are at different temperatures, and those different temperatures change over time due to several mechanisms. In this notebook we will look at two fundamental mechanisms advection and diffusion . Let's think about the temperature in the ocean. Since the ocean is a fluid that is in motion, a warm \"parcel\" of water can flow move to a new location due to the physical motion of the water itself this is advection . And even if the water doesn't move, temperature or a higher concentration of some substance dissolved in the fluid can spread out due to molecular mechanisms this is diffusion . \"\"\" md\"\"\" In this notebook we will restrict ourselves to one spatial dimension 1D . So we will think about the temperature T , for example, being a function T t, x of two independent variables time, t space, x We want to calculate a value of the temperature T for each possible pair of values t, x , i.e. for all time 0 and all positions. The temperature at a given point will change due to different physical processes. We need to model this by writing down equations describing each physical process and how it affects the temperature. Since there are now two independent variables, t and x , we can expect to end up with derivatives with respect to both of these variables, so that the rate of change of temperature in time at a given point depends also on gradients of the temperature in space . This will lead to a partial differential equation that relates partial derivatives of T . In the context of climate modelling, we can think of x as being the latitude , supposing that the temperature is the same at all points with the same latitude. In this way we could model the fact that the poles are cold and the equator is warm, and we could model how heat flows from hot to cold. However, we clearly cannot model actual ocean currents like this, which would require two, or even three, spatial dimensions. \"\"\" md\"\"\" Visualising advection diffusion \"\"\" md\"\"\" Here is a visualisation of the physical processes of advection and diffusion in one dimension that we will discuss and build during this notebook. \"\"\" md\"\"\" U bind UU Slider 1 0.01 1, show value true, default 0 D bind DD Slider 0.2 0.001 0.2, show value true, default 0 \"\"\" md\"\"\" Temperature profiles and discretization \"\"\" md\"\"\" An ordinary differential equation needs an initial value for each variable. Similarly, we will need an initial function T 0 x that gives us the temperature at each position x . Let's suppose that the position is restricted to the interval 0, L x . As usual, to represent the continuous function T 0 x on a computer, we will need to discretise it in some way, i.e. approximate the continuous function by a finite set of numbers in the computer. The simplest but far from the only discretisation method is to sample the function at discrete grid points or nodes x i , for i 1, \\dots, N x . For simplicity we will take these equally spaced, with spacing x i 1 x i \\delta x L x N x . \"\"\" md\"\"\" For example, let's consider the following initial temperature profile \"\"\" T₀ x sin 2π x ^2 0.5 T₀ x sin 2π x 2 cos 4π x 0.2 md\"\"\" and define the grid points as follows \"\"\" begin Nₓ 20 Lₓ 1.0 δx Lₓ Nₓ xs δx 2 δx Lₓ end xs md\"\"\" It turns out to be a good idea to take the grid points at the centre of each interval, so that we have N x intervals and N x grid points, starting at x 1 \\delta x 2 and finishing at x N L x \\delta x 2 . \"\"\" md\"\"\" We call such a function of x at a given time a temperature profile . Let's draw it both as a function and as a heatmap \"\"\" md\"\"\" We will denote by T^0 i the initial temperature at grid point number i . \"\"\" md\"\"\" A useful way to think about T^n i is as some kind of spatial average of T t n, x over the interval of positions between neighbouring grid points, so T i is the average over the interval between x i \\frac \\delta x 2 and x i \\frac \\delta x 2 . We can thus think of the following piecewise constant approximation to the original continuous function \"\"\" let δx xs 2 xs 1 p plot 0 0.001 Lₓ, T₀, label \"T₀\", lw 1, ls dash scatter xs, T₀. xs , label \"sampled\" scatter xs, zero. xs , label \"x nodes\", alpha 0.5, ms 3, lw 2 for i in 1 length xs plot xs i δx 2, T₀ xs i , xs i δx 2, T₀ xs i , c green, lw 4, lab false plot xs i δx 2, 0 , xs i δx 2, T₀ xs i , xs i δx 2, T₀ xs i , xs i δx 2, 0 , c green, lw 1, lab false, ls dash, alpha 0.3 end xlabel \"x\" ylabel \"T₀ x \" end md\"\"\" Advection \"\"\" md\"\"\" Now let's think of this profile as representing the temperature in each small volume, or \"parcel\", of fluid. Let's suppose that the fluid is moving to the right with a constant, uniform speed U . Uniform here means that the speed is the same in all parts of the fluid. Then the temperature profile should also move with the fluid We call a quantity, such as the temperature, that is carried along with the fluid a tracer . If we fix our attention at a single, fixed point in space, say the grid point x i , the temperature there will vary over time, due to the fact that the fluid is moving past it. How it varies in time depends on the values at neighbouring grid points, since they determine how much heat will be transported into and out of the current cell. The point of view where we fix our attention at one point in space is called Eulerian . The alternative is to follow a parcel of fluid as it moves along in space this is called Lagrangian . \"\"\" md\"\"\" Visualising fluxes in a fluid \"\"\" md\"\"\" Let's visualise what happens as the fluid moves past a grid point, or rather the cell centered at a grid point. We will visualise tracer particles moving inside the fluid \"\"\" N 5000 begin xx abs. 2 . 4 . rand N .^ 2 . 1.5 yy rand N end δ 0.8 md\"\"\" show particles entering and leaving in \\delta t bind show particles CheckBox \"\"\" md\"\"\" t bind t Slider 0 0.001 2, show value true, default 0 \"\"\" md\"\"\" Time stepping \"\"\" md\"\"\" We want to model how the temperature profile changes in time due to the flow of the fluid. We'll do so by looking at each cell and asking how much heat enters and leaves the cell in a given time step, of duration \\delta t . \"\"\" md\"\"\" Let's call T^n i the approximate unknown average value of T in the cell at position x i and at the n th time step t n , i.e. an approximation of T t n, x i , where t n n \\, \\delta t . Then T^ n 1 i \\simeq T t n \\delta t, x i and T^ n i 1 \\simeq T t n, x i \\delta x . Note that the superscript n in these algorithms does not mean a power it's just a label for the time step. We could write T i^ n instead, but that is annoying to both write and read, so we omit the parentheses. \"\"\" md\"\"\" Suppose the fluid is moving to the right with speed U . During a time step of duration \\delta t , the temperature T^n i at cell i changes for two reasons some heat enters cell i some heat leaves cell i Note that most of the fluid that starts within cell i remains within that cell during the time step if the time step is short enough , as we see from the visualisation above. To calculate how much heat enters and leaves, note that only heat in the region of fluid within a distance U \\, \\delta t from the boundary of the cell will cross into that cell. So a proportion U \\, \\delta t \\delta x of the amount in cell i crosses the boundary. We will blur the distinction between \"amount of heat\" and temperature. Hence, roughly an amount T^n i U \\delta t \\delta x will leave cell number i and cross into cell i 1 the cell to the right . Similarly, an amount T^n i 1 U \\delta t \\delta x will enter cell i from the neighbouring cell i 1 on the left. Hence we arrive at the following T^ n 1 i T^ n i T^n i 1 T^n i \\, U \\, \\delta t \\delta x. Note that on the right hand side we have quantities at the time step n , and on the left at time step n 1 . So this tells us how to update our quantities from slice n to slice n 1 . \"\"\" md\"\"\" Continuous limit Advection equation PDE \"\"\" md\"\"\" Rearranging the previous equation we get \\frac T^ n 1 i T^ n i \\delta t \\frac T^n i 1 T^n i \\delta x \\, U. \"\"\" md\"\"\" Taking the continuum limit when \\delta t \\to 0 and \\delta x \\to 0 , we recognise the definition of partial derivatives with respect to time and space variables from multivariable calculus. Note the different indices that change on the two sides of the equation. Denoting these partial derivatives using \\partial , we arrive at the advection equation \\frac \\partial T t, x \\partial t U \\frac \\partial T t, x \\partial x , or for short \\frac \\partial T \\partial t U \\frac \\partial T \\partial x . Since T is a function of both x and t , and this equation involves partial derivatives with respect to both of the independent variables, this is a partial differential equation PDE . It describes how the function T t, x changes continuously as a function both of time and space. Although there are some analytical methods to solve PDEs, often it's necessary to use numerical methods. Here we'll look at simple numerical methods to solve such equations. \"\"\" md\"\"\" Numerics for the advection equation \"\"\" md\"\"\" Let's return to the version of the equation in which the value at the following time step is isolated T^ n 1 i T^ n i \\left U \\frac \\delta t \\delta x \\right T^n i T^n i 1 . In the last term on the right hand side, we see that we require combinations of values of T at the same time step from different places, with certain coefficients. \"\"\" md\"\"\" There are many approaches to implementing this numerically. The simplest is to directly transcribe the equation for the i th entry of the vector. Calling `T` the current vector, i.e. \\mathbf T ^n T^n i i 1, \\ldots, N x , and `T′` the new vector at the next time step, we have the following basic expression T′ i T i δt U T i 1 T i δx But now we realise a problem What should we do when i 1 ? This will try to access the index 0 of the vector `T`, which does not exist \"\"\" md\"\"\" Boundary conditions This illustrates the necessity of choosing boundary conditions that specify what happens at the edge of the domain. For simplicity we will choose to use periodic boundary conditions . This is a convenient mathematical fiction that allows us to treat all cells as being on the same footing, by wrapping the system around a torus, so that cells i 1 and i N x are neighbours. \"\"\" md\"\"\" We can then write this as follows, where we separate out the case i 1 \"\"\" function advection T, δt, δx, U N length T T′ similar T create new vector of the same length bulk cells for i in 2 N T′ i T i δt U T i T i 1 δx end boundary cells T′ 1 T 1 δt U T 1 T N δx periodic return T′ end md\"\"\" This performs a single time step of the advection equation it takes in the current vector of T s and returns the new T s after the step. Note that this is just like a step of the Euler method for solving ODEs, but where many spatial coordinates are updated at the same time. Effectively we are solving a system of coupled ODEs \"\"\" δt 0.001 U 0.2 assign colours to particles cs map xx do x if U δ x 0 1 elseif 1 U δ x 1 2 else 0 end end begin plot 0, 1, 1, 0, 0 , 0, 0, 1, 1, 0 , series shape, alpha 0.5, fill true, ratio 1, label false, leg false new xx xx . U . t scatter xx . U . t, yy, ms 1.5, alpha 0.1, c gray if show particles scatter new xx cs . 0 , yy cs . 0 , ms 1.5, alpha 0.5, c cs cs . 0 end plot 1.5, 2 , 0, 0 , c black plot 1.5, 2 , 1, 1 , c black xlims 2, 2 ylims 0.1, 1.1 as svg plot axis true, yticks 0, 1 end md\"\"\" Unfortunately this does not behave as we expect instead of preserving the shape of the profile over time, it is decaying. This is due to the way we are approximating. A better way to discretize the spatial derivative is using the following centered difference \\frac \\partial T t n, x i \\partial x \\simeq \\frac T^n i 1 T^n i 1 2 \\delta x \"\"\" function advection2 T, δt, δx, U N length T T′ similar T create new vector of the same length for i in 2 N 1 T′ i T i δt U T i 1 T i 1 2δx end periodic boundary T′ 1 T 1 δt U T 2 T N 2δx T′ N T N δt U T 1 T N 1 2δx return T′ end md\"\"\" Diffusion \"\"\" md\"\"\" Another key physical process is diffusion . This models how temperature or mass spreads out from hot or high concentration regions towards regions where it is cold or where there is a low concentration. Physical mechanism Random walks The physical mechanism behind this is random motion this is the continuous limit of equations describing the evolution of the probability distribution in space and time of a cloud of random walkers. This is the same process that we studied in lecture 2.6. Using our current notation, there we showed that the probability distribution of a cloud of random walkers satisfies the following time evolution p^ n 1 i \\frac 1 2 p^n i 1 p^n i 1 If now we say that the walkers jump only with a certain probability, with a large probability to stay in the same place, and that these random walkers are the carriers of heat, then we get T^ n 1 i \\kappa T^n i 1 2 T^n i T^n i 1 . Watch this video https www.youtube.com watch?v a3V0BJLIo c from last semester's class to see Grant Sanderson explaining this. \"\"\" md\"\"\" Continuous limit Heat equation PDE \"\"\" md\"\"\" Introducing \\delta x as the spatial discretisation, and \\delta t as the time step, we get T^ n 1 i \\kappa \\frac \\delta t \\delta x^2 T^n i 1 2 T^n i T^n i 1 . \"\"\" md\"\"\" The continuous limit is the following heat equation or diffusion equation \"\"\" md\"\"\" \\frac \\partial T \\partial t \\kappa \\frac \\partial^2 T \\partial x^2 . \"\"\" md\"\"\" Here, \\kappa is the heat diffusivity , which says how quickly heat spreads out. In the context of diffusion of mass the equivalent is the diffusion coefficient , D . \"\"\" md\"\"\" To obtain a numerical method to solve this equation, we again need to discretise this, in particular the second derivative. One possible discretisation is \\frac \\partial^2 T \\partial x^2 t n, x i \\simeq \\frac T^n i 1 2 T^n i T^n i 1 \\delta x^2 . \"\"\" md\"\"\" This may again be transcribed directly into code \"\"\" function diffusion T, δt, δx, D N length T T′ similar T create new vector of the same length for i in 2 N 1 T′ i T i δt D T i 1 2T i T i 1 δx^2 end periodic boundary T′ 1 T 1 δt D T 2 2T 1 T N δx^2 T′ N T N δt D T 1 2T N T N 1 δx^2 return T′ end md\"\"\" The advection diffusion PDE \"\"\" md\"\"\" Finally we can combine both mechanisms, to describe a tracer that is both being advected at a constant speed and diffusing. This basically utilises the composition of the advection and diffusion functions \"\"\" function advection diffusion T, δt, δx, U, D temp advection2 T, δt, δx, U return diffusion temp, δt, δx, D end md\"\"\" Function library \"\"\" function temperature heatmap x, T p heatmap x, 0. , collect T' , clims 1., 1. , cbar false, xticks nothing, yticks nothing return p end begin p1 plot 0 0.001 Lₓ, T₀, label \"T₀\", lw 3 scatter xs, T₀. xs , label \"sampled\" scatter xs, zero. xs , label \"x nodes\", alpha 0.5, ms 3 xlabel \"x\" ylabel \"T₀\" for x in xs plot x, 0 , x, T₀ x , ls dash, c black, label \"\", alpha 0.5 end hline 0 , ls dash, lab false p2 temperature heatmap xs, T₀. xs plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 end function evolve method, xs, δt, U, t final 10.0, f₀ T₀ T f₀. xs δx xs 2 xs 1 t 0.0 ts t results T while t t final T′ method T, δt, δx, U new push results, T′ t δt push ts, t T copy T′ end return ts, results end tt, results evolve advection diffusion, xs, δt, UU, DD md\"\"\" time step bind n6 Slider 1 length results , show value true \"\"\" ts, evolution evolve advection, xs, δt, U bind n Slider 1 length evolution , show value true let p1 plot xs, evolution n , m o, xlim 0, 1 , ylim 1.1, 1.1 , title \"t round ts n , digits 2 \", leg false p2 temperature heatmap xs, evolution n plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 end ts2, evolution2 evolve advection2, xs, δt, 0.1 n2 slider bind n2 Slider 1 length evolution2 , show value true let p1 plot xs, evolution2 n2 , m o, xlim 0, 1 , ylim 3.1, 3.1 , title \"t round ts2 n2 , digits 2 \", leg false p2 temperature heatmap xs, evolution2 n2 plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 end ts3, evolution3 evolve diffusion, xs, δt, 0.01 bind n3 Slider 1 length evolution3 , show value true let p1 plot xs, results n6 , m o, xlim 0, 1 , ylim 3.1, 3.1 , title \"t round ts3 n3 , digits 2 \", leg false p2 temperature heatmap xs, results n6 plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 , clim 1, 1 end let p1 plot xs, evolution3 n3 , m o, xlim 0, 1 , ylim 3.1, 3.1 , title \"t round ts3 n3 , digits 2 \", leg false p2 temperature heatmap xs, evolution3 n3 plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 , clim 1, 1 end ts5, evolution5 evolve advection diffusion, xs, δt, 1.0, 0.01 bind n5 Slider 1 length evolution5 , show value true let p1 plot xs, evolution5 n5 , m o, xlim 0, 1 , ylim 1.1, 1.1 , title \"t round ts3 n5 , digits 2 \", leg false p2 temperature heatmap xs, evolution5 n5 plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 , clim 1, 1 end "},{"url":"climate_science/climate2_snowball_earth/","title":"Snowball Earth and hysteresis","tags":["lecture","module3","dynamics","track_math","track_climate","hysteresis","equilibrium","climate","modeling","differential equation","ODE","plotting","feedback","runaway","nonlinear","snowball earth"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v cdIgr 2nUvI\" image \"https user images.githubusercontent.com 6933510 136199703 5edb4791 f9c0 4872 b0a7 7c9b1b6502d5.png\" section 6 order 6 title \"Snowball Earth and hysteresis\" layout \"layout.jlhtml\" youtube id \"cdIgr 2nUvI\" description \"\" tags \"lecture\", \"module3\", \"dynamics\", \"track math\", \"track climate\", \"hysteresis\", \"equilibrium\", \"climate\", \"modeling\", \"differential equation\", \"ODE\", \"plotting\", \"feedback\", \"runaway\", \"nonlinear\", \"snowball earth\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using DifferentialEquations, Plots, PlutoUI, LinearAlgebra end TableOfContents depth 4 md\"\"\" Julia Concepts `sign x ` \"\"\" md\"\"\" The sign or signum latin for \"sign\" function \"\"\" md\"\"\" `sign x ` returns 0 if x is 0, or ±1 for positive negative x. \"\"\" begin scatter sign, 5 .1 5, legend false, m c, ms 3, size 600, 300 title \"The sign function is discontinuous at 0\" xlabel \"x\" ylabel \"sign x \" end sign Inf sign Inf md\"\"\" Mathematics multiple equilibria. Using computation to explore hysteresis. \"\"\" md\"\"\" The function f y,a \\rm sign y a y can be written ``f y,a \\left\\ \\begin array l 1 a y \\ \\ \\rm if \\ y 0 \\ \\ \\textrm root at a 1 \\textrm if a 1 \\\\ \\ \\ \\ 1 a y \\ \\ \\rm if \\ y 0 \\ \\ \\textrm root at a 1 \\textrm if a 1 \\end array \\right.`` we will ignore y 0 . Notice that for 1 a 1 there are two roots. \"\"\" begin f y,a sign y a y f y,a,t f y,a just for the difeq solver end md\"\"\" The graph of `z f y,a ` consists of two parallel half planes. On the left below we intersect that graph with planes of constant `a`. On the right, we have the intersection with `z 0`. \"\"\" md\"\"\" a bind a Slider 5 .1 5, show value true, default 0 \"\"\" begin plot y f y,a, 0 , 9, eps , xlabel \"y\" , ylabel \"sign y y a\", lw 3,color blue plot y f y,a, 0 , eps ,9, lw 3, color blue ylims 5,10 xlims 9,9 hline 0 , ls dash, color pink, legend false if a 1 annotate 2.2 a, 4, text round 1 a,digits 1 , position right,11, red scatter 1 a , 0 ,m circle,ms 10,color red end if a 1 annotate 1 a, 4, text round 1 a,digits 1 , position right,11, blue scatter 1 a , 0 ,m circle,ms 10,color blue end vline 1 a , ls dash, color gray vline 1 a , ls dash, color gray vline 0 ,ls dash, color pink P1 title \" Plot of f y,a a vs y\" begin plot a a 1, 5 1, lw 2, c blue annotate a, 3 a, \"a a\" plot a a 1, 1 5, lw 2, c blue, legend false xlabel \"a\" ylabel \"y\" title \"Solution to f y,a 0\" if a 1 annotate 4, .7 a, text round 1 a,digits 1 , position center,11, red hline 1 a ,c gray,ls dash scatter a , 1 a ,m circle,ms 10,color red end if a 1 annotate 4, 1.3 a, text round 1 a,digits 1 , position center,11, blue hline 1 a ,c gray,ls dash scatter a , 1 a ,m circle,ms 10,color blue end ylims 5,5 P2 annotate 0, 4.7, text \"Two roots if 1 a 1\",6, blue vline a ,c gray,ls dash plot P1 ,P2,layout 1, 2 end end md\"\"\" The above are two section views of the surface z f y,a . Left constant a. Right z 0. \"\"\" begin s1 surface 5 .1 .1, 4 .1 4, f, legend false s2 surface .00001 .1 4.1, 4 .1 4, f, legend false surface 6 .05 4, 5 .05 4, f, legend false, alpha .5 plot 0, 6 , 1, 5 , 0,0 , c red plot 0,5 , 1, 4 , 0,0 , c red xlabel \"y\" ylabel \"a\" end gr md\"\"\" Solution to y' f y,a with y 0 y₀ \"\"\" md\"\"\" y₀ bind y₀ Slider 6 .1 6, show value true, default 2.0 \"\"\" sol solve ODEProblem f, y₀, 0, 10.0 , a function plotit y₀, a sol solve ODEProblem f, y₀, 0, 10.0 , a p plot sol , legend false, background color inside black , ylims 7, 7 , lw 3, c red plot direction field xs Float64 ys Float64 lrx LinRange xlims p ..., 30 for x in lrx for y in LinRange ylims p ..., 30 v 1, f y, a ,x v . 100 lrx 2 lrx 1 push xs, x v 1 , x v 1 , NaN push ys, y v 2 , y v 2 , NaN end end if a 1 hline 1 a ,c white,ls dash end if a 1 hline 1 a ,c white,ls dash end hline 0 ,c pink,ls dash plot xs, ys, alpha 0.7, c yellow ylabel \"y\" annotate .5,y₀,text \"y₀\",color red if a 1 annotate 5,1 a,text round 1 a,digits 3 ,color white end if a 1 annotate 5, 1 a .4,text round 1 a,digits 3 ,color white end title \"Solution to y' f y,a \" return p end plotit y₀, a md\"\"\" Hysteresis Increasing then decreasing ``a`` Let's increase a by .25 from 4 to 4 then decrease from 4 to 4. Every time we change a, we let 10 units of time evolve, enough to reach the equilibriumf for that a, and watch the y values. We see that when 1 a 1 it's possible to be at the \"negative\" equilibrium or the \"positive\" equilibrium, depending on how you got there. \"\"\" let Δt 10 t 0.0 y₀ 5 a 4 sol solve ODEProblem f, y₀, t, t Δt , a p plot sol annotate 10, 5,text \"a \",7 for i 1 32 a .25 t Δt y₀ sol t sol solve ODEProblem f, y₀, t, t Δt , a if 1≤a ≤1 p plot sol,xlims 0, t , legend false, fillrange 5,fillcolor gray,alpha .5 else p plot sol,xlims 0, t , legend false end if i%4 0 annotate t, 5,text round a,digits 1 , 7, blue end end for i 1 32 a .25 t Δt y₀ sol t sol solve ODEProblem f, y₀, t, t Δt , a if 1≤a≤1 p plot sol,xlims 0, t , legend false, fillrange 5,fillcolor gray,alpha .5 else p plot sol,xlims 0, t , legend false end if i%4 0 annotate t, 5,text round a,digits 1 , 7, red end end ylabel \"y\" as svg plot end md\"\"\" The dependence of the state of a system on its history, what we observe above, is known as hysteresis Greek ὑστέρησις for lagging behind . \"\"\" md\"\"\" Application to Snowball Earth, the ice albedo feedback from Henri Drake's lecture Review of the last climate lecture. Recall from Lecture 20 Part I https www.youtube.com watch?v Gi4ZZVS2GLA&t 15s that the the zero dimensional energy balance equation is \\begin gather \\color brown C \\frac dT dt \\ \\color black \\ \\color orange \\frac 1 α S 4 \\ \\color black \\ \\color blue A BT \\ \\color black \\ \\color grey a \\ln \\left \\frac \\text CO ₂ \\text CO ₂ \\text PI \\right , \\end gather \"\"\" html\"\"\" img src \"https raw.githubusercontent.com hdrake hdrake.github.io master figures planetary energy balance.png\" height 230 \"\"\" md\"\"\" Today, we will ignore changes in CO₂, so that \\ln \\left \\frac \\text CO ₂ \\text PI \\text CO ₂ \\text PI \\right \\ln 1 0 and the model simplifies to \\begin gather \\color brown C \\frac dT dt \\ \\color black \\ \\color orange \\frac 1 α S 4 \\ \\color black \\ \\color blue A BT . \\end gather The dynamics of this Ordinary Differential Equation ODE are quite simple because it is linear we can rewrite it in the form \\dot T f T t where f x \\alpha x \\beta is a linear function of x. A linear ODE permits only one stable solution, \\dot T 0 , which in Lecture 20 we found was Earth's pre industrial temperature T 0 14 °C. In this lecture, we show how a small modification that makes one term in our simple climate model non linear completely changes its dynamics, allowing us to explain the existence of both \"Snowball Earth\" and the relatively warm pre industrial climate that allowed humans to thrive. \"\"\" md\"\"\" 1 Background Snowball Earth Geological evidence shows that the Neoproterozoic Era 550 to 1000 million years ago is marked by two global glaciation events, in which Earth's surface was covered in ice and snow from the Equator to the poles see review by Pierrehumbert et al. 2011 https www.annualreviews.org doi full 10.1146 annurev earth 040809 152447 . \"\"\" html\"\"\" img src \"https news.cnrs.fr sites default files styles asset image full public assets images frise earths glaciations 72dpi.jpg?itok MgKrHlIV\" height 500 \"\"\" html\"\"\" img src \"https upload.wikimedia.org wikipedia commons d df Ice albedo feedback.jpg\" height 350 \"\"\" md\"\"\" We can represent the ice albedo feedback crudely in our energy balance model by allowing the albedo to depend on temperature \\alpha T \\begin cases \\alpha i & \\mbox if \\ \\ T \\leq 10\\text °C &\\text completely frozen \\\\ \\alpha i \\alpha 0 \\alpha i \\frac T 10 20 & \\mbox if \\ \\ 10\\text °C \\leq T \\leq 10\\text °C &\\text partially frozen \\\\ \\alpha 0 &\\mbox if \\ \\ T \\geq 10\\text °C &\\text no ice \\end cases \"\"\" md\"\"\" 1.2 Adding the ice albedo feedback to our simple climate model First, we program albedo as a function of temperature. \"\"\" md\"\"\" To add this function into our energy balance model from Lecture 20 Part I https www.youtube.com watch?v Gi4ZZVS2GLA&t 15s which we've copied into the cell below , all we have to do is overwrite the definition of the `timestep ` method to specify that the temperature dependent albedo should be updated based on the current state \"\"\" module Model const S 1368 solar insolation W m^2 energy per unit time per unit area const α 0.3 albedo, or planetary reflectivity unitless const B 1.3 climate feedback parameter W m^2 °C , const T0 14. preindustrial temperature °C absorbed solar radiation α α, S S S 1 α 4 W m^2 outgoing thermal radiation T A A, B B A B T const A S 1. α 4 B T0 W m^2 . greenhouse effect CO2 a a, CO2 PI CO2 PI a log CO2 CO2 PI const a 5.0 CO2 forcing coefficient W m^2 const CO2 PI 280. preindustrial CO2 concentration parts per million ppm CO2 const t CO2 PI constant CO2 concentrations const C 51. atmosphere and upper ocean heat capacity J m^2 °C function timestep ebm append ebm.T, ebm.T end ebm.Δt tendency ebm append ebm.t, ebm.t end ebm.Δt end tendency ebm 1. ebm.C absorbed solar radiation α ebm.α, S ebm.S outgoing thermal radiation ebm.T end , A ebm.A, B ebm.B greenhouse effect ebm.CO2 ebm.t end , a ebm.a, CO2 PI ebm.CO2 PI begin mutable struct EBM T Array Float64,1 t Array Float64,1 Δt Float64 CO2 Function C Float64 a Float64 A Float64 B Float64 CO2 PI Float64 α Float64 S Float64 end Make constant parameters optional kwargs EBM T Array Float64,1 , t Array Float64,1 , Δt Float64, CO2 Function C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S EBM T, t, Δt, CO2, C, a, A, B, CO2 PI, α, S Construct from float inputs for convenience EBM T0 Float64, t0 Float64, Δt Float64, CO2 Function C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S EBM T0 , t0 , Δt, CO2 C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S end begin function run ebm EBM, end year Real while ebm.t end end year timestep ebm end end run ebm run ebm, 200. run for 200 years by default end CO2 hist t CO2 PI 1 . fractional increase t fractional increase t t . 1850. 220 .^3 begin hist EBM T0, 1850., 1., CO2 hist, C C, B B, A A run hist, 2020. end begin CO2 RCP26 t CO2 PI 1 . fractional increase t . min. 1., exp. t . 1850. . 170 100 RCP26 EBM T0, 1850., 1., CO2 RCP26 run RCP26, 2100. CO2 RCP85 t CO2 PI 1 . fractional increase t . max. 1., exp. t . 1850. . 170 100 RCP85 EBM T0, 1850., 1., CO2 RCP85 run RCP85, 2100. end end md\"\"\" 1.1 The ice albedo feedback In Lecture 20, we used a constant value α Model.hist.α for Earth's planetary albedo, which is a reasonable thing to do for small climate variations relative to the present such as the difference between the present day and preindustrial climates . In the case of large variations, however, this approximation is not very reliable. While oceans are dark and absorbant, α ocean \\approx 0.05 , ice and snow are bright and reflective \\alpha ice,\\,snow \\approx 0.5 to 0.9 . Thus, if much of the ocean's surface freezes over, we expect Earth's albedo to rise dramatically, causing more sunlight to be reflected to space, which in turn causes even more cooling and more of the ocean to freeze, etc. This non linear positive feedback effect is referred to as the ice albedo feedback see illustration below . \"\"\" function α T α0 Model.α, αi 0.5, ΔT 10. if T ΔT return αi elseif ΔT T ΔT return αi α0 αi T ΔT 2ΔT elseif T ΔT return α0 end end begin T example 20. 1. 20. plot size 600, 400 , ylims 0.2, 0.6 plot 20, 10 , 0.2, 0.2 , fillrange 0.6, 0.6 , color lightblue, alpha 0.2, label nothing plot 10, 20 , 0.2, 0.2 , fillrange 0.6, 0.6 , color red, alpha 0.12, label nothing plot T example, α. T example , lw 3., label \"α T \", color black plot ylabel \"albedo α\\n planetary reflectivity \", xlabel \"Temperature °C \" annotate 15.5, 0.252, text \"completely\\nfrozen\", 10, darkblue annotate 15.5, 0.252, text \"no ice\", 10, darkred annotate 0.3, 0.252, text \"partially frozen\", 10, darkgrey end function Model.timestep ebm ebm.α α ebm.T end Added this line append ebm.T, ebm.T end ebm.Δt Model.tendency ebm append ebm.t, ebm.t end ebm.Δt end md\"\"\" 2 Multiple Equilibria OR the existence of \"alternate Earths\" Human civilization flourished over the last several thousand years in part because Earth's global climate has been remarkably stable and forgiving. The preindustrial combination of natural greenhouse effect and incoming solar radiation yielded temperatures between the freezing and boiling points of water across most of the planet, allowing ecoystems based on liquid water to thrive. The climate system, however, is rife with non linear effects like the ice albedo effect , which reveal just how fragile our habitable planet is and just how unique our stable pre industrial climate was. We learned in Lecture 20 that in response to temperature fluctuations, net negative feedbacks act to restore Earth's temperature back towards a single equilibrium state in which absorbed solar radiation is balanced by outgoing thermal radiation. Here, we explore how non linear positive feedbacks can temporarily result in a net positive feedback and modify Earth's state space. \"\"\" md\"\"\" 2.1 Exploring the non linear ice albedo feedback In Lecture 20 Part II https www.youtube.com watch?v D3jpfeQCISU , we learned how introducing non linear terms in ordinary differential equations can lead to complex state spaces that allow for multiple fixed points e.g. for \\dot x \\mu x^ 2 . Let's explore how this plays out with the non linear ice albedo feedback by varying the initial condition T 0 \\equiv T t 0 and allowing the system to evolve for 200 years. \"\"\" begin T0 slider bind T0 interact Slider 60. 0.0025 30., default 24., show value true md\"\"\" T₀ T0 slider °C\"\"\" end begin ebm interact Model.EBM Float64 T0 interact , 0., 1., Model.CO2 const Model.run ebm interact, 200 end md\"We can get an overview of the behavior by drawing a set of these curves all on the same graph \" begin p equil plot xlabel \"year\", ylabel \"temperature °C \", legend bottomright, xlims 0, 205 , ylims 60, 30. plot 0, 200 , 60, 60 , fillrange 10., 10. , fillalpha 0.3, c lightblue, label nothing annotate 120, 20, text \"completely frozen\", 10, darkblue plot 0, 200 , 10, 10 , fillrange 30., 30. , fillalpha 0.09, c red, lw 0., label nothing annotate 120, 25, text \"no ice\", 10, darkred for T0 sample in 60. 5. 30. ebm Model.EBM T0 sample, 0., 1., Model.CO2 const Model.run ebm, 200 plot p equil, ebm.t, ebm.T, label nothing end T un 7.5472 for δT in 1.e 2 2, 1., 0., 1., 2. ebm un Model.EBM T un δT, 0., 1., Model.CO2 const Model.run ebm un, 200 plot p equil, ebm un.t, ebm un.T, label nothing, linestyle dash end plot p equil, 200 , Model.T0 , markershape circle, label \"Our pre industrial climate stable ''warm'' branch \", color orange, markersize 8 plot p equil, 200 , 38.3 , markershape circle, label \"Alternate universe pre industrial climate stable ''cold'' branch \", color aqua, markersize 8 plot p equil, 200 , T un , markershape diamond, label \"Impossible alternate climate unstable branch \", color lightgrey, markersize 8, markerstrokecolor white, alpha 1. p equil end md\"We see that for T₀ ⪆ round T un, digits 2 °C, all of the curves seem to converge on the T 14°C equilibrium or fixed point that we saw in Lecture 20. Curves that start below this value warm up and while curves that start above this value will cool down. For T₀ ⪅ round T un, digits 2 °C, however, the temperatures converge on a much colder equilibrium around T 40°C. This is the Snowball Earth equilibrium. These two states are referred to as stable equilibria because even if the state gets temporarily pushed slightly away from its equilibrium, it will eventually converge right back to its equilibrium. So what happens is T₀ ≈ round T un, digits 2 °C? For some exact temperature near there, there is indeed an equilibrim state if you start with that temperature you will stay there forever. However, if the temperature starts off even one one hundredth of a degree above or below this exact value, we see that temperatures eventually converge to one of the other two equilibria. Thus, we call this intermediate equilibrium an unstable equilibrium , because any infinitesimal push away will cause it to careen away towards another state. \" md\"\"\" 2.2 Radiative stability analysis We can understand why our model has two stable equilibria and one unstable equilibrium by applying concepts from dynamical systems theory. Recall that, with fixed CO₂ concentrations, our energy balance model differential equation can be expressed as C\\frac d\\,T d\\,t \\text ASR T \\text OTR T , where now the Absorbed Solar Radiation ASR is also temperature dependent because the albedo α T is. In particular, by plotting the right hand side tendency terms as a function of the state variable T , we can plot a stability diagram for our system that tells us whether the planet will warm C \\frac d\\,T d\\,t 0 or cool C \\frac d\\,T d\\,t 0 . \"\"\" begin T samples 60. 1. 30. OTR Model.outgoing thermal radiation. T samples ASR Model.absorbed solar radiation. α α T sample for T sample in T samples imbalance ASR . OTR end begin p1 stability plot legend topleft, ylabel \"energy flux W m² \", xlabel \"temperature °C \" plot p1 stability, T samples, OTR, label \"Outgoing Thermal Radiation\", color blue, lw 2. plot p1 stability, T samples, ASR, label \"Absorbed Solar Radiation\", color orange, lw 2. p2 stability plot ylims 50, 45 , ylabel \"energy flux W m² \", xlabel \"temperature °C \" plot 60., 30. , 100, 100 , fillrange 0, 0 , color blue, alpha 0.1, label nothing plot 60., 30. , 100, 100 , fillrange 0, 0 , color red, alpha 0.1, label nothing annotate 58, 40, text \"cooling\", left, darkblue annotate 58, 38, text \"warming\", left, darkred plot p2 stability, T samples, imbalance, label \"Radiative Imbalance\\n ASR OTR \", color black, lw 2. plot 7.542 , 0 , markershape diamond, markersize 6, color lightgrey, markerstrokecolor darkgrey, label nothing plot Model.T0 , 0 , markershape circle, markersize 6, color orange, markerstrokecolor black, label nothing plot 38.3 , 0 , markershape circle, markersize 6, color aqua, markerstrokecolor black, label nothing p stability plot p1 stability, p2 stability, layout 1, 2 , size 680, 300 end md\" 3 Transitioning to and from Snowball Earth 3.1 Turning up the Sun Over the entire history of the Earth, the Sun is thought to have brightened by about 40%.\" html\"\"\" img src \"https rainbow.ldeo.columbia.edu courses s4021 ec98 fys.gif\" alt \"Plot showing increasing solar luminosity over time\" height 300 \"\"\" md\"In the Neoproterozoic ~700 million years ago , the Sun was 93% as bright as it is today, such that the incoming solar radiation was S 1272 W m², Earth's average temperature plunged to T 50 °C, and Earth's ice covered surface had a high albedo reflectivity of α i 0.5 . 3.2 Did the increasing brightness of the Sun melt the Snowball? If we start out in the Neoproterozoic climate and all we do is increase solar insolation to today's value of S 1368 W m², can we warm the planet up to the pre industrial temperature of T 14 °C? \" md\"\"\" Extend upper limit of insolation bind extend S CheckBox default false \"\"\" if extend S md\"\"\" \"Cold\" branch bind show cold CheckBox default false | \"Warm\" branch bind show warm CheckBox default false | Unstable branch bind show unstable CheckBox default false \"\"\" else show cold true nothing end if extend S md\"\"\" Abrupt climate transitions In this model, temperature variations are fairly smooth unless temperatures rise above 10°C or fall below 10°C, in which case the ice albedo positive feedback kicks in and causes an abrupt climate transition . While this is just a simple hypothetical model, these kinds of abrupt climate transitions show up all the time in the paleoclimate record and in more realistic climate model simulations. https www.pnas.org content pnas 115 52 13288 F1.large.jpg This simulation teaches us that we should not take the stability of our climate for granted and that pushing our present climate outside of its historical regime of stability could trigger catastrophic abrupt climate transitions. \"\"\" end md\"\"\" 3.3 If not the Sun, how did Snowball Earth melt? The leading theory is that a slow but steady outgassing of CO₂ from volcanoes eventually caused a strong enough greenhouse gas effect to offset the cooling effect of the frozen surface's high albedo and raise temperatures above the melting point 10 °C. \"\"\" html\"\"\" img src \"https hartm242.files.wordpress.com 2011 03 snowball earth.jpg\" width 680 \"\"\" md\"\"\" In Homework 9 , you will extend the above model to include the effect of CO₂ and determine how much CO2 would need to be added to the snowball for it to melt. \"\"\" md\"\"\" 4 Towards realistic climate modelling In this simple model, the preindustrial climate of T 14 °C is so warm that there is no ice anywhere on the planet. Indeed, the only two valid stable climates are one with no ice or one with ice everywhere . So how did Earth's preindustrial climate, which was relatively stable for thousands of years, have substantial ice caps at the poles? \"\"\" md\"\"\" The \"Aquaplanet\", a simple three dimensional ocean world climate model An \"Aquaplanet\" is a three dimensional global climate simulation of a hypothetical planet covered entirely by a single global Ocean. While this is of course very different from Earth, where 27% of the planet is covered in land, the \"Aquaplanet\" exhibits many of the same characteristics as Earth and is much more realistic than our zero dimensional climate model above. The video below shows that the Aquaplanet simulation exhibits a third equilibrium state, with a mostly liquid ocean but ice caps at the poles , in addition to the two we found in our zero dimensional model. In Homework 10 2020 class , you will build a simple two dimensional version of the aqua planet and explore its stability. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid lYm IyBHMUs params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" Appendix \"\"\" md\" Pluto Magic\" function ingredients path String this is from the Julia source code evalfile in base loading.jl but with the modification that it returns the module instead of the last object name Symbol basename path m Module name Core.eval m, Expr toplevel, eval x Expr core, eval name, x , include x Expr top, include name, x , include mapexpr Function, x Expr top, include mapexpr, name, x , include path m end md\"\"\" Bifurcation diagram helper functions \"\"\" function restart ebm ebm ebm.T ebm.T end ebm.t ebm.t 1 end function plot trajectory p, x, y lw 8 n size x,1 plot x, y, color \"black\", alpha collect 1 n 1 n 1. , linewidth collect 0. lw n lw , label nothing plot x end , y end , color \"black\", marker c, markersize lw 2 1.2, label nothing, markerstrokecolor nothing, markerstrokewidth 0. return p end begin Smin 1200 Smax 1800 Smax limited 1650 Svec Smin 1. Smax Svec vcat Svec, reverse Svec Tvec zeros size Svec local T restart 100. for i, S enumerate Svec ebm Model.EBM T restart, 0., 5., Model.CO2 const ebm.S S Model.run ebm, 400. T restart ebm.T end Tvec i deepcopy T restart end md\" Data structures for storing warm & cool branch climates \" end md\"\"\" For insolations S between Smin W m² and Smax limited W m², temperatures always remain below 10°C and the planet remains completely frozen. What if we extend the upper limit on insolation so that the Sun becomes bright enough to start melting ice? \"\"\" begin T unstable branch S, A, B, αi, α0 A B S 4B 1 0.5 αi α0 1 S αi α0 80B S unstable Smin 2 Smax T unstable T unstable branch. S unstable, Model.A, Model.B, 0.5, 0.3 T unstable .~ 0.3 . α. T unstable . 0.5 . NaN md\" Unstable branch solution \" end function add labels p plot p, xlabel \"year\", ylabel \"temperature °C \", legend bottomright, xlims 5, 205 , ylims 60, 30. plot p, 5, 200 , 60, 60 , fillrange 10., 10. , fillalpha 0.3, c lightblue, label nothing annotate 120, 20, text \"completely frozen\", 10, darkblue plot p, 5, 200 , 10, 10 , fillrange 30., 30. , fillalpha 0.09, c red, lw 0., label nothing annotate p, 120, 25, text \"no ice\", 10, darkred end begin p interact plot ebm interact.t, ebm interact.T, label nothing, lw 3 plot 0. , T0 interact , label nothing, markersize 4, markershape circle add labels p interact end | as svg begin Sneo Model.S 0.93 Tneo 48. md\" Initial conditions \" end begin if extend S solarSlider bind S Slider Smin 2. Smax, default Sneo md\"\"\" Smin W m² solarSlider Smax W m²\"\"\" else solarSlider bind S Slider Smin 2. Smax limited, default Sneo md\"\"\" Smin W m² solarSlider Smax limited W m²\"\"\" end end begin md\"\"\" Move the slider below to change the brightness of the Sun solar insolation S S W m² \"\"\" end begin ebm Model.EBM Tneo, 0., 5., Model.CO2 const ebm.S Sneo ntraj 10 Ttraj repeat NaN , ntraj Straj repeat NaN , ntraj md\" Data structures for storing trajectories of recent climates \" end begin S warming mask 1 size Svec 1 . size Svec . 2 p plot xlims Smin, Smax limited , ylims 55, 75 , title \"Earth's solar insolation bifurcation diagram\" plot Model.S, Model.S , 55, 75 , color yellow, alpha 0.3, lw 8, label \"Pre industrial present insolation\" if extend S plot p, xlims Smin, Smax if show cold plot Svec warming mask , Tvec warming mask , color blue,lw 3., alpha 0.5, label \"cool branch\" end if show warm plot Svec . warming mask , Tvec . warming mask , color \"red\", lw 3., alpha 0.5, label \"warm branch\" end if show unstable plot S unstable, T unstable, color darkgray, lw 3., alpha 0.4, ls dash, label \"unstable branch\" end end plot legend topleft plot xlabel \"solar insolation S W m² \", ylabel \"Global temperature T °C \" plot Model.S , Model.T0 , markershape circle, label \"Our preindustrial climate\", color orange, markersize 8 plot Model.S , 38.3 , markershape circle, label \"Alternate preindustrial climate\", color aqua, markersize 8 plot Sneo , Tneo , markershape circle, label \"neoproterozoic 700 Mya \", color lightblue, markersize 8 plot trajectory p, reverse Straj , reverse Ttraj , lw 9 plot Smin, Smax , 60, 60 , fillrange 10., 10. , fillalpha 0.3, c lightblue, label nothing annotate Smin 12, 19, text \"completely\\nfrozen\", 10, darkblue, left plot Smin, Smax , 10, 10 , fillrange 80., 80. , fillalpha 0.09, c red, lw 0., label nothing annotate Smin 12, 15, text \"no ice\", 10, darkred, left end | as svg begin S restart ebm ebm ebm.S S Model.run ebm, 500 insert Straj, 1, copy S pop Straj insert Ttraj, 1, copy ebm.T end pop Ttraj end "},{"url":"climate_science/how_to_collaborate_on_software/","title":"GitHub & Open Source Software","tags":["lecture","module3","track_julia","track_data","track_climate","github","open source","collaboration","pull request","fork","software development"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v 7N9Vvc8amGM\" image \"https user images.githubusercontent.com 6933510 136199704 ba6d0586 34bf 490c 8fd0 6959ab42cd23.png\" section 5 order 5 title \"GitHub & Open Source Software\" layout \"layout.jlhtml\" youtube id \"7N9Vvc8amGM\" description \"\" tags \"lecture\", \"module3\", \"track julia\", \"track data\", \"track climate\", \"github\", \"open source\", \"collaboration\", \"pull request\", \"fork\", \"software development\" using Markdown using InteractiveUtils begin using HypertextLiteral using PlutoTest using PlutoUI end md\"\"\" How to collaborate on software What is the simplest way to collaborate on project online? You can email files to each other This works for small projects, but at some point, you will need something more suited to the task. For example Encyclopedia a wiki system School essay Google Drive Software project GitHub For software projects, people of use GitHub, or similar git based tools to collaborate on their work. Why is that? \"\"\" TableOfContents aside false, depth 1 md\"\"\" Why not Google Drive? \"\"\" md\"\"\" Google Drive or Dropbox, Nextcloud, etc is a platform for realtime collaboration on files. Besides synchronizing single files like the video above , Google Drive also allows you to synchronize entire folders between multiple computers and the cloud. That's awesome So do people use it for collaborative software projects? Well, generally not. Software projects have some unique properties that require different tools. As today's introduction into git , let's look at the differences between a software project and a non software project , and why platforms like Google Drive don't work well for collaborating on code. Note that we will take a different perspective that other introductions to git . Let's start out with a realtime synchronization system like Google Drive as our default, and think about why we might want to change its functionality to make it more suitable for software. \"\"\" md\"\"\" Reason 1 sensitive to change Reason 1 In software projects, small changes often make a big difference. Changes to one file affect the entire program. For example, here is what Pluto.jl looks like after adding a single character to the source code \"\"\" md\"\"\" While a single character can mean disaster, this sensitivity also means that programming languages are quite powerful A small change will often do the trick. \"\"\" md\"\"\" Synchronizing every change This sensitivity to changes makes realtime synchronization unsuitable for software projects. To see why, let's say that I want to change `sqrt` to `log` in the following code \"\"\" md\"\"\" If you would synchronize every change while making that edit removing the letters `s`, `q`, `r`, `t`, typing `l`, `o`, `g` , then you are publishing broken code to your online code repository. If you were sharing changes in realtime like Google Drive , then other people would get those broken states while working on their features. \"\"\" md\"\"\" Synchronizing every keystroke \"\"\" md\"\"\" However, if we only publish code manually , we can make sure that we never publish broken code. This means that my collaborators, who might be working on the project at the same time, will not have to deal with errors that are coming from my work in progress changes. \"\"\" md\"\"\" Synchronizing manually \"\"\" md\"\"\" Even larger chunks In the previous example, we found that we can avoid publishing invalid syntax by not synchronizing on every keystroke like Google Docs, but by synchronizing manually. We also don't want to synchronize the file every time it is saved , because you often try multiple things before finding something that works. \"\"\" md\"\"\" Synchronizing every save \"\"\" md\"\"\" Synchronizing manually \"\"\" pass2 html\" span style 'opacity .3' ✅ span \" pass \"✅\" | HTML fail \"❌\" | HTML md\"\"\" Reason 2 branches and forks Reason 2 It should be easy to split a project into multiple branches copies of the codebase that can be worked on independently. And after some time, we want to compare branches and merge the new additions into one. Something that you may have done create a copy of your `presentation.ppt` called `presentation with pictures.ppt` before adding your pictures, and if it didn't work, you can go back to yesterday's backup. Now that's great, but what if your teammate also made a copy `presentation with titlepage.ppt` where they also made a change? In this situation, you have created two branches of the same file in parallel, and it will be difficult to combine those changes back into a single `presentation this one really.ppt` file. \"\"\" md\"\"\" Reason 3 automation Reason 3 Git is a platform for automation , and can be a mechanism to automatically test and review changes, release weekly versions, etc etc. Programmers love to automate things, and git allows all sorts of automation. At first, this can be intimidating , and it will take years of experience to learn all the possible tricks. Someone who does 'git magic' as a profession is called a DevOps https en.wikipedia.org wiki DevOps engineer . Don't worry about this too much when getting started with GitHub, and ask others for help if necessary. \"\"\" md\"\"\" Automatic testing While we don't recommend learning about git automation right away, one thing you will probably encounter when contributing to an open source project is automatic testing . More on this later \"\"\" bigbreak html\"\"\" br br br br br br br br \"\"\" bigbreak bigbreak md\"\"\" Our first contribution documentation Today will be a kick start into contributing to open source projects The easiest way to make a great contribution to packages is to contribute a documentation improvement . For our example today, we will look at the package ClimateMARGO.jl https github.com ClimateMARGO ClimateMARGO.jl , a climate economic model. This package is hosted on GitHub, which means that all code and previous versions of code is available at github.com ClimateMARGO ClimateMARGO.jl https github.com ClimateMARGO ClimateMARGO.jl . The authors also use the same GitHub site to manage their TO DOs — called Issues https github.com ClimateMARGO ClimateMARGO.jl issues — and to accept contributions from others — called Pull Requests https github.com ClimateMARGO ClimateMARGO.jl pulls . Today, we are going to make a Pull Request \"\"\" md\"\"\" What is a Pull Request? Julia packages are open source , which means that you are allowed and encouraged to make your own copy of the source code we call this a fork and modify it. If you made some good changes on your fork then you can suggest to changes to the original project by submitting your code as a Pull Request PR . This is a feature built in to GitHub, and this is how most code collaboration happens online. \"\"\" md\"\"\" Video tutorial will be available here after the live lecture. \"\"\" md\"\"\" After the Pull Request Submitting your Pull Request is a great feeling, but it's not the last step After your submission, the PR will be reviewed by the project authors, and they will leave their feedback. It is common that a PR is not merged accepted directly, but that you are asked to make some small changes. This is possible because a Pull Request is not static — you can make changes to the PR after submitting it Remember that a PR is from one branch to another . You can make changes to your PR simply by adding additional commits to the from branch. \"\"\" md\"\"\" As an example of a Pull Request in the wild, let's look at this PR to `JuliaLang julia` github.com JuliaLang julia pull 40596 https github.com JuliaLang julia pull 40596 . The code change is not so important to us right now, but pay attention to the communication around the code change. \"\"\" md\"\"\" How to use GitHub Enough talk, let's create our first repository \"\"\" md\"\"\" Requirements To follow this introduction, you will need Create an account at github.com github.com . Add a friendly profile picture For Windows & MacOS users, download GitHub Desktop https desktop.github.com , a graphical program to easily manage git repositories on your disk. Linux users can use the slightly more advanced program GitKraken https www.gitkraken.com . A code editor. If you don't have a code editor yet, or if you are using Notepad, gedit or Notepad , we highly recommend VS Code https code.visualstudio.com , an open source, beginner friendly programming environment. It may seem like something new and difficult to learn, but it is easy to use, and because it understands the code that you are writing, it will help you as a beginner programmer. \"\"\" md\"\"\" Video tutorial will be available here after the live lecture. \"\"\" md\"\"\" ⛑ Git trouble Sooner or later, you will find yourself in git trouble . Unlike Google Drive, it can happen that synchronization requires manual intervention, for example, git might say `🤐 merge conflict` Which means that someone changed a file right before you were going to commit. \"\"\" md\"\"\" Fonsi's Git Trouble Fix™ Many git problems have an 'official' solution, and it will take time to learn all of these techniques. In the meantime, here is my fool proof way of solving many tricky git situations \"\"\" md\"\"\" Step 1 Use GitHub Desktop to find out which files you have changed. Are the changes important? Step 2 Take any files that you want to keep, and copy them to a different folder, e.g. your desktop. Step 3 Remove your local clone of the repository, and move any leftover files to the trash. Step 4 Clone the repository again, and copy your files back to their original place, overwriting the cloned files. \"\"\" bigbreak md\"\"\" How to fork and contribute code \"\"\" md\"\"\" Video tutorial will be available here after the live lecture. \"\"\" md\"\"\" Tests People often write tests for their code As part of the codebase, there will be a `test` folder containing scripts that will import the project and run some basic checks. \"\"\" function double x x x end test double 3 6 test double 0 0 md\"\"\" One reason to write tests is to pin specific behaviour, protecting yourself from accidentally changing it later. For example, if you fix a bug that `double 2 ` returns `40` instead of `4`, you would also add a test for `double 2 4`. Months later, when you are changing `double` for another use case, the old test insures that you are not accidentally breaking something that was once fixed. \"\"\" md\"\"\" Running tests You can run the tests of any Julia package by opening the Julia REPL, and typing ```julia julia v1.6 pkg test Example ``` As mentioned before, many project use github magic to automatically run their tests on a server for every change, and you can view the test results online. An important application is running tests for every Pull Request. This means that package authors can quickly review your changes, knowing that it does not break anything. \"\"\" bigbreak md\"\"\" Extra tips \"\"\" md\"\"\" Contributing does not only mean contributing code https opensource.guide how to contribute https opensource.guide how to contribute \"\"\" md\"\"\" Create a \"test\" repository Create multiple \"\"\" md\"\"\" Appendix \"\"\" function ingredients path String this is from the Julia source code evalfile in base loading.jl but with the modification that it returns the module instead of the last object name Symbol basename path m Module name Core.eval m, Expr toplevel, eval x Expr core, eval name, x , include x Expr top, include name, x , include mapexpr Function, x Expr top, include mapexpr, name, x , include path m end Layout ingredients download \"https raw.githubusercontent.com fonsp disorganised mess 5a59ad7cff1e760b997a54ffa0f8fa202ac16db3 Layout.jl\" stackrows x permutedims hcat x... , 2, 1 flex x Union AbstractVector,Base.Generator kwargs... flex x... kwargs... begin Base. kwdef struct Div contents style Dict end Div x Div contents x function Base.show io IO, m MIME\"text html\", d Div h htl \"\"\" div style d.style d.contents div \"\"\" show io, m, h end end outline x Div x, Dict \"border\" \"3px solid rgba 0,0,0,.3 \", \"border radius\" \"3px\", \"padding\" \"5px\", Div md\"\"\" ```julia function height p c1 sqrt p c2 end ``` \"\"\", md\"to\", md\"\"\" ```julia function height p c1 log p c2 end ``` \"\"\" , Dict \"display\" \"flex\", \"justify content\" \"space evenly\", \"align items\" \"center\", function flex args... kwargs... Div contents collect args , style Dict \"display\" \"flex\", \"flex \" String k string v for k, v in kwargs ... end flex md\"\"\" Before Schermafbeelding 2021 05 04 om 18 51 27 https user images.githubusercontent.com 6933510 117040056 c10b8280 ad09 11eb 9384 d211156440b1.png \"\"\", md\"\"\" After a single change Schermafbeelding 2021 05 04 om 18 50 00 https user images.githubusercontent.com 6933510 117039958 a46f4a80 ad09 11eb 90fa c1264d896648.png \"\"\" | outline function grid items AbstractMatrix fill width Bool true Div contents Div. vec permutedims items, 2,1 , style Dict \"display\" fill width ? \"grid\" \"inline grid\", \"grid template columns\" \"repeat size items,2 , auto \", \"column gap\" \"1em\", , end grid nothing md\"`sqrt`\" md\"`sq`\" md\"` `\" md\"`lo`\" md\"`log`\" \"Your computer local \" pass fail fail fail pass \"Online remote \" pass fail fail fail pass grid nothing md\"`sqrt`\" md\"`sq`\" md\"` `\" md\"`lo`\" md\"`log`\" md\"Your computer local \" pass fail fail fail pass md\"Online remote \" pass nothing nothing nothing pass grid nothing md\"`sqrt`\" md\"Let's try `sin`\" md\"Let's try `cos`\" md\"Try `log`\" \"Your computer local \" pass fail fail pass \"Online remote \" pass fail fail pass grid nothing md\"`sqrt`\" md\"Let's try `sin`\" md\"Let's try `cos`\" md\"Try `log`\" \"Your computer local \" pass fail fail pass \"Online remote \" pass nothing nothing pass vocabulary x grid stackrows htl \" span style 'font size 1.2rem font weight 700 ' code k code span \" , v for k, v in x fill width false htl \" em remote em \" md\"The version that is on the internet, you browse the remote on github.com.\", htl \" em local em \" md\"What's on your computer. Use `pull`, `commit` and `push` to synchronize this with the remote. Google Drive does this all the time.\" | vocabulary \"fetch\" md\"Make your local git aware of any changes online. Do this often \", \"pull\" md\"Apply any changes on the remote version to your local copy. This will get the two in sync . Do this often \", \"commit\" md\"Create a collection of changes to files, ready to be `push`ed.\", \"push\" md\"Publish any local `commit`s to the remote version.\", | vocabulary function aside x htl \"\"\" style media min width calc 700px 30px 300px aside.plutoui aside wrapper position absolute right 11px width 0px transform translate 0, 40% aside.plutoui aside wrapper div width 300px style aside class \"plutoui aside wrapper\" div x div aside \"\"\" end html\"\"\" p If you have not used Google Drive before, here is a small demonstration p video src \"https user images.githubusercontent.com 6933510 117038375 d8497080 ad07 11eb 8260 34e96414131a.mov\" data canonical src \"https user images.githubusercontent.com 6933510 117038375 d8497080 ad07 11eb 8260 34e96414131a.mov\" controls \"controls\" muted \"muted\" class \"d block rounded bottom 2 width fit\" style \"max height 640px \" video \"\"\" | aside md\"\"\" ^sidenote Language design side note There are languages that are designed to be robust against small changes, and you can actually use realtime collaboration with those Examples are glitch.com glitch.com for collaborative HTML and CSS editing these languages can ignore syntax errors and continue , and the more experimental language Dark https darklang.com which uses a special editor that does not allow you to type errors . \"\"\" | aside md\"\"\" ^1 If you are working on a fork , then by creating a PR, you also give the original project authors access to make changes to the from branch of the PR. This is a useful feature, allowing you to work together on the same codebase. \"\"\" | aside md\"\"\" Test driven design Some people like to first write their tests, which will fail initially, and then write the code to solve the problem. While working on the code, they keep re running the tests, until all checks are green This can be an effective and rewarding way to work on a software problem If you are following this course, then you will already be familiar with this concept The homework exercises were all designed with the test driven principle. \"\"\" | aside "},{"url":"climate_science/inverse_climate_model/","title":"Climate Economics","tags":["lecture","module3","climate","modeling","track_climate","track_math","plotting","track_julia","economics","optimization","JuMP","simulation"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v nm86 hDwYTU\" image \"https user images.githubusercontent.com 6933510 136199660 315b045b f144 4009 9282 7fe6d1f5d41b.gif\" section 10 order 10 title \"Climate Economics\" layout \"layout.jlhtml\" youtube id \"nm86 hDwYTU\" description \"\" tags \"lecture\", \"module3\", \"climate\", \"modeling\", \"track climate\", \"track math\", \"plotting\", \"track julia\", \"economics\", \"optimization\", \"JuMP\", \"simulation\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import Pkg ENV \"JULIA MARGO LOAD PYPLOT\" \"no thank you\" Pkg.activate mktempdir Pkg.add Pkg.PackageSpec name \"Plots\", version \"1\" , Pkg.PackageSpec name \"ClimateMARGO\", version v\"0.3.2\" , Pkg.PackageSpec name \"PlutoUI\", version \"0.7\" , Pkg.PackageSpec name \"AbstractPlutoDingetjes\" , Pkg.PackageSpec name \"HypertextLiteral\", version \"0.9\" , Pkg.PackageSpec name \"Underscores\", version \"2\" , using Plots using Plots.Colors using ClimateMARGO using ClimateMARGO.Models using ClimateMARGO.Optimization using ClimateMARGO.Diagnostics using PlutoUI using AbstractPlutoDingetjes using HypertextLiteral using Underscores Plots.default linewidth 5 html\"\"\" style loading message display none style \"\"\" end TableOfContents html\" div id 'loading message' style 'font size 2rem font style italic font family system ui font weight 700 margin 2rem ' Loading packages, please wait... div \" const initial 1 Dict \"M\" 2090, 6 const initial 2 Dict \"M\" 2080, .7 const initial 3 Dict \"M\" 2080, .7 const initial 4 Dict \"M\" 2080, 0.7 , \"R\" 2120, 0.2 , HTML \"\"\" p When editing this notebook inside Pluto, use the img src \"https cdn.jsdelivr.net gh ionic team ionicons 5.5.1 src svg eye outline.svg\" style \"width 1em height 1em margin bottom .2em \" icon next to cells to view their code. p \"\"\" blob el, color \"red\" htl \"\"\" div style \" background color padding 1em 1em 1em 1em border radius 2em \" el div \"\"\" function infeasablewarning x htl \"\"\" margo infeasible x margo infeasible label No solution found margo infeasible label margo infeasible style margo infeasible opacity .1 margo infeasible margo infeasible label opacity .7 display block position absolute transform translate 50%, 50% font family \"Vollkorn\", system ui font style italic font size 40px top 50% left 50% white space nowrap style \"\"\" end status ok x x ∈ \"OPTIMAL\", \"LOCALLY SOLVED\", \"ALMOST OPTIMAL\", \"ALMOST LOCALLY SOLVED\" feasibility overlay x status ok x.status ? as html infeasablewarning begin hidecloack name HTML \"\"\" style plj cloack. name opacity 0 display block style \"\"\" \"A trick to hide a cell without creating a variable dependency, to make it simpler for PlutoSliderServer.jl.\" cloak name x htl \" plj cloack class name x plj cloak \" end colors baseline colorant\" dddddd\", baseline emissions colorant\" dddddd\", baseline concentrations colorant\" dddddd\", baseline temperature colorant\" dddddd\", baseline damages colorant\" dddddd\", temperature colorant\" edc949\", above paris colorant\" e1575910\", M colorant\" 4E79A7\", R colorant\" F28E2C\", A colorant\" 59A14F\", G colorant\" E15759\", T max colorant\" 00000080\", controls colorant\" af7aa1\", damages colorant\" e15759\", avoided damages colorant\" e49734\", benefits colorant\" 7abf5e\", emissions colorant\"brown\", emissions 1 colorant\" 4E79A7\", concentrations colorant\"brown\", names baseline \"Baseline\", baseline emissions \"Baseline\", baseline concentrations \"Baseline\", baseline temperature \"Baseline\", baseline damages \"Baseline\", temperature \"Temperature\", above paris \"Above Paris\", M \"Mitigation\", R \"Removal\", A \"Adaptation\", G \"Geo engineering\", T max \"Goal temperature\", controls \"Controls\", damages \"Damages\", avoided damages \"Avoided Damages\", benefits \"Benefits\", emissions \"Emissions\", emissions 1 \"Emissions\", concentrations \"Concentrations\", assert keys colors keys names begin struct BondDefault x default end Base.get bd BondDefault bd.default Base.show io IO, m MIME\"text html\", bd BondDefault Base.show io, m, bd.x BondDefault end bind which graph 2 Select \"Emissions\" \"Concentrations\" \"Temperature\" bind which graph 4 Select \"Emissions\" \"Concentrations\" \"Temperature\" \"Costs and benefits\" Tmax 9 slider bind Tmax 9 Slider 0 0.1 5 default 2 allow overshoot 9 cb bind allow overshoot 9 CheckBox function multiplier unit Real, factor Real 2, suffix String \"%\" h htl \"\"\" script const unit unit const factor factor const suffix suffix const input html` input type range min 1 max 1 step .01 value 0 ` const output html` input disabled style \"width 1.8em display inline block overflow x hidden \" input ` const output html`` const left Math.round 100 factor \"%\" const right Math.round 100 factor \"%\" const reset html` a href \" \" title \"Reset\" style 'padding left .5em' img width \"14\" src \"https cdn.jsdelivr.net gh ionic team ionicons 5.0.0 src svg arrow undo sharp.svg\" img a ` const span html` div style \"margin left 2em \" \\ left \\ input \\ right \\ reset div ` const on slider output.value Math.round 100 Math.pow factor, input.value input.title Math.round 100 Math.pow factor, input.value \"%\" reset.style.opacity input.valueAsNumber 0 ? \"0\" \"1\" input.oninput on slider on slider const on box input.value output.value reset.style.opacity input.valueAsNumber 100 ? \"0\" \"1\" output.oninput on box reset.onclick e input.value 0 on slider e.preventDefault span.dispatchEvent new CustomEvent \"input\", Object.defineProperty span, \"value\", get unit Math.pow factor, input.value , set val input.value Math.log2 val unit Math.log2 factor on slider return span script \"\"\" BondDefault h, unit end function Carousel elementsList wraparound Bool false, peek Bool true, assert peek carouselHTML map elementsList do element htl \"\"\" div class \"carousel slide\" element div \"\"\" end h htl \"\"\" div style .carousel box width 100% overflow hidden .carousel container top 0 left 0 display flex width 100% flex flow row nowrap transform translate 10%, 0px transition transform 200ms ease in out .carousel controls display flex justify content center align items center .carousel controls button margin 8px width 6em .carousel slide min width 80% style script const div currentScript.parentElement const buttons div.querySelectorAll \"button\" const max length elementsList let count 0 const mod n, m n % m m % m const clamp x, a, b Math.max Math.min x, b , a const update ui count buttons 0 .disabled wraparound && count 0 buttons 1 .disabled wraparound && count max 1 div.querySelector \".carousel container\" .style `transform translate \\ 10 count 80 %, 0px ` const onclick e const new count count parseInt e.target.dataset.value if wraparound count mod new count, max else count clamp new count, 0, max 1 div.value count 1 div.dispatchEvent new CustomEvent \"input\" update ui div.value 1 e.preventDefault buttons.forEach button button.addEventListener \"click\", onclick div.value count 1 update ui div.value 1 script div class \"carousel box\" div class \"carousel container\" carouselHTML div div div class \"carousel controls\" button data value \" 1\" Previous button button data value \"1\" Next button div div \"\"\" BondDefault h,1 end preindustrial concentrations 280 end of oil 2150 cannot mitigate when fossil fuels are depleted pp k, color c, label n for k, c, n in zip keys names , colors, names ... gauss stdev 30 begin default usage error error \"Example usage \\n\\n intially 1,2 bind x f x \\n\" macro initially Any default usage error end macro initially default, bind expr Expr if bind expr.head macrocall || bind expr.args 1 Symbol \" bind\" return default usage error end warn if the first argument is a bind if default isa Expr && default.head macrocall && default.args 1 Symbol \" bind\" return default usage error end esc intially function default, bind expr end function intially function default, bind expr sym bind expr.args 3 gensym setval bond quote if isdefined sym sym default end setval sym bond bind sym bind expr.args 4 PlutoRunner.Bond if sym isa Missing sym setval end bond end end end html\"\"\" style margo knob display block cursor pointer width 32px height 32px transform translate 8px, 16px background red border radius 100% border width 5px border style solid border color rgb 255 255 255 43% border opacity .2 position absolute top 0px left 0px margo knob label transform translate 32px, 8px display block position absolute left 0 top 0 white space nowrap background d6eccb font family system ui padding .4em border radius 11px font weight 600 pointer events none opacity 0 .wiggle margo knob animation wiggle 5s ease in out animation delay 600ms .wiggle margo knob label animation fadeout 1s ease in out animation delay 3s animation fill mode both keyframes fadeout from opacity 1 to opactiy 0 keyframes wiggle 0% transform translate 8px, 16px 2% transform translate 8px, 16px 5% transform translate 24px, 16px 10% transform translate 8px, 16px 15% transform translate 8px, 16px 17% transform translate 8px, 0px 20% transform translate 8px, 32px 25% transform translate 8px, 16px style \"\"\" initial mrga 1 Dict \"M\" 2070, 0.7 , \"R\" 2100, 0.4 , \"G\" 2170, 0.3 , \"A\" 2110, 0.1 , initially initial mrga 1 bind input 8 begin controls 8 MRGA gaussish input 8 \"M\" ... , gaussish input 8 \"R\" ... , gaussish input 8 \"G\" ... , gaussish input 8 \"A\" ... , plotclicktracker2 plot controls controls 8 , initial mrga 1, end function plotclicktracker p Plots.Plot draggable Bool false we need to render the plot before its dimensions are available plot render repr MIME\"image svg xml\" , p plot render repr MIME\"image svg xml\" , p these are the bounding boxes of our plot big bbox p.layout small plotarea p 1 the axis limits xl xlims p yl ylims p with this information, we can form the linear transformation from screen coordinate plot coordinate this is done on the JS side, to avoid one step in the Julia side we send the linear coefficients r x offset xl 1 xl 2 xl 1 small.x0 1 small.a 1 , x scale big.a 1 small.a 1 xl 2 xl 1 , y offset yl 2 yl 1 small.x0 2 small.a 2 yl 2 yl 1 yl 1 , y scale big.a 2 small.a 2 yl 2 yl 1 , x min xl 1 , TODO add margin x max xl 2 , y min yl 1 , y max yl 2 , HTML \"\"\" script id \"hello\" const body PlutoRunner.publish to js plot render const mime \"image svg xml\" const img this ?? document.createElement \"img\" let url URL.createObjectURL new Blob body , type mime img.type mime img.src url img.draggable false img.style.cursor \"pointer\" const clamp x,a,b Math.min Math.max x, a , b img.transform f clamp f 0 r.x scale r.x offset , r.x min , r.x max , clamp f 1 r.y scale r.y offset , r.y min , r.y max , img.fired false const val current undefined if this null Object.defineProperty img, \"value\", get val.current, set , const handle mouse e const svgrect img.getBoundingClientRect const f e.clientX svgrect.left svgrect.width, e.clientY svgrect.top svgrect.height if img.fired false img.fired true val.current img.transform f img.dispatchEvent new CustomEvent \"input\" , img.addEventListener \"click\", onclick img.addEventListener \"pointerdown\", e if draggable img.addEventListener \"pointermove\", handle mouse handle mouse e const mouseup e img.removeEventListener \"pointermove\", handle mouse document.addEventListener \"pointerup\", mouseup document.addEventListener \"pointerleave\", mouseup return img script \"\"\" end years 2020 12.0 2200 function gaussish mean, magnitude my stdev gauss stdev 1 magnitude map years do t clamp 1.5 magnitude 0.4 exp 1 t mean t mean 2 my stdev my stdev 1.0 0.4 , 0.0, 1.0 end end function default parameters ClimateModelParameters result deepcopy ClimateMARGO.IO.included configurations \"default\" result.domain years isa Domain ? years Domain step years , first years , last years result.economics.baseline emissions ramp emissions result.domain result.economics.extra CO₂ zeros size result.economics.baseline emissions return result end function opt controls temp model parameters default parameters opt parameters... model ClimateModel model parameters model optimizer optimize controls model opt parameters..., print raw status false result model, status ClimateMARGO.Optimization.JuMP.termination status model optimizer | string, return Dict model parameters model parameters, model results model ... end bigbreak html\"\"\" div style \"height 10em \" div \"\"\" bigbreak bigbreak bigbreak bigbreak function setfieldconvert value, name Symbol, x setfield value, name, convert typeof getfield value, name , x end function enforce maxslope controls dt step years , max slope Dict \"mitigate\" 1. 40., \"remove\" 1. 40., \"geoeng\" 1. 80., \"adapt\" 0. controls.mitigate 1 0.0 controls.remove 1 0.0 controls.geoeng 1 0.0 controls.adapt 1 0.0 for i in 2 length controls.mitigate controls.mitigate i clamp controls.mitigate i , controls.mitigate i 1 max slope \"mitigate\" dt, controls.mitigate i 1 max slope \"mitigate\" dt controls.remove i clamp controls.remove i , controls.remove i 1 max slope \"remove\" dt, controls.remove i 1 max slope \"remove\" dt controls.geoeng i clamp controls.geoeng i , controls.geoeng i 1 max slope \"geoeng\" dt, controls.geoeng i 1 max slope \"geoeng\" dt controls.adapt i clamp controls.adapt i , controls.adapt i 1 max slope \"adapt\" dt, controls.adapt i 1 max slope \"adapt\" dt end end function costs dict costs, model Dict discounted costs, total discounted sum costs . model.domain.dt , end model results model ClimateModel Dict controls model.controls, computed Dict temperatures Dict baseline T adapt model , M T adapt model M true , MR T adapt model M true, R true , MRG T adapt model M true, R true, G true , MRGA T adapt model M true, R true, G true, A true , , emissions Dict baseline effective emissions model , M effective emissions model M true , MRGA effective emissions model M true, R true , , concentrations Dict baseline c model , M c model M true , MRGA c model M true, R true , , damages Dict baseline costs dict damage model discounting true , model , MRGA costs dict damage model M true, R true, G true, A true, discounting true , model , , costs Dict M costs dict cost model M true, discounting true , model , R costs dict cost model R true, discounting true , model , G costs dict cost model G true, discounting true , model , A costs dict cost model A true, discounting true , model , MRGA costs dict cost model M true, R true, G true, A true, discounting true , model , , , const cost bars scale 70 total discounted costs, model sum costs . model.domain.dt colors js Dict k,string \" \", hex v for k,v in pairs colors names js Dict pairs names function plotclicktracker2 p Plots.Plot, initial Dict draggable Bool true we need to render the plot before its dimensions are available plot render repr MIME\"image svg xml\" , p plot render repr MIME\"image svg xml\" , p these are the bounding boxes of our plot big bbox p.layout small plotarea p 1 the axis limits xl xlims p yl ylims p with this information, we can form the linear transformation from screen coordinate plot coordinate this is done on the JS side, to avoid one step in the Julia side we send the linear coefficients r x offset xl 1 xl 2 xl 1 small.x0 1 small.a 1 , x scale big.a 1 small.a 1 xl 2 xl 1 , y offset yl 2 yl 1 small.x0 2 small.a 2 yl 2 yl 1 yl 1 , y scale big.a 2 small.a 2 yl 2 yl 1 , x min xl 1 , TODO add margin x max xl 2 , y min yl 1 , y max yl 2 , aspect ratio big.a 1 big.a 2 , htl \"\"\" script id \"hello\" const initial initial const colors colors js const names names js const body AbstractPlutoDingetjes.Display.published to js plot render const mime \"image svg xml\" const knob name const k html` margo knob id \\ name margo knob label 👈 Move me margo knob label margo knob ` k.style.backgroundColor colors name return k const aspect ratio r.aspect ratio const wrapper this ?? html` div style 'touch action none ' img style 'width 100% aspect ratio \\ aspect ratio background white ' \\ Object.keys initial .map knob div ` const img wrapper.firstElementChild let url URL.createObjectURL new Blob body , type mime invalidation.then URL.revokeObjectURL url Call `fetch` on the URL to trigger the browser to make it ready. let fetch promise fetch url Promise.race fetch promise, invalidation.then x null .then r if r null img.type mime img.src url img.draggable false const clamp x,a,b Math.min Math.max x, a , b wrapper.transform f clamp f 0 r.x scale r.x offset , r.x min , r.x max , clamp f 1 r.y scale r.y offset , r.y min , r.y max , wrapper.inversetransform f f 0 r.x offset r.x scale , f 1 r.y offset r.y scale , const set knob coord k, coord const svgrect img.getBoundingClientRect const r wrapper.inversetransform coord k.style.left `\\ r 0 svgrect.width px` k.style.top `\\ r 1 svgrect.height px` wrapper.fired already false wrapper.last render time Date.now If running for the first time if this null console.log \"Creating new plotclicktracker...\" will contain the currently dragging HTMLElement const dragging current undefined const value ...initial Object.defineProperty wrapper, \"value\", get value, set x console.log \"old\", value, \"new\", x Object.assign value, x Object.entries value .forEach k,v set knob coord wrapper.querySelector `margo knob \\ k ` , v , Event listener for pointer move const allow only one event per render false const on pointer move e if Object.keys initial .includes dragging.current.id const svgrect img.getBoundingClientRect const f e.clientX svgrect.left svgrect.width, e.clientY svgrect.top svgrect.height if allow only one event per render || wrapper.fired already false const new coord wrapper.transform f value dragging.current.id new coord set knob coord dragging.current, new coord wrapper.classList.toggle \"wiggle\", false wrapper.fired already true wrapper.dispatchEvent new CustomEvent \"input\" , Add the listeners wrapper.addEventListener \"pointerdown\", e window.getSelection .empty dragging.current e.target if draggable wrapper.addEventListener \"pointermove\", on pointer move on pointer move e const mouseup e wrapper.removeEventListener \"pointermove\", on pointer move document.addEventListener \"pointerup\", mouseup document.addEventListener \"pointerleave\", mouseup wrapper.onselectstart false Set knobs to initial positions, using the inverse transformation new Promise r img.onload r .then Array.from wrapper.querySelectorAll \"margo knob\" .forEach k set knob coord k, initial k.id Intersection observer to trigger to wiggle animation const observer new IntersectionObserver es es.forEach e if Date.now wrapper.last render time 500 wrapper.classList.toggle \"wiggle\", e.isIntersecting , rootMargin `20px`, threshold 1, observer.observe wrapper wrapper.classList.toggle \"wiggle\", true return wrapper script \"\"\" end begin mutable struct MRGA T M T R T G T A T end function MRGA M TM,R TR nothing,G TG nothing where TM,TR,TG MRGA Union TM,TR,TG,Nothing M,R,G,nothing end function MRGA M TM nothing,R TR nothing,G TG nothing,A TA nothing where TM,TR,TG,TA MRGA Union TM,TR,TG,TA M,R,G,A end MRGA x MRGA x,x,x,x splat mrga MRGA M mrga.M, R mrga.R, G mrga.G, A mrga.A Base.collect mrga MRGA mrga.M, mrga.R, mrga.G, mrga.A Base.getindex x MRGA, pos MRGA Bool collect x collect pos Base.getindex x MRGA, tech Symbol getfield x, tech Base.eachindex m MRGA M, R, G, A Base.enumerate mrga MRGA M, mrga.M , R, mrga.R , G, mrga.G , A, mrga.A Base.any m MRGA Bool m.M || m.R || m.G || m.A Base.all m MRGA Bool m.M && m.R && m.G && m.A MRGA end md\"\"\" Emissions mitigation and carbon dioxide removal to minimize climate suffering This interactive article lets you – the reader– run MARGO https github.com ClimateMARGO ClimateMARGO.jl , a simple climate model, to explore what it takes to avoid the catastrophic impacts of global warming. The code in this webpage is reactive , meaning that the graphs and numbers automatically update whenever you change the climate model's inputs. By Henri F. Drake https hdrake.github.io and Fons van der Plas Can you limit human caused global warming to \"well below 2ºC\"? \"\"\" md\"\"\" image https user images.githubusercontent.com 6933510 118835384 3ad36c80 b8c3 11eb 995d 70cba3b23846.png From ClimateMARGO.jl https github.com ClimateMARGO ClimateMARGO.jl \"\"\" md\"\"\" Overview of the climate problem from greenhouse gas emissions to climate suffering Human emissions of greenhouse gases, especially Carbon Dioxide CO₂ , increase the stock of greenhouse gases in the atmosphere. For every molecule of CO₂ emitted, about 50% are taken up by plants, soils, or the ocean within a few years, while the rest remains in the atmosphere. The effects of other greenhouse gases, such as Methane and CFCs, and other forcing agents, can approximately be converted into the \"CO₂ equivalent\"– or CO₂ₑ– concentrations that would lead to the same climate forcing . Greenhouse gases get their name because they trap invisible heat radiation emitted by Earth's surface and atmosphere from escaping to space, much like greenhouses trap hot air from rising when it is warmed by the Sun. This \"greenhouse effect\" causes the temperature to rise globally, although some places warm more and faster than others. Warmer temperatures exacerbate both the frequency and intensity of \"natural\" disasters, such as heat waves, coastal flooding from major hurricanes, and inland flooding from torrential rain. These climate impacts lead to enhanced climate suffering, which economics typically attempt to quantify suffering in terms of lost money or welfare. In the interactive article below, we invite you to explore the benefits of emissions mitigation and carbon dioxide removal in reducing climate suffering, and the trade offs with their costs. \"\"\" md\"\"\" Mitigating emissions Human greenhouse gas emissions are the result of fossil fuel burning e.g. for transportation, electricity generation, heating, industry , unsustainable agriculture, and land use changes. We refer to any actions or policies that reduce these emissions as mitigation . The MARGO model lumps all potential mitigation into a single number the percentage of baseline emissions that are mitigated in a given year. Baseline emissions are the emissions that would arise in a hypothetical future world absent of climate policy. In our hypothetical no policy world, we assume that emissions will go to zero by 2150 even without climate policy, perhaps because of public health concerns regarding other forms of air pollution, the development of new zero carbon technologies, or running out of extractable fossil fuels resources. In the plot below, drag the blue dot around to vary the amount and timing of mitigation, and observe how these changes affect key climate variables, using the drop box menu CO₂ₑ emissions, CO₂ₑ concentrations, and global temperature. \"\"\" md\"\"\" Cost & damages \"\"\" bind cost benefits narrative slide Carousel md\"\"\" 1. The costs of climate suffering In the absence of climate action, temperatures would rise over 4.5ºC above preindustrial levels 1800 to 1850 average , causing catastrophic climate impacts. MARGO attempts to quantify this suffering by translating the degree of warming into economic damages in \\ year . The curve below shows how climate damages rise over time, as a percentage of the World Gross Domestic Product WGDP in that year, due to uncontrolled temperature increases. \"\"\", md\"\"\" 2. Avoiding climate damages Emissions mitigation limits future warming and climate suffering Damages curve . The economic benefits of mitigation are given by the difference in damages relative to the no policy scenario Baseline curve minus Damages curve . In the figure below, drag around the blue dot to change the future mitigation strategy, and observe how the Avoided damages the grey area change \"\"\", md\"\"\" 3. Cost benefit analysis Unfortunately, mitigating CO₂ₑ emissions also carries a cost. In MARGO, the marginal cost of mitigation is proportional to the fraction of CO₂ₑ emissions that have been mitigated in a given year, increasing up to a maximum of 70 per metric ton of CO₂ₑ at 100% mitigation. This naturally leads to a cost benefit analysis . We search for the most beneficial, or optimal , scenario the one with the maximum net present benefits . In the figure below, try finding a mitigation strategy that optimizes these Net benefits . \"\"\" wraparound false if cost benefits narrative slide 1 hidecloack \"cost benefits narrative input\" end md\"\"\" Picking up the slack carbon dioxide removal While substantial emissions mitigations are necessary to reduce future climate suffering, they can not make up for the hundreds of billions of tons of CO₂ that humans have already emitted. However, both natural and technological methods for removing CO₂ from the atmosphere exist. Although they are presently miniscule compared to the tens of gigatons scale of global emissions, experts expect that they will play a key role in the future. In MARGO, we do not distinguish between different carbon dioxide removal methods, and further assume that the carbon is stored permanently. Drag the yellow dot in the figure below to modify the amount and timing of carbon dioxide removal . \"\"\" md\"\"\" MARGO's automated optimization In the above example, you manually adjusted the timing and amount of mitigation and carbon dioxide removal, but did not have much control on the shape of curves. Using a computer algorithm, we can do this optimization step automatically and faster , without having to assume anything about the shape of the mitigation and carbon dioxide removal curves. \"\"\" blob md\"\"\" Maximum temperature `0.0 °C` Tmax 9 slider `5.0 °C` Allow temperature overshoot allow overshoot 9 cb \"\"\", \" c5710014\" md\"\"\" Appendix This interactive page is a Pluto notebook https github.com fonsp Pluto.jl , which means that it contains its own source code, and we encourage you to take a look behind the scenes. Use the button \"Edit or run\" on the top right to modify the notebook and reproduce the result yourself. \"\"\" md\"\"\" Example of running the model \"\"\" md\"\"\" Plot functions \"\"\" plot controls c ClimateMARGO.Models.Controls plot controls MRGA c.mitigate, c.remove, c.geoeng, c.adapt yearticks collect 2020 20 2200 function finish p plot p xlim 2020,2201 , xticks yearticks, size 680,200 , grid false, end function plot costs result ClimateModel show baseline Bool true, show controls Bool true, show damages Bool true, title \"Control costs & climate damages\" p plot ylim 0,25.1 , left margin 2.25Plots.Measures.mm, ylabel \"trillion USD year\", title nothing || plot p title title baseline show baseline && plot p, years, damage result discounting true pp.baseline damages..., fillrange zero years , fillopacity .2, linestyle dash, control costs controlled damages damage result M true, R true, G true, A true, discounting true show controls && plot p, years, controlled damages . cost result M true, R true, G true, A true, discounting true fillrange controlled damages, fillopacity .2, pp.controls... controlled damages show damages && plot p, years, controlled damages fillrange zero years , fillopacity .2, pp.damages... finish p end function plot concentrations result ClimateModel relative to preindustrial Bool true Tmax 5 p relative to preindustrial ? plot ylim 0,4.5 , yticks 1,2,3,4 , yformatter x string Int x , \"×\" , title \"Atmospheric CO₂ₑ concentration, relative to 1800 1850\", plot ylim 0,1400 , ylabel \"ppm\", title \"Atmospheric CO₂ₑ concentration\", factor relative to preindustrial ? preindustrial concentrations 1 baseline plot p, years, c result . factor pp.baseline concentrations..., linestyle dash, controlled temperature plot p, years, c result M true, R true . factor pp.concentrations... finish p end function plot controls controls MRGA title nothing p plot ylim 0,1 , title nothing || plot p title title for tech, c in enumerate controls if c nothing which tech M ? years . end of oil eachindex years plot p, years which , c which pp tech ... end end finish p end function plot emissions result ClimateModel p plot ylim 3,11 , left margin 1.75Plots.Measures.mm, ylabel \"ppm year\", title \"Global CO₂ₑ emissions\", baseline plot p, years, effective emissions result pp.baseline emissions..., linestyle dash, controlled plot p, years, effective emissions result M true, R true fillrange zero years , fillopacity .2, pp.emissions... finish p end function plot emissions pretty result ClimateModel offset the x values so that framestyle origin will make the y axis pass through 2020 instead of 0. yuck R x x 2020 L x x 2020 Tmax 5 p plot ylim 3,11 , ylabel \"ppm year\", framestyle origin, xformatter string ∘ Int ∘ R, baseline plot p, L. years , effective emissions result pp.baseline emissions..., linestyle dash, controlled temperature plot p, L. years , effective emissions result M true, R true fillrange zero L. years , fillopacity .2, pp.emissions... finish p plot p xlim L. extrema years , xticks L. yearticks , end function plot temp result ClimateModel Tmax 5 setup p plot ylim 0,Tmax , yticks 0,1,2,3 , yformatter x string \" \", Int x , \" °C\" , title \"Global warming relative to 1800 1850\", shade dangerously high temperatures for a in 2,3 plot p, collect extrema years , a,a , linewidth 0, label nothing, fillrange Tmax,Tmax , fillcolor colors.above paris end baseline plot p, years, T adapt result splat MRGA false ... pp.baseline temperature..., linestyle dash, controlled temperature plot p, years, T adapt result splat MRGA true ... pp.temperature... finish p end md\"\"\" Plot dots input magic \"\"\" md\"\"\" Running the model \"\"\" function forward controls temp controls MRGA MRGA , model parameters default parameters model ClimateModel model parameters translations Dict M mitigate, R remove, G geoeng, A adapt, for k, v in enumerate controls if v nothing setfieldconvert model.controls, translations Symbol k , copy v end end enforce maxslope model.controls model return Dict model parameters model parameters, model results model ... end initially initial 1 bind input 1 begin local t input 1 \"M\" 1 local y input 1 \"M\" 2 controls 1 MRGA M gaussish t, clamp .07 10 y , 0, 1 , R gaussish t, clamp .07 10 y 0.25, 0, 1 , result 1 forward controls temp controls 1 plotclicktracker2 plot emissions result 1 , initial 1 end plot temp result 1 initially initial 2 bind input 2 begin controls 2 MRGA M gaussish input 2 \"M\" ... , result 2 forward controls temp controls 2 plotclicktracker2 plot controls controls 2 title \"Deployment of mitigation\" , initial 2 end if which graph 2 \"Emissions\" plot emissions result 2 elseif which graph 2 \"Concentrations\" plot concentrations result 2 relative to preindustrial true else plot temp result 2 end initially initial 3 bind input 3 begin controls 3 MRGA M gaussish input 3 \"M\" ... , result 3 forward controls temp controls 3 plotclicktracker2 plot controls controls 3 title \"Deployment of mitigation\" , initial 3 end | cloak \"cost benefits narrative input\" if cost benefits narrative slide 1 local uncontrolled ClimateModel default parameters plot costs uncontrolled show controls false, show baseline false elseif cost benefits narrative slide 2 plot costs result 3 show controls false else plot costs result 3 end initially initial 4 bind input 4 begin controls 4 MRGA M gaussish input 4 \"M\" ... , R gaussish input 4 \"R\" ... , result 4 forward controls temp controls 4 plotclicktracker2 plot controls controls 4 title \"Deployment of mitigation\" , initial 4 end if which graph 4 \"Emissions\" plot emissions result 4 elseif which graph 4 \"Concentrations\" plot concentrations result 4 relative to preindustrial true elseif which graph 4 \"Temperature\" plot temp result 4 else plot costs result 4 end shortname MRGA \"M\",\"R\",\"G\",\"A\" mediumname MRGA \"mitigate\", \"remove\", \"geoeng\", \"adapt\" longname MRGA \"Mitigation\", \"Removal\", \"Geo engineering\", \"Adaptation\" let default cost let e default parameters .economics MRGA e.mitigate cost, e.remove cost, e.geoeng cost, e.adapt cost end blob htl \"\"\" h4 Which controls? h4 style .controltable thead th, .controltable tbody td text align center .controltable input type range width 10em style table class \"controltable\" thead th th th Enabled? th th style \"text align center \" Cost multiplier th thead tbody tr th longname.M th td bind enable M 9 CheckBox default true td td bind cost M 9 multiplier default cost.M, 4 td tr tr th longname.R th td bind enable R 9 CheckBox default true td td bind cost R 9 multiplier default cost.R, 4 td tr tr th longname.G th td bind enable G 9 CheckBox default false td td bind cost G 9 multiplier default cost.G, 4 td tr tr th longname.A th td bind enable A 9 CheckBox default false td td bind cost A 9 multiplier default cost.A, 4 td tr tbody table \"\"\" , \" c500b40a\" end control enabled 9 MRGA enable M 9, enable R 9, enable G 9, enable A 9, control cost 9 MRGA Float64 cost M 9 , Float64 cost R 9 , Float64 cost G 9 , Float64 cost A 9 , output 9 let parameters default parameters parameters.economics.mitigate cost control cost 9.M parameters.economics.remove cost control cost 9.R parameters.economics.geoeng cost control cost 9.G parameters.economics.adapt cost control cost 9.A modify the parameters here opt controls temp parameters temp overshoot allow overshoot 9 ? 999.0 Tmax 9, temp goal Tmax 9, max deployment let e control enabled 9 Dict \"mitigate\" e.M ? 1.0 0.0, \"remove\" e.R ? 1.0 0.0, \"geoeng\" e.G ? 1.0 0.0, \"adapt\" e.A ? 0.4 0.0, end, end plot controls output 9.result.controls | feasibility overlay output 9 plot plot temp output 9.result , years, zero years . Tmax 9 lw 2, pp.T max... md\"\"\" Cost bars \"\"\" function avoided damages bars result td x total discounted x, result baseline damages td damage result discounting true controlled damages td damage result splat MRGA true ..., discounting true avoided damages baseline damages controlled damages costs td cost result splat MRGA true ..., discounting true htl \"\"\" script const colors colors js const names names js const baseline damages baseline damages const controlled damages controlled damages const avoided damages avoided damages const costs costs const scale cost bars scale const bar offset, width, color html` span style \"margin left \\ offset % width \\ width % opacity .7 display inline block background \\ color height 1.2em margin bottom .2em \" span ` return html` div \\ bar 0, controlled damages scale, colors.damages span style \"opacity .6 \" Controlled damages b \\ Math.ceil controlled damages trillion USD b . div div style \"border bottom 2px solid eee margin bottom 4px \" \\ bar 0, baseline damages scale, colors.baseline span style \"opacity .6 \" Baseline damages b \\ Math.ceil baseline damages trillion USD b . span div div style \"font style italic \" \\ bar controlled damages scale, avoided damages scale, colors.avoided damages span Avoided damages b \\ Math.ceil avoided damages trillion USD b . span div ` script \"\"\" end if cost benefits narrative slide 2 avoided damages bars result 3 end function cost bars result offset damages false td x total discounted x, result baseline damages td damage result discounting true controlled damages td damage result splat MRGA true ..., discounting true avoided damages baseline damages controlled damages costs td cost result splat MRGA true ..., discounting true htl \"\"\" script const colors colors js const names names js const baseline damages baseline damages const controlled damages controlled damages const avoided damages avoided damages const costs costs const scale cost bars scale const bar offset, width, color html` span style \"margin left \\ offset % width \\ width % opacity .7 display inline block background \\ color height 1.2em margin bottom .2em \" span ` div \\ bar 0, baseline damages scale, colors.baseline Baseline damages b \\ Math.ceil baseline damages trillion USD b . div const extra offset offset damages ? controlled damages scale 0 return html` div \\ bar extra offset, costs scale, colors.controls span style \"opacity .6 \" Control costs b \\ Math.ceil costs trillion USD b . span div div style \"border bottom 2px solid eee margin bottom 4px \" \\ bar extra offset, avoided damages scale, colors.avoided damages span style \"opacity .6 \" Avoided damages b \\ Math.ceil avoided damages trillion USD b . span div div style \"font style italic \" title \"Net benefits Avoided damages minus the cost of getting there.\" \\ bar extra offset costs scale, avoided damages costs scale, colors.benefits span Net benefits b \\ Math.ceil avoided damages costs trillion USD b . span div ` script \"\"\" end cost bars result 1 if cost benefits narrative slide 3 cost bars result 3 offset damages true else bigbreak end md\"\"\" MRGA struct \"\"\" function MR x T,y T where T MRGA T x, y, zero x , zero x end "},{"url":"climate_science/odes_and_parameterized_types/","title":"ODEs and parameterized types","tags":["lecture","module3","epidemiology","plotting","ODE","differential equation","track_julia","track_math","programming","DifferentialEquations","type","structure","track_climate"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v S71YIZ8e7MQ\" image \"https user images.githubusercontent.com 6933510 136199713 25eb2e90 c5cd 4e11 8463 6f6069a81a51.png\" section 2 order 2 title \"ODEs and parameterized types\" layout \"layout.jlhtml\" youtube id \"S71YIZ8e7MQ\" description \"\" tags \"lecture\", \"module3\", \"epidemiology\", \"plotting\", \"ODE\", \"differential equation\", \"track julia\", \"track math\", \"programming\", \"DifferentialEquations\", \"type\", \"structure\", \"track climate\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using DifferentialEquations, Plots, PlutoUI, MappedArrays TableOfContents md\"\"\" Julia concepts Using libraries Libraries are built around types and objects Plot recipes Defining how to plot our objects Making objects into callable functions Parameterized types \"\"\" md\"\"\" ODEs and parameterized types \"\"\" md\"\"\" In the previous lecture we started to look at Ordinary Differential Equations ODEs . These are equations relating a function to its derivatives, telling us how to move at each moment in time. For example, exponential decay is modelled by \\dot u p \\, u, in other words \\dot u t p \\, u t for all real t \\ge 0 , with initial condition u 0 u 0 where u t is the number of light bulbs still functioning at time t , the number of radioactive nuclei that have not yet decayed, or the number of people who are still infectious and have not yet recovered. The parameter p is the decay rate. We saw that time stepping methods provide a class of numerical methods for solving such initial value problems, of which the Euler method is the simplest example. Unfortunately the Euler method is not a good choice, since it is not accurate enough. In numerical analysis classes like 18.330 https github.com mitmath 18330 various better methods are derived. Fortunately those methods are available in a Julia library or package, `DifferentialEquations.jl`. \"\"\" md\"\"\" The `DifferentialEquations.jl` library \"\"\" md\"\"\" DifferentialEquations.jl https diffeq.sciml.ai v2.0 often abbreviated as DiffEq is a Julia library for solving many types of differential equations. It has become the best most complete and fastest general library for solving ordinary differential equations and many other types in any programming environment. It is also a very good example of the way that Julia libraries are often structured. We will see some basic usage examples and how Julia types are used both to control the library and to store the input and output to the solvers . \"\"\" md\"\"\" Basic usage of DifferentialEquations.jl \"\"\" md\"\"\" Let's try to solve the above equation that we derived in the previous lecture, \\dot u p \\, u . We are using u as the name of the dependent variable for consistency with the `DifferentialEquations.jl` documentation and internal structure. We write a general ODE in 1 variable as \\dot u f p t, u . The right hand side of the ODE is given by a function f , which may depend on a parameter p , i.e. a constant which does not change for any given run of the ODE. f may also depend explicitly on time. In our example we do indeed have a parameter p that we want to change, but f does not have an explicit time dependence, so we write \"\"\" f u, p, t p u md\"\"\" Note that you always need the three input arguments `u`, `p` and `t`, in that order, even if you don't use them. \"\"\" md\"\"\" Now we define our initial condition u 0 and the time span to integrate over, given as a tuple initial\\ time, final\\ time \"\"\" begin u0 100.0 time span 0.0, 10.0 end md\"\"\" To set up the problem instance we use a type `ODEProblem` that is defined in the `DifferentialEquations.jl` package, into which we pass all the information necessary to define the problem. The parameters must go in the following order \"\"\" md\"\"\" For more advanced use there are also some additional, optional, keyword arguments. Note that the displayed output does not currently include information about the function, nor the parameters. \"\"\" md\"\"\" To solve the ODE we call the `solve` function \"\"\" md\"\"\" What happened here? A suitable solver i.e. an algorithm to calculate the solution was chosen automatically , and it chose certain moments in time at which to output information about the approximate, but very accurate solution. In this particular case it chose to output data at only eight points in time between t 0 and t 10 . Let's try to plot the `solution` object \"\"\" md\"\"\" Plot recipes \"\"\" md\"\"\" Two surprising things happen. Firstly, there is no reason to expect this to have worked `solution` is some kind of Julia object, but somehow `Plots.jl` knows how to plot it. This is because `DifferentialEquations.jl` defines a plot recipe . This specifies a way to turn solution objects into plots Any package can do this relatively easily. \"\"\" md\"\"\" The second surprise is that the output looks like a smooth curve, rather than just 8 points. Let's see those points on top of the curve. We can extract the relevant data from the `solution` object \"\"\" md\"\"\" We see that the package in fact gives not only the value at those points, but it is in fact also capable of calculating an approximate solution at any intermediate point, using interpolation . In fact, we can access this by treating `solution` as if it were a function \"\"\" md\"\"\" For this particular ODE we know the analytical solution. Let's compare them as we vary the parameter p \"\"\" md\"\"\" p bind p Slider 0.0 0.1 2.0, show value true \"\"\" problem ODEProblem f, u0, time span, p solution solve problem plot solution, size 500, 300 , label \"solution\" scatter solution.t, solution.u, label \"discrete output\" begin tt 3.5 solution tt end scatter tt , solution tt , label \"t tt \", ms 5, m square let problem ODEProblem f, u0, time span, p solution solve problem plot solution, linewidth 3, xlabel \"t\", yaxis \"x t \", label \"numerical\", size 500, 300 plot t u0 exp p t , lw 3, ls dash, label \"exact\" ylims 0, 100 title \"p p\" end md\"\"\" We see that the numerical and exact solutions are to the eye indistinguishable, and that the package is fast enough to calculate the solution basically in real time. \"\"\" md\"\"\" Systems of ODEs \"\"\" md\"\"\" Now let's try to solve the SIR equations \\begin aligned \\dot s & \\beta s i \\\\ \\dot i & \\beta s i \\gamma i\\\\ \\dot r & \\gamma i \\end aligned \"\"\" md\"\"\" We need to think of the system in a vector form, \\dot \\mathbf x \\mathbf f \\mathbf p t, \\mathbf x , where the vector \\mathbf x is a vector of all the variables, \\mathbf x x 1, x 2, x 3 s, i, r the function \\mathbf f is a vector valued function \\mathbb R ^3 \\to \\mathbb R ^3 , where f k gives the right hand side of the equation for \\dot x k and the vector \\mathbf p \\beta, \\gamma is a vector of the parameters. \"\"\" x0 0.99, 0.01, 0.0 function SIR x, p, t s, i, r x unpack the vectors into scalar values β, γ p build a new vector to return return β s i, β s i γ i, γ i end md\"\"\" Now we see that the solverr has recognised that everything is a vector, and it returns a vector at each time stamp. Again we can plot \"\"\" md\"\"\" β bind β Slider 0.5 0.01 2.0, default 1.0, show value true γ bind γ Slider 0.5 0.01 2.0, default 0.1, show value true \"\"\" params β, γ SIR problem ODEProblem SIR, x0, 0.0, 50.0 , params sol solve SIR problem plot sol md\"\"\" It knows to plot each variable separately. We can instead plot combinations of variables in phase space or state space \"\"\" plot sol, idxs 1, 2 , xlabel \"s\", ylabel \"i\", arrow true, xlims 0.1, 1.0 , size 500, 300 md\"\"\" And even in 3D \"\"\" let plotly p plot sol, vars 1, 2, 3 , xlabel \"s\", ylabel \"i\", zlabel \"r\" gr p end md\"\"\" The ModelingToolkit.jl https mtk.sciml.ai stable tutorials ode modeling library provides ways to make creating ODE models more intuitive, using symbolic equation objects. \"\"\" md\"\"\" Making objects into callable functions \"\"\" md\"\"\" One of the interesting things we have seen is the way that the `solution` object can be called using the normal syntax for calling functions \"\"\" solution 3.5 md\"\"\" Not all types allow this. For example, if we define a matrix \"\"\" A rand 2, 2 md\"\"\" we cannot usually call it with a vector \"\"\" A rand 2 md\"\"\" If we wanted to do so, we could, however, using the following syntax \"\"\" A Matrix x A x md\"\"\" Note that this makes objects instances of the type callable, rather than the type itself, which we can already call as a constructor to create objects of that type \"\"\" Matrix 1 2 3 4 md\"\"\" For example, if we wanted to write our own type to store the output of the Euler method, we could do something like \"\"\" begin struct SimpleEulerOutput times Vector Float64 values Vector Float64 end function soln SimpleEulerOutput t use interpolation to calculate the value at time t using `soln.times` and `soln.values` end end md\"\"\" The definition of the function makes it callable. This says that \"the way to call an object called `soln`, of type `EulerOutput`, is the following \". Note that inside the function you will need to access the `times` and `values` from inside the `soln` object, using `soln.times` and `soln.values`. Note that at the time of writing, Pluto requires that you put this call definition in the same cell as the type definition. \"\"\" md\"\"\" Digging into objects \"\"\" md\"\"\" A key feature of the `DifferentialEquations.jl` library, and of most other libraries in Julia, is the way they define and use new types . As we have seen previously, we can define new types using `struct` which store data, and then define functions which act on those types. \"\"\" md\"\"\" Let's start by looking at the `ODEProblem` type \"\"\" problem md\"\"\" To see what fields, or attributes, the object contains we can use \"\"\" fieldnames typeof problem md\"\"\" In Pluto and other interactive environments we can get the same information using `problem. TAB `, i.e. by typing the TAB key after `problem.` \"\"\" md\"\"\" As usual we can extract the data contained in the object as usual using `.` \"\"\" problem.u0 problem.tspan md\"\"\" To see everything contained in the object, we can use `dump` \"\"\" dump problem md\"\"\" This seems much more complicated than you might expect. The library is clearly doing more than just storing the data that we provide in a type The main thing that has happened is that the function we passed in has been processed by wrapping it into yet another type, `ODEFunction`. This contains information such as how to calculate the derivative of the function that can be provided for more advanced usage of the package. \"\"\" md\"\"\" Similarly we can look inside the solution object \"\"\" fieldnames typeof solution dump solution md\"\"\" The solution is even more complicated, containing not only the data that was calculated, but also all information about which algorithms were used to solve the problem and tables of coefficients for interpolation. \"\"\" md\"\"\" Parameterized types \"\"\" md\"\"\" Let's look now at the type of the DiffEq objects \"\"\" typeof problem md\"\"\" We see that indeed `problem` is an object of type `ODEProblem`, but that it also has several type parameters , which are listed inside the curly braces ` ` and ` ` \"\"\" typeof problem .parameters... md\"\"\" For example, we see that the second type parameter is of type `Tuple Float64, Float64 `, which is the type of the variable `time span` \"\"\" typeof time span md\"\"\" One of the reasons for using parameterized types like this is for efficiency Julia is fastest when it knows the exact types of every variable, since then it can generate efficient machine code. \"\"\" md\"\"\" Making parameterized types \"\"\" md\"\"\" Let's see how and why we would make a parameterized type. Let's think again about the `EulerOutput` type that we started to write above. What happens if, as in the `DifferentialEquations.jl` library, we would like to handle both scalar ODEs one single real dependent variable , and vector ODEs? We can no longer specify that the output type is a `Vector Float64 `, so we would need to leave it unspecified \"\"\" struct SimpleEulerOutput2 times values end md\"\"\" However, it turns out that we then lose efficiency speed . What we would need is two different types one in which we specify the type of `values` as `Vector Float64 `, and the other as `Vector Vector Float64 `. Julia provides a mechanism to define both at once this is what parameterized types do \"\"\" md\"\"\" Let's define our own `MyODEProblem` type as a simple example. We specify type parameters for each variable \"\"\" struct MyODEProblem T,U t0 T tfinal T u0 U end md\"\"\" Note that the starting and final times have been specified to require the same type `T`. \"\"\" md\"\"\" Now if we create objects of this type, we see that Julia matches the types we pass in to the correct type parameters \"\"\" newprob MyODEProblem 3, 4, 100.0 newprob.t0 newprob.tfinal newprob.u0 md\"\"\" If we change the types of the inputs, the type parameters change \"\"\" MyODEProblem 3.0, 4.0, 100.0 md\"\"\" If we mix types for the first two arguments then we get an error, since Julia can't match to a single type `T` \"\"\" MyODEProblem 3.0, 5, 100.0 md\"\"\" We can use vectors \"\"\" MyODEProblem 3, 4, 17.0, 18.0 "},{"url":"climate_science/optimization_with_JuMP/","title":"Solving inverse problems","tags":["lecture","module3","optimization","JuMP","track_math","track_climate","track_julia","modeling","plotting"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v nm86 hDwYTU\" image \"https user images.githubusercontent.com 6933510 136200827 89647ae8 cb06 42ea a18d 5f64e9cc2b25.png\" section 11 order 11 title \"Solving inverse problems\" layout \"layout.jlhtml\" youtube id \"nm86 hDwYTU\" description \"\" tags \"lecture\", \"module3\", \"optimization\", \"JuMP\", \"track math\", \"track climate\", \"track julia\", \"modeling\", \"plotting\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using PlutoUI, Plots, Statistics, JuMP, Ipopt end TableOfContents md\"\"\" Solving inverse problems Optimization with JuMP \"\"\" md\"\"\" Forward and inverse problems \"\"\" md\"\"\" In a forward problem we provide inputs and calculate what the resulting output is using a model. In an inverse problem https en.wikipedia.org wiki Inverse problem we have a goal output and wish to find which inputs to the model will produce that goal. To do so we often need to solve an optimization problem . JuMP is a modeling language embedded in Julia. It allows us to write down optimization problems in a natural way these are then converted by JuMP into the correct input format to be sent to different solvers , i.e. software programs that choose which optimization algorithms to apply to solve the optimization problem. \"\"\" md\"\"\" Unconstrained optimization \"\"\" mm Model Ipopt.Optimizer md\"\"\" a bind a Slider 3 0.01 3, show value true, default 0 \"\"\" f x x^2 a x 2 min value let model Model Ipopt.Optimizer tell JuMP about our function register model, f, 1, f, autodiff true declare a variable variable model, 10 ≤ x ≤ 10 set the objective function to optimize NLobjective model, Min, f x optimize model value x end begin plot f, size 400, 300 , leg false plot x x^2 2, ls dash, alpha 0.5 scatter min value , f min value , xlims 5, 5 , ylims 10, 20 end md\"\"\" Constrained optimization \"\"\" md\"\"\" Often we need to add constraints , i.e. restrictions, to the problem. \"\"\" g x 3x 4 constraint x, y y x k x, y x^2 y^2 1 b slider bind b Slider 5 0.1 5, show value true, default 0 minx, miny let model Model Ipopt.Optimizer register model, k, 2, k, autodiff true register model, constraint, 2, constraint, autodiff true variable model, 10 ≤ x ≤ 10 variable model, 10 ≤ y ≤ 10 NLobjective model, Min, k x, y NLconstraint model, constraint x, y b optimize model x value x , y value y end md\"\"\" b b slider \"\"\" gr begin r 5 0.1 5 contour r, r, k, leg false contour r, r, constraint, levels b , ratio 1, lw 3 scatter minx , miny end plotly surface 5 0.1 5, 5 0.1 5, k, alpha 0.5 md\"\"\" y x b so y x b z x^2 y^2 x^2 x b ^2 \"\"\" begin xs 5 0.1 5 ys xs . b end b slider begin surface 5 0.1 5, 5 0.1 5, k, alpha 0.5 plot xs, ys, k. xs, ys , lw 3 scatter minx , miny , k minx, miny , zlim 10, 50 , xlim 5, 5 , ylim 5, 5 end "},{"url":"climate_science/our_first_climate_model/","title":"Our first climate model","tags":["lecture","module3","climate","modeling","track_climate","track_math","plotting","data","ODE","differential equation","CSV","dataframe","track_data"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v J1UsMa1cTeE\" image \"https user images.githubusercontent.com 6933510 136199705 7bdb6bb0 8698 43a1 87e6 c073ab102da5.png\" section 4 order 4 title \"Our first climate model\" layout \"layout.jlhtml\" youtube id \"J1UsMa1cTeE\" description \"\" tags \"lecture\", \"module3\", \"climate\", \"modeling\", \"track climate\", \"track math\", \"plotting\", \"data\", \"ODE\", \"differential equation\", \"CSV\", \"dataframe\", \"track data\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using DifferentialEquations using Plots, PlutoUI using LinearAlgebra using CSV, DataFrames end TableOfContents title \"📚 Table of Contents\", indent true, depth 4, aside true md\"\"\" This lecture is based on the fall 2020 \"Introduction to Climate Modeling\" lecture by Henri Drake. Henri's video https computationalthinking.mit.edu Fall20 lecture20 , Henri's original notebook https github.com mitmath 18S191 blob Fall20 lecture notebooks week11 01 energy balance model.jl where he describes the Energy Balance Model. \"\"\" md\"\"\" Pre Computational Thinking traditional ode \"analytic\" solutions \"\"\" md\"\"\" Notation ``y y t `` is a function of time t Inititally t 0 , y 0 y 0 We write ``y'`` for ``dy dt``. \"\"\" md\"\"\" ``y' `` constant ``y' a`` html\" br \" Solution ``y t at y 0`` \"\"\" md\"\"\" ``y' `` linear in y ``y' a by`` html\" br \" Solution ``y t \\frac a b e^ bt 1 y 0 e^ bt `` Comment Equilibrium obtained if b\\ne0 by solving y' 0 for y giving equilibrium y a b . Also can be obtained by letting t\\rightarrow \\infty in the solution killing the exponential terms. \"\"\" md\"\"\" ``y' `` linear in y \"forcing term\" in t ``y' a by f t `` html\" br \" Solution ``y t e^ bt \\left y 0 \\int 0^t e^ bu a f u du \\right `` \"\"\" md\"\"\" Computational Thinking \"\"\" md\"\"\" Review Solving an ODE y' f y in Julia 0. Define f y,p,t 1. ODEProblem f, f 0 , time span, parameters 2. solve 3. plot etc \"\"\" f y, a, b, forcing c ,t a b y forcing c t forcing c t c t md\"\"\" a bind a Slider 0 .01 10, show value true, default 0 b bind b Slider 0 .1 5, show value true, default 0 y₀ bind y₀ Slider 5 .1 15, show value true, default 2.0 c bind c Slider 0 .1 5, show value true, default 0.0 \"\"\" sol solve ODEProblem f, y₀, 0, 10.0 , a, b, forcing c y₀ begin p plot sol , legend false, background color inside black , ylims 0, 10 , lw 3, c red plot direction field xs Float64 ys Float64 lrx LinRange xlims p ..., 30 for x in lrx for y in LinRange ylims p ..., 30 v 1, a b y forcing c x v . 20 lrx 2 lrx 1 plot x, x v 1 , y, y v 2 , alpha 0.5, c gray push xs, x v 1 , x v 1 , NaN push ys, y v 2 , y v 2 , NaN end end hline b 0 ? 0 a b ,c white,ls dash plot xs, ys, alpha 0.7, c yellow ylabel \"y\" annotate .5,y₀,text \"y₀\",color red title \"Solution to y' t a by forcing c t \" end md\"\"\" Background climate physics The simplest climate model can be conceptualized as ```math \\begin align \\text \\color brown change in heat content & \\text \\color orange absorbed solar radiation energy from the Sun's rays \\newline & \\text \\color blue outgoing thermal radiation i.e. blackbody cooling to space \\newline & \\text \\color grey human caused greenhouse effect trapped outgoing radiation \\end align ``` where each of these is interpreted as an average over the entire globe hence \"zero dimensional\" . \"\"\" html\"\"\" img src \"https raw.githubusercontent.com hdrake hdrake.github.io master figures planetary energy balance.png\" height 225 \"\"\" md\"\"\" 1.1 Incoming 🌞 Absorbed solar radiation an example of \\mathrm temp ' constant \"\"\" md\"\"\" Heating the earth nonstop \"\"\" md\"\"\" At Earth's orbital distance from the Sun, the power of the Sun's rays that intercept the Earth is equal to \"\"\" S 1368 solar insolation W m^2 energy per unit time per unit area md\"A small fraction\" α 0.3 albedo, or planetary reflectivity unitless md\"\"\" In math we just write down a differential equation, but in the physical world there are physical variables to identify. In our baking the earth example, we will identify the following quantities Industrial Revolution Start 1850 Avg Temperature in 1850 14.0 °C Solar Insolation S 1368 W m^2 energy from the sun Albedo or plentary reflectivity α 0.3 atmosphere and upper ocean heat capacity C 51 J m^2 °C Earth Baking Formula html\" br \" `` C\\ \\mathrm temp ' t S 1 α 4 `` round S 1 α 4 sigdigits 4 \"\"\" md\"\"\" of this incoming solar radiation is reflected back out to space by reflective surfaces like white clouds, snow, and ice , with the remaining fraction 1 \\alpha being absorbed. Since the incoming solar rays are all approximately parallel this far from the Sun, the cross sectional area of the Earth that intercepts them is just a disc of area \\pi R^ 2 . Since all of the other terms we will consider act on the entire surface area 4\\pi R^ 2 of the spherical Earth, the absorbed solar radiation per unit surface area averaged over the entire globe is reduced by a factor of 4. https www.open.edu openlearn ocw pluginfile.php 101161 mod oucontent oucontent 890 639dcd57 ce3f1c3a s250 3 002i.jpg The absorbed solar radiation per unit area is thus \\textcolor orange \\text absorbed solar radiation \\equiv \\frac S 1 \\alpha 4 \"\"\" absorbed solar radiation S 1 α 4 W m^2 begin C 51. atmosphere and upper ocean heat capacity Wyr m^2 °C temp₀ 14.0 preindustrial temperature °C end p1 ODEProblem temp, p, t 1 C absorbed solar radiation, temp₀, 0.0, 170 begin plot solve p1 , legend false, background color inside black, xlabel \"years from 1850 \", ylabel \"Temperature °C\" hline temp₀,temp₀ ,c white,ls dash annotate 80, 25 temp₀, text \"Preindustrial Temperature temp₀ °C\",color white title \"Absorbing Solar Radiation only \" end md\"\"\"The heat content C temp is determined by the temperature temp in Kelvin and the heat capacity of the climate system. While we are interested in the temperature of the atmosphere, which has a very small heat capacity, its heat is closely coupled with that of the upper ocean, which has a much larger heat capacity of \"\"\" md\"\"\" The change in heat content over time is thus simply given by \\frac d CT dt . Since the heat capacity of sea water hardly changes with temperature, we can rewrite this in terms of the change in temperature with time as \\color brown \\text change in heat content \\ C \\frac dtemp dt \"\"\" md\"\"\" 1.2 Outgoing ♨ thermal radiation \"\"\" md\"\"\" The outgoing thermal radiation term \\mathcal G T or \"blackbody cooling to space\" represents the combined effects of negative feedbacks that dampen warming , such as blackbody radiation , and positive feedbacks that amplify warming , such as the water vapor feedback . Since these physics are too complicated to deal with here, we linearize the model combining the incoming and the outgoing. We assume that the preindustrial world was in energy balance, and thus the equilibrium temperature is the preindustrial temperature. Thus we assume temp' t B temp 0 temp t for some value of B . The minus sign in front of temp t indicating it restores equilibrium. \"\"\" md\"\"\" The value that has been chosen is \"\"\" B 1.3 climate feedback parameter W m^2 °C , md\"\"\" start\\ temp bind start temp Slider 0 28 show value true, default 14 \"\"\" p2 ODEProblem temp, p, t 1 C B temp₀ temp , start temp, 0.0, 170 begin plot solve p2 , legend false, background color inside black, xlabel \"years from start\", ylabel \"Temperature °C\", ylim 0, 30 hline temp₀,temp₀ ,c white,ls dash annotate 80, temp₀, text \"Preindustrial Temperature temp₀ °C\", bottom,color white title \"Energy Balance Model Healthy Earth \" end md\"\"\" 1.3 Greenhouse 🏭 Human caused greenhouse effect Empirically, the greenhouse effect is known to be a logarithmic function of gaseous carbon dioxide CO₂ concentrations \\color grey \\text human caused greenhouse effect \\ \\mbox forcing\\ coef \\ln \\left \\frac \\text CO ₂ \\text CO ₂ \\text PreIndust \\right , How this depends on time into the future depends on human behavior Time is not modelled in the above equation. \"\"\" md\"where\" forcing coef 5.0 CO2 forcing coefficient W m^2 CO₂ PreIndust 280. preindustrial CO2 concentration parts per million ppm greenhouse effect CO₂ forcing coef log CO₂ CO₂ PreIndust begin CO2 present 420. CO2 range 280 2 .^ range 1, stop 3,length 100 plot CO2 range, greenhouse effect. CO2 range , lw 2.5, label nothing, color black plot CO₂ PreIndust , greenhouse effect CO₂ PreIndust , marker circle, ms 6, linecolor white, color blue, lw 0, label \"pre industrial PI \" plot CO2 present , greenhouse effect CO2 present , marker circle, ms 6, color red, linecolor white, lw 0, label \"present day 2020 \" plot xticks 280, 280 2, 280 4, 280 8 , legend bottomright, size 400, 250 plot ylabel \"Radiative forcing W m² \", xlabel \"CO₂ concentration ppm \" end begin CO₂ t CO₂ PreIndust no emissions CO₂ t CO₂ PreIndust 1.01^t test model CO₂ t CO₂ PreIndust 1 t 220 ^3 end greenhouse effect CO₂ 15 p3 ODEProblem temp, p, t 1 C B temp₀ temp greenhouse effect CO₂ t , start temp, 0.0, 170 begin plot solve p3 , legend false, background color inside black, xlabel \"years from 1850\", ylabel \"Temperature °C\", ylim 10, 20 hline temp₀,temp₀ ,c white,ls dash annotate 80, temp₀, text \"Preindustrial Temperature temp₀ °C\", bottom,color white title \"Model with CO₂\" end begin years 1850 2030 plot years, CO₂. years. 1850 , lw 3, legend false xlabel \"year\" ylabel \"CO₂ ppm \" end md\"\"\" Observations from Mauna Loa Volcano Mauna Loa Volcano https i.pinimg.com originals df 1a e7 df1ae72cfd5e6d0d535c0ec99e708f6f.jpg information is available at https www.ncei.noaa.gov pub data paleo icecore antarctica law law2006.txt https www.ncei.noaa.gov pub data paleo icecore antarctica law law2006.txt . \"\"\" CO2 historical data url \"https gml.noaa.gov webdata ccgg trends co2 co2 mm mlo.csv\" CO2 historical path download CO2 historical data url begin offset findfirst startswith \" \" , readlines CO2 historical path CO2 historical data raw CSV.read CO2 historical path, DataFrame header offset, skipto offset 1, first CO2 historical data raw, 11 end md\"\"\" Data is in the column `\"average\"`. \"\"\" md\"\"\" Oh no, missing data 99.99 \"\"\" validrowsmask CO2 historical data raw , \"average\" . 0 CO2 historical data CO2 historical data raw validrowsmask, begin plot CO2 historical data , \"decimal date\" , CO2 historical data , \"average\" , label \"Mauna Loa CO₂ data Keeling curve \" plot years, CO₂. years. 1850 , lw 3 , label \"Cubic Fit\", legend topleft title \"CO₂ observations and fit\" end md\"\"\" We will use this fit to compare against historical temperatures. \"\"\" md\"\"\" Climate feedback BB bind BB Slider 0 .1 4, show value true, default B Ocean Heat Capacity CC bind CC Slider 10 .1 200, show value true, default C \"\"\" p4 ODEProblem temp, p, t 1 CC BB temp₀ temp greenhouse effect CO₂ t , start temp, 0.0, 170 md\"\"\" Best and worst case projections of future global warming \"\"\" md\"\"\"Consider two divergent hypothetical futures 1. a low emissions world in which emissions decrease such that CO2 concentrations stay below 500 ppm by 2100 known in climate circles as \"RCP2.6\" and 2. a high emissions world in which emissions continue increasing and CO2 concentrations soar upwards of 1200 ppm \"RCP8.5\" . \"\"\" md\"\"\" https raw.githubusercontent.com mitmath 18S191 Spring21 notebooks week12 predictthefuture.svg \"\"\" md\"\"\" In the low emissions scenario, the temperature increase stays below ΔT 2 °C by 2100, while in the high emissions scenario temperatures soar upwards of 3.5ºC above pre industrial levels. \"\"\" md\"Although the greenhouse effect due to human caused CO₂ emissions is the dominant forcing behind historical and future projected warming, modern climate modelling considers a fairly exhaustive list of other forcing factors aerosols, other greenhouse gases, ozone, land use changes, etc. . The video below shows a breakdown of these forcing factors in a state of the art climate model simulation of the historical period.\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid E7kMr2OYKSU params \"modestbranding 1&rel 0\" lite youtube \"\"\" solp4 solve p4 begin T url \"https data.giss.nasa.gov gistemp graphs graph data Global Mean Estimates based on Land and Ocean Data graph.txt\" T df CSV.read download T url ,DataFrame, header false, skipto 6,delim \" \" T df T df , 1,6 end begin plot years,solp4. years. 1850 ,lw 2,label \"Predicted Temperature from model\", legend topleft xlabel \"year\" ylabel \"Temp °C\" plot parse. Float64, T df ,1 , parse. Float64, T df ,2 . 14.15, color black, label \"NASA Observations\", legend topleft end "},{"url":"climate_science/predicting_the_weather/","title":"Why we can't predict the weather","tags":["lecture","module3","track_climate","track_math","bifurcation","nonlinear","ODE","differential equation","continuous","plotting","dynamics","climate","modeling","DifferentialEquations"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v M3udLzIHtsc\" image \"https user images.githubusercontent.com 6933510 136199708 af8acad2 4172 4fa7 911e e30300efb5ee.png\" section 3 order 3 title \"Why we can't predict the weather\" layout \"layout.jlhtml\" youtube id \"M3udLzIHtsc\" description \"\" tags \"lecture\", \"module3\", \"track climate\", \"track math\", \"bifurcation\", \"nonlinear\", \"ODE\", \"differential equation\", \"continuous\", \"plotting\", \"dynamics\", \"climate\", \"modeling\", \"DifferentialEquations\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Plots, PlutoUI, LaTeXStrings, Roots, DifferentialEquations using LinearAlgebra end TableOfContents md\"\"\" Why we can predict the climate but can't predict the weather \"\"\" md\"\"\" Weather vs. climate \"\"\" md\"\"\" Will it rain tomorrow? We should be able to predict that using observations about the current state of the atmosphere and mathematical models in the form of partial differential equations that predict how the wind will move the air around, what the temperature will be, and how much water can precipitate out. What about predicting rain next week? Next year? We know from experience that we can only predict the weather in the short term, up to a week or so and still only with a certain probability . Predicting even one month out doesn't seem possible. On the other hand, we can talk about the climate and how it behaves on much longer time scales, e.g. \"the average temperature in in Boston in April is 55°F\". The short term weather has a significant random or apparently random component, but becomes much more predictable when we look at statistical properties like averages and standard deviations this is then called climate . \"\"\" md\"\"\" Nonlinear dynamics stability and bifurcations \"\"\" md\"\"\" In this notebook we will see that the simplest possible model of the weather, a set of 3 coupled, nonlinear ODEs known as the Lorenz equations , can have unpredictable behaviour, known as deterministic chaos . We will build up to that by looking at two simpler models of dynamics, in 1D and 2D. \"\"\" md\"\"\" Reminder Ordinary Differential Equations ODEs \"\"\" md\"\"\" Recall that we are looking at differential equations , with the goal of modelling the evolution in time of the climate. The simplest such models are ordinary differential equations ODEs , where one or a few continuous variables evolve continuously in time, with a model that specifies their instantaneous rate of change as a function of their current values. The general form of an ODE that does not depend explicitly on time, i.e. that is autonomous , is \\frac dx t dt f x t , or \\dot x t f x t , with an initial condition x t 0 x 0 . Here \\dot x t denotes the derivative of the function t \\mapsto x t at time t . \"\"\" md\"\"\" We have seen that the simplest numerical method to solve such an equation is the forward Euler method , in which we convert this equation into an explicit time stepping routine we take a small time step of length h and approximate the derivative as \\frac dx t dt \\simeq \\frac x t h x t h , giving the numerical method x n 1 x n h f x n , where x n is the approximation of the true solution x t n at the n th time step t n . \"\"\" md\"\"\" 1D Modelling bacterial growth \"\"\" md\"\"\" Let's use this to simulate a simple nonlinear ODE describing the dynamics of a population of bacteria. The bacteria reproduce at a rate \\lambda provided that there is sufficient food, in which case we would have \\dot x \\lambda x . But the need for enough food will put a limit on the sustainable population to a value K , sometimes called the carrying capacity . The simplest model for the combined effect of growth and saturation is as follows \\dot x \\lambda \\, x \\, K x . When x is close to 0 , the growth rate is \\lambda , but that rate decreases as x increases. This is sometimes called the logistic https en.wikipedia.org wiki Logistic function Logistic differential equation differential equation, although that name does not seem particularly helpful. \"\"\" md\"\"\" Our goal is to use computational thinking, but we will actually not be interested so much in the exact dynamics in time, but rather in the qualitative features of the behaviour of the system. For example, at long times formally t \\to \\infty does the population get arbitrarily large? Or does it, for example, oscillate around a particular value? Or does it converge to a particular size? This forms the subject of nonlinear dynamics or dynamical systems theory. \"\"\" md\"\"\" Let's simulate the system using the Euler method to try to guess the answer to this question. Note that there are many much more sophisticated methods for solving ODEs collected in the DifferentialEquations.jl package. We should never use the Euler method in practice, but should use a tried and tested library instead, and algorithms that provide much better accuracy in the solutions, if we are interested in faithful numerical results. \"\"\" md\"\"\" It's useful to rescale the variables to the simplest form \\dot x x \\, 1 x . We can do so by defining new adimensional space and time variables x' and t' as follows x K \\, x' , giving K \\dot x' \\lambda K \\, x' K K x' , and then t' \\lambda K \\, t , giving dx' dt' x' 1 x' . \"\"\" md\"\"\" We define a function representing the right hand side of the ODE \"\"\" logistic x x 1 x md\"\"\" Let's simulate this with the Euler method and plot the trajectory x t as a function of time t \"\"\" md\"\"\" Normally we would not choose the Euler method to calculate actual trajectories, but it is enough for our purposes of understanding the qualitative behaviour of the equations. \"\"\" md\"\"\" We see that for this particular initial condition, the solution seems to settle down to a fixed value after some time, and then remains at that value thereafter. Such a value is called a fixed point , a stationary point , or a steady state of the ODE. \"\"\" md\"\"\" Qualitative behaviour Fixed points and their stability \"\"\" gr fmt png, dpi 150, size 300, 200 gr fmt svg gr fmt png, dpi 300, size 400, 300 md\"\"\" Let's see what happens for other initial conditions \"\"\" md\"\"\" x₀ bind x0 Slider 0.9 0.001 3.0, default 0.5, show value true \"\"\" md\"\"\" To get an overview of the behaviour we can draw all the results on a single graph \"\"\" md\"\"\" We see that all the curves starting near to x 0 1.0 seem to converge to 1 at long times. If the system starts exactly at 0 then it stays there forever. However, if it starts close to 0, on either side, then it moves away from 0 on that same side of 0 starting from a negative value x becomes ever more negative. Even though negative populations have no meaning in the original interpretation as the dynamics of a population, we can still ask study the dynamics of the equation with negative initial conditions, since it may model other systems too. The special values x^ 1 1 and x^ 2 0 are called stationary points or fixed points of the differential equation. If we start at x^ i , then the derivative there is f' x^ i 0 , and hence we cannot move away from x^ i The fixed points can be found as zeros or roots of the function f , i.e. values x^ such that f x^ 0 . \"\"\" md\"\"\" We see, though, that the two types of fixed points are qualitatively different trajectories that start close to x^ 1 1 move towards x^ 1 , whereas trajectories that start close to x^ 2 0 move away from it. We say that x^ 1 is a stable fixed point and x^ 2 is an unstable fixed point . In general it is not possible to find analytical formulas for the position and stability of fixed points instead, we can use numerical root finding algorithms , for example the Newton method. There are various Julia packages with implementations of these algorithms we will use `Roots.jl`, which works well for scalar valued functions. \"\"\" md\"\"\" State space Vector field and phase portrait \"\"\" md\"\"\" If we want to find the whole trajectory for a given initial condition then we need to solve the equations, either numerically or analytically. However, we may want less information about the system, for example the long time or asymptotic dynamics. It turns out that we can obtain some information about that without explicitly solving the ODE This is the qualitative approach to studying nonlinear systems. Instead of drawing trajectories x t as a function of time t , as we did above, let's use a different graphical representation, where we draw state space or phase space This is the set \"space\" of all possible values of the dependent variables \"states\" . For the above ODE there is only a single dependent variable, x , so the state space is the real line, \\mathbb R . At each possible value of x , the ODE gives us information about the rate of change of x t at that point. Let's draw an arrow at that point, pointing in the direction that a particle placed at that point would move to the right if \\dot x 0 and to the left if \\dot x 0 . \"\"\" md\"\"\" This vector field indeed gives us a qualitative picture of the dynamics. It does not tell us how fast the dynamics will occur in each region, but it indicates what the tendency is. We have coded the fixed points according to their stability this may be calculated using the derivative evaluated at the fixed point, f' x^ , since this derivative controls the behaviour of nearby initial conditions x^ \\delta x . The unstable fixed point is shown as a green square, and the stable fixed point as a grey circle we will use this convention throughout the notebook. \"\"\" md\"\"\" Bifurcations \"\"\" md\"\"\" Now suppose that there is a parameter \\mu in the system that can be varied. For each value of \\mu we have a different ODE \\dot x f \\mu x . For example, \\dot x \\mu x^2. Let's draw the state space for each different value of \\mu \"\"\" g μ, x μ x^2 md\"\"\" μ bind λ Slider 1.0 0.05 1, show value true \"\"\" md\"\"\" Now let's collect all the vector fields into a single plot. The horizontal axis now represents the different possible values of the parameter \\mu \"\"\" md\"\"\" We see that at the critical value \\mu c 0 there is a qualitative change in behaviour of the system for \\mu c 0 there are two fixed points, whereas for \\mu c 0 there are no fixed points at all. Such a qualitative change is called a bifurcation . In this particular case the two fixed points collide in a saddle node or fold bifurcation. \"\"\" md\"\"\" 1D Bistability and hysteresis \"\"\" md\"\"\" Now let's look at the dynamics of the following system \\dot x \\mu x x^3. \"\"\" h μ, x μ x x^3 md\"\"\" Let's plot the bifurcation diagram again \"\"\" md\"\"\" We see that there is a range of values of \\mu for which there are three coexisting fixed points , two stable and one unstable. Since there are two stable fixed points in which the system can remain, we say that the system is bistable . \"\"\" md\"\"\" Now that we understand what the plots mean and the dynamics, let's plot just the fixed points x^ \\mu as a function of \\mu . Such a plot is called a bifurcation diagram \"\"\" md\"\"\" The pieces of curve are called branches . \"\"\" md\"\"\" Hysteresis \"\"\" md\"\"\" Suppose we now think about slowly varying the parameter \\mu . If we change the parameter \\mu by a little, the system is no longer at a fixed point, since the position of the fixed point moves when \\mu changes. However, the system will then relax it will follow the dynamics at the new value of \\mu , and will rapidly converge to the new fixed point nearby for that new value of \\mu . For example, starting at \\mu 2 , the system will stay on the lower black stable branch until \\mu 0.4 or so. At that point, two fixed points collide and annihilate each other After that there is no longer a fixed point nearby. However, there is another fixed point much further up that will now attract all trajectories, so the system rapidly transitions to that fixed point. Now suppose we decrease the parameter again. The system will now track the upper branch until \\mu 0.4 or so, when again it will jump back down. For each parameter value \\mu in the interval 0.4, 0.4 there is bistability , i.e. coexistence of two fixed points with the same value of \\mu together with a third, unstable fixed point that is not observable . The fact that the system tracks different stable branches depending on where we started, i.e. on the history of the dynamics, is known as hysteresis . \"\"\" md\"\"\" Hysteretic behaviour like this is found in many scientific and engineering contexts, including switches in biology, for example genetic switches, and in the historical dynamics of the earth's climate. \"\"\" md\"\"\" Slow fast systems \"\"\" md\"\"\" What are we actually doing when we let the parameter \\mu vary? Effectively we now have a system of two coupled ODEs, for example \\dot x \\mu x x^3 \\dot \\mu \\epsilon, where \\mu varies at a slow speed \\epsilon . On a time scale much shorter than 1 \\epsilon , the dynamics of x \"does not know\" that \\mu is changing, so it will converge to a fixed point x^ \\mu for the current value of \\mu . An associated term is adiabatic approximation . However, in fact \\mu does gradually change, so the value of x will effectively \"slide along\" the curve x t \\simeq x^ \\mu t , tracking the curve of fixed points as \\mu changes. Once \\mu reaches a critical value \\mu c , however, there is no longer a nearby fixed point to move to, and the vector field will move the system to rapidly transition to the far away alternative fixed point. If we now reverse the dynamics of \\mu , we slide back along the upper branch. \"\"\" md\"\"\" 2D Oscillations in chemical reactions the Brusselator model \"\"\" md\"\"\" Bifurcations are not restricted to 1D systems indeed, the dynamics can be much richer in higher dimensions. For example, let's look at the Brusselator model https en.wikipedia.org wiki Brusselator . This models a chemical reaction that oscillates , known as a chemical clock https en.wikipedia.org wiki Chemical clock \"\"\" md\"\"\" \\begin aligned \\dot x & a x^2 y bx x \\\\ \\dot y & b x x^2 y \\end aligned \"\"\" function brusselator xx, p, t x, y xx a, b p return a x^2 y b x x, b x x^2 y end md\"\"\" a bind a Slider 0.0 0.1 5.0, show value true, default 1.0 b bind b Slider 0.0 0.1 5.0, show value true, default 1.5 \"\"\" begin u0 1, 1 tspan 0.0, 50.0 params a, b end begin prob ODEProblem brusselator, u0, tspan, params soln solve prob end gr dpi 300 let tspan 0.0, 10.0 params a, b p1 plot leg false, background color inside black for x in 0 1.0 5 for y in 0 1.0 5 u0 x, y prob ODEProblem brusselator, u0, tspan, params soln solve prob plot p1, soln, vars 1, 2 , xlims 0, 5 , ylims 0, 5 , ratio 1, lw 1.5 p2 plot soln plot p1, p2, ylims 0, 5 end end plot direction field xs Float64 ys Float64 for x in 0 0.1 5 for y in 0 0.1 5 v brusselator x, y , params, 0 v . norm v 30 plot x, x v 1 , y, y v 2 , alpha 0.5, c gray push xs, x v 1 , x v 1 , NaN push ys, y v 2 , y v 2 , NaN end end plot xs, ys, alpha 0.7, c gray as svg p1 md\"\"\" Trajectories in state space for the Brusselator model p1 \"\"\" end md\"\"\" We see that there is a critical value of b above which the fixed point becomes unstable and gives rise to an attracting periodic orbit. This is a Hopf bifurcation https en.wikipedia.org wiki Hopf bifurcation . \"\"\" md\"\"\" 3D Chaos in the Lorenz equations \"\"\" md\"\"\" The Lorenz equations https en.wikipedia.org wiki Lorenz system form a very simplified model of convection in a layer of fluid representing the atmosphere, first investigated in a famous paper by Edward Lorenz, published in 1963, about work done at MIT. They represent a pioneering numerical investigation of chaotic behaviour . The Lorenz equations are a set of three coupled ODEs. They have an apparently simple form, with only two nonlinear terms \"\"\" md\"\"\" \\begin align \\dot x & \\sigma y x \\\\ 6pt \\dot y & x \\rho z y \\\\ 6pt \\dot z & x y \\beta z \\end align \"\"\" md\"\"\" Nonetheless, we will see that they can exhibit remarkably complex behaviour. Indeed, most nonlinear ODEs with at least three variables can be expected to exhibit similarly complicated behaviour. \"\"\" md\"\"\" Let's solve the system using `DifferentialEquations.jl`. We will take the classical parameter values \\sigma 10 and \\beta 8 3 , but we will allow \\rho to vary its classical value is 28 . \"\"\" function lorenz u, p, t x, y, z u σ, ρ, β p dx σ y x dy x ρ z y dz x y β z return dx, dy, dz end md\"\"\" ρ bind ρ Slider 0.0 0.1 100.0, show value true, default 10.0 \"\"\" lorenz params σ 10.0, ρ ρ, β 8 3 begin lorenz prob ODEProblem lorenz, 0.01, 0.01, 0.01 , 0.0, 100.0 , lorenz params lorenz soln solve lorenz prob end plot lorenz soln, vars 1, 2, 3 , xlabel \"x\", ylabel \"y\", zlabel \"z\", xlims 25, 25 , ylims 25, 25 , zlims 0, 60 md\"\"\" As \\rho increases we see a sequence of bifurcations. Above a critical value, trajectories converge to a fractal strange attractor , on which the dynamics is chaotic . \"\"\" md\"\"\" Deterministic chaos occurs when nearby initial conditions separate exponentially fast in state space. This has been given the name butterfly effect the perturbation to the atmosphere's state caused by a butterfly flapping its wings could end up being magnified to modify the direction in which a tornado moves. We can see this in a simple way by perturbing the initial condition slightly and calculating the distance between the two systems as a function of t \"\"\" begin ϵ 1e 10 lorenz prob2 ODEProblem lorenz, 0.01 ϵ, 0.01 ϵ, 0.01 ϵ , 0.0, 50.0 , lorenz params lorenz soln2 solve lorenz prob2 end begin ts 0 0.01 50 distances norm lorenz soln2 t lorenz soln t for t in ts end plot ts, distances, yscale log10, label \"distance\", xlabel \"t\", leg topleft md\"\"\" We indeed see a significant window over which the distance grows exponentially straight line on the semi logarithmic graph for large enough values of \\rho , before it saturates at a value of the order of the diameter of the attractor. The rate of the exponential growth is known as the Lyapunov exponent there are better ways to measure it more accurately. \"\"\" md\"\"\" Since the Lorenz equations model the atmosphere, we could expect that the dynamics of the atmosphere is at least as complicated. \"\"\" md\"\"\" We can also plot the x coordinate of each trajectory for comparison \"\"\" begin plot lorenz soln, vars 1, label \"original\", size 500, 300 , leg topleft plot lorenz soln2, vars 1, label \"perturbed\" ylabel \"x t \" end md\"\"\" At long enough times the trajectories separate and behave very differently. \"\"\" md\"\"\" Now let's look at the \"climate\" in this model, namely the average value of each coordinate over a long time \"\"\" mean v sum v length v begin T 1000.0 lorenz prob3 ODEProblem lorenz, 1 ϵ, 1 ϵ, 1 ϵ , 0.0, T , lorenz params lorenz soln3 solve lorenz prob3 lorenz prob4 ODEProblem lorenz, 1, 1, 1 , 0.0, T , lorenz params lorenz soln4 solve lorenz prob4 end mean abs. lorenz soln3 t for t in T 2 T mean abs. lorenz soln4 t for t in T 2 T md\"\"\" We see that the average mean value of each component is approximately the same, even though the individual trajectories are wildly different. This is an example of how statistical properties can be the same, even if individual behaviour is very different, and motivates the idea that climate i.e. \"average weather\" can be stable, even if day to day variations differ a lot. \"\"\" md\"\"\" Function library \"\"\" euler step f, x, h x h f x function euler f, x0, h, t final ts 0.0 xs x0 x x0 t 0.0 while t t final x euler step f, x, h t h push xs, x push ts, t end return ts ts, xs xs a named tuple end results euler logistic, 0.5, 0.01, 20.0 begin plot results.ts, results.xs, size 400, 300 , leg false, xlabel L\"t\", ylabel L\"x t \", lw 3 scatter results.ts 1 , results.xs 1 ylims 0.4, 1.1 end let p plot xlabel L\"t\", ylabel L\"x t \", leg false, ylim 1, 2 results euler logistic, x0, 0.01, 5.0 plot results.ts, results.xs, alpha 1, lw 3 scatter 0.0 , x0 hline 0.0 , ls dash as svg p end let p plot xlabel L\"t\", ylabel L\"x t \", leg false, ylim 1, 2 for x0 in 0.5 0.05 2.0 for x0 in 0.0 0.1 2.0 for x0 in 0.5 0.1 2.0 results euler logistic, x0, 0.05, 5.0 for x0 in 0.5 0.05 2.0 results euler logistic, x0, 0.01, 10.0 plot p, results.ts, results.xs, alpha 0.8, lw 1, arrow true end md\"\"\" Trajectories for ẋ x 1 x p \"\"\" end derivative f, x, h 0.001 f x h f x h 2h \"Draw 1D vector field using centred arrows\" function horiz vector field f xlo, xhi 2, 2.5 arrow size 0.07 tol 1e 5 tolerance to check for fixed point p plot size 400, 100 , xlim xlo, xhi , ylim 0.5, 0.5 , leg false, yticks for x in xlo 0.2 xhi d f x derivative if d tol plot x arrow size, x arrow size , 0, 0 , arrow true, c blue, alpha 0.5, lw 1.5 elseif d tol plot x arrow size, x arrow size , 0, 0 , arrow true, c red, alpha 0.5, lw 1.5 end end roots find zeros f, 10, 10 for root in roots stability sign derivative f, root if stability 0 scatter root , 0 , c green, alpha 0.5, m square, ms 5 unstable else scatter root , 0 , c black, alpha 0.5, ms 6 stable end end p end \"Draw vertical vector field of 1D ODE on plot p with parameter μ\" function vector field p, μ, f xlo, xhi 2, 2 arrow size 0.07 tol 1e 5 tolerance to check for fixed point for x in xlo 0.2 xhi d f x derivative if d tol plot μ, μ , x arrow size, x arrow size , arrow 3.0, 2.0 , arrowstyle triangle, c blue, alpha 0.4, lw 1.5 elseif d tol plot μ, μ , x arrow size, x arrow size , arrow true, c red, alpha 0.4, lw 1.5 end end roots find zeros f, 10, 10 for root in roots stability sign derivative f, root if stability 0 scatter μ , root , c green, alpha 0.5, m square, ms 2 unstable else scatter μ , root , c black, alpha 0.5, ms 3 stable end end p end function vector field f p plot leg false, xticks , xrange 1, 1 , size 100, 200 vector field p, 0, f end vector field logistic begin vector field x g λ, x end function bifurcation diagram h p plot leg false, ratio 1 for μ in 2 0.15 2 vector field p, μ, x h μ, x end xlabel \"μ\" ylabel \"fixed points and dynamics with given μ\" return p end bifurcation diagram g bifurcation diagram h function fixed points f p plot leg false, ratio 1 for μ in 2 0.01 2 roots find zeros x f μ, x , 10, 10 for root in roots stability sign derivative x f μ, x , root if stability 0 scatter μ , root , c green, alpha 0.5, m square unstable else scatter μ , root , c black, alpha 0.5 stable end end end xlabel L\"\\mu\" ylabel L\"x^ \\mu \" return p end fixed points h "},{"url":"climate_science/resistors_and_stencils/","title":"Resistors, stencils and climate models","tags":["lecture","module3","PDE","differential equation","ghost cell","boundary condition","climate","modeling","track_climate","track_math","stencil"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v DdTWgBlDgr0\" image \"https user images.githubusercontent.com 6933510 136200635 33c007ff 89f6 48dc b1d3 eb56fd16003e.gif\" section 8 order 8 title \"Resistors, stencils and climate models\" layout \"layout.jlhtml\" youtube id \"DdTWgBlDgr0\" description \"\" tags \"lecture\", \"module3\", \"PDE\", \"differential equation\", \"ghost cell\", \"boundary condition\", \"climate\", \"modeling\", \"track climate\", \"track math\", \"stencil\" using Markdown using InteractiveUtils using PlutoUI, OffsetArrays TableOfContents md\"\"\" Julia `CartesianIndex` `OffsetArray` from `OffsetArrays` \"\"\" md\"\"\" Resistors, Equilibrium, and Poisson's equation. 17 48 minute video \"\"\" md\"\"\" This video is really about the interaction of the discrete and the continuous. We set up a grid of resistor problem, and we show that solving this problem is the discretized version of solving a partial differential equation known as Poisson's equation. There are some references to fall 2020 which date this lecture, including a reference to the Biden Trump election, and a reference to John Urschel's lecture which some of you might want to check out John Urschel's video https youtu.be rRCGNvMdLEY \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid UKG xk2F3Ak params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" Stencils first 2.5 minutes or so Don't miss the fun video effects \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid sbqPVPLHl5Q params \"modestbranding 1&rel 0&end 180\" lite youtube \"\"\" md\"\"\" In the remainder of this notebook, we will show how to set up ghost cells in Julia so as to be able to apply a stencil. \"\"\" md\"\"\" Cartesian Indices \"\"\" md\"\"\" Grab a 6x7 matrix that we'd like to apply a stencil to. \"\"\" data rand 1 9,6,7 md\"\"\" A \"Cartesian index\" lets you access an element with one index variable \"\"\" i CartesianIndex 2,3 data i md\"\"\" Obtain all the Cartesian Indices of your data matrix. \"\"\" I CartesianIndices data Dump I md\"\"\" Offset Arrays \"\"\" md\"\"\" An offset array lets you index in ways other than the standard 1 m, 1 n \"\"\" A OffsetArray zeros Int,8,9 , 0 7 ,0 8 for i ∈ I A i data i copy data end A A 1,1 A 0,0 A I . data I md\"\"\" Neighborhood a 3x3 window built from Cartesian Indices \"\"\" neighborhood CartesianIndices 1 1, 1 1 md\"\"\" Grab all the neighborhoods of `A`. \"\"\" A i. neighborhood for i ∈ I md\"\"\" Stencil \"\"\" stencil 0 1 0 1 4 1 0 1 0 sum A i. neighborhood . stencil for i ∈ I md\"\"\" Notice the result is the same size as the original data, and the stencil \"worked\" on the edges. \"\"\" md\"\"\" Other boundary conditions. We just saw 0 boundary conditions, what about periodic or zero derivative? \"\"\" begin B copy A B 0, B 6, periodic B 7, B 1, B ,0 B ,7 B ,8 B ,1 B 0, B 1, zero derivative B 7, B 7 B ,0 B ,1 B ,8 B ,7 B end for i∈I B i sum A i. neighborhood . stencil end B md\"\"\" Climate Models in the Real World \"\"\" md\"\"\" play from t 28 122,330 1200 \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid mOJ0jABAhq4 params \"modestbranding 1&rel 0\" lite youtube \"\"\" "},{"url":"climate_science/time_stepping/","title":"Time stepping","tags":["lecture","module3","epidemiology","track_math","track_climate","plotting","continuous","discrete","ODE","differential equation","modeling"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v 3Y5gVyO8KcI\" image \"https user images.githubusercontent.com 6933510 136199718 ff811eb3 aad6 4d6b 99e0 f6bf922816b4.png\" section 1 order 1 title \"Time stepping\" layout \"layout.jlhtml\" youtube id \"3Y5gVyO8KcI\" description \"\" tags \"lecture\", \"module3\", \"epidemiology\", \"track math\", \"track climate\", \"plotting\", \"continuous\", \"discrete\", \"ODE\", \"differential equation\", \"modeling\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, StatsBase, Statistics PlutoUI.TableOfContents md\"\"\" Modeling component failure Discrete and continuous \"\"\" md\"\"\" Let's think about a simple model for failure of components such as light bulbs. We actually derived this global, or macroscopic, model from a microscopic stochastic model a few lectures ago. Components can fail at any moment but we'll start off by checking once per day to count the number that have failed during that day. Then we'll check several times per day. This is still a discrete model. Finally we'll see how we can turn this into a continuous model where we can talk about the number that have failed by any real time t . \"\"\" md\"\"\" Checking failures once per day integer time steps \"\"\" md\"\"\" Let's call N k the average number of bulbs that are still functioning on day number k , starting from an initial number N 0 . We can find an equation for the number N k 1 that are still functioning at the end of day number k 1 by working out how many fail on day k 1 . Let's call p the probability that each bulb fails each day. For example, if 10% of the bulbs fail each day then p 0.1 . If there are 100 bulbs and 10% fail on the first day, then 10 fail, so there will be 90 remaining. In general, if a proportion p of the N k fail, in total we expect p \\, N k to fail. Hence N k 1 N k p \\, N k or N k 1 N k p N k. In this very simple model we can actually solve this recurrence relation analytically to find the number still functioning at time t N k 1 1 p N k so N k 1 p N k 1 1 p ^2 N k 2 \\cdots hence N k N 0 \\, 1 p ^k. \"\"\" md\"\"\" Checking failures n times per day \"\"\" md\"\"\" Now suppose instead we ask how many light bulbs fail in half a day. If 10% fail per day, it's natural to think that 5% fail in half a day. However, this is not quite right due to the effect of compounding as in compound interest . If 5% fail and then 5% of the remainder fail, then how many are left? \"\"\" 1 0.05 1 0.05 md\"\"\" So slightly fewer than 10% in total have failed, due to the effect of compounding. Nonetheless, the result is approximately right, so let's take that. \"\"\" md\"\"\" So let's suppose that n times a day, a proportion p n fail for example, 10% and twice a day gives 5% failing each time, approximately . Then the number remaining after the first failure check on day k is \"\"\" md\"\"\" N k \\frac 1 n \\textstyle 1 \\frac p n N k Here we have used a subscript since we are in a discrete situation. We could also have written instead N k \\textstyle \\frac 1 n . The solution at the next day is N k 1 N k \\, 1 \\textstyle \\frac p n ^n. And the full solution for the number remaining after k days is N k N 0 \\, \\textstyle 1 \\frac p n ^ nk \"\"\" md\"\"\" Let's plot these to see what they look like \"\"\" md\"\"\" n n slider bind n Slider 0 8, show value true \"\"\" p 0.4 let N0 100 T 20 N N0 1 p ^t for t in 0 T plot 0 T, N, m o, alpha 0.5, ms 3, label \"once daily\", lw 2 N2 N0 1 p5 2 ^ t for t in 0 2T plot 0 0.5 T, N2, m o, alpha 0.5, ms 2, label \"twice a day\" N4 N0 1 p5 4 ^ t for t in 0 4T plot 0 0.25 T, N4, m o, alpha 0.5, ms 2, label \"four times a day\" N N0 1 p 2^n ^ t for t in 0 2^n T plot 0 2.0^ n T, N, m o, alpha 0.5, ms 2, label \" 2^n times per day\" xlabel \"days k \" ylabel \"N k\" title \" 2^n times per day\" plot t N0 exp p5 t end md\"\"\" Continuous time \"\"\" md\"\"\" Thinking back to the class on \"discrete to continuous\", we see that we are producing more and more discrete values to keep track of, but after a while the curve they trace out does not really change. It thus makes sense to define a limiting object , which is what would happen if you could take smaller and smaller time steps in the right way, i.e. take the limit as n \\to \\infty . In that case we could imagine being able to calculate the average number N t of bulbs that are functioning at time t , where t can be any positive real number. \"\"\" md\"\"\" In calculus we learn ways to see that \"\"\" md\"\"\" 1 p n ^n converges to \\exp p as n \\to \\infty . \"\"\" md\"\"\" An alternative approach is to look at the time evolution in terms of differences . In a time 1 n a proportion p n decays. Here we are thinking of \\delta t 1 n as the time step between consecutive checks. If there are N t bulbs functioning at a time t and we take a small time step of general length \\delta t , a proportion p \\, \\delta t should fail, so that \"\"\" md\"\"\" N t \\delta t N t p \\, \\delta t \\, N t . \"\"\" md\"\"\" Dividing through by \\delta t we find \"\"\" md\"\"\" \\frac N t \\delta t N t \\delta t p \\, N t . \"\"\" md\"\"\" We now recognise the left hand side of the equation if we take the limit as \\delta t \\to 0 , we have exactly the definition of the derivative \\frac dN t dt . Hence, taking that limit we obtain \"\"\" md\"\"\" \\frac dN t dt p \\, N t with N 0 N 0 , the initial number. This is an ordinary differential equation it is an equation relating the value of the function N t at time t to the derivative slope of that function at that point. This relationship must hold for all t . It is not obvious that this equation even makes sense although it should do, given the way we have derived it , but in differential equations courses we see that it does make sense under some technical conditions , and uniquely specifies a function satisfying the ODE together with the initial condition. This is called an \"initial value problem\". \"\"\" md\"\"\" In this particular case, once again we are lucky enough to be able to solve this equation analytically N t is a function whose derivative is a multiple of the same function, and hence it must be exponential \"\"\" md\"\"\" N t N 0 \\exp p \\, t \"\"\" md\"\"\" This is an alternative way to define the exponential function. We can add this to the above plot \"\"\" n slider let N0 100 T 20 N N0 1 p ^t for t in 0 T plot 0 T, N, m o, alpha 0.5, ms 3, label \"once daily\", lw 2 N2 N0 1 p5 2 ^ t for t in 0 2T plot 0 0.5 T, N2, m o, alpha 0.5, ms 2, label \"twice a day\" N4 N0 1 p5 4 ^ t for t in 0 4T plot 0 0.25 T, N4, m o, alpha 0.5, ms 2, label \"four times a day\" N N0 1 p 2^n ^ t for t in 0 2^n T plot 0 2.0^ n T, N, m o, alpha 0.5, ms 2, label \" 2^n times per day\" xlabel \"days k \" ylabel \"N k\" title \" 2^n times per day\" plot t N0 exp p t , label \"continuous\", lw 2 end md\"\"\" We see graphically that this is indeed the correct limiting curve. \"\"\" md\"\"\" In this context, p is called a rate it is a probability per unit time , i.e. a ratio of probability and time. To get the probability of decaying in a time \\delta t , we multiplied p by \\delta t . \"\"\" md\"\"\" Let's summarise what we have found \"\"\" md\"\"\" | Step type | Time stepping | Difference | Solution | | | | | | | Integer | N k 1 1 p N k | N k 1 N k p N k | N k N 0 1 p ^k | Rational | N k \\frac 1 n \\textstyle 1 \\frac p n N k | N k \\frac 1 n N k \\textstyle \\frac p n N k | N k N 0 1 \\frac p n ^ n k | Continuous | N t \\delta t 1 p \\, \\delta t N t | \\frac dN t dt p \\, N t | N t N 0 \\exp p \\, t \"\"\" md\"\"\" SIR model \"\"\" md\"\"\" Let's look at a more complicated example, the SIR model of the spread of an epidemic, or of a rumour, in a population. You are surely familiar with models of this type modelling the spread of COVID 19, and from the homework. As in the homework, we can make a fully discrete, stochastic agent based model, where we give microscopic rules specifying how individual agents interact with one another. When we run such models with large enough systems, we observe that the results are often quite smooth. An alternative approach is to try to write down macroscopic discrete equations that describe the dynamics of averages. Often it is easier to understand the behaviour of such systems by formulating a continuous version of the model. Some people make discrete models because they're not happy with continuous models large discrete models can also be computationally wasteful. On the other hand, they can also include effects that might be more difficult to model in a continuous framework, e.g. non local effects or things that \"don't become continuous very well\". \"\"\" md\"\"\" Discrete time SIR model \"\"\" md\"\"\" First let's think about the SI model agents can be susceptible S and infectious I . A susceptible person becomes infectious when they come into contact with an infectious person, with some probability. \"\"\" md\"\"\" Let's call S t and I t be the number of susceptible and infectious people at time t , respectively, and let's call N the total number of people. Let's suppose that at each time step, each infectious person has the chance to interact with one other person on average . That person will be chosen at random from the total population of size N . A new infection occurs only if that chosen person is susceptible, which happens with probability S t N , and only if the infection attempt is successful, with probability b , say. Hence the change in the number of infectious people after that step is \"\"\" md\"\"\" \\Delta I t I t 1 I t b \\, I t \\, \\left \\frac S t N \\right \"\"\" md\"\"\" The decrease in S t is also given by \\Delta I t . \"\"\" md\"\"\" There is also recovery, with a constant probability c at each step, once you are infectious. It is useful to normalize by N , so we define the proportions of the population that are susceptible, infectious and recovered as s t \\frac S t N \\quad i t \\frac I t N \\quad r t \\frac R t N . \"\"\" md\"\"\" Including recovery with probability c we obtain the discrete time SIR model \"\"\" md\"\"\" \\begin align s t 1 & s t b \\, s t \\, i t \\\\ i t 1 & i t b \\, s t \\, i t c \\, i t\\\\ r t 1 & r t c \\, i t \\end align \"\"\" md\"\"\" Continuous time SIR model \"\"\" md\"\"\" We can now go through the same process as with the failure model, where we take time steps of length \\delta t instead, and replace probabilities b and c with rates \\beta and \\gamma . Taking the limit \\delta t \\to 0 we get \"\"\" md\"\"\" \\begin align \\frac ds t dt & \\beta \\, s t \\, i t \\\\ \\frac di t dt & \\beta \\, s t \\, i t & \\gamma \\, i t \\\\ \\frac dr t dt & & \\gamma \\, i t \\end align \"\"\" md\"\"\" We can think of this as a model of a chemical reaction with species S, I and R. The term s t i t is known as the mass action https en.wikipedia.org wiki Law of mass action form of interaction. Note that no analytical solutions of these simple nonlinear ODEs are known as a function of time However, parametric solutions are known https arxiv.org abs 1403.2160 . \"\"\" md\"\"\" Below is an example simulation of the discrete time model. \"\"\" begin NN 100 SS NN 1 II 1 RR 0 end ss, ii, rr SS NN, II NN, RR NN p infection, p recovery 0.1, 0.01 TT 1000 function discrete SIR s0, i0, r0, T 1000 s, i, r s0, i0, r0 results s s, i i, r r for t in 1 T Δi p infection s i Δr p recovery i s new s Δi i new i Δi Δr r new r Δr push results, s s new, i i new, r r new s, i, r s new, i new, r new end return results end SIR discrete SIR ss, ii, rr begin ts 1 length SIR discrete time SIR plot plot ts, x.s for x in SIR , m o, label \"S\", alpha 0.2, linecolor blue, leg right, size 400, 300 plot ts, x.i for x in SIR , m o, label \"I\", alpha 0.2 plot ts, x.r for x in SIR , m o, label \"R\", alpha 0.2 xlims 0, 500 end md\"\"\" Time stepping The Euler method \"\"\" md\"\"\" Above we showed how we can think of Ordinary Differential Equations ODEs as arising in a natural way from discrete time models where we take time steps. What about if somebody gives us an ODE how should we solve it numerically? In fact, we do the reverse process We discretize the equation and reduce it to a system where we take time steps Suppose the differential equation is \\dot x f x The simplest such method is the Euler method we approximate the derivative using a small but not too small time step h what we called \\delta t above \\dot x \\simeq \\frac x t h x t h giving x t \\delta t \\simeq x t h \\, f x . The Euler method with constant time step is then the following algorithm x k 1 x k h \\, f x k If we have several variables in our ODE, we can wrap the variables up into a vector and use the same method for the ODE \\dot \\mathbf x \\mathbf f \\mathbf x the Euler method becomes \\mathbf x k 1 \\mathbf x k h \\, \\mathbf f \\mathbf x k , where \\mathbf f denotes is a vector valued function mapping the vector of variables to the vector of right hand sides of the ODEs. \"\"\" md\"\"\" However, in general the Euler method is not a good algorithm to simulate the dynamics of an ODE We can see why that might be from the graphs at the start of this notebook taking time steps like this actually does a bad job at approximating the continuous curve. Numerical analysis courses show how to design better numerical methods to approximate the true solution of an ODE more accurately. The Julia SciML DifferentialEquations.jl https diffeq.sciml.ai stable tutorials ode example ecosystem provides a large suite of methods for solving ODEs and many other types of differential equations using state of the art methods. \"\"\" "},{"url":"data_science/discrete_and_continuous/","title":"Discrete and Continuous","tags":["lecture","module2","track_math","discrete","continuous"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v H6Dcx3YeTkE\" image \"https user images.githubusercontent.com 6933510 136196552 ce16c06f bd12 427f 80e5 aedb1fbc734a.png\" section 7 order 7 title \"Discrete and Continuous\" layout \"layout.jlhtml\" youtube id \"H6Dcx3YeTkE\" description \"\" tags \"lecture\", \"module2\", \"track math\", \"discrete\", \"continuous\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, HypertextLiteral, Graphs, GraphPlot, Printf, SpecialFunctions TableOfContents md\"\"\" Julia concepts printing using css fancy stuff not really julia pedagogical concepts curiosity based learning bridging what is often two different communities \"\"\" md\"\"\" Discrete and Continuous \"\"\" md\"\"\" An exact technical definition of discrete and continuous can be difficult, Nonetheless the idea of discrete mathematics is associated with finite or countably many values that are isolated. The set 1,...,n and the integers are both discrete sets. \"\"\" md\"\"\" DISCRETE MATH OBJECTS examples 1. Finite Sets 1,2,...,100 2. Infinite Discrete Sets ``\\mathbb Z `` integers ``\\ \\ldots, 2, 1,0,1,2,\\ldots\\ `` 3. Graphs \"\"\" gplot barabasi albert 150, 2 md\"\"\" By contrast, entire intervals or the whole real line is associated with continuous mathematics. In fact, mathematicians have worked painstakingly to define these terms, inventing fields such as point set topology that among other things can recognize the discrete, and analysis to rigorously work with the continuous. \"\"\" md\"\"\" Continuous real line continuous data image png base64,iVBORw0KGgoAAAANSUhEUgAAAWUAAACNCAMAAABYO5vSAAACT1BMVEX 8AAAAAAP e3t7 AACZAMzj4 91dXXg4P AHtwcHBnZ2f9 f8AlAAAlgDz8 v7 8AmwDx8fG5ubn5 f V1f 2tv R0dG6uv9cXP Pz 9lZf AwP rq 9fVsbGzx8f8gIP c3P Tk o6P9XV 96enqVlZUAoAD 4OD4 jz6P X1 8tLf87O AIednf9ycv Bgf8jI 9DQ0MAvgD 4v96ev IyP IiP9HR 99ff9aWv 0dH xMT TEz qKj srKZmf9AQP mpv q9 pbW1v ZWX AI Li7 dnZPT 7e01NTWhoaFISEj jIwWFhb seH goL urr PDz GBj lZVMwEz AHD wP h H n91pzGn9y 6xsbH XFwArAD 2u693r3N 83 Q0NzAKf w DV5tWP2I8wwDD j7YzoTP da67Xty7Q9yoQcuo4Kj OpL s8hgAMl1xnWezJ7Et8PET uMSsX S6v Hp3 h9J41njBS8jChPn On fKf AGH XKO6hL8AGgAAegDMAAApAACZANlaAJEAAF0AAKKNADtxAD4AQQAAzwDaAABMAACZAPoAAM2pAQFRAHxjc2Mskiy2SkqjZmZmZndSUsdaWpXZNIb qP nbljrmP ZMBRsFHKgdfenP S1ZKzmNfJcdu9 71j3mOI9oi7D S HMnNd Ld8asd LOYsiPJ8mAxID Qra1T v runPZO PAN7evtzFi WNAL3bt ZstqmftLyi e7OsWcAJai 6LYhdDDUMX0Xau1AAAZuElEQVR4nO1di18TWZauVFJIpJIQqPAKRMLLEEhiEgMBCSAQeQXloQF52ARQ3gwg2LRA6yzuztqzs7Pu7G7PvqalUQdcsW1s7LbRGbf1D9t7KxUIUMmtV3q7Z LVLyQkt6pufXXqu ece sWhrGDqsuJ8Esc4XDkilmbwvEzElXkLxkOHE8QvjbpwK1cymkShe jJ4 EXGQRPe7QCN4 mY3j5aVRi2jOGCtxgHL01hIK0GVKKXQZN7oIQBaqQG6GHq9AWGCd1QoPLjMverG8ejyD5FQrNlAVuMWJR6U5C1TCWd K12cjtpVViOujnzAM2kQOimYd2GEFajtYMm5E7ExzBvKXH7WMGxpQa6sRRxycHs syuBgH ygcEsWbmyIVkSTVlFagDXgjuhbSshpxY1omu1olo24xYIjDVXTaoluf4l23OJwoM5XmqMuEUtz6qMLYl69Md Cc7hSWQEEoxXDjUgvA5iFHrGPNCde3lCoR1x5gOXy5OgldMBIG3AcVaXcVkS1dVZgx1m4Ht1o5RhRhSrw7BxhLEPxoyqdiZrILGtKNbRtJgCzKEdcU3W4PUHXaokiqTrYfOQjbTkTr8Mw3InSDKoCwXKCI78A2DNSe BFaEeZMl6QUM XZXdFPri8z2BkBl6Zp8Hxc2yFCiocha10mwdIhojo8CW4wf7TzuXBK4 dlvPO5OuduL0AsoyyZVpKnQg9BSw70Jdg0J6jw11IH1xUGwKNKJXG15bdrfSGG6CvjFeBl8XOUqoUljGCXxL0QOAaytnrW5CTby 0MMebj9exU5hhob0UUM0MJ8qWabFw4oWIgwAWgmYZCCLCX0nIt1Tq86vw CjnPqcSL3Q6cb4051vsDQ2lOowy0nTjRraWLc9qP9eQAwWrIKcAK7VaWA8qh95ABb3 HGN9hEYruzWz7ow7C0hGmgVhy6AxdkOW6xDHwIXlLIslDZSMWqZAp9Mk26PacmIlQ1MaPy9DF3wjdboGXZ3TmcN6nZNhYpVlYRe4grSKioZsHU2cu8oI6prHesITQ VLQ9lyKW3LuFEXtRSoXZoT1dLmgmuLStNXolxQoIhRhblUl5OV02AxorYTDRQHHwOQbEdeLmnOfE223pgRvRDKloFiuLFWXI am8bB3pyEwY1b9MAM2S CA4AmshJ1TkGpQsGeHISmEMmyG dAsrvS6igH hud5QzLOQTLdryqELeg9gadUIT7kJyP4 WZ gaE6w1cNHt2LnJ wM8QkcjAqHwORoEmOeEcCKLs Q2IqiRmosQtDzQWaJIBida06AXceGEWhba NKudC32JVThPWT4MjRFRXUzD4VrJyqzUJeeiy Uhq1qaxylJSKFkWaPjdIkncMwC5VsRF GPgoLc 8axBFHHHHEEUccccQRRxw Q6Rw6ZhNSflRC0laKQ6bIrkV4lT1w3sPvV9KRxeWJ53msMXTpzlU4 IlOboQdjaVw 5OnuJQ6GIRh 2lFnEgIf3CRxGOL JJSili1kiXFXHYgewEuhB2 gKHup6UcTjqdNkFdKEU2VkOhU5f4LC 0zIOZzW1oygCy lFH7HSLC quMhUQ3bxyG9mr9drPrIZLqcipSjKARl8Pp8BfjjBjeWDayeFKinJZcsvRWKZTC4oSA6tkHL6LIdTf5FhmQzVkg3ytkgsY20yFjNPkcs6ZMzOjxE4ttijGrh1mGZOLGNRzMZXO hvrC3G LNMfty7t RJb8nxQhFYJnV2qzGzgUmzcbNlhmXDtenbyytDBvbrP7UjkiLK22QdZ4 8RqYUyQA CtX1sC2PTbi0avXU6iGaxdqyp0YB0ejhzTLV ykBsXTcmtlZJt1W2JVpqQiuwM2Wg4pB9i83KW1N8 0spxRWqo1dFzBoyzLZWXn4rykfnYUkR7DlMZXK5FKrTa7FQzvgZsuRGlJDiyKIRh9flnuXiCDuHCvEzrLOEuwHZfrb djy6F2l0qZsUi6PsrIZWZflbTSjYaoRNOQDWyYP2bJ5UW3S0svUSNjXHG25LQLL3X6GZYUHK LH8h2a4j3wOnbkrCwHR8ZBFNIpfvIiZ1smf k3V21K2 Ly1bUhtkJRdZnGWcbXSJGfZUiWpcpppMuS5AcY6zG5gsvUD2Ffp8pOydEoupTK kP7Psvd8iJZhELhSJUVMZ ChnwDvO4dLZQuazu Zp4 xLI1jf7i4oV09P5Oyy7K5TdvA0uml6ZR1kq1nYy0KYZl2VlaU1JOyKLjbwNqtdYEXlr13yGK8sCvQiQrfvX3PFe9H6QZvH3Gqfyv8X38A89d aZpmaG56R95rnqAj Sw6UHgt 8ENRm8JkwxYfnBP NclWH5dwJY heeu qNLWTLy8JZhlKX1HHwbwdbmd 6gqqsDbhiwvK 8rVl4v4e0 z9G6fyvzYKt WrNuhjwEUwyx10OHr6gOcLSSfAUpQku1REf6KXzzdCuvz7z5NOnAotSR1tJ0 eOrIcw6WOC T70YKn j2kyw 42SbLOnUiaRoS1FS0SlZWxF8T r8k999xrB86vMj5U6CYzr0Dazqf 5XiGTLf8N9J13quHQyUoX30Sa7cPLzk38I2fIfTp46cXiBm7 QcSlChU FSL0UarRPX2KsmD32G1EFddl0yJMDzSu6ocYuRnKaPIMMy83F3HwMbN H ILhmNg9ViZFdollzYwQy3Y6LiEjupfhCPrLD0OS0claSN72UaT1g61fx4mDQ5MnBb9jjuOIk5aybgrq8lz4tyL9Zay2OUhyt4Gvv1z2nFGMN8cKsfvLBeVBkluDQ9r4 MtDa1AwmmxfsjpyWGpETy7oY7QdzqgFZaMoVNfDsZ95YEqtmto4RLJYfxnzdQ82K5oHaw38I yyRzMEsTPzmGV 7BF2sqPeiFv0zLjBFB62jI1 uWxr mblGvuonPTo nLS0YST HRbJFsG19jwk1vDY0d2IDaPgXlqu2o98APvbFFe2YvHfyxjKRQpW6TJyXc0hMYW8svJ5d7sX7kWKYyOnsdgS7CmFkWyZVaIz8ntgzfLkffHLfPJw5YRlWq7GOkEnO2IUN8TjJRzSh2L1eUwcMwvS8Yyp1MvMr8sL4pYXWbnJBf3Ib2Ny1EXccnin 7gwnIbhwss5WwSh0LcWr82DizLL0TsC0LvIpVL508qB 6w9FQOPVIpFzkUkqdyoIbkVCk5h8Pjtj95arrwe1fjiOMvGST PvGYwst3BZLChN7 T5UMlfwYyuD1jo2hS FBxB5LDihJLinp47kOlZWTnSFw3PzoyvTKaOx57ttaX53rM6MLcoXBd9lTLJjo3s3nz3neNEPq9LixqkEYzZPzy7bbK2xhkJQwbz1VB6b ZwRdkis87TWNLR6BK2vePyO2P2Hv74yE5DMgyDZmCqK5eK3JplQ2rcRYNszjLm3ANDEu1fYMvu4rMFFULGz1jHcEQew89 FZJ7EKZpmF0dwZzDE3rfWz54wkwnAgYHKZAuu8Wxx2GDztdEKu8bIgzdA8pzNyz77i05i5mdt5C8 wp3mS7pKy2ZTLvxyKmTWTY4uBgDagNZlWJaEZkHyFzuFfqeVljiE0MOnl7V7u6 TWBUnG8Ure1kxO011SsAdQuXZTTKsdDSnDPVoX4HnCNDEnQQNoqO5qVjygaW4RwnJJMLu8QxB3OEszqSuEFButTgHWXLamnF eb7q9Ng keV5oY4JC33pAHZgIqKa0gYER0V6zobrR7 c3N sBz4MCquyFOfxtYnvnPrF3h6tm5NYZjU6L0Vh1ToA1j95WNs0DVR6d gbY88PYGLN5XO2amNC6ehZNAfUilzxBNECSFQ uX2l 0Nys8Hfzr LrJYJYekdszyztETuvcjmtA9w4o8Va7zQ6SsuBRev5TQMwOq cX7urvDs01P l7duvhE9tFg3DE9qJp PqwMDwgEk7cUucZhg8LcCGa1pqwJ rfn8Xby8DNn1L38PuqLc7xM6317itlGPBLXbgMDs07kwjcg6SI1UGjd dyTXb Csqt NV55CY264jom8xoO15Mqd1DXhvTQRcG3xjrsPw1V5RNNecB38V3UCeB3l7Ga8AwZtvnxE7f8Q2gZ xR04rJTtwvDAj32i0J2jSHJV25FRe4Riavmpbuzxta rPxUrIyKNsxcC81QN0YvgJsGWv Ukg0DMsanOGyzX xvO bqAW1eAjb8lI vYGMTs0tA27VstmiO3vuaxEwvnhyhPzgVbogHrkZPCa0bHsS6VtsnhSqZyOnbds3gLMbsmhLZsx7 LAS3HpDJ vtv2yobjLr7he7Bl84G k2f4VAJZfYIBlYMvYm92v33NZicq24sYzmgqnEfZiU8kUr9YPNH7zN7Gb88q1zlh5cYDliZ4nXvOqSjvgxcx9w8NinQxfMYl5GqEXVwxik Zqfmt7N3e fYP13r8PhwmUvXnNSZdJtxPPzKIajHg9ajYKFoyuXQVWDJpAKBmxQt cyBjmXQ1oB2C7Z5YiLjHUXlf4232G8zW8WZa eXstAXv7bAHaMlaGccrgUFkVFTm5ZAaO1wuY73h0cvrVkGH0S5tyWlAQxR3m1QDQZam2Zmj3K2rOG7Diluaaap4hK03rtW3i2fEhL5FBFhQAkSi14KiJYNhQMnQT BVDK01K9jHM0sH7VB14KVni09cCBMNjgCMyugWZx9ttYukt77XyKnFn1LlLIwH6Fb5OIBk3hazNHd5FtXpRKlsGfoZC0U7T6xOWY3Y I37Bn XETNyImo8uMoZu25QxivtCgCy lGpjBuDHNdeKqfCjZ8S2IJbxc4JjirIvm5Qr HLafOHt0QakY7nL7 etx4fwiCDu82cZDkrkNLEWKzT988pvYivMkGXJFMMz6BeWjduHMJbhTHJ69ER8kXDz9tXliAPmJIG3R6Uel4rl81dECgZQjIVt maVXIEbW4XPFzm0dtXWH5tUEQPAslaq ijox10XJRgCWdY04Dhyds IMDy0KSMMY5YIQDEk6 UrblT4GwX2 DHY3CN2 R8vDEtEsAyD7OUhCfvxj8E7IR3L1TUKvzA3eR bBLErQL8EhiUhwCD7ZmxZlkwxoB nuCxuG5t7C7sCnCodYFlQWBIEDLInYxlkA5an5tDFuADm42rECQawZaEsO4WHJRi1smxbE1nzqICKIRHL1dCPE80yy 1RaJTW48YKEV7C5LLt9mgMwz vlGQsw3ujxPlxglmGIfY5Ec9kGl2zLcdspAAGWVZPieskCQH4cQ8Gq8Wy N2CEJYTRAV wGNesTX9MobCDHVZGpY9jX5 i0dkCPXtd4JsmQ7 RLBsWFlW3h2NSe8qBCV3aSekYVkCPw7DZoQpBgxLEA8tio5rt5XzMRopAEDKTdqJLUk2dfmK4opYWcZ2CWJGwGowLCnk1X99BENryqYYBtkjAYlaP DH a z7IU6jhnixrdC1svCRYUlmGHappz2xKr9M29pJVIMKBhdYgWjbJPYE2LLkGWLiLAEwzrnlWuxGigHh9ZqRQ7EYHD gPxgqGZJQhBtgzCEiPqCVJRMTofQ4 ZPGfS9ogbVMTg8nWFeMEo 1SYLovskwLwrczHMC0nmS1Xt9SIFgysZPM7gtOgoqNIzrcWitFl0P5Nxs6TI8cmXBuS3FXiq66VQNd2v9vmNkDuCMgEwbdJhTAUu9iPHBtfnZPmTjSDFLHT5uOvuA32PAYypp0dIpHi9Xp QndVlmhKYtubHEccccQhFj6WJZZZ Z8tqMQEViRycT1GJ6dpTIa9T8ZuVBcl75P43nfBKHtd9rr30CtaaSpDr9dnZh59ZVY5kI fx2BfibIpfLkLXlfnpRg7Tsr7jgZ9fSNPXm5sSJMIFQtydnZm9hdhr91H0YofTBJ8BJlcUnWdy7ampuWmJltwUQbf 0WFKcN98r7h4ScvF8fD7Nbc13fr6YZWqzKtcpnPkA2 6svSpbXIZzeYWYJDr9loxQscuLOySl9IL1XMkgkWRzKHkQeezpX 28vKten FbDcnr86 fDVyqgYn9K82rM sDE1BQidWj8g9NbqhsqkMgVUpnEBp7C6 3xtd1eNyPFbh D8xaM7j8JfUUPvvFZc7wYqfHRJLMA4DLQwUD5N513lw2KNT OjVpS2mz6NqKwGmW4KuEwqdUDVY3JNhTTDO94DrHhiYG54 JaAUSeeRv VZnhrsICbKY BefJnCYhQcsOW5KimlWVEPfwWhYfzVzuD9jXZFPokHHKV2tTTM7c19lIdeLrPco9JPfVky2um E9og9FjtxSKKzXNUrD8 hM tzyEkIWLZbl 2bYSTBJ03rWJ7tA2j78cA3Sa0ydU6iehq8nbo1ZvyIUPYqqura2uvjwoeqQAAHFjV0A qRs3cnpGbmRMLl9l qQOrFoEvF4oVSPAeg9uCvYuqtTrIrZsMBhgd0mz LRcGbFHHEkYlb5 hJzKwY3Xhz YuyAtA XI4aPon1cyFgx1WZr2xTwQmLh18K 3x2VaFbvN881XBLFc9vrNASUzxMJSuPP19nt49 smahtZzKDP5LSMjIZye2altTL649CPYVIyXd53kM0DarV2Y3XfkwO6DAclAgdPRFDSreA5rvbN6zfvH8 MzM7uzO5rMXxayf4hln0 M0s jWAWcftfcgWemQi85oxMq9XKPMREz8 a 5ebRNuyvG98fH19gxkUMP7U5NK61Kb9mZfHptRTA vrPRNTU7cibgOFdgW 8RiPd2c 2SMIOEv7Z8 Y794QxN7 yJkXs8BP3rux9O7ZJmI0TYIdr3IHxxjBiV8qzzlwnhMMYA X921ZoC4Pr2 AgEOt0j6F 5nXTSa1SaVVq9XrTIGxgEqtVdEvreCBA108ByPuBp8A8yLtA7F3n luhyC n5mhLZf8E x9JyO5pKQMFSDoLLTpkmk47rRnJxYk1AGfgx9T TabSFv vVrrAhy6VE9g7dN7IJfesUWXtocJS8YC4Pep1fThCVdgQGjs18XTlv 8sE18UVJSggFVYJq31zsL25vEQrCHtfcGsVBSwomrLNxZAccWafLyEpNJjMqx4Faeg PF63IP7SNvbQUfMpey1XMLfPJOaLWrwa2Zt1ymgRGvl8SGtdoBgX3avkaejlxJQpDDFwTxjlGEdwSR TWwb qfx8DSOU6oB1gOd TcRrw1jeeQPaDLIv3lrblU By kONmptm FXDtPz KOzwSPANjKrXQdFF1jULQUAFA8p8ZRSj7X0DwB4Zl8nug19w2QeXg9QemS6VV4lXZuTx7W4GXLNJfNpuP9 3dAva977yRKcEzYJ4DLAu05WK thzEe4L4EHKQP90D0nEnxDIw6q hB7Q9a86E3b1D5ViBOlcW1uXyqke UqwuHwOZvjil1v5wLNjzPnWpVwWyfP6KkAD7BZzbbHb2C0j0W4JYwrB7IZZ3F4ivsLdvXjx UYYgusBxwLIus56Zx4 fZA7S5jFo3OoBHsbqMZKpYZNJ8HDmy828U3Jld2ZCD4KBLAPjBd7X4xDLMNs5s7MD3xCpjQI7XsiwnGWln8VTCKgu56UZUvjLh0Cmu4Arx1iy Yd91808sqFWCb4r21B9nm9f2TXgDz8j7nzYWaC1AU7A9f7NLPTu7uRh5DadV17a244alWgSqITEVjoogShtxS3lWclZ tCjFblCvL98BOY54CozSQzziIm5ScrcNzKgVYmZiou3Aeh29j59nqzBgo n IqeGHEn9PgdaMtf3 vtLXkOvo58c7Ymu8GdWInbGZZJd34WaMR0lTjOL30kuS6bt15ObTHuhfmlykQnQL0 bAxo1RM iN88d5D3ejUaugF89jHwn2EUuEBzvLBzDyOfE3eukRSFlSwQC19E3EaBw rICMsukxoSRtq8I wwf3m 6ZUUumw2H7h1YxPBtNEYjAUluyebIyh4NCCqJt6B6vRuEx8 LH3YhpYNn7yqSdbQB0uBL1geesQA BdWu GQu6xJK8TDXTtO6Jz hrFgz y8JCwfgjeYIOoLqFSmHzjMQ1lc293VyH uw4j4ExRieHxkmuZjqtdwD9B8aKL7a0AxIo e0zTgRsuhoISiSc7gGZV4Om8ykWtZ70oMBtkGq Md7xkYMaOq5mkcrKHnSz0f qa2pfH4MtjONdYuA3JBfBoklQJVobBrR0I Cih1tMxnInz6apjlJlRY8cPZZm6gyP0PMbz1LyUd3VdSXRN6EOU y 3wP RhfNkJGW gHJx6Pp0Ly0Q4Y3dW8By1JlJ4DzXhfssaxqcOJ5Z pN9pguHigWfKN7c0lW7rxjtCjY0RmW57OuQBMDHjN87LILQt3h 8G WDmKQAJYHbDdz z40eO9fJt Okp8Wimsba4s9BroDioHBU3x0qQZL1M18AZQ2SC2w23dHW5ol8GvwE5n1he7bz4id6Oc wW7cd QAKHdOzEbV 0iQRrJAeLcXZILq7T1GCQixPwQ Ubu0 4waOA7iPUd4HX yavEj496dkAvAQgn1cS zJbUffEdF7pkqcTeV Fzgu8zrRneSimpv 639Oy4kQ7f7r8J8i7v8YqfGYIXmz d 7horIXyNPHujuOKvw0g5Qmg SRx8UF0j8EEwcfBAt0LYsKI4 CA0rEj4M fiQKOdZrn4 KCEw8TjOIp2umMVzpERF47YoZ0evNXl90sxgjmOCKBZrm2 3ijkAVJxcES74roH KltUcQVI3YAZuzBuq5318RZjiGqrytaamtqWuKCEVO0KxSwN1DkJNdxIEB3TMUDwFgDdrzGSY49fDGbXPAni 8DqSQuwvSdulEAAAAASUVORK5CYII \"\"\" md\"\"\" Heard in the hallways I only like discrete math. I only like continuous math. \"\"\" md\"\"\" Indexing and Function Evaluation Analogy ``v i`` ith element of v vs. f x evaluate f at x These are different, right? In the one you are extracting an element, and in the other you are applying what in some high schools might be called a \"function machine.\" However, a moment's thought tells you that a vector is really a discrete function, in that the argument can take on the values i 1,2,...,n and the evaluation is v i . That's a function. In fact, think of a range object such as 2 2 20. You could think of this as just a shorthand for the vector 2,4,...,20 but in fact when you index into this \"vector\" like thing, you are indeed explicitly evaluating a function i.e. i 2i. \"\"\" 2 2 20 this expands the \"iterator\" into an ordinary vector 2 2 20 7 Extracts an element from Memory of course there is an address calculation 2 2 20 7 Compute 2 7 more or less begin f x 2x f 7 Compute 2 7 end md\"\"\" Any which way v is a function \"machine\" whose input is \\ 1,2,3,4,5,6,7,8,9,10\\ \"\"\" gr md\"\"\" Area \"\"\" md\"\"\" Area of a circle using regular polygons \"\"\" md\"\"\" n bind sides Slider 3 100, show value true, default 6 \"\"\" area s s 2 sin 2π s begin θ 0 .01 1 2π plot cos. θ ,sin. θ , ratio 1, axis false, legend false, ticks false, lw 4, color black, fill false plot cos. θ ,sin. θ , ratio 1, axis false, legend false, ticks false, lw 4, color white, fill true, alpha .6 ϕ 0 sides 2π sides for i 1 sides plot Shape 0,cos ϕ i ,cos ϕ i 1 , 0,sin ϕ i ,sin ϕ i 1 , fill true,lw 0 end title \"Area sides 2 sin 2π sides ≈ area sides π π\" end area0 area. 2 .^ 2 10 Area of polygons with sides 4, 8, ..., 1024 π md\"\"\" A carefully chosen convolution 1 3,4 3 \"\"\" area1 4 3 area0 i 1 . 1 3 area0 i for i 1 length area0 1 function colorgoodbad should be, given indexofmistake something findfirst collect should be . collect given , length given 1, htl \"\"\" span style \"color inherit\" given 1 indexofmistake 1 span span style \"color red\" given indexofmistake end span \"\"\" end colorgoodbad string float π , string 22 7 area2 16 15 area1 i 1 . 1 15 area1 i for i 1 length area1 1 md\"\"\" Another carefully chosen convolution 1 15,16 15 , do you see the pattern? \"\"\" big π area3 64 63 area2 i 1 . 1 63 area2 i for i 1 length area2 1 area4 128 127 area3 i 1 . 1 127 area3 i for i 1 length area3 1 md\"\"\" Why does this work? \"\"\" md\"\"\" Area s `` s 2 \\sin 2\\pi s \\pi \\frac 2\\pi^3 3 s^ 2 \\frac 2\\pi^5 15 s^ 4 \\frac 4\\pi^7 315 s^ 6 \\ldots`` as `` s \\rightarrow \\infty ``. \"\"\" md\"\"\" Area s `` \\pi c 1 s^2 c 2 s^4 \\ldots`` Area 2s `` \\pi c 1 4s^2 c 2 16s^4 \\ldots`` \"\"\" md\"\"\" Think about taking 4 3 Area 2s 1 3 Area s . Now we have ``\\pi c s^ 4 `` as the leading term so doubling the s approximately reduces the area error by 16, when before it was only 4. etc. \"\"\" areab s s 2 sin big 2 big π s begin area0b areab. big. 2,4,8,16,32,62,128,256,512,1024,2048,4096,8192,16384,32768,65536 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area0b end end begin area1b 4 3 area0b i 1 . 1 3 area0b i for i 1 length area0b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area1b end end string area1b end colorgoodbad sprintf \"%.30f\" big π , sprintf \"%.30f\" big area1b end begin area2b 16 15 area1b i 1 . 1 15 area1b i for i 1 length area1b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area2b end end begin area3b 64 63 area2b i 1 . 1 63 area2b i for i 1 length area2b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area3b end end begin area4b 256 255 area3b i 1 . 1 255 area3b i for i 1 length area3b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area4b end end begin area5b 1024 1023 area4b i 1 . 1 1023 area4b i for i 1 length area4b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area5b end end begin area6b 4096 4095 area5b i 1 . 1 4095 area5b i for i 1 length area5b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area6b end end begin area7b 16384 16383 area6b i 1 . 1 16383 area6b i for i 1 length area6b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area7b end end begin area8b 65536 65535 area7b i 1 . 1 65535 area7b i for i 1 length area7b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area8b end end big π md\"\"\" Area using inscribed squares \"\"\" bind s Slider 2 40, show value true let plot for i s s plot i s,i s , 1,1 ,color RGB 0,1,0 ,lw 1 plot 1,1 , i s,i s ,color RGB 0,1,0 ,lw 1 end P plot cos. θ ,sin. θ , ratio 1, axis false, legend false, ticks false, lw 3, color black plot P h 1 s a 0 xx floor √2 2h x xx h y x plot Shape x, x, x ,x , y, y ,y, y , color RGB 1,0,0 ,alpha .7 a a Int 2 xx ^2 for i s xx 1 , j s 1 x i h y j h if x^2 y^2≤1 & x h ^2 y h ^2 ≤1 & x^2 y h ^2 ≤1 & x h ^2 y^2 ≤1 plot Shape x, x, x h ,x h , y, y h ,y h, y , color blue plot Shape x h, x h, x , x , y, y h ,y h, y , color blue plot Shape x, x, x h ,x h , y h, y , y, y h , color blue plot Shape x h, x h, x , x , y h, y , y, y h , color blue plot Shape y, y h ,y h, y , x, x, x h ,x h , color blue plot Shape y h, y , y, y h , x, x, x h ,x h , color blue plot Shape y, y h ,y h, y , x h, x h, x , x , color blue plot Shape y h, y , y, y h , x h, x h, x , x , color blue a 8 end end xlabel \"s s\" title \" a s^2 a h^2 π π\" plot end md\"\"\" Imagine you didn't have the idea of area \"\"\" begin N 1024 h 1 N v randn N end bind j Slider 1 9, show value true, default 6 J N ÷ 2^j begin plot c 0 cumsum v . √h plot 0 N . N,c scatter 0 J N . N, c 1 J end ,legend false,m o,ms 5, color red, lw 5 plot ylims 2,2 xlabel \"time\" ylabel \"position\" end md\"\"\" A random walk is a discrete random function. It is defined at grid points. Brownian motion is a continuous random function. It is defined on an entire interval. If one has an instance of a Brownian motion, you can say its exact value at, say, .7. If one looks at the random variable that represents evaluation at .7 it is a normal distribution. \"\"\" md\"\"\" Alan's essay In what sense does the continuous even exist? The fact of the matter is that there are limits that give the same answer no matter how you get there, and these limits are important to us. For example, no matter how you cover an area, by little rectangles, the sum always converges to what we intuitively call area. The normal distribution is interesting in that no matter which starting finite distribution we might take, if add n independent copies and normalize to variance 1 we get the same limit. Again, there are so many ways to start, and yet we always end up with the same thing. Continuous mathematics is full of so many examples, where discrete objects end up behaving the same. Indeed what happens as discrete objects get larger and larger, their complexity gets out of control if one wants to keep track of every detail, but they get simpler in their aggregate behavior. \"\"\" f x,t exp x^2 t √ π t bind t Slider .01 .01 8, show value true begin x 3 .01 3 plot x, f. x,t , ylims 0,1 , legend false end begin surface 2 .05 2, .2 .01 1, f, alpha .4, c Reds, legend false for t .2 .1 1 plot 2 .05 2, fill t,length 2 .05 2 , f. 2 .05 2,t , c black end xlabel \"x\" ylabel \"t\" plot end plotly md\"\"\" Sum and a Definite Integral \"\"\" md\"\"\" Cumsum and an Indefinite Integral \"\"\" md\"\"\" Discrete Finite Differencing Filters and Derivatives Gradients \"\"\" md\"\"\" Discrete and continuous convolutions e.g. probability densities \"\"\" md\"\"\" Discrete Random Walks and Brownian Motion \"\"\" md\"\"\" Binomial Distribution and the Normal Distribution \"\"\" md\"\"\" Discrete and the Continuous Fourier Transform \"\"\" P binomial n,k for n 0 5,k 0 5 P P' begin A 1 . beta. 1 6 ', 0 5 . 1 1 5 A 1, . 1 round. Int,A end function blue s String htl \" span style 'color hsl 200deg, 60%, 50% ' s span \" end md\"\"\" It is not unusual for students and professors to gravitate towards the discrete or the continuous. We wish to point out, that the discrete and the continuous are so closely related, that it is worthwhile to be comfortable with both. Up until fairly recently, much of computer science was often associated with discrete mathematics, while computational science and engineering was associated with physical systems, hence continuous mathematics. blue \"That is blurring these days \" The popularity of machine learning has brought continuous optimization ideas such as gradient descent into the world of computer science and the impact of the physical world on us all e.g. climate change, pandemics is motivating applications in computer science. The newfound popularity of Data science and statistics is also mixing the discrete with the continuous. \"\"\" md\"\"\" blue \"Continuous math often lets you replace complicated large systems with lots of details with a simpler abstraction that is easier to work with.\" \"\"\" md\"\"\" blue \"The combination of continuous and discrete is often more useful than either one alone.\" \"\"\" md\"\"\" blue \"Machine Learning, Pandemics, climate change, etc. show how critical continuous math is these days.\" \"\"\" blue \"asdf\" function pyramid rows Vector Vector horizontal false, padding x 8, padding y 2, style padding \" padding y px padding x px\" render row xs htl \"\"\" div padder padder htl \" span style style x span \" for x in xs padder padder div \"\"\" htl \"\"\" style .pyramid flex direction column display flex font family monospace font size 0.75rem .pyramid.horizontal flex direction row .pyramid div display flex flex direction row .pyramid.horizontal div flex direction column .pyramid div span hover background rgb 255, 220, 220 font weight 900 .pyramid div padder flex 1 1 auto .pyramid div span text align center style div class \"pyramid\", horizontal ? \"horizontal\" \"vertical\" render row. rows div \"\"\" end pyramid area0,area1 , horizontal true pyramid area0,area1,area2 , horizontal true pp x colorgoodbad string float π , sprintf \"%.15f\" x pyramid pp. area0 , pp. area1 , pp. area2 , pp. area3 , pp. area4 , horizontal true "},{"url":"data_science/linearmodel_datascience/","title":"Linear Model, Data Science, & Simulations","tags":["lecture","module2","track_julia","track_data","csv","dataframe","statistics","plotting","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v O6NTKsR8TjQ\" image \"https user images.githubusercontent.com 6933510 136199721 8fd577cb d6f3 492d bbdc 37bc74664ca7.png\" section 8 order 8 title \"Linear Model, Data Science, & Simulations\" layout \"layout.jlhtml\" youtube id \"O6NTKsR8TjQ\" description \"\" tags \"lecture\", \"module2\", \"track julia\", \"track data\", \"csv\", \"dataframe\", \"statistics\", \"plotting\", \"interactive\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, DataFrames, CSV, GLM, Statistics, LinearAlgebra, Distributions TableOfContents md\"\"\" Julia concepts for data science Data Frames `DataFrames.jl` `CSVread`, `CSVwrite` `CSV.jl` `lm` linear model `GLM.jl` ` formula` formula macro to specify variables to analyze `GLM.jl` Underscore as digits separator e.g. `1 000` for 1000 The value of fast simulations. \"\"\" md\"\"\" In this lecture we will simulate a real world statistical application for the purpose of understanding what statistics is about. It is very helpful in simulations to be able to run many examples fast. \"\"\" md\"\"\" Fahrenheit and Celsius Data Set \"\"\" n 10 x sort rand 10 100, n y 5 9 . x . 17.7777777 same as y 5 9 . x . 32 begin plot x,y, m c, mc red,legend false xlabel \"°F\" annotate 4,16,text \"°C\",11 plot x, x. 30 . 2 Dave's cool approximation end x y md\"\"\" Julia Data Frames I like to think of a Data Frame as a matrix with labels. \"\"\" md\"\"\" Data Frame by Columns with labels \"\"\" data DataFrame °F x,°C y Label data md\"\"\" Data Frame with a matrix \"\"\" begin data2 DataFrame x y , auto convert Matrix to DataFrame rename data2, \"°F\",\"°C\" add column labels end Matrix data2 Convert back to a matrix lose label information md\"\"\" Julia Comment about types Notice that x y converts all the data to floats, but columns of a data frame can have different types. \"\"\" md\"\"\" Reading Writing CSV comma separated values Files \"\"\" md\"\"\" Writing Data to a CSV file readable by spreadsheet software. \"\"\" CSV.write \"testCSVwrite.csv\", data md\"\"\" Reading Data from a CSV file to a DataFrame \"\"\" data again CSV.read \"testCSVwrite.csv\", DataFrame data again ,\"°F\" or data again ,1 data again ,1 md\"\"\" Noisy Data Add some random noise to the celsius readings \"\"\" md\"\"\" The noise slider so I can find it easily \"\"\" md\"\"\" noise bind noise Slider 0 .5 1000, show value true \"\"\" begin noisy data copy data Noisy DataFrame noisy data , \"°C\" . noise randn n yy noisy data , \"°C\" noisy data end md\"\"\" Statistics Software Outputs Mysterious Tables example output from the \"linear model\" `lm` which we store in the variable `ols` for ordinary least squares by contrast weighted least squares treats the vertical displacements with unequally with differing weights. \"\"\" ols lm formula °C ~ °F , noisy data noisy data md\"\"\" This lecture is about explaining the meaning and significance to every part of this table. \"\"\" md\"\"\" Regression a few ways \"\"\" md\"\"\" The \"Coef.\" column in the table gives the slope and intercept of the best fit line \"\"\" b, m one. x x \\ yy The mysterious linear algebra solution using \"least squares\" begin scatter x, yy,m c,mc red, label \"noisy data\", ylims 40, 40 for i 1 length data ,2 plot x i ,x i , m x i b,yy i , color gray, ls dash, label false end xlabel \"°F\" annotate 15,16,text \"°C\",11 plot x, m. x . b, color blue, label \"best fit line\" plot x,y,alpha .5, color red, label \"theory\" theoretical plot legend top end function linear regression x,y a direct computation from the data n length x x0 x. mean x y0 y. mean y mᵉ sum x0 . y0 sum x0.^2 slope estimate bᵉ mean y mᵉ mean x intercept estimate s2ᵉ sum mᵉ. x . bᵉ . y .^2 n 2 noise estimate bᵉ, mᵉ, s2ᵉ end linear regression x,yy md\"\"\" So why is it called \"Regression\" anyway? http blog.minitab.com blog statistics and quality data analysis so why is it called regression anyway Dalton's original meaning not quite what it means today. \"\"\" md\"\"\" Demystifying the word \"Model\" Step I The Model is y m x b σ randn . This means that out there in the real world are b, m, and σ. You don't know them. Step II You do, however, have data points x and y which allow you to compute an bᵉ, mᵉ, and σᵉ. A statistician would call these estimates based on your data points. If you ran the experiment again, you would get different data points. The computer lets us run the experiment as many times as we want just to see what happens. In summary, there are three kinds of variables. The model variables b, m, and σ which are unknown. The predictor variable x which is considered fixed and known. The response variable y which is considered noisy. \"\"\" md\"\"\" Understanding the relationship `°C ~ 1 °F` \"\"\" ols md\"\"\" `°C ~ 1 °F` means the celsius y is Coef1 1 Coef2 °F , in general `y ~ 1 x1 x2 x3` is shorthand for ``y c 0 c 1 x 1 c 2 x 2 c 3 x 3``, etc. \"\"\" md\"\"\" Simulating the real world running many noisy models \"\"\" function simulate σ,howmany linear regression x,y . σ randn length x for i 1 howmany linear regression x,y . σ sqrt 12 .5 . rand length x for i 1 howmany linear regression x,y . σ rand 1,1 ,length x for i 1 howmany end howmany 100 000 md\"\"\" Julia underscore as a digits separator \"\"\" md\"\"\" Simulated intercepts howmany simulations \"\"\" md\"\"\" σ bind σ Slider 0 .1 3, show value true, default 1 \"\"\" s simulate σ, howmany s 1 first simulation, intercept, slope, estimation of noise σ begin histogram first. s , alpha .6, bins 100, norm true vline 17.777777 ,color white title \"intercept\" xlims 17.7777 3, 17.7777 3 ylims 0,1 plot legend false end md\"\"\" Experimental mean of the intercept \"\"\" mean first. s , 17.777777 md\"\"\" Experimental std of the intercept \"\"\" std first. s md\"\"\" Statisticians know an exact formula for the theoretical std of the intercept \"\"\" sb σ norm x norm x. mean x sqrt n md\"\"\" Simulated slopes howmany simulations \"\"\" begin histogram getindex. s, 2 , alpha .6, bins 100, norm true, legend false title \"slope\" vline 5 9 ,color white xlims 5 9 .1, 5 9 .1 ylims 0,100 end md\"\"\" Sample mean of the slope \"\"\" mean getindex. s, 2 , .555555 md\"\"\" Sample std of the slope. \"\"\" std getindex. s, 2 md\"\"\" Statisticians know a formula for the theoretical std of the slope. \"\"\" σ norm x. mean x md\"\"\" Simulated σ howmany simulations \"\"\" begin histogram last. s . σ^2 n 2 , alpha .6, bins 100, norm true,legend false vline 1 ,color white title \"residual\" vline n 2 ,color white, lw 4 xlims 0,20 ylims 0,.13 plot x pdf Chisq n 2 ,x , lw 4 , color red plot end mean last. s σ^2 std last. s σ^2 sqrt n 2 2 md\"\"\" The Linear Model Table \"\"\" ols lm formula °C ~ °F , noisy data 24.3784 19.0397 0.686156 0.330459 md\"\"\" The Coef column is just the regression formula for the best line \"\"\" mᵉ, bᵉ, σ²ᵉ linear regression x, yy md\"\"\" The Std. error column \"\"\" md\"\"\" Above we saw that statisticians had formulas for the exact std of the slope and intercept `std intercept σ norm x norm x. mean x sqrt n ` ` std slope σ norm x. mean x ` \"\"\" md\"\"\" Let's replace σ with our estimate √σ²ᵉ \"\"\" sqrt σ²ᵉ norm x norm x. mean x sqrt n sqrt σ²ᵉ norm x. mean x md\"\"\" See those are the numbers in the magic table above. I always love when I can reproduce the numbers myself. It makes me feel I understand it. \"\"\" md\"\"\" The t column \"\"\" md\"\"\" The t column, is simply the Coeff column divided by the Std. error column which we will use in a hypothesis test in the upcoming column. \"\"\" md\"\"\" The t distribution \"\"\" md\"\"\" In a statistics class you will likely see a random variable known as a t distribution. with parameter k . It is the ratio of a standard normal to a χ distribution with parameter k. Let's just use `randn` to simulate. For the data sets of most of today's experiments, the normal distribution is close enough to t, that nobody needs to even use t much anymore. In any event, with a t or a normal we are using this distribution because we are cognizant of the fact that the true σ is unknown and is merely being estimated. \"\"\" rand t k sqrt k randn norm randn k md\"\"\" k bind k Slider 3 100, show value true \"\"\" begin histogram rand t k for i 1 100000 , norm true, bins 500, label false plot x pdf TDist k ,x , lw 4 , color red, label \"t dist\" plot x pdf Normal ,x , color green, lw 2, label \"normal dist\" xlims 3, 3 ylims 0, .4 end md\"\"\" The Pr |t| column is the area of the curve outside of the interval t,t . \"\"\" md\"\"\" In statistics we ask if the coefficient ought to be considered 0 which means in this case the data has no intercept or does not depend on x or whether the coefficients are signficant with some probability. The Pr |t| column gives us the probablity that we should accept the hypothesis that the coefficients might reasonably be just 0. In a proper statistical test, you should decide at what level you might be willing to accept the hypothesis, example .99, .95, or .9 might be a reasonable level, and if the test gives a smaller probability, you will accept that the coefficients are signficant. It is not proper to produce the able and then decide whether to use .99, say. \"\"\" md\"\"\" Degrees of Freedom \"\"\" md\"\"\" It is interesting to see that the sum of squares of a demeaned Gaussian vector is the size 1. This is the reason for the n 1 in the sample mean for variance. \"\"\" md\"\"\" How many degrees of freedom are in a \"demeaned\" vector of normals? \"\"\" mean v randn 17 v. mean v sum v.^2 for i 1 1 000 000 md\"\"\" If you ever wondered why the sample variance always has you dividing by n 1 and not n, this is the crux of the reason. \"\"\" "},{"url":"data_science/optimization/","title":"Optimization","tags":["lecture","module2","track_juli","track_climate","track_data","track_math","optimization"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v 44RA9fclTdA\" image \"https user images.githubusercontent.com 6933510 136199719 a56a217b cd36 4da2 b407 7285dcec94df.png\" section 9 order 9 title \"Optimization\" layout \"layout.jlhtml\" youtube id \"44RA9fclTdA\" description \"We use Optim.jl and JuMP.jl to optimize a function automatically finding the input that maximizes output.\" tags \"lecture\", \"module2\", \"track juli\", \"track climate\", \"track data\", \"track math\", \"optimization\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using PlutoUI, Plots, Statistics, Optim, JuMP, Ipopt, ForwardDiff TableOfContents md\"\"\" Julia concepts Named tuples \"\"\" md\"\"\" Line Fitting Many Ways \"\"\" md\"\"\" Last lecture we did some line fitting \"regression\" . Let's see how we can actually solve that problem. \"\"\" md\"\"\" Exploratory Data Analysis \"\"\" md\"\"\" Let's start off by making some noisy data. \"\"\" md\"\"\" n bind n Slider 3 10 200, show value true \"\"\" x sort rand 10 100, n y 5 9 . x . 17.7777777 . 5 . randn. same as y 5 9 . x . 32 begin plot x,y, m c, mc red, legend false, ls dash xlabel \"°F\" ylabel \"°C\" plot x, x. 30 . 2 Dave's cool approximation end md\"\"\" Least Squares fitting to a straight line \"\"\" md\"\"\" Suppose we are given data x i and measurements y i . Least squares fitting a straight line means finding the best ``m`` slope and ``b`` intercept that minimize the \"error\" distance from the data in a least squares sense \\min m,b \\sum b m x i y i ^2 \"\"\" md\"\"\" Direct Formulas \"\"\" md\"\"\" The Statistician's formula \"\"\" begin m cov x,y var x same as x. mean x ⋅ y. mean y sum abs2,x. mean x b mean y m mean x b b, m m end md\"\"\" Julia Named Tuples \"\"\" nt first 1, next 2, last 3.1 kind of handy typeof nt plot x m x b, lw 3, alpha 0.7 md\"\"\" The Linear Algebraist's Formula \"\"\" md\"\"\" This is even shorter, but you need to know linear algebra. But it also generalizes. \"\"\" one. x x \\y even shorter but you need to know linear algebra, but generalizes md\"\"\" Optimization Methods Since the problem is an optimization problem, we can use optimization software to obtain an answer. This is overkill for lines, but generalizes to so many nonlinear situations, including neural networks as in machine learning. \"\"\" md\"\"\" Optim.jl A package written entirely in Julia for optimization \"\"\" md\"\"\" Optim.jl Documentation https julianlsolvers.github.io Optim.jl stable \"\"\" md\"\"\" We can ask software to just solve the problem \\min b,m \\sum i 1 ^n b m x i y i ^2 or ``\\min b,m `` `loss b,m ` \"\"\" loss b, m sum b m x i y i ^2 for i 1 n result optimize loss, 0.0,0.0 optimize f with starting guess result.minimizer md\"\"\" Functions of Functions and Computing Power Optimization such as ``\\min b,m `` `loss b, m ` is an example of a fairly heavy function of a function. By this we mean that the input is a function such as `loss b, m ` and the output is the location or value of a minimum, say. By \"heavy\" we mean that typically a large amount of computing power is needed. Not that many years ago, computers were not strong enough for realistic problems. Modern day machine learning and so much more is enabled because computers can now surround entire codes with optimization, or if the software is compatible, automatic differentiation. \"\"\" md\"\"\" JuMP.jl A popular modelling language for Optimization Problems JuMP Julia for Mathematical Programming \"\"\" let n length x model Model Ipopt.Optimizer variable model, b variable model, m objective model, Min, sum b m x i y i ^2 for i in 1 n set silent model optimize model b value b , m value m end md\"\"\" Gradients \"\"\" md\"\"\" The above optimization methods made no explicit mention of derivative or gradient information. For simple problems, gradients can be hand calculated, but for many real problems this is impractical. \"\"\" md\"\"\" Hand Computation \"\"\" md\"\"\" `` \\frac \\partial \\partial b \\sum i 1 ^n b m x i y i ^2 2\\sum i 1 ^n b m x i y i `` `` \\frac \\partial \\partial m \\sum i 1 ^n b m x i y i ^2 2\\sum i 1 ^n x i b m x i y i `` \"\"\" begin ∇loss b,m,i 2 b m x i y i . 1,x i ith summand ∇loss b,m sum ∇loss b,m,i for i 1 n end md\"\"\" Finite Difference Evaluation \"\"\" ∇loss .1,.3 begin finite difference ϵ .000000001 loss .1 ϵ ,.3 loss .1 ,.3 ϵ . loss .1 ,.3 . ϵ end md\"\"\" Automatic Differentiation AutoDiff We're all so good at calculus I suppose, but nothing like letting the computer do it. For real problems, what you learned in calculus is impractical. Note Autodiff is not finite differences. It is not as problematic as finite differences is in that with finite differences it can be hard to know which ϵ to use, etc. \"\"\" ∇loss .1,.3 hand computation ForwardDiff.gradient loss, .1,.3 md\"\"\" Gradient Descent can be difficult for complicated functions \"\"\" let b, m 0, 0 starting guess for i 1 25 db, dm ∇loss b,m Getting a good step size can be really hard I worked out the line search η by hand η sum b m x i y i db dm x i for i 1 n sum db dm x i ^2 for i 1 n b, m b, m . η . db, dm end b b, m m end md\"\"\" Hoping for ` b 18.1716, m 0.56601 ` \"\"\" md\"\"\" Stochastic Gradient Descent Pick one term of the sum or a few of them and update according to the respective gradient. This is what works in machine learning. \"\"\" let b, m 0.0, 0.0 for t 1 10 000 000 η .00002 there seems to be an art to picking these steplengths b, m b, m . η ∇loss b,m, rand 1 n end b b, m m end md\"\"\" Bells and Whistles for optim.jl \"\"\" optimize loss, 0.0,0.0 , BFGS , autodiff forward optimize loss, 0.0,0.0 , BFGS optimize loss, 0.0,0.0 , GradientDescent optimize loss, 0.0,0.0 , GradientDescent , autodiff forward "},{"url":"data_science/pca/","title":"Principal Component Analysis","tags":["lecture","module2","track_data","data","statistics","matrix","linear algebra","track_math"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v iuKrM NzxCk\" image \"https user images.githubusercontent.com 6933510 136196577 512cee99 aebf 48a9 97b8 358d5ca561ca.png\" section 1 order 1 title \"Principal Component Analysis\" layout \"layout.jlhtml\" youtube id \"iuKrM NzxCk\" description \"In this notebook we will start looking at more general kinds of data, not only images, and we'll try to extract some information from the image using statistical methods, namely principal component analysis. This method tries to answer the questions \\\"which 'directions' are the most important in the data\\\" and \\\"can we reduce the dimensionality number of useful variables of the data\\\"?\" tags \"lecture\", \"module2\", \"track data\", \"data\", \"statistics\", \"matrix\", \"linear algebra\", \"track math\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import ImageMagick using PlutoUI using Colors, ColorSchemes, Images using Plots using LaTeXStrings using Statistics, LinearAlgebra standard libraries end PlutoUI.TableOfContents aside true md\"\"\" Introduction Understanding data In this notebook we will start looking at more general kinds of data , not only images, and we'll try to extract some information from the image using statistical methods, namely principal component analysis https en.wikipedia.org wiki Principal component analysis . This method tries to answer the questions \"which 'directions' are the most important in the data\" and \"can we reduce the dimensionality https en.wikipedia.org wiki Dimensionality reduction number of useful variables of the data\"? This can be viewed as another take on the question of finding and exploiting structure in data. It also leads towards ideas of machine learning . \"\"\" md\"\"\" Rank of a matrix \"\"\" md\" Flags\" md\"Let's start off by recalling ideas about multiplication tables or outer products \" outer v, w x y for x in v, y in w outer 1 10, 1 12 md\"\"\" Each column is a multiple in general not an integer multiple of any other column and each row is a multiple of any other row. This is an example of a structured matrix , i.e. one in which we need to store less information to store the matrix than the full m \\times n table of numbers even though no element is 0 . For example, for an outer product we only need m n numbers, which is usually much less. \"\"\" md\"Some flags give simple examples of outer products, for example\" flag outer 1, 0.1, 2 , ones 6 ones 6 flag2 outer 1, 0.1, 2 , 1, 1, 1, 3, 3, 3 md\"\"\" Note that outer products are not always immediate to recognise just by looking at an image But you should be able to recognise that there is some kind of structure. \"\"\" md\" Matrix rank\" md\"\"\" If a matrix can be written exactly as a single multiplication table outer product, we say that its rank is 1, and we call it a rank 1 matrix. Similarly, if it can be written as the sum of two outer products, it has rank 2 , etc. \"\"\" md\"Let's see what a random rank 1 matrix looks like \" w 300 image outer 1 0.4 rand 50 , rand w md\"\"\" It has a characteristic checkerboard or patchwork look. \"\"\" md\"\"\" Here's a random rank 2 matrix \"\"\" md\"\"\" We see that it starts to look less regular. \"\"\" md\"\"\" Exercise Make an interactive visualisation of a random rank n matrix where you can vary n . \"\"\" md\"\"\" Effect of noise \"\"\" md\"\"\" Now what happens if we add a bit of noise , i.e. randomness, to a rank 1 matrix? \"\"\" noisy image image . 0.03 . randn. md\"\"\"The noisy image now has a rank larger than 1. But visually we can see that it is \"close to\" the original rank 1 matrix. Given this matrix, how could we discover that it is close to a structured, rank 1 matrix? We would like to be able to find this out and say that the matrix is close to a simple one.\"\"\" md\" Images as data\" md\"\"\" Now let's treat the image as a data matrix , so that each column of the image matrix is a vector representing one observation of data. In data science it is often the rows that correspond to observations. Let's try to visualize those vectors, taking just the first two rows of the image as the x and y coordinates of our data points \"\"\" image 1 2, 1 20 begin xx image 1, yy image 2, end md\" From images to data\" md\"\"\" We would like to visualise this data with the `Plots.jl` package, passing it the x and y coordinates as arguments and plotting a point at each x i, y i pair. We obtain the following plot of the original rank 1 matrix and the noisy version \"\"\" gr begin xs noisy image 1, ys noisy image 2, scatter xs, ys, label \"noisy\", m ., alpha 0.3, ms 4, ratio 1 scatter xx, yy, leg topleft, label \"rank 1\", ms 3, alpha 0.3, size 500, 400 , m square, c red, framestyle origin title \"Plotting a rank 1 matrix gives a straight line \" end md\"We see that the exact rank 1 matrix has columns that lie along a line through the origin in this representation, since they are just multiples of one another. E.g. If x 1, y 1 and x 2, y 2 are two columns with x 2 cx 1 and y 2 cy 1 , then y 2 x 2 y 1 x 1 , so they lie along the same line through the origin. The approximate rank 1 matrix has columns that lie close to the line \" md\"So, given the data, we want to look at it do see if it lies close to a line or not. How can we do so in an automatic way? \" md\"\"\" Measuring data cloud \"size\" using statistics \"\"\" md\"Looking at this cloud of data points, a natural thing to do is to try to measure it How wide is it, and how tall?\" md\"\"\"For example, let's think about calculating the width of the cloud, i.e. the range of possible x values of the data. For this purpose the y values are actually irrelevant. \"\"\" md\"\"\" A natural idea would be to just scan the data and take the maximum and minimum values. However, real data often contains anomalously large values called outliers , which would dramatically affect this calculation. Instead we need to use a statistical method where we weight the data and average over all the data points. This process will hopefully be affected less by outliers, and will give a more representative idea of the size of the bulk of the data. \"\"\" md\"\"\" A first step in analysing data is often to centre it data around 0 by subtracting the mean, sometimes called \"de meaning\" \"\"\" begin xs centered xs . mean xs ys centered ys . mean ys end scatter xs centered, ys centered, ms 5, alpha 0.5, ratio 1, leg false, framestyle origin md\"\"\" Measuring a \"width\" of a data set \"\"\" md\"\"\" A natural way to measure the width of a data set could be to measure some kind of width separately in both the x and y directions, in other words by projecting the data onto one of the axes, while ignoring the other one. Let's start with the x coordinates of the centred data and try to average them. If we literally average them we will get 0 since we have subtracted the mean . We could ask \"on average how far away from the origin is the data\". This would give the mean absolute deviation https en.wikipedia.org wiki Average absolute deviation \"\"\" begin scatter xs centered, ys centered, ms 5, alpha 0.5, ratio 1, leg false, framestyle origin scatter xs centered, zeros size xs centered , ms 5, alpha 0.1, ratio 1, leg false, framestyle origin for i in 1 length xs centered plot xs centered i , ys centered i , xs centered i , 0 , ls dash, c black, alpha 0.1 end plot end mean abs. xs centered md\"\"\" This is a perfectly good computational measure of distance. However, there is another measure which is easier to reason about theoretically analytically \"\"\" md\"\"\" Root mean square distance Standard deviation \"\"\" md\"\"\" The standard deviation is the root mean square distance of the centered data from the origin. In other words, we first square the distances or displacements from the origin, then take the mean of those, giving the variance . However, since we have squared the original distances, this gives a quantity with units \"distance ^2 \", so we need to take the square root to get back to a measurable length \"\"\" begin σ x √ mean xs centered.^2 root mean square distance from 0 σ y √ mean ys centered.^2 end md\"This gives the following approximate extents standard deviations of the cloud \" begin scatter xs centered, ys centered, ms 5, alpha 0.5, ratio 1, leg false, framestyle origin vline 2 σ x, 2 σ x , ls dash, lw 2, c green hline 2 σ y, 2 σ y , ls dash, lw 2, c blue annotate 2σ x 0.93, 0.03, text L\"2\\sigma x\", 14, green annotate 2σ x 0.88, 0.03, text L\" 2\\sigma x\", 14, green annotate 0.05, 2σ y 1.13, text L\"2\\sigma y\", 14, blue annotate 0.06, 2σ y 1.14, text L\" 2\\sigma y\", 14, blue end md\"\"\" We expect most around 95% of the data to be contained within the interval \\mu \\pm 2 \\sigma , where \\mu is the mean and \\sigma is the standard deviation. This assumes that the data is normally distributed , which is not actually the case for the data generated above. \"\"\" md\" Correlated data\" md\"\"\" However, from the figure it is clear that x and y are not the \"correct directions\" to use for this data set. It would be more natural to think about other directions the direction in which the data set is mainly pointing roughly, the direction in which it's longest , together with the approximately perpendicular direction in which it is narrowest. We need to find from the data which directions these are, and the extent width of the data cloud in those directions. However, we cannot obtain any information about those directions by looking separately at x coordinates and y coordinates, since within the same bounding boxes that we just calculated the data can be distributed in many different ways. Rather, the information that we need is encoded in the relationship between the values of x i and y i for the points in the data set . For our data set, when x is large and negative, y is also rather negative when x is 0, y is near 0 , and when x is large and positive, so is y . We say that x and y are correlated literally they are mutually \"co\" related, such that knowing some information about one of them allows us to predict something about the other. For example, if I measure a new data point from the same process and I find that x is around 0.25 then I would expect y to be within the range 0.05 to 0.2 , and it would be very surprising if y were 0.5. \"\"\" md\"\"\" Although there are standard methods to calculate this correlation, we prefer to hone our intuition using computational thinking instead \"\"\" md\"\"\" We want to think about different directions , so let's introduce an angle \\theta to describe the direction along which we are looking. We want to calculate the width of the cloud along that direction . Effectively we are changing coordinates to a new coordinate, oriented along the line. To do this requires more linear algebra than we are assuming in this course, but let's see what it looks like \"\"\" md\" Rotating the axes\" md\"\"\"By rotating the axes we can \"look in different directions\" and calculate the width of the data set \"along that direction\". What we are really doing is a perpendicular projection of the data onto that direction.\"\"\" M xs centered ys centered ' eigvals cov M' . 199 imax argmax M 1, svdvals M M gr R θ cos θ sin θ sin θ cos θ md\"In the following figure, we are rotating the axis red arrow around in the left panel. In the right panel we are viewing the data from the point of view of that new coordinate direction, in other words projecting onto that direction, effectively as if we rotated our head so the red vector was horizontal \" md\"\"\" degrees bind degrees Slider 0 360, default 28, show value true \"\"\" md\"\"\" Rotating the data \"\"\" θ π degrees 180 radians p1 begin scatter M 1, , M 2, , ratio 1, leg false, ms 2.5, alpha 0.5, framestyle origin projected cos θ sin θ M . cos θ sin θ ' scatter projected 1, , projected 2, , m 3, alpha 0.1, c green lines x reduce vcat, M 1, i , projected 1, i , NaN for i in 1 size M, 2 lines y reduce vcat, M 2, i , projected 2, i , NaN for i in 1 size M, 2 for i in 1 size M, 2 plot M 1, i , projected 1, i , M 2, i , projected 2, i , ls dash, c black, alpha 0.1 end plot lines x, lines y, ls dash, c black, alpha 0.1 plot 0.7 . cos θ , sin θ , 0.7 . cos θ , sin θ , lw 1, arrow true, c red, alpha 0.3 xlims 0.7, 0.7 ylims 0.7, 0.7 scatter M 1, imax , M 2, imax , ms 3, alpha 1, c yellow annotate 0, 1.2, text \"align arrow with cloud\", red, 10 end p1 p2 begin M2 R θ M scatter M2 1, , M2 2, ,ratio 1, leg false, ms 2.5, alpha 0.3, framestyle origin, size 500, 500 plot 0.6, 0 , 0.6, 0 , lw 3, arrow true, c red, xaxis false, yaxis false, xticks , yticks scatter M2 1, imax , M2 2, imax , ms 3, alpha 1, c yellow xlims 0.7, 0.7 ylims 0.7, 0.7 scatter M2 1, , zeros size xs centered , ms 3, alpha 0.1, ratio 1, leg false, framestyle origin, c green lines2 x reduce vcat, M2 1, i , M2 1, i , NaN for i in 1 size M2, 2 lines2 y reduce vcat, M2 2, i , 0, NaN for i in 1 size M2, 2 for i in 1 size M2, 2 plot M2 1, i , M2 2, i , M2 1, i , 0 , ls dash, c black, alpha 0.1 end plot lines2 x, lines2 y, ls dash, c black, alpha 0.1 σ std M2 1, vline 2σ, 2σ , ls dash, lw 2 plot 0.5 cos θ , cos θ , 0.5 sin θ , sin θ , c black, alpha 0.5, lw 1, arrow true plot 0.5 sin θ , sin θ , 0.5 cos θ , cos θ , c black, alpha 0.5, lw 1, arrow true plot 0.5 cos θ , cos θ , 0.5 sin θ , sin θ , c black, alpha 0.5, lw 1, arrow true plot 0.5 sin θ , sin θ , 0.5 cos θ , cos θ , c black, alpha 0.5, lw 1, arrow true title \"σ round σ, digits 4 \" annotate 2σ 0.05, 0.05, text \"2σ\", 10, green annotate 2σ 0.05, 0.05, text \" 2σ\", 10, green end plot p2 md\"\"\" We see that the extent of the data in the direction \\theta varies as \\theta changes. Let's plot the variance in direction \\theta as a function of \\theta \"\"\" begin variance θ var R θ M 1, variance θ AbstractArray variance θ 1 end p3 begin plot 0 360, variance. range 0, 2π, length 361 , leg false, size 400, 200 scatter degrees , σ^2 xlabel \"θ\" ylabel \"variance in direction θ\" end σs svdvals M variances σs.^2 . 199 1 . σs M md\"\"\"The direction in which the variance is maximised gives the most important direction It is the direction along which the data \"points\", or the direction which best distinguishes different data points. This is often called the first principal component in statistics, or the first singular vector in linear algebra. We can also now quantify how close the data is to lying along that single line, using the width in the perpendicular direction if that width is \"much smaller\" than the width in the first principal direction then the data is close to being rank 1. \"\"\" md\"The simplest way to maximise this function is to evaluate it everywhere and find one of the places where it takes the maximum value \" begin θs 0 0.01 2π fs variance. θs θmax θs argmax fs θmin θs argmin fs fmax variance θmax fmin variance θmin end begin scatter xs centered, ys centered, ms 5, alpha 0.3, ratio 1, leg false, framestyle origin plot 0, 0 , 2 sqrt fmax . cos θmax , sin θmax , arrow true, lw 3, c red plot 0, 0 , 2 sqrt fmin . cos θmin , sin θmin , arrow true, lw 3, c red end md\"\"\" Note that the directions that maximise and minimise variance are perpendicular. This is always the case, as shown using the Singular Value Decomposition SVD in linear algebra. There are different ways to think about this procedure. We can think of it as effectively \"fitting an ellipse\" to the data, where the widths of the ellipse axes show the relative importance of each direction in the data. Alternatively we can think of it as fitting a multivariate normal distribution by finding a suitable covariance matrix. \"\"\" begin circle cos. θs sin. θs ' stretch 2 sqrt fmax 0 0 2 sqrt fmin ellipse R θmax stretch circle plot ellipse 1, , ellipse 2, , series shape, alpha 0.4, fill true, c orange end md\"\"\" Note also that an ellipse is the image of the unit circle under a linear transformation. We are effectively learning what the best linear transformation is that transforms the unit circle into our data cloud. \"\"\" md\" Higher dimensions\" md\"\"\" Can we generalise this to dimensions higher than 2? Let's think about 3D. If we take columns of the first three rows of the original image, we have vectors in 3D. If we plot these vectors in 3D, a rank 1 matrix will give a straight line in 3D, while a rank 2 matrix will give a plane in 3D. Rank 2 noise gives a noisy cloud lying close to a plane. Similarly to what we did above, we need to calculate the ellipsoid that best fits the data. The widths of the axes of the ellipsoid tell us how close to being a line or a plane rank 1 or rank 2 the data is. \"\"\" md\"In more than 3D we can no longer visualise the data, but the same idea applies. The calculations are done using the SVD. If the widths of the ellipsoid in some directions are very small, we can ignore those directions and hence reduce the dimensionality of the data, by changing coordinates to the principal components.\" md\"\"\" What is the Singular Value Decomposition SVD ? \"\"\" md\"\"\" The Singular Value Decomposition SVD is a way of writing any matrix in terms of simpler matrices. Thinking in terms of linear transformations, any linear transformation T has the same effect as a sequence of three simple transformations T rotation₂ ∘ stretch ∘ rotation₁ In terms of matrices, for any matrix M it is possible to write M U \\, \\Sigma \\, V^\\text T where U and V are orthogonal matrices, i.e. they satisfy U U^\\text T I and V V^\\text T I , and \\Sigma is a diagonal matrix. Orthogonal matrices have determinant \\pm 1 , so they leave areas unchanged. They are rotations, possibly combined with reflections. There are algorithms from numerical linear algebra to calculate this decomposition. In Julia we call the `svd` function, e.g. \"\"\" let M 2 1 1 1 svd M end md\"\"\" Let's look at the action of the matrix on the unit disc. To generate points in the unit disc we generate points in 1, 1 ^2 and reject those lying outside \"\"\" begin unit disc 1.0 . 2.0 . rand 2 for i in 1 2000 unit disc reduce hcat, x for x in unit disc if x 1 ^2 x 2 ^2 1 end scatter unit disc 1, , unit disc 2, , ratio 1, leg false, alpha 0.5, ms 3 md\"\"\" t bind tt Slider 0 0.01 1, show value true \"\"\" pp1 begin scatter unit disc 1, , unit disc 2, , ratio 1, leg false, alpha 0.5, title \"stretch rotate\" result 1 tt tt tt 1 unit disc scatter result 1, , result 2, , alpha 0.2 ylims 3, 3 xlims 3, 3 end pp2 begin UU, Sigma, VV svd 1 tt tt tt 1 scatter unit disc 1, , unit disc 2, , ratio 1, leg false, alpha 0.5, title \"stretch\" result2 Diagonal Sigma unit disc scatter result2 1, , result2 2, , alpha 0.2 ylims 3, 3 xlims 3, 3 end plot pp2, pp1 md\"\"\" Rotations in 300 dimensions \"\"\" md\"\"\" We have been thinking of 300 points in 2 dimensions. Instead, we could think about 2 points in 300 dimensions . In some sense, this is what \"transpose really does\" \"\"\" md\"\"\" The V in the SVD is another rotation that we \"cannot see\" in the above pictures. In our case it is a rotation in 300 dimensions But in fact we can visualise it as follows, where we multiply our data by a 300 \\times 300 orthogonal matrix We first take a random anti symmetric matrix, i.e. one for which M M^\\text t . We then turn that into an orthogonal matrix by taking the so called matrix exponential . \"\"\" begin dim size M, 2 anti symmetric randn dim, dim anti symmetric anti symmetric' end bind t Slider 0 0.0002 1, show value true, default 0.0 begin M rotated M exp t anti symmetric scatter M rotated 1, , M rotated 2, , leg false, alpha 0.5, scatter M 1, , M 2, , alpha 0.5 ylims 0.3, 0.3 xlims 0.6, 0.6 end md\"\"\" We see that the data rotates around in 300 dimensions, but always is projected to the same ellipse. \"\"\" U, Σ, V svd M, full true M18 M V scatter M18 1, , M18 2, , alpha 0.5, leg false, ratio 1, xlim 5, 5 md\" Appendix\" begin show image M get. Ref ColorSchemes.rainbow , M . maximum M show image x AbstractVector show image x' end show image flag show image flag2 show image image begin image2 outer 1 0.4 rand 50 , rand w outer rand 52 , rand w show image image2 end show image image show image noisy image show image image 1 2, 1 20 loss M1, M2 sum M1 i M2 i ^2 for i in 1 length M1 if ismissing M2 i function split up v, m, n return v 1 m , v m 1 m n , v m n 1 2m n , v 2m n 1 2m 2n end function ff v, m, n v1, w1, v2, w2 split up v, m, n loss outer v1, w1 outer v2, w2 , M3 end ff2 v ff v, m, n "},{"url":"data_science/random_variables_as_types/","title":"Random Variables as Types","tags":["lecture","module2","track_julia","type","programming","probability","interactive","random","track_math","Symbolics"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v xKAO38UsXo0\" image \"https user images.githubusercontent.com 6933510 136196570 478bbb89 05fb 4799 99a0 0ede06354cb6.png\" section 4 order 4 title \"Random Variables as Types\" layout \"layout.jlhtml\" youtube id \"xKAO38UsXo0\" description \"\" tags \"lecture\", \"module2\", \"track julia\", \"type\", \"programming\", \"probability\", \"interactive\", \"random\", \"track math\", \"Symbolics\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Statistics, Plots, PlutoUI, LinearAlgebra, Symbolics TableOfContents aside true md\"\"\" Concepts for today We won't be introducing any new Julia functions in this lecture, but we are going to show off a key way in which Julia really shines its type system . With a few carefully chosen definitions, we can gain a great deal of power Concepts for today Types for code organisation of abstract concepts Abstract types Subtypes Building up expressions \"under the hood\" using types `sum` over an iterator generator expression \"\"\" md\"\"\" Random variables as types \"\"\" md\"\"\" This lecture might appear to be about random variables and it is . But we would be thrilled if you see it rather as a more general discussion on software engineering and abstractions, since the principles involved extend to many different contexts. \"\"\" md\"\"\" Random variables \"\"\" md\"\"\" Recall that a random variable X is an object which has different possible outcomes x , to which we assign probabilities \\mathbb P X x . The correspondence of probabilities to outcomes is called the probability distribution of the random variable. \"\"\" md\"\"\" Gaussian distributions \"\"\" md\"\"\" Let's remind ourselves of a very important type of distribution, namely the Gaussian distribution with mean \\mu and standard deviation \\sigma or variance \\sigma^2 . We can sample from a Gaussian distribution with mean 0 and variance 1 with the `randn` function short for \"random normal\" . We can then shift and scale to get the Gaussian distribution that we want \"\"\" md\"\"\" μ bind μ Slider 3 0.01 3, show value true, default 0.0 σ bind σ Slider 0.01 0.01 3, show value true, default 1.0 \"\"\" bell curve x exp x^2 2 √ 2π bell curve x, μ, σ bell curve x μ σ σ bell curve 0, 3, 2 md\"\"\" Sum of two Gaussians \"\"\" md\"\"\" Let's try to add two Gaussians. From the empirical experimental point of view the sum of two random variables is easy to compute Just sample both of them and add the resulting values to get the value of the random variable that is their sum \"\"\" md\"\"\" We see that we get back a Gaussian again In fact, the sum of two Gaussians with means \\mu 1 and \\mu 2 , and variances \\sigma 1^2 and \\sigma 2^2 , is a Gaussian again with mean \\mu 1 \\mu 2 and variance \\sigma 1^2 \\sigma 2^2 . Below we will see how to encode this fact computationally. \"\"\" md\"\"\" Theoretical random variables vs. sampling What should a theoretical random variable be able to do? What should we be able to do with sampling? Naming , e.g. `Gaussian` Parameters , e.g. \\mu and \\sigma^2 Theoretical Theoretical mean Theoretical variance Theoretical sum of two random variables Theoretical product of two random variables Probability distribution Sampling Sample a random variable Sample mean Sample variance Sample sum Histogram \"\"\" md\"\"\" Why define a type at all? \"\"\" md\"\"\" How can we represent a random variable in software? In some languages there are different names for the various functions associated to a random variable, but no name for the random variable itself For example, in R there is a standard naming convention in R, with `d` for density, etc., followed by the name like `norm` for normal. The indicators are `d` for the density `p` for the distribution function `q` for the quantile function `r` for generating random variates See e.g. Normal distribution in R https www.rdocumentation.org packages stats versions 3.6.2 topics Normal Chi squared distribution in R https www.rdocumentation.org packages stats versions 3.6.2 topics Chisquare What's wrong with this? All these functions are referring to an underlying random variable or probability distribution , which you will find in any course in probability, and yet there's no way to refer to the underlying mathematical object \"\"\" md\"\"\" Instead, we would like to be able to refer to the random variable or probability distribution itself. We should be able to provide a type with the name and parameters of a random variable, but not yet specify how to generate random instances variates ? It turns out that this is a very good example of thinking ahead by providing an abstraction . We can later provide a means for random sampling and even, if a new algorithm comes along that is more efficient, we can replace it some day \"\"\" md\"\"\" Defining abstract types for random variables \"\"\" md\"\"\" We define an abstract type using `abstract type Name end`, where ` Name ` is replaced with the name of the type. We can think of an abstract type as being a collection of types that share a particular property. In our case, we want to create a type to represent \"any random variable\", and also the sub types \"any continuous valued random variable\" and \"any discrete valued random variable\". This will allow us to specify later on whether a given concrete i.e. particular random variable is discrete or continuous. We use ` ` to denote sub type \"\"\" begin abstract type RandomVariable end abstract type DiscreteRandomVariable RandomVariable end abstract type ContinuousRandomVariable RandomVariable end end md\"\"\" Defining a type for a Gaussian random variable \"\"\" md\"\"\" Let's start off by looking at Gaussian random variables, also called normal random variables. We reminded ourselves above what they look like. \"\"\" md\"\"\" Name and parameters \"\"\" begin struct Gaussian ContinuousRandomVariable μ mean σ² variance end Gaussian Gaussian 0.0, 1.0 normalised Gaussian with mean 0 and variance 1 end G Gaussian 1, 2 md\"\"\" Note that here we have created a Gaussian random variable with given parameter values, without sampling from it. \"\"\" md\"\"\" Theoretical mean and variance \"\"\" md\"\"\" Now we can extend the `mean`, `var` variance and `std` standard deviation functions from the `Statistics` library to act on this object \"\"\" begin Statistics.mean X Gaussian X.μ Statistics.var X Gaussian X.σ² end md\"\"\" Planning ahead Standard deviation for any random variable, not just Gaussians \"\"\" md\"\"\" Once we have defined the variance, we know how to calculate the standard deviation it's just the square root of the variance. But, thinking ahead, this is true for any random variable, so we can define it to act on any random variable that we will define later \"\"\" md\"\"\" This is an example of good software design. \"\"\" md\"\"\" Sum of two Gaussian random variables \"\"\" md\"\"\" Gaussians have a special property the sum of two Gaussians is always a Gaussian. We say that Gaussians are stable distributions there are others https en.wikipedia.org wiki Stable distribution . Note that we don't need random samples for this theoretical observation, embodied in the following code \"\"\" Base. X Gaussian, Y Gaussian Gaussian X.μ Y.μ, X.σ² Y.σ² begin G1 Gaussian 0, 1 G2 Gaussian 5, 6 end md\"\"\" The theoretical product of two Gaussians is not Gaussian we will do the general case later. \"\"\" md\"\"\" Probability distribution of a Gaussian \"\"\" md\"\"\" A Gaussian random variable is a continuous random variable, i.e. it has a continuous range of possible outcomes. The possible range of outcomes is called the support of the distribution. For a Gaussian it is the whole real line, \\infty, \\infty . \"\"\" md\"\"\" One way to specify a continous random variable X is via its probability density function , or PDF , f X . The probability that X lies in the interval a, b is given by an area under the curve f X x from a to b \\mathbb P X \\in a, b \\int a ^b f X x \\, dx. \"\"\" md\"\"\" For a Gaussian distribution with mean \\mu and variance \\sigma^2 , the PDF is given by f X X \\frac 1 \\sqrt 2\\pi \\sigma^2 \\exp \\left \\frac 1 2 \\left \\frac x \\mu \\sigma \\right ^2 \\right . \"\"\" pdf X Gaussian x exp 0.5 x X.μ ^2 X.σ² √ 2π X.σ² pdf G pdf Gaussian 0.0 md\"\"\" μ bind μμ Slider 3 0.01 3, show value true, default 0.0 σ bind σσ Slider 0.01 0.01 3, show value true, default 1.0 \"\"\" begin plot pdf Gaussian μμ, σσ , leg false xlims 6, 6 ylims 0, 0.5 end md\"\"\" Sampling from a Gaussian distribution \"\"\" md\"\"\" We can also specify how to sample from a Gaussian distribution. We can re purpose `rand` for this \"\"\" md\"\"\" More general distributions \"\"\" md\"\"\" Let's recall the Bernoulli distribution from last lecture. This represents a weighted coin with probability p to come up \"heads\" 1 , and probability 1 p to come up \"tails\" 0 . Note that this is a discrete random variable the possible outcomes are the discrete values 0 and 1 . \"\"\" struct Bernoulli DiscreteRandomVariable p Float64 end B Bernoulli 0.25 md\"\"\" Again we can specify the theoretical mean and variance \"\"\" begin Statistics.mean X Bernoulli X.p Statistics.var X Bernoulli X.p 1 X.p end md\"\"\" And again `std` just works \"\"\" md\"\"\" Finally we specify how to sample \"\"\" Base.rand X Bernoulli Int rand X.p md\"\"\" Adding two random variables \"\"\" md\"\"\" What happens if we add two Bernoulli random variables? There are two routes we could go We could use the known theoretical sum, or we could write a general purpose tool. Let's do the latter. \"\"\" md\"\"\" When we add two Bernoulli random variables we do not get a Bernoulli back. To see this it's enough to observe that the sum can have the outcome 2, which is impossible for a Bernoulli. So the result is just the random variable \"the sum of these two given random variables\". In general it won't even have a common name. So we actually need to define a new type to represent the \"sum of two given random variables\", which itself will be a random variable \"\"\" struct SumOfTwoRandomVariables RandomVariable X1 RandomVariable X2 RandomVariable end begin B1 Bernoulli 0.25 B2 Bernoulli 0.6 end md\"\"\" Now we can define the sum of two random variables of any type \"\"\" Base. X1 RandomVariable, X2 RandomVariable SumOfTwoRandomVariables X1, X2 data μ . σ . randn 10^5 transform standard normal begin data1 4 . sqrt 0.3 . randn 10^5 data2 6 . sqrt 0.7 . randn 10^5 total data1 data2 end G1 G2 Base.rand X Gaussian X.μ √ X.σ² randn md\"\"\" For example, let's sum two Bernoullis \"\"\" B1 B2 md\"\"\" However, for the special case of Gaussians we still get the correct result we have not overwritten the previous definition \"\"\" G1 G2 md\"\"\" Now we need to define the various functions on this type representing a sum \"\"\" Statistics.mean S SumOfTwoRandomVariables mean S.X1 mean S.X2 Statistics.mean total mean G mean G1 G2 mean G1 mean G2 mean B1 B2 md\"\"\" To have a simple equation for the variance, we need to assume that the two random variables are independent . Perhaps the name should have been `SumOfTwoIndependentRandomVariables`, but it seems too long. \"\"\" Statistics.var S SumOfTwoRandomVariables var S.X1 var S.X2 var G Statistics.std X RandomVariable sqrt var X Statistics.std total std G mean B , var B , std B md\"\"\" How can we sample from the sum? It's actually easy \"\"\" Base.rand S SumOfTwoRandomVariables rand S.X1 rand S.X2 md\"\"\" Now it's easy to look at the sum of a Bernoulli and a Gaussian. This is an example of a mixture distribution https en.wikipedia.org wiki Mixture distribution . \"\"\" md\"\"\" Let's extend the `histogram` function to easily draw the histogram of a random variable \"\"\" md\"\"\" Now... What if we sum more random variables? \"\"\" mixture Bernoulli 0.25 Bernoulli 0.75 Gaussian 0, 0.1 md\"\"\" Generic programming `sum` \"\"\" md\"\"\" Now we have defined ` `, Julia's generic definition of `sum` can kick in to define the sum of many random variables \"\"\" S sum Bernoulli 0.25 for i in 1 30 md\"\"\" Note that we do not need the ` ... ` in the following expression. There is no need to actually create an array of random variables instead we are using an iterator or generator expression \"\"\" mean S var S md\"\"\" This is a big deal Everything just works. By the way, the sum of n Bernoulli random variables with the same probability p is called a binomial random variable with parameters n, p . \"\"\" md\"\"\" If we were worried about performance, we would probably want to define a separate `Binomial` type, rather than using nested sums of Bernoullis. You will do this in the homework. \"\"\" md\"\"\" χ₁² distribution \"\"\" md\"\"\" Another well known distribution is the chi squared distribution, which is defined as the sum of squares of Gaussians. Chi 1 squared \\chi 1^2 is the square of a single Gaussian \"\"\" struct ChiSquared1 ContinuousRandomVariable end Base.rand X ChiSquared1 rand Gaussian ^2 histogram rand Gaussian μμ, σσ for i in 1 10^4 , alpha 0.5, norm true Plots.histogram X RandomVariable kw... histogram rand X for i in 1 10^6 , norm true, leg false, alpha 0.5, size 500, 300 , kw... begin histogram data, alpha 0.2, norm true, bins 100, leg false, title \"μ μ , σ σ \", size 500, 300 xlims 6, 6 ylims 0, 0.7 xs μ σ, μ, μ σ plot 6 0.01 6, x bell curve x, μ, σ , lw 2 plot μ σ 0.01 μ σ , x bell curve x, μ, σ , fill true, alpha 0.5, c purple plot μ, μ , 0.05, bell curve μ, μ, σ , ls dash, lw 2, c white annotate μ, 0.03, text \"μ\", white annotate μ σ, 0.03, text \"μ σ\", yellow annotate μ, 0.03, text \"μ\", white end histogram total, alpha 0.5, leg false, norm true, size 500, 300 histogram Bernoulli 0.25 Bernoulli 0.75 histogram Bernoulli 0.25 Gaussian 0, 0.1 histogram mixture histogram S rand mixture rand S histogram ChiSquared1 md\"\"\" We can get the \\chi n^2 distribution by summing several \\chi 1^2 \"\"\" histogram sum ChiSquared1 for i in 1 4 md\"\"\" Using symbolics \"\"\" md\"\"\" We can even do some of this symbolically , e.g. \"\"\" variables μ₁, σ₁², μ₂, σ₂² introduce symbolic variables from Symbolics.jl Gaussian μ₁, σ₁² Gaussian μ₂, σ₂² Gaussian 17, 3 Gaussian μ₂, σ₂² "},{"url":"data_science/random_vars/","title":"Sampling and Random Variables","tags":["lecture","module2","track_data","track_math","random","statistics","track_julia","image","probability","plotting","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v 7HrpoFZzITI\" image \"https user images.githubusercontent.com 6933510 136196576 70e45c9d ef0e 4498 bf61 58d9ae854c3e.png\" section 2 order 2 title \"Sampling and Random Variables\" layout \"layout.jlhtml\" youtube id \"7HrpoFZzITI\" description \"\" tags \"lecture\", \"module2\", \"track data\", \"track math\", \"random\", \"statistics\", \"track julia\", \"image\", \"probability\", \"plotting\", \"interactive\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import ImageMagick using Plots, PlutoUI, Colors, Images, StatsBase, Distributions using Statistics end PlutoUI.TableOfContents aside true md\"\"\" Julia Useful tidbits \"\"\" md\"\"\" The package that each function comes from is shown in brackets, unless it comes from `Base`. \"\"\" md\"\"\" Julia `Base` library no `using` required `if...else...end` `Dict` Julia's dictionary type `÷` or `div` integer division type `\\div` tab `sum S ` sum of elements in the collection `S`, e.g. an array `count S ` count the number of true elements of a Boolean collection `rand S ` random sampling from a collection `S` `Statistics.jl` pre loaded standard library just needs `using` `mean S ` calculate the mean of a collection `S` `std S ` calculate the standard deviation of a collection `S` `StatsBase.jl` `countmap` `Plots.jl` `histogram x ` Plot a histogram of data vector `x` Plots `bar d ` Plot a bar graph of categorical data Plots `Colors.jl` `distinguishable colors n ` Make `n` distinguishable colours Colors \"\"\" md\"\"\" Random sampling with `rand` \"\"\" md\"\"\" The `rand` function in Julia is quite versatile it tries to generate, or sample , a random object from the argument that you pass in \"\"\" rand 1 6 rand 2, 3, 5, 7, 11 rand \"MIT\" rand 'a' 'z' typeof 'a' 'z' rand random number between 0 and 1 rand methods rand md\"\"\" We can take random objects from a collection of objects of any type, for example \"\"\" distinguishable colors 10 typeof distinguishable colors 10 rand distinguishable colors 3 from Colors.jl package md\"\"\" Several random objects \"\"\" md\"\"\" To sample several random objects from the same collection, we could write an array comprehension \"\"\" rand 1 6 for i in 1 10 md\"\"\" But in fact, just adding another argument to `rand` does the trick \"\"\" rand 1 6, 10 md\"\"\" In fact, you can also generate not only random vectors, but also random matrices \"\"\" rand 1 6, 10, 12 rand distinguishable colors 5 , 10, 10 md\"\"\" We can also use random images \"\"\" penny image load download \"https www.usacoinbook.com us coins lincoln memorial cent.jpg\" begin head penny image , 1 end÷2 tail penny image , end÷2 end end rand head, tail , 5, 5 md\"\"\" Uniform sampling \"\"\" md\"\"\" So far, each use of `rand` has done uniform sampling , i.e. each possible object as the same probability. Let's count heads and tails using the `countmap` function from the `StatsBase.jl` package \"\"\" tosses rand \"head\", \"tail\" , 10000 toss counts countmap tosses \"tail\" 3 typeof \"tail\" 3 toss counts \"tail\" md\"\"\" We see that `countmap` returns a dictionary `Dict` , which maps keys to values we will say more about dictionaries in another chapter. \"\"\" prob tail toss counts \"tail\" length tosses md\"\"\" As we increase the number of tosses, we \"expect\" the probability to get closer to 1 2 . \"\"\" md\"\"\" Tossing a weighted coin \"\"\" md\"\"\" How could we model a coin that is weighted , so that it is more likely to come up heads? We want to assign a probability p 0.7 to heads, and q 0.3 to tails. \"\"\" md\"\"\" One way would be to generate random integers between 1 and 10 and assign heads to a subset of the possible results with the desired probability, e.g. 1 7 get heads, and 8 10 get tails \"\"\" function simple weighted coin outcome if rand 1 10 ≤ 7 \"heads\" else could have elseif \"tails\" end return outcome end function simple weighted coin2 if rand 1 10 ≤ 7 \"heads\" else could have elseif \"tails\" end end result for i in 1 10 end result nothing simple weighted coin2 md\"\"\" Note that `if` statements have a return value in Julia. \"\"\" md\"\"\" How could we generalise this to an arbitrary probability p ∈ 0, 1 ? We can generate a uniform floating point number between 0 and 1, and check if it is less than p . This is called a Bernoulli trial . \"\"\" rand rand 0.314159 md\"\"\" Note that comparisons also return a value in Julia. Here we have switched from heads tails to true false as the output. \"\"\" md\"\"\" Let's make that into a function \"\"\" bernoulli p rand p bernoulli 0.7 md\"\"\" p bind p Slider 0.0 0.01 1.0, show value true, default 0.7 \"\"\" countmap bernoulli p for i in 1 1000 md\"\"\" Bar charts and histograms \"\"\" md\"\"\" Once we have generated several random objects, it is natural to want to count how many times each one occurred. \"\"\" md\"\"\" Let's roll a fair die 1000 times \"\"\" md\"\"\" An obvious way to find the counts would be to run through the data looking for 1s, then run through again looking for 2s, etc. \"\"\" md\"\"\" Note that this is not the most efficient algorithm \"\"\" md\"\"\" We can plot categorical data using a bar chart , `bar` in Plots.jl. This counts each discrete item. \"\"\" rolls rand 1 6, 100000 try modifying 100 by adding more zeros rolls . 1 count rolls . 1 counts count rolls . i for i in 1 6 begin bar counts, alpha 0.5, leg false, size 500, 300 hline length rolls 6 , ls dash title \"number of die rolls length rolls \" ylims 0, length rolls 3 end md\"\"\" Probability densities Rolling multiple dice \"\"\" roll dice n sum rand 1 12, n trials 10^6 md\"\"\" Converging shape \"\"\" md\"\"\" n bind n Slider 1 50, show value true \"\"\" experiment roll dice n experiment sum randn ^2 for i in 1 n data experiment for t in 1 trials data histogram data, alpha 0.5, legend false, bins 200, c lightsalmon1, title \"n n\" c RGB 0.1, 0.2, 0.3 md\"\"\" Here we have switched from a bar chart to a histogram , which counts the number of items falling into a given range or bin . When n is small this tends to look like a bar chart, but it looks like a \"smooth bar chart\" as n gets larger, due to the aggregation . \"\"\" md\"\"\" Does the above histogram look like a bell to you? \"\"\" begin bell load download \"https encrypted tbn0.gstatic.com images?q tbn ANd9GcRmxRAIQt L X99A 4FoP3vsC l WHlC3TtAw&usqp CAU\" bell 1 end 9÷10, end md\"\"\" Normalising the y axis \"\"\" md\"\"\" Notice that the shape of the curve seems to converge to a bell shaped curve, but the axes do not. What can we do about this? We need to normalise so that the total shaded area is 1. We can use the histogram option `norm true` to do so. \"\"\" histogram data, alpha 0.5, legend false, bins 50, norm true, c lightsalmon1, title \"n n\", ylims 0, 0.05 md\"\"\" Normalising the x axis \"\"\" md\"\"\" As we changed n above, the range of values on the x axis also changed. We want to find a way to rescale x at the same time so that both axes and shape stop changing as we increase n . We need to make sure that the data is centred in the same place we will choose 0. And we need to make sure that the width is the same we will divide by the standard deviation. \"\"\" σ std data mean data , std data normalised data data . mean data . std data histogram normalised data, bins 5 1 2σ 1 σ 5, norm true, alpha 0.5, leg false, ylim 0, 0.41 , size 500, 300 plot x exp x^2 2 √ 2π , lw 2, c red, alpha 0.5 md\"\"\" Note that in the limit, the data becomes continuous , no longer discrete. The probability of any particular value is 0 We then talk about a probability density function , f x the integral of this function over the interval a, b gives the probability of being in that interval. \"\"\" md\"\"\" Options for plotting functions \"\"\" md\"\"\" Other options for the `histogram` function options `legend false` turns off the legend key , i.e. the plot labels `bins 50` specifies the number of bins can also be a vector of bin edges `linetype stephist` use steps instead of bars `alpha` a general plot option specifying transparency 0 invisible 1 opaque `c` or `color` a general plot option for the colour `lw` line width default 1 There are several different ways to specify colours. Here http juliagraphics.github.io Colors.jl stable namedcolors is a list of named colours, but you can also specify `RGB 0.1, 0.2, 0.3 `. \"\"\" md\"\"\" Note that `linetype stephist` will give a stepped version of the histogram \"\"\" md\"\"\" Sampling from other distributions \"\"\" md\"\"\" dof bind dof Slider 1 50, show value true \"\"\" chisq data rand Chisq dof , 100000 histogram chisq data, norm true, bins 100, size 500, 300 , leg false, alpha 0.5, xlims 0, 10 √ dof histogram sum randn .^2 for 1 dof for 1 100000 , norm true, alpha 0.5, leg false "},{"url":"data_science/random_walks backup 1/","title":"Random Walks","tags":["lecture","module2","programming","track_julia","plotting","structure","type","interactive","random","statistics","track_math","track_data"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v 14hHtGJ4s g\" image \"https user images.githubusercontent.com 6933510 136196563 f4b5b44c 5252 4e67 8c82 c550de891c55.png\" section 5 order 5 title \"Random Walks\" layout \"layout.jlhtml\" youtube id \"14hHtGJ4s g\" description \"\" tags \"lecture\", \"module2\", \"programming\", \"track julia\", \"plotting\", \"structure\", \"type\", \"interactive\", \"random\", \"statistics\", \"track math\", \"track data\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, BenchmarkTools TableOfContents aside true md\"\"\" Julia concepts Benchmarking BenchmarkTools.jl Plotting in a loop Generic programming Mutable vs immutable structs Vectors of vectors Aliasing of memory `cumsum` \"\"\" md\"\"\" Motivation Dynamics of hard discs \"\"\" md\"\"\" Brown observed Brownian motion in 1827 Large particles like sand or pollen in water move around seemingly at random. Einstein explained this in 1905 as repeated impacts with water molecules. We can visualise that with a simulation of hard discs bouncing off one another. Even though the dynamics is not random each disc follows Newton's laws if we just look at a single one of them it looks random. \"\"\" md\"\"\" Visualising random walks \"\"\" md\"\"\" A random walk models random motion in time and space. At each time step an object moves in a random direction. Let's visualise the result in 2 dimensions. \"\"\" md\"\"\" N bind N Slider 1 6, show value true, default 1 \"\"\" md\"\"\" t bind t Slider 1 10^N, show value true, default 1 \"\"\" md\"\"\" We see that the dynamics closely resembles, at least qualitatively, that of the hard disc. \"\"\" md\"\"\" Why use random walks? Why should we model with random processes? Either we need to get a lot of data to precisely characterise a system, or we assume it's random It may well be that can have all the details, but by making a simplifying assumption we actually gain more understanding . Examples Stock price going up and down Pollutants getting dispersed in the air Neutral genes moving through a population \"\"\" md\"\"\" Simple random walk The simplest version is called simple random walk one object jumps on the integers at each time step it jumps left or right. Each such step is a new random variable, taking values \\pm 1 at each step, each with probability 1 2 in the simplest case . We can think of this as a scaling of a Bernoulli random variable. In order to simulate this, we need to know how to generate the jumps. \"\"\" md\"\"\" Julia Benchmarking \"\"\" md\"\"\" There are various ways we could generate random values \\pm 1 . Let's use this as an opportunity to learn about benchmarking in Julia, i.e. measuring run time to compare performance. In this case we will do \"micro benchmarks\", which try to compare tiny pieces of code which we intend to run many millions of times, so that small differences in run time can be significant. The `BenchmarkTools.jl` package provides relatively easy to use tools for measuring this, by running the code many times and calculating statistics. The ` btime` macro is a simple way to estimate actual running time. Each option should be enclosed in a function. \"\"\" md\"\"\" Here are a few different ways we could generate random steps \"\"\" begin step1 rand 1, 1 step2 2 rand 0.5 1 step3 2 rand Bool 1 step4 sign randn end with terminal do btime step1 btime step2 btime step3 btime step4 end md\"\"\" Trajectory of a random walk \"\"\" md\"\"\" We can now calculate the trajectory of a 1D random walk as it takes several steps. It starts at an initial position, for example, 0, and takes consecutive steps \"\"\" function walk1D N x 0 xs x for i in 1 N x step1 push xs, x end return xs end begin plot for i in 1 10 plot walk1D 100 , leg false, size 500, 300 , lw 2, alpha 0.5 end plot end md\"\"\" Making it more general Random walks using types \"\"\" md\"\"\" Now suppose we want to think about more general random walks, for example moving around in 2D. Then we need to generalise the above function. Based on our experience from last time, you should suspect that a good way to do this is with types . We will define \"\"\" abstract type Walker end struct Walker1D Walker pos Int end position w Walker w.pos step w Walker1D rand 1, 1 struct Walker2D Walker x Int y Int end position w Walker2D w.x, w.y update w W, step where W Walker W position w step step w Walker2D rand 1, 0 , 0, 1 , 1, 0 , 0, 1 update w Walker2D, step Vector Walker2D w.x step 1 , w.y step 2 function trajectory w W, N where W W is a type parameter ws position w for i in 1 N pos position w w update w, step w push ws, position w end return ws end trajectory Walker1D 0 , 10 traj trajectory Walker2D 0, 0 , 10^N begin plot traj 1 t , ratio 1, leg false, alpha 0.5, lw 2 scatter traj 1 , traj t , c red, green xlims minimum first. traj 1, maximum first. traj 1 ylims minimum last. traj 1, maximum last. traj 1 end md\"\"\" Random walks as sum of random variables \"\"\" md\"\"\" We can connect up with the discussion from last lecture about the sum of random variables The position S n of a random walk at time n is a random variable that is the sum of n random variables which are often independent and id S n X 1 \\cdots X n where X i is the random variable modelling the i th step of the walk. Often each X i will be independent random variables of the same type, in which case we talk about an independent and identically distributed IID collection. In principle we could use the method from last lecture to model this, but there is a difficulty since we are interested in all intermediate steps The values S n 1 and S n , for example, are not independent even though the X i are . For example, if S n 1 is large, then S n is also about as large, so they are highly correlated. \"\"\" md\"\"\" Cumulative sum \"\"\" md\"\"\" Suppose we generate steps X i , e.g. \"\"\" steps rand 1, 1 , 10 md\"\"\" The trajectory, or sample path, of the random walk, is the collection of positions over time. These are the partial sums \\begin align S 1 & X 1 \\\\ S 2 & X 1 X 2 \\\\ S 3 & X 1 X 2 X 3 \\end align etc. \"\"\" md\"\"\" We can calculate all of these values using the `cumsum` \"cumulative sum\", also called \"prefix sum\" or \"scan\" function \"\"\" cumsum steps md\"\"\" Let's plot this \"\"\" plot cumsum steps , m o, leg false, size 500, 300 md\"\"\" Trajectories vs evolving probability distributions \"\"\" md\"\"\" So far we have looked at single trajectories of random walks. We can think of this as the equivalent of sampling using `rand`. Suppose that we were to conceptually run millions or billions of trajectories of our random walk. At each time step, we can ask for the probability distribution in space , averaged over all those walks. Note that we now have two variables. We could do this by literally running many walks and computing histograms. Instead we can look at how probabilities move around. Let's call p i^t the probability of being at site i at time t . We can then ask what the probability of being at site i at time t 1 . It must have been in one of the neighbouring sites at time t , so p i^ t 1 \\textstyle \\frac 1 2 p i 1 ^ t p i 1 ^ t . This is sometimes called the master equation even though that is rather a useless, non descriptive name . It describes how the probability distribution \"\"\" md\"\"\" At time t we have a whole probability distribution, which we can think of as a vector \\mathbf p ^t . Let's write a function to evolve the vector to the next time step. \"\"\" function evolve p p′ similar p make a vector of the same length and type to store the probability vector at the next time step for i in 2 length p 1 p′ i 0.5 p i 1 p i 1 end p′ 1 0 p′ end 0 return p′ end md\"\"\" Wait... Do you recognise this? We have seen this before This is just a convolution , like the blurring kernel from image processing except that in our particular model `p i ` itself does not contribute to the value of `new p i `. \"\"\" md\"\"\" Note that just as we did with images, we have a problem at the edges of the system. We need to specify boundary conditions . For now we have put 0s at the boundary. This corresponds to probability escaping at the boundary any probability that arrives at the boundary i.e. the first or last cell at a given time step does not stay in the system. We can think of the probability as analogous to a chemical that disappears \"\"\" md\"\"\" We also need to specify an initial condition \\mathbf p 0 . This tells us where our walker is at time 0 . Suppose that all walkers start at 0 . We will place this in the middle of our vector. Then the probability at that location is 1, while the probability elsewhere is 0 \"\"\" function initial condition n p0 zeros n p0 n ÷ 2 1 1 return p0 end md\"\"\" Now let's try and visualise the time evolution. \"\"\" function time evolution p0, N ps p0 p p0 for i in 1 N p evolve p push ps, copy p end return ps end p0 initial condition 101 ps time evolution p0, 100 ps 2 md\"\"\" t bind tt Slider 1 length ps , show value true, default 1 \"\"\" plot ps tt , ylim 0, 1 , leg false, size 500, 300 ps tt M reduce hcat, ps ' heatmap M, yflip true "},{"url":"data_science/random_walks/","title":"Random Walks","tags":["lecture","module2","programming","track_julia","plotting","structure","type","interactive","random","statistics","track_math","track_data"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v 14hHtGJ4s g\" image \"https user images.githubusercontent.com 6933510 136196563 f4b5b44c 5252 4e67 8c82 c550de891c55.png\" section 5 order 5 title \"Random Walks\" layout \"layout.jlhtml\" youtube id \"14hHtGJ4s g\" description \"\" tags \"lecture\", \"module2\", \"programming\", \"track julia\", \"plotting\", \"structure\", \"type\", \"interactive\", \"random\", \"statistics\", \"track math\", \"track data\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, BenchmarkTools TableOfContents aside true md\"\"\" Julia concepts Benchmarking BenchmarkTools.jl Plotting in a loop Generic programming Mutable vs immutable structs Vectors of vectors Aliasing of memory `cumsum` \"\"\" md\"\"\" Motivation Dynamics of hard discs \"\"\" md\"\"\" Brown observed Brownian motion in 1827 Large particles like sand or pollen in water move around seemingly at random. Einstein explained this in 1905 as repeated impacts with water molecules. We can visualise that with a simulation of hard discs bouncing off one another. Even though the dynamics is not random each disc follows Newton's laws if we just look at a single one of them it looks random. \"\"\" md\"\"\" Visualising random walks \"\"\" md\"\"\" A random walk models random motion in time and space. At each time step an object moves in a random direction. Let's visualise the result in 2 dimensions. \"\"\" md\"\"\" N bind N Slider 1 6, show value true, default 1 \"\"\" md\"\"\" t bind t Slider 1 10^N, show value true, default 1 \"\"\" md\"\"\" We see that the dynamics closely resembles, at least qualitatively, that of the hard disc. \"\"\" md\"\"\" Why use random walks? Why should we model with random processes? Either we need to get a lot of data to precisely characterise a system, or we assume it's random It may well be that can have all the details, but by making a simplifying assumption we actually gain more understanding . Examples Stock price going up and down Pollutants getting dispersed in the air Neutral genes moving through a population \"\"\" md\"\"\" Simple random walk The simplest version is called simple random walk one object jumps on the integers at each time step it jumps left or right. Each such step is a new random variable, taking values \\pm 1 at each step, each with probability 1 2 in the simplest case . We can think of this as a scaling of a Bernoulli random variable. In order to simulate this, we need to know how to generate the jumps. \"\"\" md\"\"\" Julia Benchmarking \"\"\" md\"\"\" There are various ways we could generate random values \\pm 1 . Let's use this as an opportunity to learn about benchmarking in Julia, i.e. measuring run time to compare performance. In this case we will do \"micro benchmarks\", which try to compare tiny pieces of code which we intend to run many millions of times, so that small differences in run time can be significant. The `BenchmarkTools.jl` package provides relatively easy to use tools for measuring this, by running the code many times and calculating statistics. The ` btime` macro is a simple way to estimate actual running time. Each option should be enclosed in a function. \"\"\" md\"\"\" Here are a few different ways we could generate random steps \"\"\" begin step1 rand 1, 1 step2 2 rand 0.5 1 step3 2 rand Bool 1 step4 sign randn end with terminal do btime step1 btime step2 btime step3 btime step4 end md\"\"\" Trajectory of a random walk \"\"\" md\"\"\" We can now calculate the trajectory of a 1D random walk as it takes several steps. It starts at an initial position, for example, 0, and takes consecutive steps \"\"\" function walk1D N x 0 xs x for i in 1 N x step1 push xs, x end return xs end begin plot for i in 1 10 plot walk1D 100 , leg false, size 500, 300 , lw 2, alpha 0.5 end plot end md\"\"\" Making it more general Random walks using types \"\"\" md\"\"\" Now suppose we want to think about more general random walks, for example moving around in 2D. Then we need to generalise the above function. Based on our experience from last time, you should suspect that a good way to do this is with types . We will define \"\"\" abstract type Walker end struct Walker1D Walker pos Int end position w Walker w.pos step w Walker1D rand 1, 1 struct Walker2D Walker x Int y Int end position w Walker2D w.x, w.y update w W, step where W Walker W position w step step w Walker2D rand 1, 0 , 0, 1 , 1, 0 , 0, 1 update w Walker2D, step Vector Walker2D w.x step 1 , w.y step 2 function trajectory w W, N where W W is a type parameter ws position w for i in 1 N pos position w w update w, step w push ws, position w end return ws end trajectory Walker1D 0 , 10 traj trajectory Walker2D 0, 0 , 10^N begin plot traj 1 t , ratio 1, leg false, alpha 0.5, lw 2 scatter traj 1 , traj t , c red, green xlims minimum first. traj 1, maximum first. traj 1 ylims minimum last. traj 1, maximum last. traj 1 end md\"\"\" Random walks as sum of random variables \"\"\" md\"\"\" We can connect up with the discussion from last lecture about the sum of random variables The position S n of a random walk at time n is a random variable that is the sum of n random variables which are often independent and id S n X 1 \\cdots X n where X i is the random variable modelling the i th step of the walk. Often each X i will be independent random variables of the same type, in which case we talk about an independent and identically distributed IID collection. In principle we could use the method from last lecture to model this, but there is a difficulty since we are interested in all intermediate steps The values S n 1 and S n , for example, are not independent even though the X i are . For example, if S n 1 is large, then S n is also about as large, so they are highly correlated. \"\"\" md\"\"\" Cumulative sum \"\"\" md\"\"\" Suppose we generate steps X i , e.g. \"\"\" steps rand 1, 1 , 10 md\"\"\" The trajectory, or sample path, of the random walk, is the collection of positions over time. These are the partial sums \\begin align S 1 & X 1 \\\\ S 2 & X 1 X 2 \\\\ S 3 & X 1 X 2 X 3 \\end align etc. \"\"\" md\"\"\" We can calculate all of these values using the `cumsum` \"cumulative sum\", also called \"prefix sum\" or \"scan\" function \"\"\" cumsum steps md\"\"\" Let's plot this \"\"\" plot cumsum steps , m o, leg false, size 500, 300 md\"\"\" Trajectories vs evolving probability distributions \"\"\" md\"\"\" So far we have looked at single trajectories of random walks. We can think of this as the equivalent of sampling using `rand`. Suppose that we were to conceptually run millions or billions of trajectories of our random walk. At each time step, we can ask for the probability distribution in space , averaged over all those walks. Note that we now have two variables. We could do this by literally running many walks and computing histograms. Instead we can look at how probabilities move around. Let's call p i^t the probability of being at site i at time t . We can then ask what the probability of being at site i at time t 1 . It must have been in one of the neighbouring sites at time t , so p i^ t 1 \\textstyle \\frac 1 2 p i 1 ^ t p i 1 ^ t . This is sometimes called the master equation even though that is rather a useless, non descriptive name . It describes how the probability distribution \"\"\" md\"\"\" At time t we have a whole probability distribution, which we can think of as a vector \\mathbf p ^t . Let's write a function to evolve the vector to the next time step. \"\"\" function evolve p p′ similar p make a vector of the same length and type to store the probability vector at the next time step for i in 2 length p 1 p′ i 0.5 p i 1 p i 1 end p′ 1 0 p′ end 0 return p′ end md\"\"\" Wait... Do you recognise this? We have seen this before This is just a convolution , like the blurring kernel from image processing except that in our particular model `p i ` itself does not contribute to the value of `new p i `. \"\"\" md\"\"\" Note that just as we did with images, we have a problem at the edges of the system. We need to specify boundary conditions . For now we have put 0s at the boundary. This corresponds to probability escaping at the boundary any probability that arrives at the boundary i.e. the first or last cell at a given time step does not stay in the system. We can think of the probability as analogous to a chemical that disappears \"\"\" md\"\"\" We also need to specify an initial condition \\mathbf p 0 . This tells us where our walker is at time 0 . Suppose that all walkers start at 0 . We will place this in the middle of our vector. Then the probability at that location is 1, while the probability elsewhere is 0 \"\"\" function initial condition n p0 zeros n p0 n ÷ 2 1 1 return p0 end md\"\"\" Now let's try and visualise the time evolution. \"\"\" function time evolution p0, N ps p0 p p0 for i in 1 N p evolve p push ps, copy p end return ps end p0 initial condition 101 ps time evolution p0, 100 ps 2 md\"\"\" t bind tt Slider 1 length ps , show value true, default 1 \"\"\" plot ps tt , ylim 0, 1 , leg false, size 500, 300 ps tt M reduce hcat, ps ' heatmap M, yflip true "},{"url":"data_science/random_walks_II/","title":"Random Walks II","tags":["lecture","module2","track_julia","track_data","statistics","plotting","random","structure","type","programming","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v pIAFHyLmwbM\" image \"https user images.githubusercontent.com 6933510 191981723 657fd4b8 c23d 4fb9 a3f2 496b4574fac1.png\" section 6 order 6 title \"Random Walks II\" layout \"layout.jlhtml\" youtube id \"pIAFHyLmwbM\" description \"\" tags \"lecture\", \"module2\", \"track julia\", \"track data\", \"statistics\", \"plotting\", \"random\", \"structure\", \"type\", \"programming\", \"interactive\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using PlutoUI, Plots, LinearAlgebra, SparseArrays TableOfContents aside true md\"\"\" Julia concepts Customised display of objects Structured matrices in Julia `cumsum` Vectors of vectors Concatenation of vectors `hcat` `heatmap` Plots.jl `surface` Plots.jl \"\"\" md\"\"\" Pascal's triangle \"\"\" md\"\"\" Let's start by thinking about Pascal's triangle. Note that Pascal was not the first person to study these numbers https en.wikipedia.org wiki Pascal%27s triangle . \"\"\" pascal N binomial n, k for n 0 N, k 0 N pascal 10 md\"\"\" The non zero entries are the binomial coefficients the k th entry on the n th row is the coefficient of x^k in the expansion 1 x ^n , starting from n 0 in the first row and k 0 in the first column. \"\"\" md\"\"\" Note that there are 0s above the main diagonal in other words the matrix is lower triangular . Julia has special types to represent some classes of structured matrices in the standard library `LinearAlgebra` package \"\"\" L LowerTriangular pascal 10 md\"\"\" We see that the display is special the known \" structural \" zeros are shown as dots, instead of numbers. \"\"\" md\"\"\" As we have already seen, Julia also has a sparse matrix type that we could use for this, in the `SparseArrays` standard library \"\"\" sparse pascal 10 md\"\"\" For fun, let's look at where the entries of Pascal's triangle are odd. We'll make a slider \"\"\" bind n pascal Slider 1 63, show value true, default 1 sparse isodd. pascal n pascal md\"\"\" Note that the visual representation of a sparse matrix changes at a particular value of n . For larger values of n the sparsity pattern is displayed using dots for non zero values. \"\"\" md\"\"\" The pattern is quite striking \"\"\" md\"\"\" You may be interested to know that there is an alternative way to look at Pascal's triangle \"\"\" binomial i j, i for i 0 10, j 0 10 md\"\"\" and that in fact this can be produced from the previous version using matrix multiplication \"\"\" pascal 10 pascal 10 ' md\"\"\" Convolutions build Pascal's triangle \"\"\" pascal 6 md\"\"\" Where do those binomial coefficients come from? That's not how we learnt to build Pascal's triangle at school We learned to build each number up by summing two adjacent numbers from the previous row . In the lower triangular representation we we sum the number immediately above with the one to the left of it. \"\"\" md\"\"\" We can think of this as... a convolution with the vector 1, 1 Recall that convolutions like this are used as image filters , where we want to apply the same local operation everywhere along a vector i.e. one in which we modify a vector in a neighbourhood . \"\"\" md\"\"\" Random walks Independent and identically distributed random variables \"\"\" md\"\"\" The discussion of Pascal's triangle and convolutions will surprisingly turn up below. For now let's go back to thinking about random walks . Recall that in a random walk, at each tick of a clock we take a step in space, say left or right. Each spatial step is random, so we can think of each step as being a random variable with a certain probability distribution, for example the random variable that takes the value 1 with probability \\frac 1 2 and 1 also with probability \\frac 1 2 . Often we will think about random walks in which each step is \"the same\". What do we mean by \"the same\"? We don't mean that the outcome is the same, but rather that each step has the same probability distribution , i.e. the same probability of taking each possible value. In other words, each step is given by copies of the same random variable . Steps are also independent of each other the choice of step direction right now does not affect the choice at the next step . Hence the steps form a collection of independent and identically distributed random variables , or IID random variables. \"\"\" md\"\"\" Random walks as a cumulative sum \"\"\" md\"\"\" Let's call the random variable that describes the i th step X i . Then the overall position S n of the random walk at time n is given by S n X 1 \\cdots X n \\sum i 1 ^n X i. Here we have taken the initial position S 0 equal to 0. If the initial position is not zero, then this instead gives the displacement S n S 0 at time n . Recall that by the sum of two random variables we mean a new random variable whose outcomes are sums of the outcomes, with probabilities given by taking the possible pairs of outcomes giving each new outcome. \"\"\" md\"\"\" Cumulative sum \"\"\" md\"\"\" We previously looked at sums like this. The difference now is that we are interested in the entire trajectory , i.e. the sequence S 1 , S 2 , \\ldots , S n , \\ldots . The trajectory is given by partial sums \"\"\" md\"\"\" \\begin align S 1 & X 1 \\\\ S 2 & X 1 X 2 \\\\ S 3 & X 1 X 2 X 3 \\end align etc. \"\"\" md\"\"\" Note that S n 1 is not independent of S n or the other S m . E.g. if S 100 happens to be large, then S 101 will be about as large. Thinking about types, as we did a couple of lectures ago, we would need to define a new type to represent a random walk, since we cannot generate consecutive values as independent samples. \"\"\" md\"\"\" How could we calculate a trajectory of a walk? Suppose we generate steps X i , e.g. \"\"\" steps rand 1, 1 , 10 md\"\"\" The whole trajectory is given by the cumulative sum also called \"prefix sum\" or \"scan\". Julia has the function `cumsum` to calculate this \"\"\" cumsum 1, 2, 3, 4 md\"\"\" So the trajectory is given by \"\"\" cumsum steps plot cumsum steps , m o, leg false, size 500, 300 md\"\"\" Note that in Julia this is just a convenience function it will be no more performant than writing the `for` loop yourself unlike in some other languages, where `for` loops are slow . \"\"\" md\"\"\" Evolving probability distributions in time \"\"\" md\"\"\" So far we have looked at single trajectories of random walks. We can think of this as the equivalent of sampling using `rand`. Suppose that we run many trajectories of a random walk. At a given time t we can ask where all of the walks are by taking a histogram of the S t^ k , where the superscript ^ k denotes the k th trajectory out of the collection. Doing so gives us the probability distribution of the random variable S t . Let's call p i^t \\mathbb P S t i the probability of being at site i at time t . Then the probability distribution at time t is given by the collection of all the p i^t we can group these into a vector \\mathbf p ^t . Now we can ask what happens at the next time step t 1 . Taking histograms again gives the probabilities p j^ t 1 and the vector \\mathbf p ^t of all of them. But \\mathbf p ^ t 1 and \\mathbf p ^ t are related in some way, since we go from S t to S t 1 by just taking a single step. Let's think about the case of a simple random walk in one dimension. To arrive at site i at time t 1 , we must have been in one of the neighbouring sites at time t and jumped with probability \\frac 1 2 in the direction of site i . Hence we have p i^ t 1 \\textstyle \\frac 1 2 p i 1 ^ t p i 1 ^ t . This is sometimes called the master equation even though that is rather a useless, non descriptive name it describes how the probability distribution of the random walk evolves in time . \"\"\" md\"\"\" Implementing time evolution of probabilities Let's write a function to evolve a probability vector to the next time step for a simple random walk \"\"\" function evolve p p′ similar p make a vector of the same length and type to store the probability vector at the next time step for i in 2 length p 1 iterate over the bulk of the system p′ i 0.5 p i 1 p i 1 end boundary conditions p′ 1 0 p′ end 0 return p′ end md\"\"\" Wait... Do you recognise this? This is just a convolution again The kernel is now \\frac 1 2 , 0, \\frac 1 2 . Apart from the extra 0 and the \\frac 1 2 , this is the same as in Pascal's triangle... so probabilities in simple random walk behave like Pascal's triangle \"\"\" md\"\"\" Note that just as with images we have a problem at the edges of the system We need to specify boundary conditions on the first and last cells. For now we have put 0s at the boundary, corresponding to probability escaping at the boundary any probability that arrives at the boundary at a given time step does not stay in the system. We can think of the probability as analogous to a chemical moving through a system that leaves our system and e.g. moves into the outside world when it reaches an edge. \"\"\" md\"\"\" We also need to specify an initial condition \\mathbf p 0 . This tells us where our walker is at time 0 . Suppose that all walkers start at site i 0 . We will place this in the middle of our vector. Then the probability of being at 0 is 1 certainty , while the probability at any other site is 0 impossible \"\"\" function initial condition n p₀ zeros n p₀ n ÷ 2 1 1 return p₀ end md\"\"\" Now let's evolve the probability vector in time by applying the `evolve` function repeatedly, starting from the initial probability distribution \"\"\" function time evolution p0, N ps p0 p p0 for i in 1 N p evolve p push ps, copy p end return ps end md\"\"\" Let's visualise this \"\"\" begin grid size 101 p0 initial condition grid size end ps time evolution p0, 100 md\"\"\" Note that `ps` is a `Vector`, whose elements are each `Vector`s I.e. we have a vector of vectors. This is often a convenient way to build up a matrix. \"\"\" ps 2 md\"\"\" t bind tt Slider 1 length ps , show value true, default 1 \"\"\" bar ps tt , ylim 0, 1 , leg false, size 500, 300 , alpha 0.5 md\"\"\" Concatenating vectors into a matrix \"\"\" md\"\"\" Now we want to visualise this in a different way, for which we must join up concatenate all the probability vectors into a matrix \"\"\" M reduce hcat, ps ' md\"\"\" We can visualise the matrix with a heat map \"\"\" heatmap M, yflip true md\"\"\" We can also visualise this as a 3D surface \"\"\" plotly surface M md\"\"\" But this is not necessarily very clear, so let's draw it ourselves as stacked histograms \"\"\" gr ylabels grid size÷2 grid size÷2 begin plot leg false for which in 1 15 for i in 1 length ps which plot which, which , grid size÷2 i, grid size÷2 i , 0, ps which i , c which, alpha 0.8, lw 2 end end xlims 1, 15 plot end let color list red, RGB 0, 1, 0.1 , blue xs ys zs cs cs2 for which in 1 15 for i in 1 length ps which push xs, which, which, NaN push ys, ylabels i , ylabels i , NaN push zs, 0, ps which i , NaN push zs, 0, 1, NaN D push cs, color list mod1 which, 3 , color list mod1 which, 3 , color list mod1 which, 3 end push cs2, which end plot xs, ys, zs plot leg false plot 1 15, 0 cumsum sign. randn 14 , zeros 15 , alpha 0.6, m o, lw 2, c color list plot xs, ys, zs, c cs, xlims 1, 15 , ylims 30, 30 , zlims 0, 1 , lw 2.5, alpha 0.7, xticks 1 15 , yticks 20 10 20 xlabel \"t\" ylabel \"space\" end "},{"url":"data_science/simulating_component_failure/","title":"Modeling with Stochastic Simulation","tags":["lecture","module2","track_julia","probability","statistics","track_math","epidemiology","interactive","plotting","programming","type","discrete","continuous","ODE","differential equation","agent based model"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v d8BohH76C7E\" image \"https user images.githubusercontent.com 6933510 136196572 b11974d5 7335 4678 9092 630e034bbe8f.png\" section 3 order 3 title \"Modeling with Stochastic Simulation\" layout \"layout.jlhtml\" youtube id \"d8BohH76C7E\" description \"\" tags \"lecture\", \"module2\", \"track julia\", \"probability\", \"statistics\", \"track math\", \"epidemiology\", \"interactive\", \"plotting\", \"programming\", \"type\", \"discrete\", \"continuous\", \"ODE\", \"differential equation\", \"agent based model\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, StatsBase, Statistics, HypertextLiteral PlutoUI.TableOfContents aside true md\"\"\" Julia features \"\"\" md\"\"\" Extending a function from another package not `Base` Why create a new type? Abstractions and concentrating information around objects organisation Plotting shapes String interpolation \"\"\" md\"\"\" Individual based \"microscopic\" models \"\"\" md\"\"\" In individual based models, we literally specify the behaviour, or actions, of each individual in a set of rules. But often we are interested in a global picture of how the whole system, consisting of many individuals, evolves in time how many infectious individuals there are in total at a given time, for example, or the behaviour of the whole stock market. In this notebook we will see how we can start from an individual based probabilistic probabilistic model, and how sometimes we can find deterministic equations for a macroscopic system level description. Those macroscopic equations can either be in discrete time recurrence relations or difference equations , or we can take a continuous limit and convert them into ordinary differential equations . \"\"\" md\"\"\" Modelling time to success or time to failure \"\"\" md\"\"\" Let's start with a very simple model of time to success . Suppose we are playing a game in which we have a probability p of success on each turn. How many turns do we need until we succeed? For example, how many rolls of a die do we need until we roll a 6? How many rolls of 2 dice until we get a double 6? \"\"\" md\"\"\" This can be used as a model in many different situations, for example Time to failure of a light bulb or a piece of machinery time for a radioactive nucleus to decay time to recover from an infection, etc. \"\"\" md\"\"\" The basic question is Suppose we have N light bulbs that are working correctly on day 0 . If each bulb has probability p to fail on each day, how many are still working at day number t ? How long, on average, will a given bulb last? And, do light bulbs really fail exactly at midnight each night? Can you imagine a more realistic model? As usual we will take a computational thinking point of view let's code up the simulation and plot the results. Then we can step back and \"\"\" md\"\"\" Visualizing component failure \"\"\" gr begin rectangle w, h, x, y x . 0,w,w,0 , y . 0,0,h,h circle r,x,y θ LinRange 0, 2π, 30 x. r. cos. θ , y. r. sin. θ end md\"\"\" String interpolation \"\"\" md\"\"\" As an aside, how could we display a picture of Daniel Bernoulli and resize it in Pluto? To do so we use a piece of HTML, which we represent as a string. We need to substitute the value of a Julia variable into the string, which we do with string interpolation, with the syntax ` variable ` inside the string. Then we convert the string to HTML with the `HTML ... ` constructor \"\"\" bind bernoulliwidth Slider 10 10 500, show value true url \"https upload.wikimedia.org wikipedia commons thumb b b3 ETH BIB Bernoulli%2C Daniel %281700 1782%29 Portrait Portr 10971.tif %28cropped%29.jpg 440px ETH BIB Bernoulli%2C Daniel %281700 1782%29 Portrait Portr 10971.tif %28cropped%29.jpg\" s \" img src url width bernoulliwidth \" md\"\"\" Note that we can use three sets of double quotes `\"` to represent a multi line string, or to enclose another string that itself contains quotes. \"\"\" HTML s md\"\"\" Math Bernoulli random variables \"\"\" md\"\"\" Recall that a Bernoulli random variable models a weighted coin it takes the value 1, with probability p , and 0, with probability 1 p \"\"\" md\"\"\" Note that `rand p` returns a `Bool` true or false . We are converting to `Int` to get a value 1 or 0. \"\"\" md\"\"\" Let's generate sample some Bernoulli random variates \"\"\" md\"\"\" It is natural to ask what the mean , or expected value , is \"\"\" md\"\"\" If you think about how to calculate the mean sum up and divide by the total number of flips , it just gives the proportion of 1s, which should be around p . Exercise Calculate the variance of a Bernoulli random variable. Hint What happens when you sum the squares? Remember that you also need to center the data. \"\"\" md\"\"\" Julia Make it a type \"\"\" md\"\"\" Currently we need one function for sampling from a Bernoulli random variable, a different function to calculate its mean, a different function for its standard deviation, etc. From a mathematical point of view we have the concept \"Bernoulli random variable\" and we are calculating properties of that concept. Computationally we can do the same thing by creating a new object to represent \"a Bernoulli random variable\". \"\"\" struct Bernoulli p Float64 end md\"\"\" We want to be able to sample from it, using `rand`, and take its `mean`. To do so we will extend sometimes called \"overload\" the `rand` function from Julia's `Base` library, and the `mean` function from the `Statistics` standard library. Note that we are adding methods to these functions you will do this in the homework. \"\"\" Base.rand X Bernoulli Int rand X.p B Bernoulli 0.25 md\"\"\" The object `B` really represents \"a Bernoulli random variable with probability of success p \". Since all such random variables are the same, this represents any Bernoulli random variable with that probability. We should use this type any time we need a Bernoulli random variable. If you need this in another notebook you will either need to copy and paste the definition or, better, make your own mini library. However, note that types like this are already available in the `Distributions.jl` package and the new `MeasureTheory.jl` package. \"\"\" Statistics.mean X Bernoulli X.p mean B md\"\"\" Running the stochastic simulation \"\"\" md\"\"\" Let's take the simulation and run it a few times. \"\"\" T 100 md\"\"\" N bind N Slider 1 1000, show value true, default 70 p bind ppp Slider 0 0.01 1, show value true, default 0.25 t bind t Slider 1 T, show value true \"\"\" p 0.1 md\"\"\" Time evolution of the mean Intuitive derivation \"\"\" md\"\"\" The mean seems to behave in a rather predictable way over time. Can we derive this? Let N t be the number of green light bulbs at time t . This decreases because some bulbs fail. Since bulbs fail with probability p , the number of bulbs that fail at time t is, on average, p I t . Note that one time unit corresponds to one sweep of the simulation. At time t there are N t green bulbs. How many decay? Each decays with probability p , so on average p I t fail, so are removed from the number of infectious, giving the change \\Delta I t \\color lightgreen I t 1 \\color lightgreen I t \\color red p \\, I t So \\color lightgreen I t 1 \\color lightgreen I t \\color red p \\, I t or I t 1 1 p I t . \"\"\" md\"\"\" We can now take one step backwards I t 1 1 p 1 p I t 1 1 p ^2 I t 1 and then continue to solve the recurrence I t 1 p ^t \\, I 0. \"\"\" md\"\"\" Let's compare the exact and numerical results \"\"\" md\"\"\" They agree well, as they should. The agreement is expected to be better i.e. the fluctuations smaller for a larger population. \"\"\" md\"\"\" Binomial distribution \"\"\" md\"\"\" At time 0 there are N 0 light bulbs. How many will turn `` \\color red \\text red `` fail at the first step? Let's call this \\Delta N 0 . Intuitively, the mean is \\mean \\Delta N 0 p N 0 , but in fact \\Delta N 0 is a random variable In principle, it could be that no light bulbs fail, or all of them fail, but both of those events have very small probability. For each of the N 0 bulbs, i 1, \\ldots, N 0 , we have a Bernoulli random variable that tells us if bulb i will fail. Let's call them we call B 0^i . Then \\Delta N 0 \\sum i 1 ^ N 0 B 0^i \"\"\" md\"\"\" Let's make a type to represent the sum of N Bernoullis with probability p . This is called a binomial random variable . The only information that we require is just that, N and p . \"\"\" struct Binomial N Int64 p Float64 end md\"\"\" Note that does not require or even allow methods at first, as some other languages would. You can add methods later, and other people can add methods too if they can load your package. But they do not need to modify your code. \"\"\" Base.rand X Binomial sum rand Bernoulli X.p for i in 1 X.N function simulate N, p v fill 0, N, N t 0 while any v . 0 && t 100 t 1 for i 1 N, j 1 N if rand p && v i,j 0 v i,j t end end end return v end bernoulli p rand p flips Int bernoulli 0.25 for i in 1 100 mean flips function step infectious, p for i in 1 length infectious if infectious i && bernoulli p infectious i false end end return infectious end begin infected true for i in 1 N results copy step infected, ppp for i in 1 T pushfirst results, trues N end function simulate recovery p, T infectious trues N num infectious N for t in 1 T step infectious, p push num infectious, count infectious end return num infectious end begin pp 0.05 plot simulate recovery pp, T , label \"run 1\", alpha 0.5, lw 2, m o plot simulate recovery pp, T , label \"run 2\", alpha 0.5, lw 2, m o xlabel \"time t\" ylabel \"number infectious\" end exact N 1 pp ^t for t in 0 T all data simulate recovery pp, T for i in 1 30 begin plot all data, alpha 0.1, leg false, m o, ms 1, size 500, 400 , label \"\" xlabel \"time t\" ylabel \"number still functioning\" end plot mean all data , leg true, label \"mean\", lw 3, c red, m o, alpha 0.5, size 500, 400 begin plot replace. all data, 0.0 NaN , yscale log10, alpha 0.3, leg false, m o, ms 1, size 500, 400 plot mean all data , yscale log10, lw 3, c red, m o, label \"mean\", alpha 0.5 xlabel \"time t\" ylabel \"number still functioning\" end begin plot mean all data , m o, alpha 0.5, label \"mean of stochastic simulations\", size 500, 400 plot exact, lw 3, alpha 0.8, label \"deterministic model\", leg right title \"Experiment vs. theory\" xlabel \"time\" ylabel \"\"\"number of \"greens\" \"\"\" end rand B rand Binomial 10, 0.25 md\"\"\" N bind binomial N Slider 1 100, show value true, default 1 p bind binomial p Slider 0.0 0.01 1, show value true, default 0 \"\"\" begin binomial data rand Binomial binomial N, binomial p for i in 1 10000 bar countmap binomial data , alpha 0.5, size 500, 300 , leg false, bin width 0.5 end md\"\"\" Let's call q 1 p . Then for each bulb we are choosing either p failure or q non failure . This is the same as flipping n independent, weighted coins. The number of ways of choosing such that k bulbs fail is given by the coefficient of p^k in the expansion of p q ^n , namely the binomial coefficient \\begin pmatrix n \\\\ k \\end pmatrix \\frac n k \\, n k , where n 1 \\times 2 \\times \\cdots n is the factorial of n . \"\"\" md\"\"\" Continuous time If we look at the graph of the mean as a function of time, it seems to follow a smooth curve. Indeed it makes sense to ask not only how many people have recovered each day , but to aim for finer granularity. Suppose we instead increment time in steps of \\delta t the above analysis was for \\delta t 1 . Then we will need to adjust the probability of recovery in each time step. It turns out that to make sense in the limit \\delta t \\to 0 , we need to choose the probability p \\delta t to recover in time t to be proportional to \\delta t p \\delta t \\simeq \\lambda \\, \\delta t, where \\lambda is the recovery rate . Note that a rate is a probability per unit time . We get \"\"\" md\"\"\" I t \\delta t I t \\simeq \\lambda \\,\\delta t \\, I t \"\"\" md\"\"\" Dividing by \\delta t gives \\frac I t \\delta t I t \\delta t \\simeq \\lambda \\, I t We recognise the left hand side as the definition of the derivative when \\delta t \\to 0 . Taking that limit finally gives \"\"\" md\"\"\" \\frac dI t dt \\lambda \\, I t That is, we obtain an ordinary differential equation that gives the solution implicitly. Solving this equation with initial condition I 0 I 0 gives \"\"\" md\"\"\" I t I 0 \\exp \\lambda \\, t . \"\"\" md\"\"\" Alternatively, we can derive this by recognising the exponential in the limit \\delta t \\to 0 of the following expression, which is basically the expression for compounding interest \"\"\" md\"\"\" I t 1 \\lambda \\, \\delta t ^ t \\delta t I 0 \"\"\" md\"\"\" Discrete to continuous \"\"\" function plot cumulative p, N, δ 1 kw... ps p 1 p ^ n 1 for n in 1 N cumulative cumsum ps ys 0 reduce vcat, cumulative n , cumulative n for n in 1 N pop ys pushfirst ys, 0 xs 0 reduce vcat, n δ, n δ for n in 1 N plot xs, ys scatter n δ for n in 1 N , cumulative kw... end plotly begin plot size 500, 300 , leg false plot cumulative 0.1, 30, 1.0, ms 2, c red, alpha 1 plot cumulative 0.1, 30, 0.5, ms 2, c red plot cumulative 0.05, 60, 0.5 label \"\", ms 2, c lightgreen, alpha 1 plot cumulative 0.025, 120, 0.25 label \"\", ms 1, c lightgreen, alpha 1 plot cumulative 0.0125, 240, 0.125 label \"\", ms 1, c lightgreen, alpha 1 end 1 0.95^2 almost 10% begin λ log 1 0.1 plot 0 0.01 20, t 1 exp λ t , lw 1 plot 0 0.01 20, t 1 exp 0.1 t , lw 1 end md\"\"\" What does it mean to talk about a rate a probability per unit time. \"\"\" md\"\"\" How many light bulbs turn red in 1 second, half a second. Looks like 0 0. If have billions of light bulbs. \"\"\" md\"\"\" People get sick light bulbs not on discrete time clock. Limit as \\delta t \\to 0 You measure it discretely \"\"\" λ md\"\"\" SIR model \"\"\" md\"\"\" Now let's extend the procedure to the full SIR model, S \\to I \\to R . Since we already know how to deal with recovery, consider just the SI model, where susceptible agents are infected via contact, with probability \"\"\" md\"\"\" Let's denote by S t and I t be the number of susceptible and infectious people at time t , respectively, and by N the total number of people. On average, in each sweep each infectious individual has the chance to interact with one other individual. That individual is chosen uniformly at random from the total population of size N . But a new infection occurs only if that chosen individual is susceptible, which happens with probability S t N , and then if the infection is successful, with probability b , say. Hence the change in the number of infectious people after that step is. The decrease in S t is also given by \\Delta I t . \"\"\" md\"\"\" \\Delta I t I t 1 I t b \\, I t \\, \\left \\frac S t N \\right \"\"\" md\"\"\" It is useful to normalize by N , so we define s t \\frac S t N \\quad i t \\frac I t N \\quad r t \\frac R t N \"\"\" md\"\"\" Including recovery with probability c we obtain the discrete time SIR model \"\"\" md\"\"\" \\begin align s t 1 & s t b \\, s t \\, i t \\\\ i t 1 & i t b \\, s t \\, i t c \\, i t\\\\ r t 1 & r t c \\, i t \\end align \"\"\" md\"\"\" Again we can obtain this from the stochastic process by taking expectations exercise . Hint Ignore recovery to start with and take variables Y t^i that are 0 if the person is susceptible and 1 if it is infected. \"\"\" md\"\"\" And again we can allow the processes to occur in steps of length \\delta t and take the limit \\delta t \\to 0 . With rates \\beta and \\gamma we obtain the standard continuous time SIR model \"\"\" md\"\"\" \\begin align \\textstyle \\frac ds t dt & \\beta \\, s t \\, i t \\\\ \\textstyle \\frac di t dt & \\beta \\, s t \\, i t & \\gamma \\, i t \\\\ \\textstyle \\frac dr t dt & & \\gamma \\, i t \\end align \"\"\" md\"\"\" We can think of this as a model of a chemical reaction with species S, I and R. The term s t i t is known as the mass action https en.wikipedia.org wiki Law of mass action form of interaction. Note that no analytical solutions of these simple nonlinear ODEs are known as a function of time However, parametric solutions are known https arxiv.org abs 1403.2160 . \"\"\" md\"\"\" Below is a simulation of the discrete time model. Note that the simplest numerical method to solve approximately the system of ODEs, the Euler method , basically reduces to solving the discrete time model A whole suite of more advanced ODE solvers is provided in the Julia `DiffEq` ecosystem https diffeq.sciml.ai dev . \"\"\" begin NN 100 SS NN 1 II 1 RR 0 end ss, ii, rr SS NN, II NN, RR NN p infection, p recovery 0.1, 0.01 TT 1000 function discrete SIR s0, i0, r0, T 1000 s, i, r s0, i0, r0 results s s, i i, r r for t in 1 T Δi p infection s i Δr p recovery i s new s Δi i new i Δi Δr r new r Δr push results, s s new, i i new, r r new s, i, r s new, i new, r new end return results end SIR discrete SIR ss, ii, rr begin ts 1 length SIR discrete time SIR plot plot ts, x.s for x in SIR , m o, label \"S\", alpha 0.2, linecolor blue, leg right, size 400, 300 plot ts, x.i for x in SIR , m o, label \"I\", alpha 0.2 plot ts, x.r for x in SIR , m o, label \"R\", alpha 0.2 xlims 0, 500 end macro bindname name Symbol, ex Expr quote HypertextLiteral. htl \"\"\" div style 'display flex ' code style 'font weight bold' String name code bind name esc ex div \"\"\" end end bindname M Slider 2 20, show value true, default 8 bindname prob Slider 0.01 .01 1, show value true, default .1 simulation simulate M, prob bindname tt Slider 1 100, show value true, default 1 begin w .9 h .9 c RGB 0,1,0 , RGB 1,0,0 , purple 1 . simulation . tt . simulation . tt. 1 plot ratio 1, legend false, axis false, ticks false for i 1 M, j 1 M plot rectangle w,h, i, j , c black, fill true, alpha 0.5 plot circle .3,i .45,j .45 , c c i, j , fill true end for i 1 M, j 1 M if simulation i,j tt annotate i .45, j .5, text \" simulation i,j \", font 7 , white end end plot lims 0.5, M 1.1 , title \"time tt 1 failed count sum simulation. tt \" end begin plot size 500, 300 cdf count simulation .≤ i for i 0 100 bar cdf, c purple, legend false, xlim 0,tt ,alpha 0.8 end begin newcdf count simulation . i for i 0 100 bar newcdf, c RGB 0,1,0 , legend false, xlim 0,tt ,alpha 0.8 end bar countmap simulation , c red, legend false, xlim 0, tt .5 , size 500, 300 "},{"url":"homework/hw0/","title":"hw0","tags":[],"text":" A Pluto.jl notebook v0.19.45 using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Compose using PlutoUI md\" Homework 0, version 4 \" md\"\"\" Homework 0 Getting up and running First of all, welcome to the course We are excited to teach you about real world applications of scientific computing, using the same tools that we work with ourselves. This first homework is a little mic check. We'd like all MIT students to submit this zeroth homework assignment . It will not affect your grade, but it will help us get everything running smoothly when the course starts. If you're stuck or don't have much time, just fill in your name and ID and submit 🙂 \"\"\" md\"\"\" Homework Logistics Homeworks are in the form of Pluto notebooks https plutojl.org . Your must complete them and submit them on Canvas if you are an MIT student. . If you are not an MIT student, we encourage you to join Discord https discord.gg Z5qnVf8 and find someone to cross grade. HW0 is for you to get your system set up correctly and to test our grading software. You must submit it but it will not count towards your grade. \"\"\" md\" Requirements of this HW0 Install Julia and set up Pluto Do the required Exercise 0. That’s it, but if you like you can do the OPTIONAL exercises that follow.\" md\"\"\" Installation Before being able to run this notebook successfully locally, you will need to set up Julia and Pluto. Spring21 installation One you have Julia and Pluto installed, you can click the button at the top right of this page and follow the instructions to edit this notebook locally and submit. \"\"\" md\" Required Exercise 0 Making a basic function Computing the square of a number is easy you just multiply it with itself. Algorithm Given x Output x^2 1. Multiply `x` by `x`\" function basic square x return 1 this is wrong, write your code here end let result basic square 5 if result isa Number md\"\"\" warning \"Not a number\" `basic square` did not return a number. Did you forget to write `return`? \"\"\" elseif abs result 5 5 0.01 md\"\"\" correct Well done \"\"\" else md\"\"\" warning \"Incorrect\" Keep working on it \"\"\" end end md\"That's all that's required for this week. Please submit the notebook. We just wanted to make sure that you're up and running. If you want to explore further, we have included a few optional exercises below.\" md\" Optional Exercise 1 Square root by Newton's method Computing the square of a number is easy you already did it. But how does one compute the square root of a number? Algorithm Given x Output \\sqrt x 1. Take a guess `a` 1. Divide `x` by `a` 1. Set a the average of `x a` and `a`. The square root must be between these two numbers. Why? 1. Repeat until `x a` is roughly equal to `a`. Return `a` as the square root. In general, you will never get to the point where `x a` is exactly equal to `a`. So if our algorithm keeps going until `x a a`, then it will get stuck. So instead, the algorithm takes a parameter `error margin`, which is used to decide when `x a` and `a` are close enough to halt. \" md\" Exercise 1.1 Step 3 in the algorithm sets the new guess to be the average of `x a` and the old guess `a`. This is because the square root must be between the numbers `x a` and `a`. Why? \" ex 1 1 md\"\"\" your answer here \"\"\" you might need to wait until all other cells in this notebook have completed running. scroll down the page to see what's up if isdefined ex 1 1 md\"\"\"Do not change the name of the variable write you answer as `ex 1 1 \"...\"`\"\"\" end md\" Exercise 1.2 Write a function newton sqrt x which implements the above algorithm.\" function newton sqrt x, error margin 0.01, a x 2 a x 2 is the default value of `a` return x this is wrong, write your code here end newton sqrt 2 let result newton sqrt 2, 0.01 if result isa Number md\"\"\" warning \"Not a number\" `newton sqrt` did not return a number. Did you forget to write `return`? \"\"\" elseif abs result sqrt 2 0.01 md\"\"\" correct Well done \"\"\" else md\"\"\" warning \"Incorrect\" Keep working on it \"\"\" end end md\"\"\" hint `abs r s ` is the distance between `r` and `s` \"\"\" md\"\"\" hint If you're stuck, feel free to cheat, this is homework 0 after all 🙃 Julia has a function called `sqrt` \"\"\" md\" Optional Exercise 2 Sierpinksi's triangle Sierpinski's triangle is defined recursively Sierpinski's triangle of complexity N is a figure in the form of a triangle which is made of 3 triangular figures which are themselves Sierpinski's triangles of complexity N 1. A Sierpinski's triangle of complexity 0 is a simple solid equilateral triangle \" md\"To draw Sierpinski's triangle, we are going to use an external package, Compose.jl https giovineitalia.github.io Compose.jl latest tutorial . Let's import it A package contains a coherent set of functionality that you can often use a black box according to its specification. There are lots of Julia packages https juliahub.com ui Home . \" md\"Just like the definition above, our `sierpinksi` function is recursive it calls itself.\" complexity 3 if complexity 3 md\"\"\" Try changing the value of `complexity` to `5` in the cell above. Hit `Shift Enter` to affect the change. \"\"\" else md\"\"\" Great As you can see, all the cells in this notebook are linked together by the variables they define and use. Just like a spreadsheet \"\"\" end md\" Exercise 2.1 As you can see, the total area covered by triangles is lower when the complexity is higher.\" md\"\"\" Can you write a function that computes the area of `sierpinski n ` , as a fraction of the area of `sierpinski 0 `? So ``` area sierpinski 0 1.0 area sierpinski 1 0.?? ... ``` \"\"\" function area sierpinski n return 1.0 end if area sierpinski 0 1.0 && area sierpinski 1 3 4 md\"\"\" correct Well done \"\"\" else md\"\"\" warning \"Incorrect\" Keep working on it \"\"\" end md\" Let's try it out below \" md\"Complexity bind n Slider 0 6, show value true \" md\"\"\" hint Can you write `area sierpinksi n ` as a function of `area sierpinski n 1 `? \"\"\" md\"That's it for now, see you next week \" triangle compose context , polygon 1, 1 , 0, 1 , 1 2, 0 It does not matter which order you define the building blocks functions of the program in. The best way to organize code is the one that promotes understanding. function place in 3 corners t Uses the Compose library to place 3 copies of t in the 3 corners of a triangle. treat this function as a black box, or learn how it works from the Compose documentation here https giovineitalia.github.io Compose.jl latest tutorial Compose is declarative 1 compose context , context 1 4, 0, 1 2, 1 2 , t , context 0, 1 2, 1 2, 1 2 , t , context 1 2, 1 2, 1 2, 1 2 , t end function sierpinski n if n 0 triangle else t sierpinski n 1 recursively construct a smaller sierpinski's triangle place in 3 corners t place it in the 3 corners of a triangle end end sierpinski complexity sierpinski. 0 6 md\"\"\" Sierpinski's triangle of complexity n sierpinski n has area area sierpinski n \"\"\" "},{"url":"homework/hw1/","title":"Images and Arrays","tags":["homework","module1","image","track_julia","track_math","track_climate","track_data","programming","interactive","type","matrix"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 section 2.5 order 2.5 homework number 1 title \"Images and Arrays\" layout \"layout.jlhtml\" tags \"homework\", \"module1\", \"image\", \"track julia\", \"track math\", \"track climate\", \"track data\", \"programming\", \"interactive\", \"type\", \"matrix\" description \"Practice Julia basics by working with arrays of colors. At the end of this homework, you can see all of your filters applied to your webcam image \" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import ImageMagick using Images using PlutoUI using HypertextLiteral end md\"\"\" Homework 1 images and arrays `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Exercise 1 Manipulating vectors 1D images A `Vector` is a 1D array. We can think of that as a 1D image. \"\"\" example vector 0.5, 0.4, 0.3, 0.2, 0.1, 0.0, 0.7, 0.0, 0.7, 0.9 md\"\"\" html\" br \" Exercise 1.1 👉 Make a random vector `random vect` of length 10 using the `rand` function. \"\"\" random vect missing replace `missing` with your code begin colored line x Vector hcat Gray. Float64. x ' colored line x Any nothing end colored line example vector colored line random vect md\" Exercise 1.2 👉 Make a function `my sum` using a `for` loop, which computes the total of a vector of numbers.\" function my sum xs your code here return missing end my sum 1,2,3 md\" Exercise 1.3 👉 Use your `my sum` function to write a function `mean`, which computes the mean average of a vector of numbers.\" function mean xs your code here return missing end mean 1, 2, 3 md\"👉 Define `m` to be the mean of `random vect`.\" m missing replace `missing` with your code md\"\"\" Exercise 1.4 👉 Write a function `demean`, which takes a vector `xs` and subtracts the mean from each value in `xs`. Use your `mean` function \"\"\" md\"\"\" Note about mutation There are two ways to think about this exercise, you could modify the original vector, or you can create a new vector . We often prefer the second version, so that the original data is preserved. We generally only use code of the first variant in the most performance sensitive parts of a program, as it requires more care to write and use correctly. Be careful not to get carried away in optimizing code , especially when learning a new language There is a convention among Julians that functions that modify their argument have a ` ` in their name. For example, `sort x ` returns a sorted copy of `x`, while `sort x ` modifies `x` to be sorted. Tips for writing non mutating code 1. Rewriting an existing mutating function to be non mutating can feel like a 'tedious' and 'inefficient' process. Often, instead of trying to rewrite a mutating function, it's best to take a step back and try to think of your problem as constructing something new . Instead of a `for` loop, it might make more sense to use descriptive primitives like broadcasting with the dot syntax https docs.julialang.org en v1 manual functions man vectorized also for math operators https docs.julialang.org en v1 manual mathematical operations man dot operators , and map and filter https www.youtube.com watch?v O HBDZMLrM . 2. If a mutating algorithm makes the most sense for your problem, then you can first use `copy` to create a copy of an array, and then modify that copy. We will cover this topic more in the later exercises \"\"\" function demean xs your code here return missing end test vect let feel free to change your test case here to create 1.0, 1.5, 8.5 this cell is a bit funky to deal with a common pitfall from last year it regenerates the vector if you accidentally wrote a mutating function don't worry about how it works for this exercise demean to create end md\"To verify our function, let's check that the mean of the `demean test vect ` is 0 Due to floating point round off error it may not be exactly 0. \" demeaned test vect demean test vect mean demeaned test vect md\"\"\" Exercise 1.5 👉 Generate a vector of 100 elements. Where the center 20 elements are set to `1`, and all other elements are `0`. \"\"\" function create bar your code here return missing end create bar colored line create bar url \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" philip filename download url download to a local file. The filename is returned philip load philip filename md\" Hi there Philip \" philip head philip 470 800, 140 410 md\"\"\" Recall from Section 1.1 https computationalthinking.mit.edu Spring21 week1 that in Julia, an image is just a 2D array of color objects \"\"\" typeof philip md\"\"\" Every pixel i.e. element of the 2D array is of the `RGB` type \"\"\" philip pixel philip 100,100 typeof philip pixel md\"\"\" To get the values of its individual color channels, use the `r`, `g` and `b` attributes \"\"\" philip pixel.r, philip pixel.g, philip pixel.b md\"\"\" And to create an `RGB` object yourself \"\"\" RGB 0.1, 0.4, 0.7 md\"\"\" Exercise 2.1 👉 Write a function `get red` that takes a single pixel, and returns the value of its red channel. \"\"\" function get red pixel AbstractRGB your code here return missing end get red RGB 0.8, 0.1, 0.0 md\"\"\" Exercise 2.2 👉 Write a function `get reds` note the extra `s` that accepts a 2D color array called `image`, and returns a 2D array with the red channel value of each pixel. The result should be a 2D array of numbers . Use your function `get red` from the previous exercise. \"\"\" function get reds image AbstractMatrix your code here return missing end get reds philip head md\"\"\" Great By extracting the red channel value of each pixel, we get a 2D array of numbers. We went from an image 2D array of RGB colors to a matrix 2D array of numbers . Exercise 2.3 Let's try to visualize this matrix. Right now, it is displayed in text form, but because the image is quite large, most rows and columns don't fit on the screen. Instead, a better way to visualize it is to view a number matrix as an image . This is easier than you might think We just want to map each number to an `RGB` object, and the result will be a 2D array of `RGB` objects, which Julia will display as an image. First, let's define a function that turns a number into a color . \"\"\" function value as color x return RGB x, 0, 0 end value as color 0.0 , value as color 0.6 , value as color 1.0 md\"\"\" 👉 Use the functions `get reds` and `value as color` to visualize the red channel values of `philip head`. Tip Like in previous exercises, use broadcasting 'dot syntax' to apply a function element wise . Use the ➕ button at the bottom left of this cell to add more cells. \"\"\" md\"\"\" Exercise 2.4 👉 Write four more functions, `get green`, `get greens`, `get blue` and `get blues`, to be the equivalents of `get red` and `get reds`. Use the ➕ button at the bottom left of this cell to add new cells. \"\"\" md\"\"\" Exercise 2.5 👉 Write a function `mean color` that accepts an object called `image`. It should calculate the mean amounts of red, green and blue in the image and return the average color. Be sure to use functions from previous exercises \"\"\" function mean color image your code here return missing end mean color philip md\"\"\" At the end of this homework, you can see all of your filters applied to your webcam image \"\"\" function invert color AbstractRGB your code here return missing end md\"Let's invert some colors \" color black RGB 0.0, 0.0, 0.0 invert color black color red RGB 0.8, 0.1, 0.1 invert color red md\"👉 Can you invert the picture of Philip?\" philip inverted missing replace `missing` with your code md\"\"\" At the end of this homework, you can see all of your filters applied to your webcam image \"\"\" md\"\"\" Exercise 3.2 👉 Look up the documentation on the `floor` function. Use it to write a function `quantize x Number ` that takes in a value x which you can assume is between 0 and 1 and \"quantizes\" it into bins of width 0.1. For example, check that 0.267 gets mapped to 0.2. \"\"\" function quantize x Number your code here return missing end md\"\"\" Intermezzo multiple methods In Julia, we often write multiple methods for the same function. When a function is called, the method is chosen based on the input arguments. Let's look at an example These are two methods to the same function, because they have the same name, but different input types \"\"\" function double x Number return x 2 end function double x Vector return x..., x... end md\"\"\" When we call the function `double`, Julia will decide which method to call based on the given input argument \"\"\" double 24 double 1,2,37 md\"\"\" We call this multiple dispatch , and it is one of Julia's key features. Throughout this course, you will see lots of real world application, and learn to use multiple dispatch to create flexible and readable abstractions \"\"\" md\"\"\" Exercise 3.3 👉 Write the second method of the function `quantize`, i.e. a new version of the function with the same name. This method will accept a color object called `color`, of the type `AbstractRGB`. Here, ` AbstractRGB` is a type annotation . This ensures that this version of the function will be chosen when passing in an object whose type is a subtype of the `AbstractRGB` abstract type. For example, both the `RGB` and `RGBX` types satisfy this. The method you write should return a new `RGB` object, in which each component r , g and b are quantized. Use your previous method for `quantize` \"\"\" function quantize color AbstractRGB your code here return missing end md\"\"\" Exercise 3.4 👉 Write a method `quantize image AbstractMatrix ` that quantizes an image by quantizing each pixel in the image. You may assume that the matrix is a matrix of color objects. \"\"\" function quantize image AbstractMatrix your code here return missing end quantize 0.267 , quantize 0.91 md\"Let's apply your method \" quantize philip md\"\"\" Exercise 3.5 👉 Write a function `noisify x Number, s ` to add randomness of intensity s to a value x , i.e. to add a random value between s and s to x . If the result falls outside the range 0, 1 you should \"clamp\" it to that range. Julia has a built in `clamp` function, or you can write your own function. \"\"\" function noisify x Number, s your code here return missing end md\"\"\" 👉 Write the second method `noisify c AbstractRGB, s ` to add random noise of intensity s to each of the r, g, b values in a colour. Use your previous method for `noisify`. Remember that Julia chooses which method to use based on the input arguments. So to call the method from the previous exercise, the first argument should be a number. \"\"\" function noisify color AbstractRGB, s your code here return missing end md\"\"\" Noise strength \"\"\" bind color noise Slider 0 0.01 1, show value true md\"\"\" Note about array comprehension At this point, you already know of a few ways to make a new array based on one that already exists. 1. you can use a for loop to go through a array 1. you can use function broadcasting over a array 1. you can use array comprehension The third option you are about to see demonstrated below and following the following syntax ``` function to apply args for args in some iterable of your choice ``` This creates a new iterable that matches what you iterate through in the second part of the comprehension. Below is an example with `for` loops through two iterables that creates a 2 dimensional `Array`. \"\"\" md\"\"\" 👉 Write the third method `noisify image AbstractMatrix, s ` to noisify each pixel of an image. This function should be a single line \"\"\" function noisify image AbstractMatrix, s your code here return missing end noisify 0.5, 0.1 edit this test case original color red, with noise noisify color red, color noise noisify color red, strength for strength in 0 0.05 1, row in 1 10 ' md\"\"\" Exercise 3.6 Move the slider below to set the amount of noise applied to the image of Philip. \"\"\" bind philip noise Slider 0 0.01 1, show value true noisify philip head, philip noise if philip noise 1 md\"\"\" What's this? The noise intensity is `1.0`, but we can still recognise Philip in the picture... 👉 Modify the definition of the slider to go further than `1.0`. \"\"\" end md\"\"\" 👉 For which noise intensity does it become unrecognisable? You may need noise intensities larger than 1. Why? \"\"\" answer about noise intensity md\"\"\" The image is unrecognisable with intensity ... \"\"\" md\"\"\" Results \"\"\" function custom filter pixel AbstractRGB your code here return pixel end function custom filter image AbstractMatrix return custom filter. image end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" You can find out more about any function like `rand` by clicking on the Live Docs in the bottom right of this Pluto window, and typing a function name in the top. image https user images.githubusercontent.com 6933510 107848812 c934df80 6df6 11eb 8a32 663d802f5d11.png image https user images.githubusercontent.com 6933510 107848846 0f8a3e80 6df7 11eb 818a 7271ecb9e127.png We recommend that you leave the window open while you work on Julia code. It will continually look up documentation for anything you type Help, I don't see the Live Docs Try the following 🙋 Are you viewing a static preview? The Live Docs only work if you run the notebook. If you are reading this on our course website, then click the button in the top right to run the notebook. 🙋 Is your screen too small? Try resizing your window or zooming out. \"\"\" | hint hint md\"\"\"Check out this page for a refresher on basic Julia syntax Basic Julia Syntax https computationalthinking.mit.edu Spring21 basic syntax \"\"\" hint md\"\"\" In Section 1.1 https computationalthinking.mit.edu Spring21 week1 , we drew a red square on top of the image Philip with a simple command... \"\"\" md\"\"\" This exercise can be quite difficult if you use a `for` loop or list comprehension. Instead, you should use the dot syntax https docs.julialang.org en v1 manual functions man vectorized to apply a function element wise to an array. For example, this is how you get the square root of `3` ``` sqrt 3 ``` and this is how you get the square roots of 1, 2 and 3 ``` sqrt. 1, 2, 3 ``` \"\"\" | hint hint md\"`rand ` generates a uniformly random floating point number between 0 and 1 .\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let result get red RGB 0.2, 0.3, 0.4 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 0.2 correct else keep working end end let test RGB 0.2, 0, 0 RGB 0.6, 0, 0 result get reds test if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 0.2 0.6 correct else keep working end end let result invert RGB 1.0, 0.5, 0.25 I chose these values because they can be represented exactly by Float64 shouldbe RGB 0.0, 0.5, 0.75 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractRGB keep working md\"You need to return a color , i.e. an object of type `RGB`. Use `RGB r, g, b ` to create a color with channel values `r`, `g` and `b`.\" elseif result shouldbe keep working else correct end end let result quantize RGB .297, .1, .0 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractRGB keep working md\"You need to return a color , i.e. an object of type `RGB`. Use `RGB r, g, b ` to create a color with channel values `r`, `g` and `b`.\" elseif result RGB 0.2, .1, .0 keep working else correct end end let result noisify 0.5, 0 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 0.5 results noisify 0.9, 0.1 for in 1 1000 if 0.8 ≤ minimum results 0.81 && 0.99 ≤ maximum results ≤ 1 result noisify 5, 3 if result 1 correct else keep working md\"The result should be restricted to the range `` 0,1 ``.\" end else keep working end else keep working md\"What should `noisify 0.5, 0 ` be?\" correct end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined random vect not defined random vect elseif ismissing random vect still missing elseif random vect isa Vector keep working md\"`random vect` should be a `Vector`.\" elseif eltype random vect Float64 almost md\"\"\" You generated a vector of random integers. For the remaining exercises, we want a vector of `Float64` numbers. The optional first argument to `rand` specifies the type of elements to generate. For example `rand Bool, 10 ` generates 10 values that are either `true` or `false`. Try it \"\"\" elseif length random vect 10 keep working md\"`random vect` does not have the correct size.\" elseif length Set random vect 10 keep working md\"`random vect` is not 'random enough'\" else correct md\"Well done You can run your code again to generate a new vector \" end if isdefined my sum not defined my sum else let result my sum 1,2,3 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 6 keep working else correct end end end if isdefined mean not defined mean else let result mean 1,2,3 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 2 keep working else correct end end end if isdefined m not defined m elseif ismissing m still missing elseif m isa Number keep working md\"`m` should be a number.\" elseif m mean random vect keep working else correct end if isdefined mean not defined mean else let input Float64 1,2,3 result demean input if input result almost md\"\"\" It looks like you modified `xs` inside the function. It is preferable to avoid mutation inside functions, because you might want to use the original data again. For example, applying `demean` to a dataset of sensor readings would modify the original data, and the rest of your analysis would be erroneous. \"\"\" elseif ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractVector || length result 3 keep working md\"Return a vector of the same size as `xs`.\" elseif abs sum result 3 1e 10 correct else keep working end end end if isdefined create bar not defined create bar else let result create bar if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa Vector || length result 100 keep working md\"The result should be a `Vector` with 100 elements.\" elseif result 1,50,100 0,1,0 keep working else correct end end end if isdefined mean color not defined mean color else let input reshape RGB 1.0, 1.0, 1.0 , RGB 1.0, 1.0, 0.0 , 2, 1 result mean color input shouldbe RGB 1.0, 1.0, 0.5 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractRGB keep working md\"You need to return a color , i.e. an object of type `RGB`. Use `RGB r, g, b ` to create a color with channel values `r`, `g` and `b`.\" elseif result shouldbe keep working else correct end end end if isdefined quantize not defined quantize else let result quantize .3 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result .3 if quantize 0.35 .3 almost md\"What should quantize `0.2` be?\" else keep working end else correct end end end todo text HTML \"\"\" div style \"background rgb 220, 200, 255 padding 2em border radius 1em \" h1 TODO h1 repr MIME\"text html\" , text div \"\"\" bigbreak html\" br br br br br \" bigbreak md\"\"\" bigbreak Exercise 2 Manipulating images In this exercise we will get familiar with matrices 2D arrays in Julia, by manipulating images. Recall that in Julia images are matrices of `RGB` color objects. Let's load a picture of Philip again. \"\"\" md\"\"\" bigbreak Exercise 3 More filters In the previous exercises, we learned how to use Julia's dot syntax to apply a function element wise to an array. In this exercise, we will use this to write more image filters, that you can then apply to your own webcam image Exercise 3.1 👉 Write a function `invert` that inverts a color, i.e. sends r, g, b to 1 r, 1 g, 1 b . \"\"\" md\"\"\" bigbreak Camera input \"\"\" md\"\"\" bigbreak Write your own filter \"\"\" bigbreak function camera input max size 200, default url \"https i.imgur.com SUmi94P.png\" \"\"\" span class \"pl image waiting for permission\" style .pl image.popped out position fixed top 0 right 0 z index 5 .pl image video container width 250px .pl image video border radius 1rem 1rem 0 0 .pl image.waiting for permission video container display none .pl image prompt display none .pl image.waiting for permission prompt width 250px height 200px display grid place items center font family monospace font weight bold text decoration underline cursor pointer border 5px dashed rgba 0,0,0,.5 .pl image video display block .pl image .bar width inherit display flex z index 6 .pl image .bar top position absolute flex direction column .pl image .bar bottom background black border radius 0 0 1rem 1rem .pl image .bar button flex 0 0 auto background rgba 255,255,255,.8 border none width 2rem height 2rem border radius 100% cursor pointer z index 7 .pl image .bar button shutter width 3rem height 3rem margin 1.5rem auto .2rem auto .pl image video.takepicture animation pictureflash 200ms linear keyframes pictureflash 0% filter grayscale 1.0 contrast 2.0 100% filter grayscale 0.0 contrast 1.0 style div id \"video container\" div id \"top\" class \"bar\" button id \"stop\" title \"Stop video\" ✖ button button id \"pop out\" title \"Pop out pop in\" ⏏ button div video playsinline autoplay video div id \"bottom\" class \"bar\" button id \"shutter\" title \"Click to take a picture\" 📷 button div div div id \"prompt\" span Enable webcam span div script based on https github.com fonsp printi static by the same author const span currentScript.parentElement const video span.querySelector \"video\" const popout span.querySelector \"button pop out\" const stop span.querySelector \"button stop\" const shutter span.querySelector \"button shutter\" const prompt span.querySelector \".pl image prompt\" const maxsize max size const send source source, src width, src height const scale Math.min 1.0, maxsize src width, maxsize src height const width Math.floor src width scale const height Math.floor src height scale const canvas html` canvas width \\ width height \\ height ` const ctx canvas.getContext \"2d\" ctx.drawImage source, 0, 0, width, height span.value width width, height height, data ctx.getImageData 0, 0, width, height .data, span.dispatchEvent new CustomEvent \"input\" const clear camera window.stream.getTracks .forEach s s.stop video.srcObject null span.classList.add \"waiting for permission\" prompt.onclick navigator.mediaDevices.getUserMedia audio false, video facingMode \"environment\", , .then function stream stream.onend console.log window.stream stream video.srcObject stream window.cameraConnected true video.controls false video.play video.controls false span.classList.remove \"waiting for permission\" .catch function error console.log error stop.onclick clear camera popout.onclick span.classList.toggle \"popped out\" shutter.onclick const cl video.classList cl.remove \"takepicture\" void video.offsetHeight cl.add \"takepicture\" video.play video.controls false console.log video send source video, video.videoWidth, video.videoHeight document.addEventListener \"visibilitychange\", if document.visibilityState \"visible\" clear camera Set a default image const img html` img crossOrigin \"anonymous\" ` img.onload console.log \"helloo\" send source img, img.width, img.height img.src \" default url \" console.log img script span \"\"\" | HTML end bind cam data camera input function process raw camera data raw camera data the raw image data is a long byte array, we need to transform it into something more \"Julian\" something with more structure . The encoding of the raw byte stream is every 4 bytes is a single pixel every pixel has 4 values Red, Green, Blue, Alpha we ignore alpha for this notebook So to get the red values for each pixel, we take every 4th value, starting at the 1st reds flat UInt8. raw camera data \"data\" 1 4 end greens flat UInt8. raw camera data \"data\" 2 4 end blues flat UInt8. raw camera data \"data\" 3 4 end but these are still 1 dimensional arrays, nicknamed 'flat' arrays We will 'reshape' this into 2D arrays width raw camera data \"width\" height raw camera data \"height\" shuffle and flip to get it in the right shape reds reshape reds flat, width, height ' 255.0 greens reshape greens flat, width, height ' 255.0 blues reshape blues flat, width, height ' 255.0 we have our 2D array for each color Let's create a single 2D array, where each value contains the R, G and B value of that pixel RGB. reds, greens, blues end cam image process raw camera data cam data mean color cam image invert. cam image quantize cam image noisify cam image, .5 invert. cam image quantize cam image noisify cam image, .5 custom filter cam image custom filter cam image md\" homework 1, version 9 \" "},{"url":"homework/hw10/","title":"Climate modeling I","tags":["homework","module3","track_climate","track_julia","track_data","track_math","climate","plotting","interactive","modeling","climate model","economics","bifurcation","probability"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 section 7.5 order 7.5 homework number 10 title \"Climate modeling I\" layout \"layout.jlhtml\" tags \"homework\", \"module3\", \"track climate\", \"track julia\", \"track data\", \"track math\", \"climate\", \"plotting\", \"interactive\", \"modeling\", \"climate model\", \"economics\", \"bifurcation\", \"probability\" description \"Play around with an energy balance model of the climate system, to explore the effect of doubling CO₂, and to examine the 'snowball earth' phenomenon.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using LaTeXStrings using Plots using PlutoUI using Random, Distributions end md\" homework 10, version 3 \" md\"\"\" Homework 10 Climate modeling I `18.S191`, Fall 2023 \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" html\"\"\" iframe width \"100%\" height \"300\" src \"https www.youtube.com embed Gi4ZZVS2GLA\" frameborder \"0\" allow \"accelerometer autoplay clipboard write encrypted media gyroscope picture in picture\" allowfullscreen iframe \"\"\" md\"\"\" Before working on the homework, make sure that you have watched the first lecture on climate modeling 👆. We have included the important functions from this lecture notebook in the next cell. Feel free to have a look \"\"\" module Model const S 1368 solar insolation W m^2 energy per unit time per unit area const α 0.3 albedo, or planetary reflectivity unitless const B 1.3 climate feedback parameter W m^2 °C , const T0 14. preindustrial temperature °C absorbed solar radiation α α, S S S 1 α 4 W m^2 outgoing thermal radiation T A A, B B A B T const A S 1. α 4 B T0 W m^2 . greenhouse effect CO2 a a, CO2 PI CO2 PI a log CO2 CO2 PI const a 5.0 CO2 forcing coefficient W m^2 const CO2 PI 280. preindustrial CO2 concentration parts per million ppm CO2 const t CO2 PI constant CO2 concentrations const C 51. atmosphere and upper ocean heat capacity J m^2 °C function timestep ebm append ebm.T, ebm.T end ebm.Δt tendency ebm append ebm.t, ebm.t end ebm.Δt end tendency ebm 1. ebm.C absorbed solar radiation α ebm.α, S ebm.S outgoing thermal radiation ebm.T end , A ebm.A, B ebm.B greenhouse effect ebm.CO2 ebm.t end , a ebm.a, CO2 PI ebm.CO2 PI begin mutable struct EBM T Array Float64,1 t Array Float64,1 Δt Float64 CO2 Function C Float64 a Float64 A Float64 B Float64 CO2 PI Float64 α Float64 S Float64 end Make constant parameters optional kwargs EBM T Array Float64,1 , t Array Float64,1 , Δt Real, CO2 Function C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S EBM T, t, Δt, CO2, C, a, A, B, CO2 PI, α, S Construct from float inputs for convenience EBM T0 Real, t0 Real, Δt Real, CO2 Function C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S EBM Float64 T0 , Float64 t0 , Δt, CO2 C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S end begin function run ebm EBM, end year Real while ebm.t end end year timestep ebm end end run ebm run ebm, 200. run for 200 years by default end CO2 hist t CO2 PI 1 . fractional increase t fractional increase t t . 1850. 220 .^3 begin CO2 RCP26 t CO2 PI 1 . fractional increase t . min. 1., exp. t . 1850. . 170 100 RCP26 EBM T0, 1850., 1., CO2 RCP26 run RCP26, 2100. CO2 RCP85 t CO2 PI 1 . fractional increase t . max. 1., exp. t . 1850. . 170 100 RCP85 EBM T0, 1850., 1., CO2 RCP85 run RCP85, 2100. end end md\"\"\" Exercise 1 policy goals under uncertainty A recent ground breaking review paper https agupubs.onlinelibrary.wiley.com doi 10.1029 2019RG000678 produced the most comprehensive and up to date estimate of the climate feedback parameter , which they find to be B \\approx \\mathcal N 1.3, 0.4 , i.e. our knowledge of the real value is normally distributed with a mean value \\overline B 1.3 W m² K and a standard deviation \\sigma 0.4 W m² K. These values are not very intuitive, so let us convert them into more policy relevant numbers. Definition Equilibrium climate sensitivity ECS is defined as the amount of warming \\Delta T caused by a doubling of CO₂ e.g. from the pre industrial value 280 ppm to 560 ppm , at equilibrium. At equilibrium, the energy balance model equation is 0 \\frac S 1 α 4 A BT eq a \\ln\\left \\frac 2\\ \\text CO ₂ \\text PI \\text CO ₂ \\text PI \\right From this, we subtract the preindustrial energy balance, which is given by 0 \\frac S 1 α 4 A BT 0 , The result of this subtraction, after rearranging, is our definition of \\text ECS \\text ECS \\equiv T eq T 0 \\frac a\\ln 2 B \"\"\" md\"\"\"The plot below provides an example of an \"abrupt 2 × CO₂\" experiment, a classic experimental treatment method in climate modelling which is used in practice to estimate ECS for a particular model. Note in complicated climate models the values of the parameters a and B are not specified a priori , but emerge as outputs of the simulation. The simulation begins at the preindustrial equilibrium, i.e. a temperature T 0 14 °C is in balance with the pre industrial CO₂ concentration of 280 ppm until CO₂ is abruptly doubled from 280 ppm to 560 ppm. The climate responds by warming rapidly, and after a few hundred years approaches the equilibrium climate sensitivity value, by definition. \"\"\" md\"\"\" ``B `` bind B slider Slider 2.5 .001 0 show value true, default 1.3 \"\"\" md\"\"\" Exercise 1.1 Develop understanding for feedbacks and climate sensitivity \"\"\" md\"\"\" 👉 Change the value of B using the slider above. What does it mean for a climate system to have a more negative value of B ? Explain why we call B the climate feedback parameter . \"\"\" observations from changing B md\"\"\" Hello world \"\"\" md\"\"\" 👉 What happens when B is greater than or equal to zero? \"\"\" observations from nonnegative B md\"\"\" Hello world \"\"\" md\"Reveal answer bind reveal nonnegative B answer CheckBox \" if reveal nonnegative B answer md\"\"\" This is known as the \"runaway greenhouse effect\", where warming self amplifies so strongly through positive feedbacks that the warming continues forever or until the oceans boil away and there is no longer a reservoir or water to support a water vapor feedback . This is thought to explain Venus' extremely hot and hostile climate, but as you can see is extremely unlikely to occur on present day Earth. \"\"\" end md\"\"\" 👉 Create a graph to visualize ECS as a function of B. \"\"\" md\"\"\" Exercise 1.2 Doubling CO₂ To compute ECS, we doubled the CO₂ in our atmosphere. This factor 2 is not entirely arbitrary without substantial effort to reduce CO₂ emissions, we are expected to at least double the CO₂ in our atmosphere by 2100. Right now, our CO₂ concentration is 415 ppm round 415 280, digits 3 times the pre industrial value of 280 ppm from 1850. The CO₂ concentrations in the future depend on human action. There are several models for future concentrations, which are formed by assuming different policy scenarios . A baseline model is RCP8.5 a \"worst case\" high emissions scenario. In our notebook, this model is given as a function of ``t``. \"\"\" md\"\"\" 👉 In what year are we expected to have doubled the CO₂ concentration, under policy scenario RCP8.5? \"\"\" expected double CO2 year let missing end md\"\"\" Exercise 1.3 Uncertainty in B The climate feedback parameter ``B`` is not something that we can control– it is an emergent property of the global climate system. Unfortunately, ``B`` is also difficult to quantify empirically the relevant processes are difficult or impossible to observe directly , so there remains uncertainty as to its exact value. A value of ``B`` close to zero means that an increase in CO₂ concentrations will have a larger impact on global warming, and that more action is needed to stay below a maximum temperature. In answering such policy related question, we need to take the uncertainty in ``B`` into account. In this exercise, we will do so using a Monte Carlo simulation we generate a sample of values for ``B``, and use these values in our analysis. \"\"\" B̅ 1.3 σ 0.4 ECS B B̅, a Model.a a log 2. . B let double CO2 t if t 0 2 Model.CO2 PI else Model.CO2 PI end the definition of A depends on B, so we recalculate A Model.S 1. Model.α 4 B slider Model.T0 create the model ebm ECS Model.EBM 14., 100., 1., double CO2, A A, B B slider Model.run ebm ECS, 300 ecs ECS B B slider p plot size 500, 250 , legend bottomright, title \"Transient response to instant doubling of CO₂\", ylabel \"temperature change °C \", xlabel \"years after doubling\", ylim .5, isfinite ecs && ecs 4 ? 4 10 , plot p, ebm ECS.t 1 , ebm ECS.t end , ecs . 1,1 , ls dash, color darkred, label \"ECS\" plot p, ebm ECS.t, ebm ECS.T . ebm ECS.T 1 , label \"ΔT t T t T₀\" end | as svg B samples let B distribution Normal B̅, σ Nsamples 5000 samples rand B distribution, Nsamples we only sample negative values of B filter x x 0, samples end histogram B samples, size 600, 250 , label nothing, xlabel \"B W m² K \", ylabel \"samples\" md\"\"\" 👉 Generate a probability distribution for the ECS based on the probability distribution function for B above. Plot a histogram. \"\"\" ECS samples missing md\" Answer \" md\"It looks like the ECS distribution is not normally distributed , even though B is. 👉 How does \\overline \\text ECS B compare to \\text ECS \\overline B ? What is the probability that \\text ECS B lies above \\text ECS \\overline B ? \" md\"👉 Does accounting for uncertainty in feedbacks make our expectation of global warming better less implied warming or worse more implied warming ?\" observations from the order of averaging md\"\"\" Hello world \"\"\" md\"\"\" Exercise 1.5 Running the model In the lecture notebook we introduced a mutable struct `EBM` energy balance model , which contains the parameters of our climate simulation `C`, `a`, `A`, `B`, `CO2 PI`, `α`, `S`, see details below a function `CO2`, which maps a time `t` to the concentrations at that year. For example, we use the function `t 280` to simulate a model with concentrations fixed at 280 ppm. `EBM` also contains the simulation results, in two arrays `T` is the array of temperatures °C, `Float64` . `t` is the array of timestamps years, `Float64` , of the same size as `T`. \"\"\" html\"\"\" style .hello td font family sans serif font size .8em max width 300px soft opacity .5 style p Properties of an code EBM code obect p table class \"hello\" thead tr th Name th th Description th tr thead tbody tr th code A code th td Linearized outgoing thermal radiation offset soft W m² soft td tr tr th code B code th td Linearized outgoing thermal radiation slope. em or em b climate feedback parameter b soft W m² °C soft td tr tr th code α code th td Planet albedo, 0.0 1.0 soft unitless soft td tr tr th code S code th td Solar insulation soft W m² soft td tr tr th code C code th td Atmosphere and upper ocean heat capacity soft J m² °C soft td tr tr th code a code th td CO₂ forcing effect soft W m² soft td tr tr th code CO2 PI code th td Pre industrial CO₂ concentration soft ppm soft td tr tbody table \"\"\" md\"\"\" You can set up an instance of `EBM` like so \"\"\" empty ebm Model.EBM 14.0, initial temperature 1850, initial year 1, Δt t 280.0, CO2 function md\"\"\" Have a look inside this object. We see that `T` and `t` are initialized to a 1 element array. Let's run our model \"\"\" simulated model let ebm Model.EBM 14.0, 1850, 1, t 280.0 Model.run ebm, 2020 ebm end md\"\"\" Again, look inside `simulated model` and notice that `T` and `t` have accumulated the simulation results. In this simulation, we used `T0 14` and `CO2 t 280`, which is why `T` is constant during our simulation. These parameters are the default, pre industrial values, and our model is based on this equilibrium. 👉 Run a simulation with policy scenario RCP8.5, and plot the computed temperature graph. What is the global temperature in the year 2100? \"\"\" simulated rcp85 model let missing end md\"\"\" Additional parameters can be set using keyword arguments. For example ```julia Model.EBM 14, 1850, 1, t 280.0 B 2.0 ``` Creates the same model as before, but with `B 2.0`. \"\"\" md\"\"\" 👉 Write a function `temperature response` that takes a function `CO2` and an optional value `B` as parameters, and returns the temperature at 2100 according to our model. \"\"\" function temperature response CO2 Function, B Float64 1.3 return missing end temperature response t 280 temperature response Model.CO2 RCP85 temperature response Model.CO2 RCP85, 1.0 md\"\"\" Exercise 1.6 Application to policy relevant questions We talked about two emissions scenarios RCP2.6 strong mitigation controlled CO2 concentrations and RCP8.5 no mitigation high CO2 concentrations . These are given by the following functions \"\"\" t 1850 2100 plot t, Model.CO2 RCP85. t , ylim 0, 1200 , ylabel \"CO2 concentration ppm \" bind t scenario test Slider t show value true, default 1850 Model.CO2 RCP26 t scenario test , Model.CO2 RCP85 t scenario test md\"\"\" We are interested in how the uncertainty in our input B the climate feedback parameter propagates through our model to determine the uncertainty in our output T t , for a given emissions scenario. The goal of this exercise is to answer the following by using Monte Carlo Simulation for uncertainty propagation 👉 What is the probability that we see more than 2°C of warming by 2100 under the low emissions scenario RCP2.6? What about under the high emissions scenario RCP8.5? \"\"\" md\"\"\" Exercise 2 How did Snowball Earth melt? In lecture 21 see below , we discovered that increases in the brightness of the Sun are not sufficient to explain how Snowball Earth eventually melted. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid Y68tnH0FIzc params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" We talked about a second theory a large increase in CO₂ by volcanoes could have caused a strong enough greenhouse effect to melt the Snowball. If we imagine that the CO₂ then decreased e.g. by getting sequestered by the now liquid ocean , we might be able to explain how we transitioned from a hostile Snowball Earth to today's habitable \"Waterball\" Earth. In this exercise, you will estimate how much CO₂ would be needed to melt the Snowball and visualize a possible trajectory for Earth's climate over the past 700 million years by making an interactive bifurcation diagram . Exercise 2.1 In the lecture notebook https github.com hdrake simplEarth blob master 2 ebm multiple equilibria.jl video above , we had a bifurcation diagram of S solar insolation vs T temperature . We increased S , watched our point move right in the diagram until we found the tipping point. This time we will do the same, but we vary the CO₂ concentration, and keep S fixed at its default present day value. \"\"\" md\"\"\" Below we have an empty diagram, which is already set up with a CO₂ vs T diagram, with a logarithmic horizontal axis. Now it's your turn We have written some pointers below to help you, but feel free to do it your own way. \"\"\" md\"\"\" We used two helper functions \"\"\" function add cold hot areas p left, right xlims p plot p, left, right , 60, 60 , fillrange 10., 10. , fillalpha 0.3, c lightblue, label nothing annotate p, left 12, 19, text \"completely\\nfrozen\", 10, darkblue, left plot p, left, right , 10, 10 , fillrange 80., 80. , fillalpha 0.09, c red, lw 0., label nothing annotate p, left 12, 15, text \"no ice\", 10, darkred, left end function add reference points p plot p, Model.CO2 PI, Model.CO2 PI , 55, 75 , color grey, alpha 0.3, lw 8, label \"Pre industrial CO2\" plot p, Model.CO2 PI , Model.T0 , shape circle, color orange, markersize 8, label \"Our preindustrial climate\" plot p, Model.CO2 PI , 38.3 , shape circle, color aqua, markersize 8, label \"Alternate preindustrial climate\" end md\"\"\" 👉 Create a slider for `CO2` between `CO2min` and `CO2max`. Just like the horizontal axis of our plot, we want the slider to be logarithmic . \"\"\" md\"\"\" 👉 Write a function `step model ` that takes an existing `ebm` and `new CO2`, which performs a step of our interactive process Reset the model by setting the `ebm.t` and `ebm.T` arrays to a single element. Which value? Assign a new function to `ebm.CO2`. What function? Run the model. \"\"\" function step model ebm Model.EBM, CO2 Real your code here return ebm end md\"\"\" 👉 Inside the plot cell, call the function `step model `. \"\"\" md\"\"\" Parameters \"\"\" CO2min 10 CO2max 1 000 000 Tneo 48 ebm Model.EBM Tneo, 0., 5., Model.CO2 const let p plot xlims CO2min, CO2max , ylims 55, 75 , xaxis log, xlabel \"CO2 concentration ppm \", ylabel \"Global temperature T °C \", title \"Earth's CO2 concentration bifurcation diagram\", legend topleft add cold hot areas p add reference points p your code here plot p, ebm.CO2 ebm.t end , ebm.T end , label nothing, color black, shape circle, end | as svg md\"\"\" The albedo feedback is implemented by the methods below \"\"\" function α T α0 Model.α, αi 0.5, ΔT 10. if T ΔT return αi elseif ΔT T ΔT return αi α0 αi T ΔT 2ΔT elseif T ΔT return α0 end end function Model.timestep ebm ebm.α α ebm.T end Added this line append ebm.T, ebm.T end ebm.Δt Model.tendency ebm append ebm.t, ebm.t end ebm.Δt end md\"\"\" If you like, make the visualization more informative Like in the lecture notebook, you could add a trail behind the black dot, or you could plot the stable and unstable branches. It's up to you \"\"\" md\"\"\" Exercise 2.2 👉 Find the lowest CO₂ concentration necessary to melt the Snowball, programmatically i.e., using code . \"\"\" co2 to melt snowball let missing end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"The function `findfirst` might be helpful.\" hint md\" ```julia bind log CO2 Slider ❓ ``` ```julia CO2 10^log CO2 ``` \" hint md\"Use a condition on the albedo or temperature to check whether the Snowball has melted.\" hint md\"Start by writing a function `equilibrium temperature CO2 ` which creates a new `EBM` at the Snowball Earth temperature T Tneo and returns the final temperature for a given CO2 level.\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" TODO html\" span style 'display inline font size 2em color purple font weight 900 ' TODO span \" "},{"url":"homework/hw2/","title":"Convolutions","tags":["homework","module1","track_data","track_julia","track_climate","track_math","convolution","matrix","interactive","image","type","webcam","programming"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 section 4.5 order 4.5 homework number 2 title \"Convolutions\" layout \"layout.jlhtml\" tags \"homework\", \"module1\", \"track data\", \"track julia\", \"track climate\", \"track math\", \"convolution\", \"matrix\", \"interactive\", \"image\", \"type\", \"webcam\", \"programming\" description \"Create your own image filters using mathematical convolution \" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Images, ImageIO, FileIO using PlutoUI using HypertextLiteral using OffsetArrays end md\"\"\" homework 2, version 3 \"\"\" md\"\"\" Homework 2 convolutions `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Exercise 1 Convolutions in 1D As we have seen in the lectures, we can produce cool effects using the mathematical technique of convolutions . We input one image M and get a new image M' back. Conceptually we think of M as a matrix. In practice, in Julia it will be a `Matrix` of color objects, and we may need to take that into account. Ideally, however, we should write a generic function that will work for any type of data contained in the matrix. A convolution works on a small window of an image, i.e. a region centered around a given point i, j . We will suppose that the window is a square region with odd side length 2\\ell 1 , running from \\ell, \\ldots, 0, \\ldots, \\ell . The result of the convolution over a given window, centred at the point i, j is a single number this number is the value that we will use for M' i, j . Note that neighbouring windows overlap. To get started, in Exercise 1 we'll restrict ourselves to convolutions in 1D. So a window is just a 1D region from \\ell to \\ell . \"\"\" md\"\"\" Exercise 1.1 Let's create a vector `v` of random numbers of length `n 100`. \"\"\" n 60 v rand n md\" Feel free to experiment with different values Let's use the function `colored line` to view this 1D number array as a 1D image. \" begin colored line x Vector hcat Gray. Float64. x ' colored line x Any nothing end colored line v md\"👉 Try changing `n` and `v` around. Notice that you can run the cell `v rand n ` again to regenerate new random values.\" md\"\"\" Exercise 1.2 We need to decide how to handle the boundary conditions , i.e. what happens if we try to access a position in the vector `v` beyond `1 n`. The simplest solution is to assume that v i is 0 outside the original vector however, this may lead to strange boundary effects. A better solution is to use the closest value that is inside the vector. Effectively we are extending the vector and copying the extreme values into the extended positions. Indeed, this is one way we could implement this these extra positions are called ghost cells . 👉 Write a function `extend v, i ` that checks whether the position i is inside `1 n`. If so, return the HTML \" br \" ``i``th component of `v` otherwise, return the nearest end value. \"\"\" function extend v AbstractVector, i return missing end md\" Some test cases \" example vector 0.8, 0.2, 0.1, 0.7, 0.6, 0.4 colored line example vector md\" Extended with 0 \" colored line 0, 0, example vector..., 0, 0 md\" Extended with your `extend` function \" md\"\"\" Exercise 1.3 👉 Write or copy the `mean` function from Homework 1, which takes a vector and returns the mean. \"\"\" function mean v return missing end md\"\"\" 👉 Write a function `box blur v, l ` that blurs a vector `v` with a window of length `l` by averaging the elements within a window from \\ell to \\ell . This is called a box blur . Use your function `extend` to handle the boundaries correctly. Return a vector of the same size as `v`. \"\"\" function box blur v AbstractArray, l return missing end colored line box blur example vector, 1 let try test v rand n original copy test v box blur test v, 5 if test v original md\"\"\" danger \"Oopsie \" It looks like your function modifies `v`. Can you write it without doing so? Maybe you can use `copy`. \"\"\" end catch end end md\"\"\" Exercise 1.4 👉 Apply the box blur to your vector `v`. Show the original and the new vector by creating two cells that call `colored line`. Make the parameter \\ell interactive, and call it `l box` instead of `l` to avoid a naming conflict. \"\"\" md\"\"\" Exercise 1.5 The box blur is a simple example of a convolution , i.e. a linear function of a window around each point, given by v' i \\sum m \\, v i m \\, k m , where k is a vector called a kernel . Again, we need to take care about what happens if v i m falls off the end of the vector. 👉 Write a function `convolve v, k ` that performs this convolution. You need to think of the vector k as being centred on the position i . So m in the above formula runs between \\ell and \\ell , where 2\\ell 1 is the length of the vector k . You will either need to do the necessary manipulation of indices by hand, or use the `OffsetArrays.jl` package. \"\"\" function convolve v AbstractVector, k return missing end md\" Edit the cell above, or create a new cell with your own test cases \" md\"\"\" Exercise 1.6 👉 Define a function `box blur kernel l ` which returns a kernel i.e. a vector which, when used as the kernel in `convolve`, will reproduce a box blur of length `l`. \"\"\" function box blur kernel l return missing end bind box kernel l Slider 1 5 box blur kernel test box blur kernel box kernel l md\"\"\" Let's apply your kernel to our test vector `v` first cell , and compare the result to our previous box blur function second cell . The two should be identical. \"\"\" let result box blur v, box kernel l colored line result end md\"\"\" Exercise 1.7 👉 Write a function `gaussian kernel`. The definition of a Gaussian in 1D is G x \\frac 1 \\sqrt 2\\pi \\sigma^2 \\exp \\left \\frac x^2 2\\sigma^2 \\right , or as a Julia function \"\"\" md\"\"\" Write a function `gauss` that takes `σ` as a keyword argument and implements this function. \"\"\" gauss x Real σ 1 1 sqrt 2π σ^2 exp x^2 2 σ^2 md\"\"\" We need to sample i.e. evaluate this at each pixel in an interval of length 2n 1 , and then normalize so that the sum of the resulting kernel is 1. \"\"\" function gaussian kernel 1D n σ 1 return missing end colored line gaussian kernel 1D 4 σ 1 md\"\"\" You can edit the cell above to test your kernel function Let's try applying it in a convolution. \"\"\" bind gaussian kernel size 1D Slider 0 6 function create bar x zeros 100 x 41 60 . 1 x end md\"\"\" Exercise 2 Convolutions in 2D Now let's move to 2D images. The convolution is then given by a kernel matrix K M' i, j \\sum k, l \\, M i k, j l \\, K k, l , where the sum is over the possible values of k and l in the window. Again we think of the window as being centered at i, j . A common notation for this operation is \\star ```math M' M \\star K ``` \"\"\" md\"\"\" Exercise 2.1 👉 Write a new method for `extend` that takes a matrix `M` and indices `i` and `j`, and returns the closest element of the matrix. \"\"\" function extend M AbstractMatrix, i, j return missing end extend 5,6,7 , 1 extend 5,6,7 , 8 extend 5,6,7 , 10 if extend v,1 missing missing else colored line extend example vector, i for i in 1 length example vector 2 end md\" Let's test it \" small image Gray. rand 5,5 md\" Extended with `0` \" get small image, i, j , Gray 0 for i,j in Iterators.product 1 7, 1 7 md\" Extended with your `extend` function \" extend small image, i, j for i,j in Iterators.product 1 7, 1 7 md\"\"\" Extending Philip \"\"\" url \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" philip filename download url download to a local file. The filename is returned philip load philip filename philip head philip 470 800, 140 410 extend philip head, i, j for i in 50 size philip head,1 51, j in 50 size philip head,2 51 md\"\"\" Exercise 2.2 👉 Implement a new method `convolve M, K ` that applies a convolution to a 2D array `M`, using a 2D kernel `K`. Use your new method `extend` from the last exercise. \"\"\" function convolve M AbstractMatrix, K AbstractMatrix return missing end test convolution let v 1, 10, 100, 1000, 10000 k 1, 1, 0 convolve v, k end colored line test convolution let result convolve v, box blur kernel test colored line result end test gauss 1D a let k gaussian kernel 1D gaussian kernel size 1D if k missing convolve v, k end end colored line test gauss 1D a test gauss 1D b let v create bar k gaussian kernel 1D gaussian kernel size 1D if k missing convolve v, k end end colored line test gauss 1D b md\" Let's test it out 🎃 \" test image with border get small image, i, j , Gray 0 for i,j in Iterators.product 1 7, 1 7 K test 0 0 0 1 2 0 1 2 0 0 0 convolve test image with border, K test md\" Edit `K test` to create your own test case \" convolve philip head, K test md\"\"\" You can create all sorts of effects by choosing the kernel in a smart way. Today, we will implement two special kernels, to produce a Gaussian blur and a Sobel edge detection filter. Make sure that you have watched the lecture about convolutions \"\"\" md\"\"\" Exercise 2.3 The 2D Gaussian kernel will be defined using G x,y \\frac 1 2\\pi \\sigma^2 \\exp\\left \\frac x^2 y^2 2\\sigma^2 \\right How can you express this mathematically using the 1D Gaussian function that we defined before? \"\"\" gauss x, y σ 1 2π σ^2 gauss x σ σ gauss y σ σ md\"\"\" 👉 Write a function that applies a Gaussian blur to an image. Use your previous functions, and add cells to write helper functions as needed \"\"\" function with gaussian blur image σ 3, l 5 return missing end md\" Let's make it interactive. 💫 \" bind face σ Slider 0.1 0.1 10 show value true bind face l Slider 0 20 show value true md\"\"\" When you set `face σ` to a low number e.g. `2.0` , what effect does `face l` have? And vice versa? \"\"\" md\"\"\" Exercise 2.4 👉 Create a Sobel edge detection filter . Here, we will need to create two filters that separately detect edges in the horizontal and vertical directions, given by the following kernels ```math G x \\begin bmatrix 1 & 0 & 1 \\\\ 2 & 0 & 2 \\\\ 1 & 0 & 1 \\\\ \\end bmatrix \\qquad G y \\begin bmatrix 1 & 2 & 1 \\\\ 0 & 0 & 0 \\\\ 1 & 2 & 1 \\\\ \\end bmatrix ``` We can think of these filters as derivatives in the x and y directions, as we discussed in lectures. Then we combine them by finding the magnitude of the gradient in the sense of multivariate calculus by defining G \\text total \\sqrt G x^2 G y^2 , where each operation applies element wise on the matrices. Use your previous functions, and add cells to write helper functions as needed \"\"\" function with sobel edge detect image return missing end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"Have a look at the lecture notes to see examples of adding interactivity with a slider. You can read the Interactivity and the PlutoUI sample notebooks to learn more, you can find them in Pluto's main menu. Right click the Pluto logo in the top left Open in new tab .\" md\"\"\" You can use the `÷` operator you type `\\div TAB ` to get it with autocomplete to do integer division . For example ```julia 8 6 ≈ 1.3333333 a floating point number 8 6 4 3 a fraction 8 ÷ 6 1 an integer ``` \"\"\" | hint hint md\"`num rows, num columns size M `\" hint md\"`num rows, num columns size K `\" hint md\"Can we just copy the 1D code? What is different in 2D?\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next exercise.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let result gaussian kernel 1D 5 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractVector keep working md\"The returned object is not a `Vector`.\" elseif size result 11, hint md\"The returned vector has the wrong dimensions.\" elseif sum result ≈ 1.0 keep working md\"You need to normalize the result.\" elseif gaussian kernel 1D 3 σ 1 gaussian kernel 1D 3 σ 2 keep working md\"Use the keyword argument `σ` in your function.\" else correct end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined extend not defined extend else let result extend 6,7 , 10 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 6 || extend 6,7 ,10 7 keep working else correct end end end if isdefined convolve not defined convolve else let x 1, 10, 100 result convolve x, 0, 1, 1 shouldbe 11, 110, 200 shouldbe2 2, 11, 110 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractVector keep working md\"The returned object is not a `Vector`.\" elseif size result size x keep working md\"The returned vector has the wrong dimensions.\" elseif result shouldbe && result shouldbe2 keep working else correct end end end if isdefined box blur kernel not defined box blur kernel else let result box blur kernel 2 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractVector keep working md\"The returned object is not a `Vector`.\" elseif size result 5, hint md\"The returned vector has the wrong dimensions.\" else x 1, 10, 100 result1 box blur x, 2 result2 convolve x, result if result1 ≈ result2 correct else keep working end end end end if isdefined extend not defined extend else let input 42 37 1 0 result extend input, 2, 2 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 42 || extend input, 1, 3 37 keep working else correct end end end bigbreak html\" br br br br br \" bigbreak bigbreak bigbreak bigbreak function camera input max size 200, default url \"https i.imgur.com SUmi94P.png\" \"\"\" span class \"pl image waiting for permission\" style .pl image.popped out position fixed top 0 right 0 z index 5 .pl image video container width 250px .pl image video border radius 1rem 1rem 0 0 .pl image.waiting for permission video container display none .pl image prompt display none .pl image.waiting for permission prompt width 250px height 200px display grid place items center font family monospace font weight bold text decoration underline cursor pointer border 5px dashed rgba 0,0,0,.5 .pl image video display block .pl image .bar width inherit display flex z index 6 .pl image .bar top position absolute flex direction column .pl image .bar bottom background black border radius 0 0 1rem 1rem .pl image .bar button flex 0 0 auto background rgba 255,255,255,.8 border none width 2rem height 2rem border radius 100% cursor pointer z index 7 .pl image .bar button shutter width 3rem height 3rem margin 1.5rem auto .2rem auto .pl image video.takepicture animation pictureflash 200ms linear keyframes pictureflash 0% filter grayscale 1.0 contrast 2.0 100% filter grayscale 0.0 contrast 1.0 style div id \"video container\" div id \"top\" class \"bar\" button id \"stop\" title \"Stop video\" ✖ button button id \"pop out\" title \"Pop out pop in\" ⏏ button div video playsinline autoplay video div id \"bottom\" class \"bar\" button id \"shutter\" title \"Click to take a picture\" 📷 button div div div id \"prompt\" span Enable webcam span div script based on https github.com fonsp printi static by the same author const span currentScript.parentElement const video span.querySelector \"video\" const popout span.querySelector \"button pop out\" const stop span.querySelector \"button stop\" const shutter span.querySelector \"button shutter\" const prompt span.querySelector \".pl image prompt\" const maxsize max size const send source source, src width, src height const scale Math.min 1.0, maxsize src width, maxsize src height const width Math.floor src width scale const height Math.floor src height scale const canvas html` canvas width \\ width height \\ height ` const ctx canvas.getContext \"2d\" ctx.drawImage source, 0, 0, width, height span.value width width, height height, data ctx.getImageData 0, 0, width, height .data, span.dispatchEvent new CustomEvent \"input\" const clear camera window.stream.getTracks .forEach s s.stop video.srcObject null span.classList.add \"waiting for permission\" prompt.onclick navigator.mediaDevices.getUserMedia audio false, video facingMode \"environment\", , .then function stream stream.onend console.log window.stream stream video.srcObject stream window.cameraConnected true video.controls false video.play video.controls false span.classList.remove \"waiting for permission\" .catch function error console.log error stop.onclick clear camera popout.onclick span.classList.toggle \"popped out\" shutter.onclick const cl video.classList cl.remove \"takepicture\" void video.offsetHeight cl.add \"takepicture\" video.play video.controls false console.log video send source video, video.videoWidth, video.videoHeight document.addEventListener \"visibilitychange\", if document.visibilityState \"visible\" clear camera Set a default image const img html` img crossOrigin \"anonymous\" ` img.onload console.log \"helloo\" send source img, img.width, img.height img.src \" default url \" console.log img script span \"\"\" | HTML end bind gauss raw camera data camera input max size 100 bind sobel raw camera data camera input max size 200 function process raw camera data raw camera data the raw image data is a long byte array, we need to transform it into something more \"Julian\" something with more structure . The encoding of the raw byte stream is every 4 bytes is a single pixel every pixel has 4 values Red, Green, Blue, Alpha we ignore alpha for this notebook So to get the red values for each pixel, we take every 4th value, starting at the 1st reds flat UInt8. raw camera data \"data\" 1 4 end greens flat UInt8. raw camera data \"data\" 2 4 end blues flat UInt8. raw camera data \"data\" 3 4 end but these are still 1 dimensional arrays, nicknamed 'flat' arrays We will 'reshape' this into 2D arrays width raw camera data \"width\" height raw camera data \"height\" shuffle and flip to get it in the right shape reds reshape reds flat, width, height ' 255.0 greens reshape greens flat, width, height ' 255.0 blues reshape blues flat, width, height ' 255.0 we have our 2D array for each color Let's create a single 2D array, where each value contains the R, G and B value of that pixel RGB. reds, greens, blues end gauss camera image process raw camera data gauss raw camera data with gaussian blur gauss camera image σ face σ, l face l sobel camera image Gray. process raw camera data sobel raw camera data Gray. with sobel edge detect sobel camera image "},{"url":"homework/hw3/","title":"Structure and language","tags":["homework","module1","track_julia","track_math","track_climate","linguistics","programming","interactive","type","structure","data","artificial intelligence"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 section 6.5 order 6.5 homework number 3 title \"Structure and language\" layout \"layout.jlhtml\" tags \"homework\", \"module1\", \"track julia\", \"track math\", \"track climate\", \"linguistics\", \"programming\", \"interactive\", \"type\", \"structure\", \"data\", \"artificial intelligence\" description \"Automatically detect the language of a piece of text, and generate realistic looking random text \" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors using PlutoUI using Compose using LinearAlgebra end md\"\"\" homework 3, version 7 \"\"\" md\"\"\" Homework 3 Structure and Language `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Introduction \"\"\" md\"\"\" So far in the class the data that we have been dealing with has mainly been in the form of images. But, of course, we know that data comes in many other forms too, as we briefly discussed in the first lecture. In this homework we will look at another very important data source written text in natural language . The word \"natural\" here is to distinguish human natural languages from computer languages. We will both analyse actual text and try to generate random text that looks like natural language. Both the analysis and synthesis of natural language are key components of artificial intelligence and are the subject of much current research https en.wikipedia.org wiki GPT 3 . \"\"\" md\"\"\" Exercise 1 Language detection In this exercise we will create a very simple Artificial Intelligence . Natural language can be quite messy, but hidden in this mess is structure , which we will look for today. Let's start with some obvious structure in English text the set of characters that we write the language in. If we generate random text by sampling choosing random characters `Char` in Julia , it does not look like English \"\"\" rand Char, 5 sample 5 random characters String rand Char, 40 join into a string md\"\"\" `Char` in Julia is the type for a Unicode https en.wikipedia.org wiki Unicode character, which includes characters like `日` and `⛄`. \"\"\" md\"\"\" Instead, let's define an alphabet , and only use those letters to sample from. To keep things simple we will ignore punctuation, capitalization, etc., and use only the following 27 characters English letters plus \"space\" \"\"\" alphabet 'a' 'z' ' ' includes the space character md\"\"\" Let's sample random characters from our alphabet \"\"\" Text String rand alphabet, 40 md\"\"\" That already looks a lot better than our first attempt But it still does not look like English text we can do better. Frequency table English words are not well modelled by this random Latin characters model. Our first observation is that some letters are more common than others . To put this observation into practice, we would like to have the frequency table of the Latin alphabet. We could search for it online, but it is actually very simple to calculate ourselves The only thing we need is a representative sample of English text. The following samples are from Wikipedia, but feel free to type in your own sample You can also enter a sample of a different language, if that language can be expressed in the Latin alphabet. Remember that the html\" img src 'https cdn.jsdelivr.net gh ionic team ionicons 5.0.0 src svg eye outline.svg' style 'width 1em height 1em margin bottom .2em ' \" button on the left of a cell will show or hide the code. We also include a sample of Spanish, which we'll use later \"\"\" md\"\"\" Exercise 1.1 Data cleaning Looking at the sample, we see that it is quite messy it contains punctuation, accented letters and numbers. For our analysis, we are only interested in our 27 character alphabet i.e. `'a'` through `'z'` plus `' '` . We are going to clean the data using the Julia function `filter`. \"\"\" filter isodd, 6, 7, 8, 9, 5 md\"\"\" `filter` takes two arguments a function and a collection . The function is applied to each element of the collection, and it must return either `true` or `false` for each element. Such a function is often called a predicate . If the result is `true`, then that element is included in the final collection. Did you notice something cool? Functions are also just objects in Julia, and you can use them as arguments to other functions Fons thinks this is super cool. html\" br \" We have written a function `isinalphabet`, which takes a character and returns a boolean \"\"\" function isinalphabet character character ∈ alphabet end isinalphabet 'a' , isinalphabet ' ' md\"👉 Use `filter` to extract just the characters from our alphabet out of `messy sentence 1` \" messy sentence 1 \" wow 2020 ¥500 blingbling \" cleaned sentence 1 missing md\"\"\" html\" br \" We are not interested in the capitalization of letters i.e. `'A'` vs `'a'` , so we want to map these to lower case before we apply our filter. If we don't, all upper case letters would get deleted. Julia has a `map` function to do exactly this. Like `filter`, its first argument is the function we want to map over the vector in the second argument. \"\"\" md\"👉 Use the function `lowercase` to convert `messy sentence 2` into a lower case string, and then use `filter` to extract only the characters from our alphabet.\" messy sentence 2 \"Awesome 😍\" cleaned sentence 2 missing md\"\"\" html\" br \" Finally, we need to deal with accents simply deleting accented characters from the source text might deform it too much. We can add accented letters to our alphabet, but a simpler solution is to replace accented letters with the corresponding unaccented base character. We have written a function `unaccent` that does just that. \"\"\" french word \"Égalité \" import Unicode \"\"\" Turn `\"áéíóúüñ asdf\"` into `\"aeiouun asdf\"`. \"\"\" unaccent str Unicode.normalize str, stripmark true samples English \"\"\" Although the word forest is commonly used, there is no universally recognised precise definition, with more than 800 definitions of forest used around the world. 4 Although a forest is usually defined by the presence of trees, under many definitions an area completely lacking trees may still be considered a forest if it grew trees in the past, will grow trees in the future, 9 or was legally designated as a forest regardless of vegetation type. 10 11 The word forest derives from the Old French forest also forès , denoting \"forest, vast expanse covered by trees\" forest was first introduced into English as the word denoting wild land set aside for hunting 14 without the necessity in definition of having trees on the land. 15 Possibly a borrowing, probably via Frankish or Old High German, of the Medieval Latin foresta, denoting \"open wood\", Carolingian scribes first used foresta in the Capitularies of Charlemagne specifically to denote the royal hunting grounds of the King. The word was not endemic to Romance languages, e. g. native words for forest in the Romance languages derived from the Latin silva, which denoted \"forest\" and \"wood land \" confer the English sylva and sylvan confer the Italian, Spanish, and Portuguese selva the Romanian silvă and the Old French selve, and cognates in Romance languages, e. g. the Italian foresta, Spanish and Portuguese floresta, etc., are all ultimately derivations of the French word. \"\"\", Spanish \"\"\" Un bosque es un ecosistema donde la vegetación predominante la constituyen los árboles y matas.1​ Estas comunidades de plantas cubren grandes áreas del globo terráqueo y funcionan como hábitats para los animales, moduladores de flujos hidrológicos y conservadores del suelo, constituyendo uno de los aspectos más importantes de la biosfera de la Tierra. Aunque a menudo se han considerado como consumidores de dióxido de carbono atmosférico, los bosques maduros son prácticamente neutros en cuanto al carbono, y son solamente los alterados y los jóvenes los que actúan como dichos consumidores.2​3​ De cualquier manera, los bosques maduros juegan un importante papel en el ciclo global del carbono, como reservorios estables de carbono y su eliminación conlleva un incremento de los niveles de dióxido de carbono atmosférico. Los bosques pueden hallarse en todas las regiones capaces de mantener el crecimiento de árboles, hasta la línea de árboles, excepto donde la frecuencia de fuego natural es demasiado alta, o donde el ambiente ha sido perjudicado por procesos naturales o por actividades humanas. Los bosques a veces contienen muchas especies de árboles dentro de una pequeña área como la selva lluviosa tropical y el bosque templado caducifolio , o relativamente pocas especies en áreas grandes por ejemplo, la taiga y bosques áridos montañosos de coníferas . Los bosques son a menudo hogar de muchos animales y especies de plantas, y la biomasa por área de unidad es alta comparada a otras comunidades de vegetación. La mayor parte de esta biomasa se halla en el subsuelo en los sistemas de raíces y como detritos de plantas parcialmente descompuestos. El componente leñoso de un bosque contiene lignina, cuya descomposición es relativamente lenta comparado con otros materiales orgánicos como la celulosa y otros carbohidratos. Los bosques son áreas naturales y silvestre \"\"\" | unaccent, unaccent french word md\"\"\" html\" br \" 👉 Let's put everything together. Write a function `clean` that takes a string, and returns a cleaned version, where accented letters are replaced by their base characters upper case letters are converted to lower case it is filtered to only contain characters from `alphabet` \"\"\" function clean text return missing end clean \"Crème brûlée est mon plat préféré.\" first sample clean first samples function letter frequencies txt ismissing txt && return missing f count. string. alphabet , txt f . sum f end alphabet sample freqs letter frequencies first sample md\"\"\" The result is a 27 element array, with values between `0.0` and `1.0`. These values correspond to the frequency of each letter. `sample freqs i 0.0` means that the i th letter did not occur in your sample, and `sample freqs i 0.1` means that 10% of the letters in the sample are the i th letter. To make it easier to convert between a character from the alphabet and its index, we have the following function \"\"\" index of letter letter findfirst isequal letter , alphabet index of letter 'a' , index of letter 'b' , index of letter ' ' md\"\"\" html\" br \" 👉 Which letters from the alphabet did not occur in the sample? \"\"\" unused letters 'a', 'b', 'c' replace with your answer md\"\"\" Random letters at the correct frequencies \"\"\" md\"\"\" By considering the frequencies of letters in English, we see that our model is already a lot better Our next observation is that some letter combinations are more common than others . Our current model thinks that `potato` is just as 'English' as `ooaptt`. In the next section, we will quantify these transition frequencies , and use it to improve our model. \"\"\" function rand sample frequencies x rand findfirst z z x, cumsum frequencies . sum frequencies end function rand sample letter frequencies alphabet rand sample frequencies end function transition counts cleaned sample count string a, b , cleaned sample for a in alphabet, b in alphabet end normalize array x x . sum x transition frequencies normalize array ∘ transition counts transition frequencies first sample md\"What we get is a 27 by 27 matrix . Each entry corresponds to a character pair. The row corresponds to the first character, the column is the second character. Let's visualize this \" md\"\"\" The brightness of each letter pair indicates how frequent that pair is here space is indicated as ` `. \"\"\" md\"\"\" Answer the following questions with respect to the cleaned English sample text , which we called `first sample`. Let's also give the transition matrix a name \"\"\" sample freq matrix transition frequencies first sample if first sample missing md\"\"\" danger \"Don't worry \" 👆 These errors will disappear automatically once you have completed the earlier exercises \"\"\" end md\"\"\"👉 What is the frequency of the combination `\"th\"`?\"\"\" th frequency missing md\"\"\"👉 What about `\"ht\"`?\"\"\" ht frequency missing md\"\"\" 👉 Write code to find which le tt ers appeared doubled in our sample. \"\"\" double letters 'a', 'b', 'c' replace with your answer md\"\"\" 👉 Which letter is most likely to follow a W ? You are free to do this partially by hand, partially using code, whatever is easiest \"\"\" most likely to follow w 'x' replace with your answer md\"\"\" 👉 Which letter is most likely to precede a W ? You are free to do this partially by hand, partially using code, whatever is easiest \"\"\" most likely to precede w 'x' replace with your answer md\"\"\" 👉 What is the sum of each row? What is the sum of each column? What is the sum of the matrix? How can we interpret these values?\" \"\"\" row sums missing col sums missing row col answer md\"\"\" Blablabla \"\"\" md\"\"\" We can use the measured transition frequencies to generate text in a way that it has the same transition frequencies as our original sample. Our generated text is starting to look like real language \"\"\" bind ex23 sample Select v String k for k, v in zip fieldnames typeof samples , samples md\"\"\" Random letters from the alphabet \"\"\" md\"\"\" Random letters at the correct frequencies \"\"\" md\"\"\" Random letters at the correct transition frequencies \"\"\" function sample text A, n first index rand sample vec sum A, dims 1 indices reduce 1 n init first index do word, prev last word freq normalize array A prev, next rand sample freq word..., next end String alphabet indices end md\"\"\" It looks like we have a decent language model, in the sense that it understands transition frequencies in the language. In the demo above, try switching the language between join string. fieldnames typeof samples , \" and \" the generated text clearly looks more like one or the other, demonstrating that the model can capture differences between the two languages. What's remarkable is that our \"training data\" was just a single paragraph per language. In this exercise, we will use our model to write a classifier a program that automatically classifies a text as either join string. fieldnames typeof samples , \" or \" . This is not a difficult task you can download dictionaries for both languages, and count matches but we are doing something much more exciting we only use a single paragraph of each language, and we use a language model as classifier. \"\"\" html\" h4 id 'mystery detect' Mystery sample h4 p Enter some text here we will detect in which language it is written p \" dont delete me bind mystery sample TextField 70, 8 , default \"\"\" Small boats are typically found on inland waterways such as rivers and lakes, or in protected coastal areas. However, some boats, such as the whaleboat, were intended for use in an offshore environment. In modern naval terms, a boat is a vessel small enough to be carried aboard a ship. Anomalous definitions exist, as lake freighters 1,000 feet 300 m long on the Great Lakes are called \"boats\". \"\"\" mystery sample md\"\"\" Let's compute the transition frequencies of our mystery sample Type some text in the box above, and check whether the frequency matrix updates. \"\"\" transition frequencies mystery sample md\"\"\" Our model will compare the transition frequencies of our mystery sample to those of our two language samples. The closest match will be our detected language. The only question left is How do we compare two matrices? When two matrices are almost equal, but not exactly, we want to quantify the distance between them. 👉 Write a function called `matrix distance` which takes 2 matrices of the same size and finds the distance between them by 1. Subtracting corresponding elements 2. Finding the absolute value of the difference 3. Summing the differences \"\"\" function matrix distance A, B return missing do something with A . B end distances map samples do sample matrix distance transition frequencies mystery sample , transition frequencies sample end try assert ismissing distances.English \"\"\" h2 It looks like this text is argmin distances h2 \"\"\" | HTML catch end md\"\"\" Exercise 2 Language generation Our model from Exercise 1 has the property that it can easily be 'reversed' to generate text. While this is useful to demonstrate its structure, the produced text is mostly meaningless it fails to model words, let alone sentence structure. To take our model one step further, we are going to generalize what we have done so far. Instead of looking at letter combinations , we will model word combinations . And instead of analyzing the frequencies of bigrams combinations of two letters , we are going to analyze n grams . Dataset This also means that we are going to need a larger dataset to train our model on the number of English words and their combinations is much higher than the number of letters. We will train our model on the novel Emma 1815 , by Jane Austen https en.wikipedia.org wiki Emma novel . This work is in the public domain, which means that we can download the whole book as a text file from `archive.org`. We've done the process of downloading and cleaning already, and we have split the text into word and punctuation tokens. \"\"\" emma let raw text read download \"https ia800303.us.archive.org 24 items EmmaJaneAusten 753 emma pdf djvu.txt\" , String first words \"Emma Woodhouse\" last words \"THE END\" start index findfirst first words, raw text 1 stop index findlast last words, raw text end raw text start index stop index end function splitwords text clean up whitespace cleantext replace text, r\"\\s \" \" \" split on whitespace or other word boundaries tokens split cleantext, r\" \\s|\\b \" end emma words splitwords emma forest words splitwords samples.English md\"\"\" Exercise 2.1 bigrams revisited The goal of the upcoming exercises is to generalize what we have done in Exercise 1. To keep things simple, we split up our problem into smaller problems. The solution to any computational problem. First, here is a function that takes an array, and returns the array of all neighbour pairs from the original. For example, ```julia bigrams 1, 2, 3, 42 ``` gives ```julia 1,2 , 2,3 , 3,42 ``` We used integers as \"words\" in this example, but our function works with any type of word. \"\"\" function bigrams words starting positions 1 length words 1 map starting positions do i words i i 1 end end bigrams 1, 2, 3, 42 md\"\"\" 👉 Next, it's your turn to write a more general function `ngrams` that takes an array and a number n , and returns all subsequences of length n . For example ```julia ngrams 1, 2, 3, 42 , 3 ``` should give ```julia 1,2,3 , 2,3,42 ``` and ```julia ngrams 1, 2, 3, 42 , 2 bigrams 1, 2, 3, 42 ``` \"\"\" function ngrams words, n return missing end ngrams 1, 2, 3, 42 , 3 ngrams forest words, 4 md\"\"\" If you are stuck, you can write `ngrams words, n bigrams words ` ignoring the true value of n , and continue with the other exercises. Exercise 2.2 frequency matrix revisisted In Exercise 1 we use a 2D array to store the bigram frequencies, where each column or row corresponds to a character from the alphabet. To use trigrams we could store the frequencies in a 3D array, and so on. However, when counting words instead of letters we run into a problem A 3D array with one row, column and layer per word has too many elements to store on our computer \"\"\" md\"\"\" Emma consists of length Set emma words unique words. This means that there are Int floor length Set emma words ^3 10^9 billion possible trigrams that's too many \"\"\" md\"\"\" html\" br \" Although the frequency array would be very large, most entries are zero . For example, \"Emma\" is a common word, but the sequence of words \"Emma Emma Emma\" never occurs in the novel. We about the sparsity of the non zero entries in a matrix. When a matrix is sparse in this way, we can store the same information in a more efficient structure . Julia's `SparseArrays.jl` package https docs.julialang.org en v1 stdlib SparseArrays index.html might sound like a logical choice, but the arrays from that package support only 1D and 2D types, and we also want to directly index using strings, not just integers. So instead we will use a dictionary , or `Dict` in Julia. Take a close look at the next example. Note that you can click on the output to expand the data viewer. \"\"\" healthy Dict \"fruits\" \"🍎\", \"🍊\" , \"vegetables\" \"🌽\", \"🎃\", \"🍕\" healthy \"fruits\" md\"\"\" Did you notice something funny? The dictionary is unordered , this is why the entries were printed in reverse from the definition. You can dynamically add or change values of a `Dict` by assigning to `my dict key `. You can check whether a key already exists using `haskey my dict, key `. 👉 Use these two techniques to write a function `word counts` that takes an array of words, and returns a `Dict` with entries `word number of occurences`. For example ```julia word counts \"to\", \"be\", \"or\", \"not\", \"to\", \"be\" ``` should return ```julia Dict \"to\" 2, \"be\" 2, \"or\" 1, \"not\" 1 ``` \"\"\" function word counts words Vector counts Dict your code here return counts end word counts \"to\", \"be\", \"or\", \"not\", \"to\", \"be\" md\"\"\" 👉 Write code to find how many times `\"Emma\"` occurs in the book. \"\"\" emma count missing md\"\"\" Great Let's get back to our n grams. For the purpose of generating text, we are going to store a completion cache . This is a dictionary where each key is an n 1 gram, and the corresponding value is the vector of all those words which can complete it to an n gram. Let's look at an example ```julia let trigrams ngrams split \"to be or not to be that is the question\", \" \" , 3 cache completions cache trigrams cache Dict \"to\", \"be\" \"or\", \"that\" , \"be\", \"or\" \"not\" , \"or\", \"not\" \"to\" , ... end ``` So for trigrams the keys are the first 2 words of each trigram, and the values are arrays containing every third word of those trigrams. If the same n gram occurs multiple times e.g. \"said Emma laughing\" , then the last word \"laughing\" should also be stored multiple times. This will allow us to generate trigrams with the same frequencies as the original text. 👉 Write the function `completion cache`, which takes an array of ngrams i.e. an array of arrays of words, like the result of your `ngram` function , and returns a dictionary like described above. \"\"\" function completion cache grams cache Dict your code here cache end let trigrams ngrams split \"to be or not to be that is the question\", \" \" , 3 completion cache trigrams end md\"\"\" What is this cache telling us? In our sample text, the words \"to be\" were followed by \"or\" and by \"that\". So if we are generating text, and the last two words we wrote are \"to be\", we can look at the cache, and see that the next word can be either \"or\" or \"that\". \"\"\" md\"\"\" Exercise 2.4 write a novel We have everything we need to generate our own novel The final step is to sample random ngrams, in a way that each next ngram overlaps with the previous one. We've done this in the function `generate from ngrams` below feel free to look through the code, or to implement your own version. \"\"\" \"\"\" generate from ngrams grams, num words Given an array of ngrams i.e. an array of arrays of words , generate a sequence of `num words` words by sampling random ngrams. \"\"\" function generate from ngrams grams, num words n length first grams cache completion cache grams we need to start the sequence with at least n 1 words. a simple way to do so is to pick a random ngram sequence rand grams ... we iteratively add one more word at a time for i ∈ n 1 num words the previous n 1 words tail sequence end n 2 end possible next words completions cache tail choice rand completions push sequence, choice end sequence end \"Compute the ngrams of an array of words, but add the first n 1 at the end, to ensure that every ngram ends in the the beginning of another ngram.\" function ngrams circular words, n ngrams words..., words 1 n 1 ... , n end completion cache ngrams circular forest words, 3 \"\"\" generate source text AbstractString, num token n 3, use words true Given a source text, generate a `String` that \"looks like\" the original text by satisfying the same ngram frequency distribution as the original. \"\"\" function generate source text AbstractString, s n 3, use words true preprocess if use words splitwords else collect end words preprocess source text if length words n \"\" else grams ngrams circular words, n result generate from ngrams grams, s if use words join result, \" \" else String result end end end md\" Interactive demo Enter your own text in the box below, and use that as training data to generate anything \" bind generate demo sample TextField 50, 5 , default samples.English md\"\"\"Using bind generate sample n letters NumberField 1 5 grams for characters\"\"\" md\"\"\"Using bind generate sample n words NumberField 1 5 grams for words\"\"\" md\"\"\" Automatic Jane Austen Uncomment the cell below to generate some Jane Austen text \"\"\" generate emma, 100 n 4 | Quote md\" Function library Just some helper functions used in the notebook.\" function Quote text AbstractString text | Markdown.Paragraph | Markdown.BlockQuote | Markdown.MD end samples.English | Quote String rand alphabet, 400 | Quote if sample freqs missing String rand sample letter sample freqs for in 1 400 | Quote end String rand alphabet, 400 | Quote String rand sample letter letter frequencies ex23 sample for in 1 400 | Quote sample text transition frequencies clean ex23 sample , 400 | Quote generate generate demo sample, 400 n generate sample n letters, use words false | Quote generate generate demo sample, 100 n generate sample n words, use words true | Quote function compimg img, labels c d for c in replace alphabet, ' ' \" \" , d in replace alphabet, ' ' \" \" xmax, ymax size img xmin, ymin 0, 0 arr j 1, i 1 for i 1 ymax, j 1 xmax compose context units UnitBox xmin, ymin, xmax, ymax , fill vec img , compose context , fill \"white\" , font \"monospace\" , text first. arr . .1, last. arr . 0.6, labels , rectangle first. arr , last. arr , fill 1.0, length arr , fill 1.0, length arr end function show pair frequencies A imshow let to rgb x RGB 0.36x, 0.82x, 0.8x to rgb. A . maximum abs. A end compimg imshow end show pair frequencies transition frequencies first sample hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"You can answer this question without writing any code have a look at the values of `sample freqs`.\" hint md\"First answer this question by looking at the pair frequency image.\" hint md\"Start out with the same code as `bigrams`, and use the Julia documentation to learn how it works. How can we generalize the `bigram` function into the `ngram` function? It might help to do this on paper first.\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let output ngrams 1, 2, 3, 42 , 2 if output isa Missing still missing elseif output isa Vector Vector keep working md\"Make sure that `ngrams` returns an array of arrays.\" elseif output 1,2 , 2,3 , 3,42 if ngrams 1,2,3 , 1 1 , 2 , 3 if ngrams 1,2,3 , 3 1,2,3 if ngrams \"a\" ,1 \"a\" correct else keep working md\"`ngrams` should work with any type, not just integers \" end else keep working md\"`ngrams x, 3 ` did not give a correct result.\" end else keep working md\"`ngrams x, 1 ` did not give a correct result.\" end else keep working md\"`ngrams x, 2 ` did not give the correct bigrams. Start out with the same code as `bigrams`.\" end end let output word counts \"to\", \"be\", \"or\", \"not\", \"to\", \"be\" if output nothing keep working md\"Did you forget to write `return`?\" elseif output Dict still missing md\"Write your function `word counts` above.\" elseif output isa Dict keep working md\"Make sure that `word counts` returns a `Dict`.\" elseif output Dict \"to\" 2, \"be\" 2, \"or\" 1, \"not\" 1 correct else keep working end end if emma count isa Missing still missing elseif emma count 865 correct else keep working end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined messy sentence 1 not defined messy sentence 1 elseif isdefined cleaned sentence 1 not defined cleaned sentence 1 else if cleaned sentence 1 isa Missing still missing elseif cleaned sentence 1 isa Vector Char keep working md\"Use `String x ` to turn an array of characters `x` into a `String`.\" elseif cleaned sentence 1 filter isinalphabet, messy sentence 1 correct else keep working end end if isdefined messy sentence 2 not defined messy sentence 2 elseif isdefined cleaned sentence 2 not defined cleaned sentence 2 else if cleaned sentence 2 isa Missing still missing elseif cleaned sentence 2 isa Vector Char keep working md\"Use `String x ` to turn an array of characters `x` into a `String`.\" elseif cleaned sentence 2 filter isinalphabet, lowercase messy sentence 2 correct else keep working end end if isdefined clean not defined clean else let input \"Aè x1\" output clean input if output isa Missing still missing elseif output isa Vector Char keep working md\"Use `String x ` to turn an array of characters `x` into a `String`.\" elseif output \"ae x\" correct else keep working end end end if isdefined unused letters not defined unused letters else if sample freqs missing md\"\"\" warning \"Oopsie \" You need to complete the previous exercises first. \"\"\" elseif unused letters isa Missing still missing elseif unused letters isa String keep working md\"Use `collect` to turn a string into an array of characters.\" elseif Set index of letter. unused letters Set findall isequal 0.0 , sample freqs correct else keep working end end if isdefined th frequency not defined th frequency elseif isdefined ht frequency not defined ht frequency else if th frequency isa Missing || ht frequency isa Missing still missing elseif th frequency ht frequency keep working md\"Looks like your answers should be flipped. Which combination is more frequent in English?\" elseif th frequency sample freq matrix index of letter 't' , index of letter 'h' && ht frequency sample freq matrix index of letter 'h' , index of letter 't' correct else keep working end end if isdefined double letters not defined double letters else let result double letters if result isa Missing still missing elseif result isa String keep working md\"Use `collect` to turn a string into an array of characters.\" elseif result isa AbstractVector Char || result isa AbstractSet Char keep working md\"Make sure that `double letters` is a `Vector`.\" elseif Set result Set alphabet diag sample freq matrix . 0 correct elseif push Set result , ' ' Set alphabet diag sample freq matrix . 0 almost md\"We also consider the space `' '` as one of the letters in our `alphabet`.\" else keep working end end end if isdefined most likely to follow w not defined most likely to follow w else let result most likely to follow w if result isa Missing still missing elseif result isa Char keep working md\"Make sure that you return a `Char`. You might want to use the `alphabet` to index a character.\" elseif result alphabet map alphabet do c sample freq matrix index of letter 'w' , index of letter c end | argmax correct else keep working end end end if isdefined most likely to precede w not defined most likely to precede w else let result most likely to precede w if result isa Missing still missing elseif result isa Char keep working md\"Make sure that you return a `Char`. You might want to use the `alphabet` to index a character.\" elseif result alphabet map alphabet do c sample freq matrix index of letter c , index of letter 'w' end | argmax correct else keep working end end end if isdefined matrix distance not defined matrix distance else try let A rand Float64, 5, 4 B rand Float64, 5, 4 output matrix distance A,B if output isa Missing still missing elseif output isa Number keep working md\"Make sure that `matrix distance` returns a number.\" elseif output 0.0 keep working md\"Two different matrices should have non zero distance.\" else if matrix distance A,B 0 || matrix distance B,A 0 keep working md\"The distance between two matrices should always be positive.\" elseif matrix distance A,A 0 almost md\"The distance between two identical matrices should be zero.\" elseif matrix distance 1 1 , 0 0 0.0 almost md\"`matrix distance 1 1 , 0 0 ` should not be zero.\" else correct end end end catch keep working md\"The function errored.\" end end todo text HTML \"\"\" div style \"background rgb 220, 200, 255 padding 2em border radius 1em \" h1 TODO h1 repr MIME\"text html\" , text div \"\"\" bigbreak html\" br br br br br \" bigbreak md\"\"\" bigbreak Exercise 1.2 Letter frequencies We are going to count the frequency of each letter in this sample, after applying your `clean` function. Can you guess which character is most frequent? \"\"\" md\"\"\" bigbreak Now that we know the frequencies of letters in English, we can generate random text that already looks closer to English Random letters from the alphabet \"\"\" md\"\"\" bigbreak Exercise 1.3 Transition frequencies In the previous exercise we computed the frequency of each letter in the sample by counting their occurences, and then dividing by the total number of counts. In this exercise, we are going to count letter transitions , such as `aa`, `as`, `rt`, `yy`. Two letters might both be common, like `a` and `e`, but their combination, `ae`, is uncommon in English. To quantify this observation, we will do the same as in our last exercise we count occurences in a sample text , to create the transition frequency matrix . \"\"\" bigbreak md\"\"\" bigbreak Exercise 1.4 Language detection \"\"\" md\"\"\" We have written a cell that selects the language with the smallest distance to the mystery language. Make sure sure that `matrix distance` is working correctly, and scroll up mystery detect to the mystery text to see it in action Further reading It turns out that the SVD of the transition matrix can mysteriously group the alphabet into vowels and consonants, without any extra information. See this paper http languagelog.ldc.upenn.edu myl Moler1983.pdf if you want to try it yourself We found that removing the space from `alphabet` to match the paper gave better results. bigbreak \"\"\" bigbreak "},{"url":"homework/hw4/","title":"Dynamic programming","tags":["homework","module1","track_julia","track_math","structure","programming","dynamic programming","matrix","recursion"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 section 9.5 order 9.5 homework number 4 title \"Dynamic programming\" layout \"layout.jlhtml\" tags \"homework\", \"module1\", \"track julia\", \"track math\", \"structure\", \"programming\", \"dynamic programming\", \"matrix\", \"recursion\" description \"\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Images, ImageIO, FileIO, TestImages, ImageFiltering using Statistics using PlutoUI using BenchmarkTools end all image urls \"https wisetoast.com wp content uploads 2015 10 The Persistence of Memory salvador deli painting.jpg\" \"Salvador Dali — The Persistence of Memory replica \", \"https i.imgur.com 4SRnmkj.png\" \"Frida Kahlo — The Bride Frightened at Seeing Life Opened\", \"https upload.wikimedia.org wikipedia commons thumb 5 5b Hilma af Klint Group IX SUW%2C The Swan No. 1 %2813947%29.jpg 477px Hilma af Klint Group IX SUW%2C The Swan No. 1 %2813947%29.jpg\" \"Hilma Klint The Swan No. 1\", \"https upload.wikimedia.org wikipedia commons thumb a a4 Piet Mondriaan%2C 1930 Mondrian Composition II in Red%2C Blue%2C and Yellow.jpg 300px Piet Mondriaan%2C 1930 Mondrian Composition II in Red%2C Blue%2C and Yellow.jpg\" \"Piet Mondriaan Composition with Red, Blue and Yellow\", \"https user images.githubusercontent.com 6933510 110993432 950df980 8377 11eb 82e7 b7ce4a0d04bc.png\" \"Mario\", removal test image Gray. rand 4,4 begin brightness c RGB mean c.r, c.g, c.b brightness c RGBA mean c.r, c.g, c.b brightness c Gray gray c end convolve img, k imfilter img, reflect k uses ImageFiltering.jl package behaves the same way as the `convolve` function used in our lectures and homeworks float to color x RGB max 0, x , max 0, x , 0 energy ∇x, ∇y sqrt. ∇x.^2 . ∇y.^2 function energy img ∇y convolve brightness. img , Kernel.sobel 1 ∇x convolve brightness. img , Kernel.sobel 2 energy ∇x, ∇y end html\"\"\" iframe width \"100%\" height \"450px\" src \"https www.youtube.com embed rpB6zQNsbQU?start 777&end 833\" frameborder \"0\" allow \"accelerometer autoplay encrypted media gyroscope picture in picture\" allowfullscreen iframe \"\"\" random seam m, n, i reduce a, b a..., clamp last a rand 1 1 , 1, n , 1 m 1 init i grant example 1 8 8 3 5 4 7 8 1 0 8 4 8 0 4 7 2 9 9 0 0 5 9 4 2 4 0 2 4 5 2 4 2 5 3 0 . 10 Gray. grant example function fib n base case basis if n 0 || n 1 `||` means \"or\" return 1 end recursion induction return fib n 1 fib n 2 end grant example grant example optimal seam 4, 3, 2, 2, 3, 3 sum grant example i, grant example optimal seam i for i in 1 6 , grant example optimal seam 2 begin struct AccessTrackerArray T,N AbstractArray T,N data Array T,N accesses Ref Int end Base.IndexStyle Type AccessTrackerArray IndexLinear Base.size x AccessTrackerArray size x.data Base.getindex x AccessTrackerArray, i Int... x.accesses 1 x.data i... Base.setindex x AccessTrackerArray, v, i... x.accesses 1 x.data i... v track access x AccessTrackerArray x, Ref 0 function track access f Function, x Array tracked track access x f tracked tracked.accesses end end md\" homework 4, version 5 \" md\"\"\" Homework 4 Dynamic programming `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Choose your image bind image url Select all image urls Maximum image size bind max height str Select string. 50,100,200,500 pixels. Using a large image might lead to long runtimes in the later exercises. \"\"\" img original load download image url max height parse Int, max height str \"Decimate an image such that its new height is at most `height`.\" function decimate to height img, height factor max 1, 1 size img, 1 ÷ height img 1 factor end, 1 factor end end img decimate to height img original, max height Gray. brightness. img float to color. energy img md\"\"\" Cutting a seam Below is a function called `remove in each row img, pixels `. It takes a matrix `img` and a vector of integers, `pixels`, and shrinks the image by 1 pixel in width by removing the element `img i, pixels i ` in every row. This function is one of the building blocks of the Image Seam algorithm we saw in the lecture. Read it and convince yourself that it is correct. \"\"\" function remove in each row img Matrix, column numbers Vector m, n size img assert m length column numbers same as the number of rows local img′ similar img, m, n 1 create a similar image with one column less for i, j in enumerate column numbers img′ i, 1 j 1 . view img i, 1 j 1 img′ i, j end . view img i, j 1 end end img′ end let seam 1,2,3,4 remove in each row removal test image, seam end let seam 1,1,1,1 remove in each row removal test image, seam end let seam 1,1,1,1 result1 remove in each row removal test image, seam result2 remove in each row result1, seam result2 end md\"Let's use our function to remove the diagonal from our image. Take a close look at the images to verify that we removed the diagonal. \" md\"\"\" Removing the seam ` 1,1,1,1 ` is equivalent to removing the first column \"\"\" md\"\"\" If we remove the same seam twice, we remove the first two rows \"\"\" md\"\"\" Brightness and Energy \"\"\" md\"\"\" First, we will define a `brightness` function for a pixel a color as the mean of the red, green and blue values. You should use this function whenever the problem set asks you to deal with brightness of a pixel. \"\"\" md\"\"\"We provide you with a convolve function below. \"\"\" md\"\"\" finally we define the `energy` function which takes the Sobel gradients along x and y directions and computes the norm of the gradient for each pixel. \"\"\" md\"\"\" Exercise 1 Building up to dynamic programming In this exercise and the following ones, we will use the computational problem of Seam carving. We will think through all the \"gut reaction\" solutions, and then finally end up with the dynamic programming solution that we saw in the lecture. In the process we will understand the performance and accuracy of each iteration of our solution. How to implement the solutions For every variation of the algorithm, your job is to write a function which takes a matrix of energies, and an index for a pixel on the first row, and computes a \"seam\" starting at that pixel. The function should return a vector of as many integers as there are rows in the input matrix where each number points out a pixel to delete from the corresponding row. it acts as the input to `remove in each row` . \"\"\" md\"\"\" Exercise 1.1 The greedy approach The first approach discussed in the lecture included below is the greedy approach you start from your top pixel, and at each step you just look at the three neighbors below. The next pixel in the seam is the neighbor with the lowest energy. \"\"\" md\"\"\" 👉 Implement the greedy approach. \"\"\" function greedy seam energies, starting pixel Int m, n size energies you can delete the body of this function it's just a placeholder. random seam size energies ..., starting pixel end md\"Before we apply your function to our test image, let's try it out on a small matrix of energies displayed here in grayscale , just like in the lecture snippet above clicking on the video will take you to the right part of the video . Light pixels have high energy, dark pixels signify low energy.\" md\"Starting pixel bind greedy starting pixel Slider 1 size grant example, 2 show value true, default 5 \" greedy seam result greedy seam grant example, greedy starting pixel let s sum grant example i,j for i, j in enumerate greedy seam result md\"\"\" Total energy round s,digits 1 \"\"\" end md\" Let's try it on the bigger image \" begin reactive references to uncheck the checkbox when the functions are updated greedy seam, img, grant example md\"Compute shrunk image bind shrink greedy CheckBox \" end md\"\"\" Exercise 1.2 Recursion A common pattern in algorithm design is the idea of solving a problem as the combination of solutions to subproblems. The classic example, is a Fibonacci number https en.wikipedia.org wiki Fibonacci number generator. The recursive implementation of Fibonacci looks something like this \"\"\" md\"\"\" Notice that you can call a function from within itself which may call itself and so on until a base case is reached. Then the program will combine the result from the base case up to the final result. In the case of the Fibonacci function, we added the solutions to the subproblems `fib n 1 `, `fib n 2 ` to produce `fib n `. An analogy can be drawn to the process of mathematical induction in mathematics. And as with mathematical induction there are parts to constructing such a recursive algorithm Defining a base case Defining an recursion i.e. finding a solution to the problem as a combination of solutions to smaller problems. \"\"\" md\"\"\" 👉 Define a `least energy` function which returns 1. the lowest possible total energy for a seam starting at the pixel at i, j 2. the column to jump to on the next move in row i 1 , which is one of j 1 , j or j 1 , up to boundary conditions. Return these two values in a tuple. \"\"\" returns lowest possible sum energy at pixel i, j , and the column to jump to in row i 1. function least energy energies, i, j m, n size energies base case if i something return energies ... , ... no need for recursive computation in the base case end induction combine results from recursive calls to `least energy`. end least energy grant example, 1, 4 md\"\"\" Expected output As shown in the lecture, the optimal seam from the point 1,4 should be \"\"\" md\"\"\" So we expect the output of `least energy grant example, 1, 4 ` to be \"\"\" md\"\"\" This is elegant and correct, but inefficient Let's look at the number of accesses made to the energies array needed to compute the least energy seam of a 10x10 image \"\"\" track access rand 10,10 do tracked least energy tracked, 1, 5 end md\"Whoa We will need to optimize this later \" md\"\"\" Exercise 1.3 Exhaustive search with recursion Now use the `least energy` function you defined above to define the `recursive seam` function which takes the energies matrix and a starting pixel, and computes the seam with the lowest energy from that starting pixel. This will give you the method used in the lecture to perform exhaustive search of all possible paths https youtu.be rpB6zQNsbQU?t 839 . \"\"\" function recursive seam energies, starting pixel m, n size energies Replace the following line with your code. rand 1 starting pixel for i 1 m end md\"\"\" We won't use this function to shrink our larger image, because it is too inefficient. Your notebook might get stuck But let's try it on the small example matrix from the lecture, to verify that we have found the optimal seam. \"\"\" recursive seam test recursive seam grant example, 4 md\"\"\" Exercise 1.4 State clearly why this algorithm does an exhaustive search of all possible paths. How does the number of possible seam grow as n increases for a `m×n` image? Big O notation is fine, or an approximation is fine . \"\"\" exhaustive observation md\"\"\" your answer here \"\"\" md\"\"\" Exercise 2 Memoization Memoization is the name given to the technique of storing results to expensive function calls that will be accessed more than once. As stated in the video, the function `least energy` is called repeatedly with the same arguments. In fact, we call it on the order of 3^n times, when there are only really m \\times n unique ways to call it Lets implement memoization on this function with first a dictionary https docs.julialang.org en v1 base collections Dictionaries for storage. \"\"\" md\"\"\" Exercise 2.1 Dictionary as storage Let's make a memoized version of least energy function which takes a dictionary and first checks to see if the dictionary contains the key i,j if it does, returns the value stored in that place, if not, will compute it, and store it in the dictionary at key i, j and return the value it computed. `memoized least energy energies, starting pixel, memory ` This function must recursively call itself, and pass the same `memory` object it received as an argument. You are expected to read and understand the documentation on dictionaries https docs.julialang.org en v1 base collections Dictionaries to find out how to 1. Create a dictionary 2. Check if a key is stored in the dictionary 3. Access contents of the dictionary by a key. \"\"\" function memoized least energy energies, i, j, memory Dict m, n size energies you should start by copying the code from your not memoized least energies function. end memoized least energy test memoized least energy grant example, 1, 4, Dict md\"\"\" Let's see how many matrix access we have now \"\"\" track access rand 10,10 do tracked memoized least energy tracked, 1, 5, Dict end function memoized recursive seam energies, starting pixel we set up the the memory note the key type Tuple Int,Int and the value type Tuple Float64,Int . If you need to memoize something else, you can just use Dict without types. memory Dict Tuple Int,Int ,Tuple Float64,Int m, n size energies Replace the following line with your code. you should start by copying the code from your not memoized recursive seam function. end memoized recursive seam grant example, 4 grant example optimal seam md\"\"\" Exercise 2.2 Matrix as storage optional The dictionary based memoization we tried above works well in general as there is no restriction on what type of keys can be used. But in our particular case, we can use a matrix as a storage, since a matrix is naturally keyed by two integers. 👉 Write a variant of `matrix memoized least energy` and `matrix memoized seam` which use a matrix as storage. \"\"\" function matrix memoized least energy energies, i, j, memory Matrix m, n size energies Replace the following line with your code. end function matrix memoized seam energies, starting pixel memory Matrix Union Nothing,Tuple Float64,Int nothing, size energies use me instead of you use a different element type memory Matrix Any nothing, size energies m, n size energies Replace the following line with your code. starting pixel for i 1 m end begin matrix memoized seam, img md\"Compute shrunk image bind shrink matrix CheckBox \" end md\"\"\" Exercise 3 Dynamic programming without recursion Now it's easy to see that the above algorithm is equivalent to one that populates the memory matrix in a for loop. Exercise 3.1 👉 Write a function which takes the energies and returns the least energy matrix which has the least possible seam energy for each pixel. This was shown in the lecture, but attempt to write it on your own. \"\"\" function least energy matrix energies result copy energies m, n size energies your code here return result end img brightness brightness. img le test least energy matrix img brightness spooky A Matrix Real map sqrt. A . maximum A do x RGB .8x, x, .8x end spooky le test md\"\"\" Exercise 3.2 👉 Write a function which, when given the matrix returned by `least energy matrix` and a starting pixel on the first row , computes the least energy seam from that pixel. \"\"\" function seam from precomputed least energy energies, starting pixel Int least energies least energy matrix energies m, n size least energies Replace the following line with your code. starting pixel for i 1 m end begin img, seam from precomputed least energy md\"Compute shrunk image bind shrink bottomup CheckBox \" end md\" Function library Just some helper functions used in the notebook.\" function mark path img, path img′ RGB. img also makes a copy m size img, 2 for i, j in enumerate path if size img, 2 50 To make it easier to see, we'll color not just the pixels of the seam, but also those adjacent to it for j′ in j 1 j 1 img′ i, clamp j′, 1, m RGB 1,0,1 end else img′ i, j RGB 1,0,1 end end img′ end function shrink n min seam Function, img Matrix Colorant , n, imgs show lightning true, n 0 && return push imgs, img e energy img seam energy seam sum e i, seam i for i in 1 size img, 1 , min j findmin map j seam energy min seam e, j , 1 size e, 2 min seam vec min seam e, min j img′ remove in each row img, min seam vec if show lightning push imgs, mark path img, min seam vec else push imgs, img′ end shrink n min seam, img′, n 1, imgs show lightning show lightning end if shrink greedy local n min 200, size img, 2 greedy carved shrink n greedy seam, img, n md\"Shrink by bind greedy n Slider 1 n show value true \" end if shrink greedy greedy carved greedy n end begin reactive references to uncheck the checkbox when the functions are updated img, memoized recursive seam, shrink n md\"Compute shrunk image bind shrink dict CheckBox \" end if shrink dict local n min 20, size img, 2 dict carved shrink n memoized recursive seam, img, n md\"Shrink by bind dict n Slider 1 n, show value true \" end if shrink dict dict carved dict n end if shrink matrix local n min 20, size img, 2 matrix carved shrink n matrix memoized seam, img, n md\"Shrink by bind matrix n Slider 1 n, show value true \" end if shrink matrix matrix carved matrix n end if shrink bottomup local n min 40, size img, 2 bottomup carved shrink n seam from precomputed least energy, img, n md\"Shrink by bind bottomup n Slider 1 n, show value true \" end if shrink bottomup bottomup carved bottomup n end function pencil X f x RGB 1 x,1 x,1 x map f, X . maximum X end function decimate img, n img 1 n end, 1 n end end hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"You can call the `least energy` function recursively within itself to obtain the least energy of the adjacent cells and add the energy at the current cell to get the total energy.\" hint md\"We recommend using a matrix with element type `Union Nothing, Tuple Float64,Int `, initialized to all `nothing`s. You can check whether the value at ` i,j ` has been computed before using `memory i,j nothing`.\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text begin function visualize seam algorithm test energies, algorithm Function, starting pixel Integer seam algorithm test energies, starting pixel visualize seam algorithm test energies, seam end function visualize seam algorithm test energies, seam Vector display img RGB. test energies for i, j in enumerate seam try display img i, j RGB 0.9, 0.3, 0.6 catch ex if ex isa BoundsError return keep working \"\" end the solution might give an illegal index end end display img end end visualize seam algorithm grant example, greedy seam result yays md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text if recursive seam test grant example optimal seam correct else keep working end let result track access rand 10,10 do tracked memoized least energy tracked, 1, 5, Dict end if result 0 nothing elseif result 200 correct else keep working md\"That's still too many accesses Did you forget to add a result to the `memory`?\" end end let aresult track access rand 10,10 do tracked memoized recursive seam tracked, 5 end if aresult 200 if memoized recursive seam grant example, 4 grant example optimal seam correct else keep working md\"The returned seam is not correct. Did you implement the non memoized version correctly?\" end else keep working md\"Careful Your `memoized recursive seam` is still making too many memory accesses, you may not want to run the visualization below.\" end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined least energy not defined least energy else let result1 least energy grant example, 6, 4 if result1 isa Tuple keep working md\"Your function should return a tuple , like ` 1.2, 5 `.\" elseif result1 isa Tuple Float64,Int keep working md\"Your function should return a tuple , like ` 1.2, 5 `.\" else result least energy grant example, 1, 4 if result isa Tuple Float64,Int keep working md\"Your function should return a tuple , like ` 1.2, 5 `.\" else a, b result if a ≈ 0.3 && b 4 almost md\"Only search the at most three cells that are within reach.\" elseif a ≈ 0.6 && b 3 correct else keep working end end end end end if isdefined least energy matrix not defined least energy matrix elseif le test isa Matrix Real keep working md\"`least energy matrix` should return a 2D array of Float64 values.\" end if isdefined seam from precomputed least energy not defined seam from precomputed least energy end function hbox x, y, gap 16 sy size y , sx size x w, h max sx 1 , sy 1 , gap sx 2 sy 2 slate fill RGB 1,1,1 , w,h slate 1 size x,1 , 1 size x,2 . RGB. x slate 1 size y,1 , size x,2 gap . 1 size y,2 . RGB. y slate end hbox float to color. convolve brightness. img , Kernel.sobel 1 , float to color. convolve brightness. img , Kernel.sobel 2 , vbox x,y, gap 16 hbox x', y' ' bigbreak html\" br br br br br \" bigbreak bigbreak bigbreak bigbreak "},{"url":"homework/hw5/","title":"Structure","tags":["homework","module2","track_julia","structure","track_math","type","matrix","linear algebra","track_data"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 section 2.5 order 2.5 homework number 5 title \"Structure\" layout \"layout.jlhtml\" tags \"homework\", \"module2\", \"track julia\", \"structure\", \"track math\", \"type\", \"matrix\", \"linear algebra\", \"track data\" description \"Create your own Julia structs and add new functionality to them, to create first class mathematical objects.\" using Markdown using InteractiveUtils using PlutoUI using LinearAlgebra begin struct FirstRankOneMatrix Your code here v Vector Float64 w Vector Float64 end Add the extra constructor here FirstRankOneMatrix v FirstRankOneMatrix v, v end ten twelve missing Your code here sqrt Base.sqrt filter Base.filter methods size function Base.size M FirstRankOneMatrix return missing Your code here end function Base.getindex M FirstRankOneMatrix, i, j return missing Your code here end ten twelve ones 10, 12 An example matrix two dimensional array function print as matrix M FirstRankOneMatrix Your code here end print as matrix ten twelve begin struct RankOneMatrix T AbstractMatrix T v AbstractVector T w AbstractVector T end Add the two extra constructors Should we make these missing by default? if so remove hint below RankOneMatrix v RankOneMatrix v, v end function Base.size M RankOneMatrix return missing Your code here end function Base.getindex M RankOneMatrix, i, j return missing Your code here end R2 RankOneMatrix 1,2 , 3,4,5 M RankOneMatrix 1 10 missing Your code here collect M typeof collect M function matvec M RankOneMatrix, x return missing Your code here end begin struct RankTwoMatrix T AbstractMatrix T Your code here A RankOneMatrix T B RankOneMatrix T end Add a constructor that uses two vectors ranges RankTwoMatrix v1, v2 RankTwoMatrix RankOneMatrix v1 , RankOneMatrix v2 end function Base.getindex M RankTwoMatrix, i, j return missing Your code here end function Base.size M RankTwoMatrix return missing Your code here end struct LowRankMatrix AbstractMatrix Float64 Your code here Ms Vector RankOneMatrix rank Int end function Base.getindex M LowRankMatrix, i, j return missing Your code here end md\" homework 5, version 3 \" md\"\"\" Homework 5 Structure `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Exercise 1 When is zero not quite zero? Students used to pure math are sometimes surprised to see numbers like `1e 15`, `1e 16`, or `1e 17` appearing in computations when `0` might have been expected. At first glance, this behaviour seems 'random' or 'noisy', but in this exercise, we will try to demonstrate some structure behind floating point artihmetic. Exercise 1.1 👉 Find all integers j with 1 ≤ j ≤ 1000 for which Julia's result satisfies `j 1 j ≠ 1`. \"\"\" md\"\"\" Notice that when you re run the computation, the result does not change. Floating point arithmetic is not random Exercise 1.2 👉 Take the smallest number `j` you found above and compute the error, i.e. the distance between `j 1 j ` and `1`. Is this an integer power of 2? Which one? `log2` might help. \"\"\" md\"\"\" Exercise 1.3 Caculate all of the following 32 23 736 3.2 23 73.6 3.2 2.3 7.36 \"\"\" md\"\"\" We wanted to show you that floating point arithmetic has structure to it. It is not the fuzz or slop that you may have seen as experimental errors in maybe a chemistry or physics class. If you are interested, we may show more later in the semester. \"\"\" md\"\"\" Exercise 2 Rank one matrices In this exercise we will go into some more detail about how to define types in Julia in order to make a structured matrix type, similar to the `OneHotVector` type from class. To begin, we will make a type to represent a rank 1 matrix. A rank 1 matrix is a matrix whose columns are all multiples of each other. This already tells us a lot about the matrix in fact, we can represent any such matrix as the outer product of two vectors `v` and `w`. Only the two vectors will be stored the matrix elements will be calculated on demand, i.e. when we index into the object. \"\"\" md\"\"\" Exercise 2.1 Let's make a `FirstRankOneMatrix` type that contains two vectors of floats, `v` and `w`. Here `v` represents a column and `w` the multipliers for each column. We include in the same cell, due to requirements of Pluto a constructor that takes a single vector `v` and duplicates it. \"\"\" md\"\"\" 👉 Create an object of type `FirstRankOneMatrix` representing the multiplication table of the numbers from 1 to 10 and the numbers from 1 to 12. Call it `ten twelve`. \"\"\" md\"\"\" Exercise 2.2 extending Base methods Right now, our `FirstRankOneMatrix` is just a container for two arrays. To make it behave like a matrix, we add methods to functions in `Base`, Julia's standard set of functions. Most of the functions you have used so far come from base, for example \"\"\" md\"\"\" These are built in functions, and each function comes with a set of methods pre defined in Julia's source code which is mostly written in Julia . Uncomment the next cell to see the full list of methods for `size`. We will add a method to this list \"\"\" md\"\"\" Let's extend the `size` and `getindex` methods so that they can also work with our rank 1 matrices. This will allow us to easily access the size and entries of the matrix. \"\"\" md\"\"\" 👉 Extend `Base.size` for the type `FirstRankOneMatrix` to return the size as a tuple of values corresponding to each direction. \"\"\" md\"\"\" 👉 Extend `Base.getindex` for the type `FirstRankOneMatrix` to return the i, j th entry of the outer product. \"\"\" Base.getindex ten twelve, 5, 11 md\"\"\" Exercise 2.3 If you ask Julia to display the value of an object of type `FirstRankOneMatrix`, you will notice that it doesn't do what we are used to, which is probably to display the whole matrix that it corresponds to. Let's see \"\"\" md\"\"\" This is what matrices normally look like in Julia \"\"\" md\"\"\" One possible workaround for this is to create a new function specifically to display a matrix of our custom type. 👉 Make a function `print as matrix` that prints the entries of a matrix of type `FirstRankOneMatrix` in a grid format. We'll test that it works below with the matrix `R` we already created. \"\"\" md\"\"\" We should now be able to see our `FirstRankOneMatrix` type matrix displayed in the terminal \"\"\" md\"\"\" Exercise 2.4 AbstractMatrix In fact, Julia together with Pluto can do some of this work for us Julia provides facilities to make our life easier when we tell Julia that our type behaves like an array. We do so by making the type a subtype of `AbstractMatrix`. This will let our type inherit the methods and attributes that come with the `AbstractMatrix` type by default including some display functions that Pluto notebooks use. Let's do all this on a new rank one matrix type `RankOneMatrix`. As we do this, we will also remove the restriction to `Float64` type entries by using a parametrised type `T`. Parametrised types allow us flexibility to handle different types of entries without repeating a lot of code we won't go too in depth about these for now. \"\"\" md\"\"\" In the cell above, we added a second 'outer' constructor that takes a single vector as argument. 👉 Make sure that you can use both constructors by trying them out below. \"\"\" md\"\"\" 👉 Add `getindex` and `size` methods for this new type. These will allow us to access entries of our custom matrices with the usual index notation `M i,j `, as well as quickly retrieving their dimensions. \"\"\" Base.getindex R2, 2, 3 md\"\"\" 👉 Create an object of the new type for the 10 \\times 10 multiplication matrix, using a single range. \"\"\" md\"\"\" You should see two things Firstly, the matrix now contains integers, instead of floats this is thanks to our parametrised type . And secondly, Julia automatically displays the matrix as we wanted, once you have defined `getindex` and `size`, provided you have told Julia that your type is a subtype of `AbstractArray` \"\"\" md\"\"\" Julia also allows us to automatically convert the matrix to a normal \"dense\" matrix, using either `collect` or `Array`. Let's try these out. You may need to re run the cell below after completing the exercises. \"\"\" md\"\"\" Fun fact we did not define a method for `Base.collect`, and yet it works This is because we told Julia that our `RankOneMatrix` is a subtype of `AbstractMatrix`, for which `Base.collect` already has a method defined in Julia's source code. This fallback method uses `Base.getindex` and `Base.size`, which we did define. \"\"\" md\"\"\" Exercise 2.5 Why do we need a special type to represent special types of structured matrices? One reason is that not only do they give a more efficient representation in space requiring less memory to store , they can also be more efficient in time, i.e. faster . For example, let's look at matrix vector multiplication . This is a fundamental part of many, many algorithms in scientific computing, and because of this, we usually want it to be as fast as possible. For a rank one matrix given by M v w^T , the matrix vector product M \\cdot x is given by w \\cdot x v . Note that w \\cdot x is a number scalar which is multiplying the vector element by element. This computation is much faster than the usual matrix vector multiplication we are taking advantage of structure \"\"\" md\"\"\" 👉 Define a function `matvec` that takes a `RankOneMatrix` `M` and a `Vector` `x` and carries out matrix vector multiplication. We will be able to compare the result with doing the matrix vector product using the corresponding dense matrix. \"\"\" md\"\"\" Exercise 3 Low rank matrices \"\"\" md\"\"\" In this exercise we will combine rank 1 matrices into low rank matrices, which are sums of rank 1 matrices. Just like in the previous exercise, we will make custom types for these matrices, and we will be able to compute the entries of a matrix on demand as we request them instead of storing all of them always . \"\"\" md\"\"\" Exercise 3.1 👉 Make a rank 2 matrix type `RankTwoMatrix` that contains two rank 1 matrices `A` and `B`. For simplicity, you can use the `FirstRankOneMatrix` type. Include in the same cell a constructor for `RankTwoMatrix` that takes two vectors and makes the rank one matrices from those vectors. Note In principle we should check when constructing the type that the input matrices have the same dimensions, but we will just assume that they do. \"\"\" md\"\"\" Exercise 3.2 👉 Construct a rank two matrix out of the rank 1 matrix representing the multiplication table of 1.0 10.0 , together with the multiplication table of 0.0 0.1 0.9 . Note that both range arguments must contain floats, so that we can add up entries. \"\"\" md\"\"\" Exercise 3.3 👉 As with last time, extend the `getindex` and `size` methods for the `RankTwoMatrix` type. Keep in mind they are already defined for `RankOneMatrix`. \"\"\" md\"\"\" Exercise 3.4 Making a custom type for rank 2 matrices is a step forward from rank 1 matrices instead of storing two vectors, we store two rank 1 matrices themselves. What if we want to represent a rank 3 matrix? We would need to store three rank 1 matrices, instead of just two. What about rank 4, rank 5, and so on? We can go even further and make a general custom type `LowRankMatrix` for rank k matrices, for general ideally low k . In this case, we should store two main things the list of rank 1 matrices that our low rank matrix is made up of, and also the rank of the matrix which is how many rank 1 matrices we are storing . 👉 Complete the definition for the type `LowRankMatrix`. Remember to store both the rank 1 matrices and the rank of the matrix itself. \"\"\" md\"\"\" Exercise 3.5 👉 Extend the `getindex` and `size` methods to work with the `LowRankMatrix` type. As before, remember that these are already defined for `RankOneMatrix`. \"\"\" function Base.size M LowRankMatrix return missing Your code here end size Base.size Base.size ten twelve Base.size R2 let comp1, comp2 RankOneMatrix 1.0 1.0 10.0 , RankOneMatrix 0.0 0.1 0.9 ex3 LowRankMatrix comp1, comp2 , 2 end md\"\"\" Exercise 3.6 👉 Extend the method `matvec` to work with our custom type `LowRankMatrix` and a `Vector` . Remember that `matvec` is already defined for `RankOneMatrix`. \"\"\" function matvec M LowRankMatrix, x return missing Your code here end md\"\"\" Exercise 3.7 One of the big advantages of our rank 1 matrices is its space efficiency to \"store\" a n\\times n matrix, we only need to actually store two vectors of n entries, as opposed of n^2 total entries. Rank 2 matrices are a litte less efficient we store two rank 1 matrices, or four vectors of n vertices. As we increase the rank, we lose space efficiency, at the cost of being able to represent more kinds of matrices. 👉 For what rank will a matrix of rank k need the same amount of storage as the dense version? Explain your answer. \"\"\" answer md\"\"\" From rank ... Because .... \"\"\" md\"\"\" Exercise 4 The SVD for structured matrices In a math class, you may or may not learn about the singular value decomposition SVD . From a computational thinking point of view, whether you have seen this before or not we hope will not matter. Here we would like you to get to \"know\" the SVD , through experience, rather than through a math lecture. This is how we see \"computational thinking\" after all. Definitely do not look up some eigenvalue definition. \"\"\" md\"\"\" Exercise 4.1 The `LinearAlgebra` package defines a function `svd` that computes the decomposition for us. Have a look at the result of calling the `svd` function. Try not to get intimidated by the large output you may need to scroll the cell output , and look at the docs for `svd`. \"\"\" biggie rand 100,100 md\"\"\" 👉 What are the singular values of `biggie`? \"\"\" singular values of biggie missing md\"\"\" Exercise 4.2 If we try to run the `svd` function on a `RankOneMatrix`, you will see that it does not work. The error is telling us that no `svd` method has been defined for our type . Let's extend the `svd` function to work on objects of our type. \"\"\" A RankOneMatrix rand 3 ,rand 4 md\"\"\" 👉 Extend `svd` to work directly on a rank one matrix, by writing a new method. Keep things simple. Inside your method, call `LinearAlgebra.svd` on a type that it is already defined for . \"\"\" function LinearAlgebra.svd A RankOneMatrix return missing end svd biggie svd A LinearAlgebra.svd A md\"\"\" Exercise 4.3 👉 Look at the singular values, how many of them are approximately non zero? Does that make sense? \"\"\" md\"\"\" This number the number of singular values that are positive is something you will or have learn ed in a linear algebra class it is known as the rank of a matrix, and is usually defined through a complicated elimination procedure. 👉 Write a function `numerical rank` that calculates the singular values of a matrix `A`, and returns how many of them are approximately zero. To keep things simple, you can assume that \"approximately zero\" means less than `tol 1e5`. \"\"\" function numerical rank A AbstractMatrix tol 1e 5 return missing end numerical rank A numerical rank rand 3,3 md\"\"\" Exercise 4.4 👉 Write a function that takes an argument k and generates the sum of k random rank one matrices of size mxn, and counts the number of essentially non zero singular values. Hint you can do this with a `for` loop, but it can also be done with `sum`, e.g. `sum i for i 1 10 `. \"\"\" function k rank ones k, m, n return missing end k rank ones 1, 3, 3 md\"\"\" Exercise 4.5 👉 What is the answer when m and n are both ≥ k ? What if one of m or n is k ? \"\"\" md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" Try writing this without a `for` loop. Ideas ☝️ Use `filter`. ☝️ Comprehensions can have `if` clauses, as in ` j for j 1 1000 if 5 j 17 `. \"\"\" | hint hint md\"\"\" The extra constructor should have the same name as the type it should take in a single argument `v`, and use it for both the column and the multipliers. \"\"\" hint md\"\"\" Check the docs for functions like `print`, `println`, `lpad` and `rpad`. Also, you don't have to know exactly how the `do` keyword works for now, but feel free to read about it it can be useful with iterating \"\"\" hint md\"\"\" If you want to specify that your argument is a range, use the `AbstractRange` type. \"\"\" hint md\"\"\" How would we implement `matvec` for a rank 2 matrix making use of the two stored rank 1 matrices? Can you generalize this to the rank k case, when we have many rank 1 matrices? \"\"\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text let example FirstRankOneMatrix 1,2 , 3,4 if ismissing print as matrix example still missing end end keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next question.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text try A RankOneMatrix 1,2,3 , 5,6,7 result LinearAlgebra.svd A if result isa Missing still missing elseif result isa LinearAlgebra.SVD almost md\"Like all other `svd` methods implemented in `LinearAlgebra` , your method should return an SVD factorization of type `LinearAlgebra.SVD` .\" else reconstructed result.U ,1 1 result.S 1 1 result.Vt 1 1, if sum abs. A . reconstructed 1e 5 correct else keep working end end catch e if e isa MethodError && e.f LinearAlgebra.svd still missing md\"Define a new method for `LinearAlgebra.svd` that takes a `RankOneMatrix` as argument.\" else rethrow e end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" let if isdefined ten twelve not defined ten twelve else if ten twelve isa Missing still missing elseif ten twelve isa FirstRankOneMatrix keep working md\"`ten twelve` should be a `FirstRankOneMatrix`.\" elseif ten twelve.v 1 10 && ten twelve.w 1 12 || ten twelve.w 1 10 && ten twelve.v 1 12 correct else keep working end end end let if isdefined RankOneMatrix not defined RankOneMatrix else R2 FirstRankOneMatrix 1,2 , 3,4,5 if ismissing Base.size R2 still missing elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else correct end end end let if isdefined FirstRankOneMatrix not defined FirstRankOneMatrix else R2 FirstRankOneMatrix 1,2 , 3,4,5 if ismissing Base.getindex R2, 1, 1 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif ismissing Base.size R2 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else shouldbe 3 4 5 6 8 10 r all CartesianIndices shouldbe do I isapprox R2 Tuple I ... , shouldbe I , rtol 1 3 end if r correct else keep working md\"`Base.getindex` is not implemented correctly.\" end end end end let if isdefined RankOneMatrix not defined RankOneMatrix else R2 RankOneMatrix 1,2 , 3,4,5 if ismissing Base.size R2 still missing elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else correct end end end let if isdefined RankOneMatrix not defined RankOneMatrix else R2 RankOneMatrix 1,2 , 3,4,5 if ismissing Base.getindex R2, 1, 1 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif ismissing Base.size R2 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else shouldbe 3 4 5 6 8 10 r all CartesianIndices shouldbe do I isapprox R2 Tuple I ... , shouldbe I , rtol 1 3 end if r correct else keep working md\"`Base.getindex` is not implemented correctly.\" end end end end if isdefined matvec not defined matvec else let M RankOneMatrix 1 10 if ismissing matvec M, ones 10 still missing end incorrect false for i in 1 5 r ones 10 . i for j in 1 10 if matvec M, r j collect M r j incorrect true end end end if incorrect keep working else correct end end end let if isdefined RankTwoMatrix not defined RankTwoMatrix else R1 RankOneMatrix 1,2 , 3,4,5 R2 RankTwoMatrix R1, R1 if ismissing Base.size R2 still missing elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else correct end end end let if isdefined RankTwoMatrix not defined RankTwoMatrix else R1a RankOneMatrix 1,2 , 3,4,5 R1b RankOneMatrix 2,1 , 3,4,5 R2 RankTwoMatrix R1a, R1b if ismissing Base.getindex R2, 1, 1 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif ismissing Base.size R2 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else shouldbe 3 4 5 6 8 10 s2 shouldbe shouldbe 2,1 , r all CartesianIndices s2 do I isapprox R2 Tuple I ... , s2 I , rtol 1 3 end if r correct else keep working md\"`Base.getindex` is not implemented correctly.\" end end end end let if isdefined LowRankMatrix not defined LowRankMatrix else R1 RankOneMatrix 1,2 , 3,4,5 R2 LowRankMatrix R1, R1 ,2 if ismissing Base.size R2 still missing elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else correct end end end let if isdefined LowRankMatrix not defined LowRankMatrix else R1a RankOneMatrix 1,2 , 3,4,5 R1b RankOneMatrix 2,1 , 3,4,5 R2 LowRankMatrix R1a, R1b ,2 if ismissing Base.getindex R2, 1, 1 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif ismissing Base.size R2 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else shouldbe 3 4 5 6 8 10 s2 shouldbe shouldbe 2,1 , r all CartesianIndices s2 do I isapprox R2 Tuple I ... , s2 I , rtol 1 3 end if r correct else keep working md\"`Base.getindex` is not implemented correctly.\" end end end end if isdefined matvec not defined matvec else let comp1, comp2 RankOneMatrix 1.0 1.0 10.0 , RankOneMatrix 0.0 0.1 0.9 if ismissing comp1 | ismissing comp2 still missing end ex4 LowRankMatrix comp1, comp2 , 2 if ismissing ex4 still missing end if ismissing matvec ex4, ones 10 still missing end incorrect false for i in 1 10 r ones 10 . i v, c matvec ex4, r , collect ex4 r for j in 1 10 if abs v j c j 1e 10 incorrect true break end end end if incorrect keep working else correct end end end if isdefined singular values of biggie not defined singular values of biggie else if singular values of biggie isa Missing still missing elseif singular values of biggie isa AbstractVector keep working md\"Return the singular values as a vector .\" elseif isapprox singular values of biggie, svdvals biggie , rtol 1e 5 correct else keep working end end let if isdefined numerical rank not defined numerical rank else result1 numerical rank 1 2 0 0 0 1e 10 5 10 0 if result1 isa Missing still missing elseif result1 isa Integer keep working md\"You should return a number.\" elseif result1 1 keep working else result2 numerical rank 1 3 0 0 0 1e 10 5 10 0 if result2 2 correct else keep working end end end end let if isdefined k rank ones not defined k rank ones else result k rank ones 1, 3, 4 if result isa Missing still missing elseif result isa AbstractMatrix keep working md\"Make sure that you return a matrix.\" elseif size result 3, 4 keep working md\"Make sure that you return a matrix of the correct size.\" else numerical rank A count 1e 5 , svdvals A if numerical rank result 1 correct else result2 k rank ones 2, 10, 4 if result2 isa AbstractMatrix keep working md\"Make sure that you return a matrix.\" elseif size result2 10, 4 keep working md\"Make sure that you return a matrix of the correct size.\" elseif numerical rank result2 2 correct else keep working end end end end end todo text HTML \"\"\" div style \"background rgb 220, 200, 255 padding 2em border radius 1em \" h1 TODO h1 repr MIME\"text html\" , text div \"\"\" bigbreak html\" br br br br br \" bigbreak bigbreak bigbreak bigbreak bigbreak "},{"url":"homework/hw6/","title":"Probability distributions","tags":["homework","module2","track_julia","track_math","track_data","structure","probability","statistics","plotting","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 section 3.5 order 3.5 homework number 6 title \"Probability distributions\" layout \"layout.jlhtml\" tags \"homework\", \"module2\", \"track julia\", \"track math\", \"track data\", \"structure\", \"probability\", \"statistics\", \"plotting\", \"interactive\" description \"Calculate a probability distribution from a dataset, experiment with different statistical models, and learn how to plot your results.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using PlutoUI, Plots md\" homework 6, version 5 \" md\"\"\" Homework 6 Probability distributions `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Exercise 1 Calculating frequencies In this exercise we practise using dictionaries in Julia by writing our own version of the `countmap` function. Recall that that function counts the number of times that a given discrete value occurs in an input data set. A suitable data structure for this is a dictionary , since it allows us to store data that is very sparse i.e. for which many input values do not occur . \"\"\" function counts data Vector counts Dict Int,Int your code here return counts end counts 7, 8, 9, 7 md\"\"\" Test that your code is correct by applying it to obtain the counts of the data vector `test data` defined below. What should the result be? Test that you do get the correct result and call the result `test counts`. \"\"\" test data 1, 0, 1, 0, 1000, 1, 1, 1000 test counts counts test data md\"\"\" Exercise 1.2 The dictionary contains the information as a sequence of pairs mapping keys to values. This is not a particularly useful form for us. Instead, we would prefer a vector of the keys and a vector of the values, sorted in order of the key. We are going to make a new version `counts2` where you do the following below . Start off by just running the following commands each in their own cell on the dictionary `test counts` you got by running the previous `counts` function on the vector `test data` so that you see the result of running each command. Once you have understood what's happening at each step, add them to the `counts2` function in a new cell. 👉 Extract vectors `ks` of keys and `vs` of values using the `keys ` and `values ` functions and convert the results into a vector using the `collect` function. \"\"\" md\"\"\" 👉 Define a variable `perm` as the result of running the function `sortperm` on the keys. This gives a permutation that tells you in which order you need to take the keys to give a sorted version. \"\"\" md\"\"\" 👉 Use indexing `ks perm ` to find the sorted keys and values vectors. Here we are passing in a vector as the index. Julia extracts the values at the indices given in that vector \"\"\" md\"\"\" Verify that your new `counts2` function gives the correct result for the vector `v` by comparing it to the true result that you get by doing the counting by hand \"\"\" md\"\"\" 👉 Create the function `counts2` that performs these steps. \"\"\" function counts2 data Vector return missing end counts2 test data md\"\"\" Exercise 1.3 👉 Make a function `probability distribution` that normalizes the result of `counts2` to calculate the relative frequencies of each value, i.e. to give a probability distribution i.e. such that the sum of the resulting vector is 1 . The function should return the keys the unique data that was in the original data set, as calculated in `counts2`, and the probabilities relative frequencies . Test that it gives the correct result for the vector `vv`. We will use this function in the rest of the exercises. \"\"\" function probability distribution data Vector return missing end probability distribution test data md\"\"\" Intermezzo function vs. begin vs. let html\" span id function begin let span \" In our lecture materials, we sometimes use a `let` block in this cell to group multiple expressions together, but how is it different from `begin` or `function`? function Writing functions is a way to group multiple expressions i.e. lines of code together into a mini program. Note the following about functions A function always returns one object . ^1 This object can be given explicitly by writing `return x`, or implicitly Julia functions always return the result of the last expression by default. So `f x x 2` is the same as `f x return x 2`. Variables defined inside a function are not accessible outside the function . We say that function bodies have a local scope . This helps to keep your program easy to read and write if you define a local variable, then you don't need to worry about it in the rest of the notebook. There are two other ways to group expressions together that you might have seen before `begin` and `let`. begin `begin` will group expressions together, and it takes the value of its last subexpression. We use it in this notebook when we want multiple expressions to always run together. let `let` also groups multiple expressions together into one, but variables defined inside of it are local they don't affect code outside of the block. So like `begin`, it is just a block of code, but like `function`, it has a local variable scope. We use it when we want to define some local temporary variables to produce a complicated result, without interfering with other cells. Pluto allows only one definition per global variable of the same name, but you can define local variables with the same names whenever you wish ^1 Even a function like `f x return` returns one object the object `nothing` — try it out \"\"\" md\"\"\" Example of a scope problem with `begin` The following will not work, because `fruits` has multiple definitions \"\"\" md\"\"\" Solved using `let` \"\"\" let vegetables \"🥦\", \"🥔\", \"🥬\" length vegetables end let vegetables \"🌽\" length vegetables end md\"\"\" This works, because `vegetables` is only defined as a local variable inside the cell , not as a global \"\"\" vegetables md\"\"\" Exercise 2 Modelling component failure with the geometric distribution In this exercise, we will investigate the simple model of failure of mechanical components or light bulbs, or radioactive decay, or recovery from an infection, or... that we saw in lectures. Let's call \\tau the time to failure. We will use a simple model, in which each component has probability p to fail each day. If it fails on day n , then \\tau n . We see that \\tau is a random variable, so we need to study its probability distribution . \"\"\" md\"\"\" Exercise 2.1 👉 Define the function `bernoulli p ` from lectures. Recall that this generates `true` with probability p and `false` with probability 1 p . \"\"\" function bernoulli p Real return missing end md\"\"\" Exercise 2.2 👉 Write a function `geometric p `. This should run a simulation with probability p to recover and wait until the individual recovers, at which point it returns the time taken to recover. The resulting failure time is known as a geometric random variable , or a random variable whose distribution is the geometric distribution . \"\"\" function geometric p Real return missing end geometric 0.25 md\"\"\" We should always be aware of special cases sometimes called \"boundary conditions\" . Make sure not to run the code with p 0 What would happen in that case? Your code should check for this and throw an `ArgumentError` as follows ```julia throw ArgumentError \"...\" ``` with a suitable error message. \"\"\" md\"\"\" 👉 What happens for p 1 ? \"\"\" interpretation of p equals one md\"\"\" blablabla \"\"\" md\"\"\" Exercise 2.3 👉 Write a function `experiment p, N ` that runs the `geometric` function `N` times and collects the results into a vector. \"\"\" function experiment p Real, N Integer return missing end small experiment experiment 0.5, 20 md\"\"\" Exercise 2.4 Let's run an experiment with p 0.25 and N 10,000 . We will plot the resulting probability distribution, i.e. plot P \\tau n against n , where n is the recovery time. \"\"\" large experiment experiment 0.25, 10000 let xs, ps probability distribution large experiment bar xs, ps, alpha 0.5, leg false end md\"\"\" 👉 Calculate the mean recovery time. \"\"\" md\"\"\" 👉 Create the same plot as above, and add the mean recovery time to the plot using the `vline ` function and the `ls dash` argument to make a dashed line. Note that `vline ` requires a vector of values where you wish to draw vertical lines. \"\"\" let your code here end md\"\"\" html\" span id note about plotting span \" Note about plotting Plots.jl has an interesting property a plot is an object, not an action. Functions like `plot`, `bar`, `histogram` don't draw anything on your screen they just return a `Plots.Plot`. This is a struct that contains the description of a plot what data should be plotted in what way? , not the picture . So a Pluto cell with a single line, `plot 1 10 `, will show a plot, because the result of the function `plot` is a `Plot` object, and Pluto just shows the result of a cell. Modifying plots Nice plots are often formed by overlaying multiple plots. In Plots.jl, this is done using the modifying functions `plot `, `bar `, `vline `, etc. These take an extra first argument a previous plot to modify. For example, to plot the `sin`, `cos` and `tan` functions in the same view, we do ```julia function sin cos plot T 1.0 0.01 1.0 result plot T, sin. T plot result, T, cos. T plot result, T, tan. T return result end ``` 💡 This example demonstrates a useful pattern to combine plots 1. Create a new plot and store it in a variable 2. Modify that plot to add more elements 3. Return the plot Grouping expressions It is highly recommended that these 3 steps happen within a single cell . This can prevent some strange glitches when re running cells. There are three ways to group expressions together into a single cell `begin`, `let` and `function`. More on this here function begin let \"\"\" md\"\"\" Exercise 2.5 👉 What shape does the distribution seem to have? Can you verify that by using one or more log scales in a new plot? \"\"\" md\"\"\" Use the widgets from PlutoUI to write an interactive visualization that performs Exercise 2.3 for p varying between 0 and 1 and N between 0 and 100,000 . You might want to go back to Exercise 2.3 to turn your code into a function that can be called again. As you vary p , what do you observe? Does that make sense? \"\"\" bind hello Slider 2 0.5 10 hello md\"\"\" Exercise 2.6 👉 For fixed N 10,000 , write a function that calculates the mean time to recover, \\langle \\tau p \\rangle , as a function of p . \"\"\" md\"\"\" 👉 Use plots of your function to find the relationship between \\langle \\tau p \\rangle and p . \"\"\" md\"\"\" Based on my observations, it looks like we have the following relationship ```math \\langle \\tau p \\rangle my \\cdot answer \\cdot here ``` \"\"\" md\"\"\" Exercise 3 More efficient geometric distributions Let's use the notation P n \\mathbb P \\tau n for the probability to fail on the n th step. Probability theory tells us that in the limit of an infinite number of trials, we have the following exact results P 1 p P 2 p 1 p , and in general P n p 1 p ^ n 1 . \"\"\" md\"\"\" Exercise 3.1 👉 Fix p 0.25 . Make a vector of the values P n for n 1, \\dots, 50 . You must of course use a loop or similar construction do not do this by hand \"\"\" Ps let your code here end md\"\"\" 👉 Do they sum to 1? \"\"\" md\"\"\" Exercise 3.2 👉 Check analytically that the probabilities sum to 1 when you include all infinitely many of them. \"\"\" md\"\"\" ```math \\sum k 1 ^ \\infty P k \\dots your \\cdot answer \\cdot here \\dots 1 ``` \"\"\" md\"\"\" Exercise 3.3 Sum of a geometric series \"\"\" md\"\"\" 👉 Plot P n as a function of n . Compare it to the corresponding result from the previous exercise i.e. plot them both on the same graph . \"\"\" md\"\"\" 👉 How could we measure the error , i.e. the distance between the two graphs? What do you think determines it? \"\"\" md\"\"\" Exercise 3.4 If p is small , say p 0.001 , then the algorithm we used in Exercise 2 to sample from geometric distribution will be very slow, since it just sits there calculating a lot of `false`s The average amount of time taken is what you found in 1.8 . \"\"\" md\"\"\" Let's make a better algorithm. Think of each probability P n as a \"bin\", or interval, of length P n . If we lay those bins next to each other starting from P 1 on the left, then P 2 , etc., there will be an infinite number of bins that fill up the interval between 0 and 1 . In principle there is no upper limit on how many days it will take to recover, although the probability becomes very small. \"\"\" md\"\"\" Now suppose we take a uniform random number r between 0 and 1 . That will fall into one of the bins. If it falls into the bin corresponding to P n , then we return n as the recovery time \"\"\" md\"\"\" 👉 To draw this picture, we need to add up the lengths of the lines from 1 to n for each n , i.e. calculate the cumulative sum . Write a function `cumulative sum`, which returns a new vector. \"\"\" function cumulative sum xs Vector return missing end cumulative sum 1, 3, 5, 7, 9 md\"\"\" 👉 Plot the resulting values on a horizontal line. Generate a few random points and plot those. Convince yourself that the probability that a point hits a bin is equal to the length of that bin. \"\"\" cumulative cumulative sum Ps md\"\"\" Exercise 3.5 👉 Calculate the sum of P 1 up to P n analytically. \"\"\" md\"\"\" ```math C n \\sum k 1 ^n P k my \\cdot answer \\cdot here ``` \"\"\" md\"\"\" 👉 Use the previous result to find analytically which bin n a given value of r \\in 0, 1 falls into, using the inequality P n 1 \\le r \\le P n . \"\"\" md\"\"\" ```math n r,p my \\cdot answer \\cdot here ``` \"\"\" md\"\"\" Exercise 3.6 👉 Implement this as a function `geomtric bin r, p `, use the `floor` function. \"\"\" function geometric bin u Real, p Real return missing end md\"\"\" We can use this to define a fast version of the `geomtric` function \"\"\" geometric fast p geometric bin rand , p geometric fast 0.25 md\"\"\" Exercise 3.7 👉 Generate `10 000` samples from `geometric fast` with p 10^ 10 and plot a histogram of them. This would have taken a very long time with the previous method \"\"\" md\"\"\" Exercise 4 Distribution of the \"atmosphere\" In this question we will implement a very simple model of the density of the atmosphere, using a random walk , i.e. a particle that undergoes random motion . We will see more about random walks in lectures. We can think of a very light dust particle being moved around by the wind. We are only interested in its vertical position, y , and we will suppose for simplicity that y is an integer. The particle jumps up and down randomly as it is moved by the wind however, due to gravity it has a higher probability p of moving downwards to y 1 than upwards to y 1 , which happens with probability 1 p . At y 1 there is a boundary condition it hits a reflective boundary the \"surface of the Earth\" . We can model this using \"bounce back\" if the particle tries to jump downwards from y 1 then it hits a reflective boundary and bounces back to y 1 . In other words it remains in the same place. \"\"\" md\"\"\" Exercise 4.1 👉 Write a simulation of this model in a function `atmosphere` that accepts `p`, the initial height `y0`, and the number of steps N as variables. \"\"\" function atmosphere p Real, y0 Real, N Integer return missing end atmosphere 0.8, 10, 50 md\"\"\" Let's simulate it for 10^7 time steps with x 0 10 and p 0.55 . Exercise 4.2 👉 Calculate and plot the probability distribution of the walker's height. \"\"\" md\"\"\" 👉 What does the resulting figure look like? What form do you think this distribution has? Verify your hypothesis by plotting the distribution using different scales. You can increase the number of time steps to make the results look nicer. \"\"\" md\"\"\" Exercise 4.3 👉 Make an interactive visualization of how the distribution develops over time. What happens for longer and longer times? \"\"\" md\"\"\" 👉 Use wikipedia to find a formula for the barometric pressure at a given altitude. Does this result match your expectations? \"\"\" md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" Exercise 1.1 👉 Write a function `counts` that accepts a vector `data` and calculates the number of times each value in `data` occurs. The input will be an array of integers, with duplicates , and the result will be a dictionary that maps each occurred value to its count in the data. For example, ```julia counts 7, 8, 9, 7 ``` should give ```julia Dict 7 2, 8 1, 9 1, ``` To do so, use a dictionary called `counts`. We can create a local variable with the same name as the function. hint md\"Do you remember how we worked with dictionaries in Homework 3? You can create an empty dictionary using `Dict `. You may want to use either the function `haskey` or the function `get` on your dictionary check the documentation for how to use these functions. \" The function should return the dictionary. \"\"\" hint md\"Remember to always re use work you have done previously in this case you should re use the function `bernoulli`.\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next question.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined counts not defined counts else let result counts 51, 52, 52,53,51 if ismissing result still missing elseif result isa Dict keep working md\"Make sure that `counts` returns a Dictionary.\" elseif result Dict 51 2, 52 2, 53 1, correct else keep working end end end if isdefined test counts not defined test counts else if test counts Dict 0 2, 1000 2, 1 4 keep working else correct end end if isdefined counts2 not defined counts2 else let result counts2 51, 52, 52,53,51 if ismissing result still missing elseif result isa Tuple AbstractVector, AbstractVector keep working md\"Make sure that `counts2` returns a Tuple of two vectors.\" elseif result 52, 51, 53 , 2, 2, 1 , correct else keep working end end end if isdefined probability distribution not defined probability distribution else let result probability distribution 51, 52, 52,53,51 if ismissing result still missing elseif result isa Tuple AbstractVector, AbstractVector keep working md\"Make sure that `counts2` returns a Tuple of two vectors.\" elseif result 1 52, 51, 53 && isapprox result 2 , 2, 2, 1 . 5 correct else keep working end end end if isdefined bernoulli not defined bernoulli else let result bernoulli 0.5 if result isa Missing still missing elseif result isa Bool keep working md\"Make sure that you return either `true` or `false`.\" else if bernoulli 0.0 false && bernoulli 1.0 true correct else keep working end end end end if isdefined geometric not defined geometric else let result geometric 1.0 if result isa Missing still missing elseif result isa Int keep working md\"Make sure that you return an integer the recovery time.\" else if result 1 samples geometric 0.2 for in 1 256 a, b extrema samples if a 1 && b 20 correct else keep working end else keep working md\"`p 1.0` should return `1` the agent recovers after the first time step.\" end end end end if isdefined cumulative sum not defined cumulative sum else let result cumulative sum 1,2,3,4 if result isa Missing still missing elseif result isa AbstractVector keep working md\"Make sure that you return an Array the cumulative sum \" elseif length result 4 keep working md\"You should return an array of the same size a `xs`.\" else if isapprox result, 1, 3, 6, 10 correct else keep working end end end end if isdefined geometric bin not defined geometric bin else let result1 geometric bin 0.1, 0.1 result2 geometric bin 0.9, 0.1 if result1 isa Missing still missing elseif result1 isa Real keep working md\"Make sure that you return a number.\" elseif all isinteger, result1, result2 if result1 21 || result2 21 || result1 22 || result2 22 correct else keep working end else keep working md\"You should use the `floor` function to return an integer.\" end end end todo text HTML \"\"\" div style \"background rgb 220, 200, 255 padding 2em border radius 1em \" h1 TODO h1 repr MIME\"text html\" , text div \"\"\" bigbreak html\" br br br br br \" bigbreak bigbreak bigbreak bigbreak bigbreak bigbreak bigbreak begin fruits \"🍒\", \"🍐\", \"🍋\" length fruits end begin fruits \"🍉\" length fruits end "},{"url":"homework/hw7/","title":"Epidemic modeling I","tags":["homework","module2","epidemiology","track_data","monte carlo","statistics","track_math","ODE","agent based model","differential equation","type","structure","plotting"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 section 7.5 order 7.5 homework number 7 title \"Epidemic modeling I\" layout \"layout.jlhtml\" tags \"homework\", \"module2\", \"epidemiology\", \"track data\", \"monte carlo\", \"statistics\", \"track math\", \"ODE\", \"agent based model\", \"differential equation\", \"type\", \"structure\", \"plotting\" description \"Simulate the spread of an epidemic by creating your own agent based model from scratch, and find statistics using the Monte Carlo method.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI md\" homework 7, version 3 \" md\"\"\" Homework 7 Epidemic modeling `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" function bernoulli p Number rand p end md\"\"\" Exercise 1 Agent based model for an epidemic outbreak types In this and the following exercises we will develop a simple stochastic model for combined infection and recovery in a population, which may exhibit an epidemic outbreak i.e. a large spike in the number of infectious people . The population is well mixed , i.e. everyone is in contact with everyone else. An example of this would be a small school or university in which people are constantly moving around and interacting with each other. The model is an individual based or agent based model we explicitly keep track of each individual, or agent , in the population and their infection status. For the moment we will not keep track of their position in space we will just assume that there is some mechanism, not included in the model, by which they interact with other individuals. Exercise 1.1 Each agent will have its own internal state , modelling its infection status, namely \"susceptible\", \"infectious\" or \"recovered\". We would like to code these as values `S`, `I` and `R`, respectively. One way to do this is using an enumerated type https en.wikipedia.org wiki Enumerated type or enum . Variables of this type can take only a pre defined set of values the Julia syntax is as follows \"\"\" enum InfectionStatus S I R md\"\"\" We have just defined a new type `InfectionStatus`, as well as names `S`, `I` and `R` that are the only possible values that a variable of this type can take. 👉 Define a variable `test status` whose value is `S`. \"\"\" test status missing md\"\"\" 👉 Use the `typeof` function to find the type of `test status`. \"\"\" md\"\"\" 👉 Convert `x` to an integer using the `Integer` function. What value does it have? What values do `I` and `R` have? \"\"\" md\"\"\" Exercise 1.2 For each agent we want to keep track of its infection status and the number of other agents that it infects during the simulation. A good solution for this is to define a new type `Agent` to hold all of the information for one agent, as follows \"\"\" mutable struct Agent status InfectionStatus num infected Int64 end md\"\"\" When you define a new type like this, Julia automatically defines one or more constructors , which are methods of a generic function with the same name as the type. These are used to create objects of that type. 👉 Use the `methods` function to check how many constructors are pre defined for the `Agent` type. \"\"\" md\"\"\" 👉 Create an agent `test agent` with status `S` and `num infected` equal to 0. \"\"\" test agent missing md\"\"\" 👉 For convenience, define a new constructor i.e. a new method for the function that takes no arguments and creates an `Agent` with status `S` and number infected 0, by calling one of the default constructors that Julia creates. This new method lives outside not inside the definition of the `struct`. It is called an outer constructor . In Pluto, a struct definition and an outer constructor need to be combined in a single cell using a `begin end` block. Let's check that the new method works correctly. How many methods does the constructor have now? \"\"\" Agent md\"\"\" Exercise 1.3 👉 Write functions `set status a ` and `set num infected a ` which modify the respective fields of an `Agent`. Check that they work. Note the bang \"` `\" at the end of the function names to signify that these functions modify their argument. \"\"\" function set status agent Agent, new status InfectionStatus your code here end md\"\"\" 👉 We will also need functions `is susceptible` and `is infected` that check if a given agent is in those respective states. \"\"\" function is susceptible agent Agent return missing end function is infected agent Agent return missing end md\"\"\" Exercise 1.4 👉 Write a function `generate agents N ` that returns a vector of `N` freshly created `Agent`s. They should all be initially susceptible, except one, chosen at random i.e. uniformly , who is infectious. \"\"\" function generate agents N Integer return missing end generate agents 3 md\"\"\" We will also need types representing different infections. Let's define an immutable `struct` called `InfectionRecovery` with parameters `p infection` and `p recovery`. We will make it a subtype of an abstract `AbstractInfection` type, because we will define more infection types later. \"\"\" abstract type AbstractInfection end struct InfectionRecovery AbstractInfection p infection p recovery end md\"\"\" Exercise 1.5 👉 Write a function `interact ` that takes an affected `agent` of type `Agent`, an `source` of type `Agent` and an `infection` of type `InfectionRecovery`. It implements a single one sided interaction between two agents If the `agent` is susceptible and the `source` is infectious, then the `source` infects our `agent` with the given infection probability. If the `source` successfully infects the other agent, then its `num infected` record must be updated. If the `agent` is infected then it recovers with the relevant probability. Otherwise, nothing happens. html\" span id interactfunction span \" \"\"\" function interact agent Agent, source Agent, infection InfectionRecovery your code here end md\"\"\" Play around with the test case below to test your function Try changing the definitions of `agent`, `source` and `infection`. Since we are working with randomness, you might want to run the cell multiple times. \"\"\" let agent Agent S, 0 source Agent I, 0 infection InfectionRecovery 0.9, 0.5 interact agent, source, infection agent agent, source source end md\"\"\" Exercise 2 Agent based model for an epidemic outbreak Monte Carlo simulation In this exercise we will build on Exercise 2 to write a Monte Carlo simulation of how an infection propagates in a population. Make sure to re use the functions that we have already written, and introduce new ones if they are helpful Short functions make it easier to understand what the function does and build up new functionality piece by piece. You should not use any global variables inside the functions Each function must accept as arguments all the information it requires to carry out its task. You need to think carefully about what the information each function requires. Exercise 2.1 👉 Write a function `step ` that takes a vector of `Agent`s and an `infection` of type `InfectionRecovery`. It implements a single step of the infection dynamics as follows Choose two random agents an `agent` and a `source`. Apply `interact agent, source, infection `. Return `agents`. \"\"\" function step agents Vector Agent , infection InfectionRecovery end md\"\"\" 👉 Write a function `sweep `. It runs `step ` N times, where N is the number of agents. Thus each agent acts, on average, once per sweep a sweep is thus the unit of time in our Monte Carlo simulation. \"\"\" function sweep agents Vector Agent , infection AbstractInfection end md\"\"\" 👉 Write a function `simulation` that does the following 1. Generate the N agents. 2. Run `sweep ` a number T of times. Calculate and store the total number of agents with each status at each step in variables `S counts`, `I counts` and `R counts`. 3. Return the vectors `S counts`, `I counts` and `R counts` in a named tuple , with keys `S`, `I` and `R`. You've seen an example of named tuples before the `student` variable at the top of the notebook Feel free to store the counts in a different way, as long as the return type is the same. \"\"\" function simulation N Integer, T Integer, infection AbstractInfection return S missing, I missing, R missing end simulation 3, 20, InfectionRecovery 0.9, 0.2 simulation 100, 1000, InfectionRecovery 0.005, 0.2 bind run basic sir Button \"Run simulation again \" let run basic sir N 100 T 1000 sim simulation N, T, InfectionRecovery 0.02, 0.002 result plot 1 T, sim.S, ylim 0, N , label \"Susceptible\" plot result, 1 T, sim.I, ylim 0, N , label \"Infectious\" plot result, 1 T, sim.R, ylim 0, N , label \"Recovered\" end md\"\"\" Exercise 2.2 Alright Every time that we run the simulation, we get slightly different results, because it is based on randomness. By running the simulation a number of times, you start to get an idea of the mean behaviour of our model. This is the essence of a Monte Carlo method You use computer generated randomness to generate samples. Instead of pressing the button many times, let's have the computer repeat the simulation. In the next cells, we run your simulation `num simulations 20` times with N 100 , p \\text infection 0.02 , p \\text infection 0.002 and T 1000 . Every single simulation returns a named tuple with the status counts, so the result of multiple simulations will be an array of those. Have a look inside the result, `simulations`, and make sure that its structure is clear. \"\"\" function repeat simulations N, T, infection, num simulations N 100 T 1000 map 1 num simulations do simulation N, T, infection end end simulations repeat simulations 100, 1000, InfectionRecovery 0.02, 0.002 , 20 md\"\"\" In the cell below, we plot the evolution of the number of I individuals as a function of time for each of the simulations on the same plot using transparency `alpha 0.5` inside the plot command . \"\"\" let p plot for sim in simulations plot p, 1 1000, sim.I, alpha .5, label nothing end p end md\"\"\" 👉 Write a function `sir mean plot` that returns a plot of the means of S , I and R as a function of time on a single graph. \"\"\" function sir mean plot simulations Vector NamedTuple you might need T for this function, here's a trick to get it T length first simulations .S return missing end sir mean plot simulations let T length first simulations .S all S counts map result result.S, simulations all I counts map result result.I, simulations all R counts map result result.R, simulations S round. sum all S counts . length simulations . 100, digits 4 , I round. sum all I counts . length simulations . 100, digits 4 , R round. sum all R counts . length simulations . 100, digits 4 end md\"\"\" 👉 Allow p \\text infection and p \\text recovery to be changed interactively and find parameter values for which you observe an epidemic outbreak. \"\"\" md\"\"\" 👉 Write a function `sir mean error plot` that does the same as `sir mean plot`, which also computes the standard deviation \\sigma of S , I , R at each step. Add this to the plot using error bars , using the option `yerr σ` in the plot command use transparency. This should confirm that the distribution of I at each step is pretty wide \"\"\" function sir mean error plot simulations Vector NamedTuple you might need T for this function, here's a trick to get it T length first simulations .S return missing end md\"\"\" Exercise 2.3 👉 Plot the probability distribution of `num infected`. Does it have a recognisable shape? Feel free to increase the number of agents in order to get better statistics. \"\"\" md\"\"\" Exercse 2.4 👉 What are three simple ways in which you could characterise the magnitude size of the epidemic outbreak? Find approximate values of these quantities for one of the runs of your simulation. \"\"\" md\"\"\" Exercise 3 Reinfection In this exercise we will re use our simulation infrastructure to study the dynamics of a different type of infection there is no immunity, and hence no \"recovery\" rather, susceptible individuals may now be re infected Exercise 3.1 👉 Make a new infection type `Reinfection`. This has the same two fields as `InfectionRecovery` `p infection` and `p recovery` . However, \"recovery\" now means \"becomes susceptible again\", instead of \"moves to the `R` class. This new type `Reinfection` should also be a subtype of `AbstractInfection`. This allows us to reuse our previous functions, which are defined for the abstract supertype. \"\"\" md\"\"\" 👉 Make a new method for the `interact ` function that accepts the new infection type as argument, reusing as much functionality as possible from the previous version. \"\"\" md\"\"\" Exercise 3.2 👉 Run the simulation 20 times and plot I as a function of time for each one, together with the mean over the 20 simulations as you did in the previous exercises . Note that you should be able to re use the `sweep ` and `simulation` functions , since those should be sufficiently generic to work with the new `step ` function Modify them if they are not. \"\"\" md\"\"\" 👉 Run the new simulation and draw I averaged over runs as a function of time. Is the behaviour qualitatively the same or different? Describe what you see. \"\"\" md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" 👉 Calculate the mean number of infectious agents of our simulations for each time step. Add it to the plot using a heavier line `lw 3` for \"linewidth\" by modifying the cell above. Check the answer yourself does your curve follow the average trend? hint md\"This exercise requires some creative juggling with arrays, anonymous functions, `map`s, or whatever you see fit \" \"\"\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined set status not defined set status else let agent Agent I,2 set status agent, R if agent.status R correct else keep working end end end if isdefined is susceptible not defined is susceptible else let result1 is susceptible Agent I,2 result2 is infected Agent I,2 if result1 isa Missing || result2 isa Missing still missing elseif result1 isa Bool || result2 isa Bool keep working md\"Make sure that you return either `true` or `false`.\" elseif result1 false && result2 true if is susceptible Agent S,3 && is infected Agent R,9 correct else keep working end else keep working end end end if isdefined generate agents not defined generate agents else let result generate agents 4 if result isa Missing still missing elseif result isa Nothing keep working \"The function returned `nothing`. Did you forget to return something?\" elseif result isa Vector || all x x isa Agent, result keep working md\"Make sure that you return an array of objects of the type `Agent`.\" elseif length result 4 almost md\"Make sure that you return `N` agents.\" elseif length Set result 4 almost md\"You returned the same agent `N` times. You need to call the `Agent` constructor `N` times, not once.\" else if sum a a.status I, result 1 almost md\"Exactly one of the agents should be infectious.\" else correct end end end end if isdefined interact not defined interact else let agent Agent S, 9 source Agent I, 0 interact agent, source, InfectionRecovery 0.0, 1.0 if source.status I || source.num infected 0 keep working md\"The `source` should not be modified if no infection occured.\" elseif agent.status S keep working md\"The `agent` should get infected with the right probability.\" else agent Agent S, 9 source Agent S, 0 interact agent, source, InfectionRecovery 1.0, 1.0 if source.status S || source.num infected 0 || agent.status S keep working md\"The `agent` should get infected with the right probability if the source is infectious.\" else agent Agent S, 9 source Agent I, 3 interact agent, source, InfectionRecovery 1.0, 1.0 if agent.status R almost md\"The agent should not recover immediately after becoming infectious.\" elseif agent.status S keep working md\"The `agent` should recover from an infectious state with the right probability.\" elseif source.status I || source.num infected 4 almost md\"The `source` did not get updated correctly after infecting the `agent`.\" else correct md\"Your function treats the susceptible agent case correctly \" end end end end end if isdefined interact not defined interact else let agent Agent I, 9 source Agent S, 0 interact agent, source, InfectionRecovery 1.0, 1.0 if source.status S || source.num infected 0 keep working md\"The `source` should not be modified if `agent` is infectious.\" elseif agent.status R keep working md\"The `agent` should recover from an infectious state with the right probability.\" elseif agent.num infected 9 keep working md\"`agent.num infected` should not be modified if `agent` is infectious.\" else let agent Agent I, 9 source Agent R, 0 interact agent, source, InfectionRecovery 1.0, 0.0 if agent.status R keep working md\"The `agent` should recover from an infectious state with the right probability.\" else correct md\"Your function treats the infectious agent case correctly \" end end end end end if isdefined interact not defined interact else let agent Agent R, 9 source Agent I, 0 interact agent, source, InfectionRecovery 1.0, 1.0 if source.status I || source.num infected 0 keep working md\"The `source` should not be modified if no infection occured.\" elseif agent.status R || agent.num infected 9 keep working md\"The `agent` should not be momdified if it is in a recoved state.\" else correct md\"Your function treats the recovered agent case correctly \" end end end bigbreak html\" br br br br br \" bigbreak bigbreak "},{"url":"homework/hw8/","title":"Epidemic modeling II","tags":["homework","module2","track_math","track_data","optimization","statistics","gradient","differentiation","automatic differentiation","continuous","probability","epidemiology","monte carlo","modeling","plotting"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 section 9.5 order 9.5 homework number 8 title \"Epidemic modeling II\" layout \"layout.jlhtml\" tags \"homework\", \"module2\", \"track math\", \"track data\", \"optimization\", \"statistics\", \"gradient\", \"differentiation\", \"automatic differentiation\", \"continuous\", \"probability\", \"epidemiology\", \"monte carlo\", \"modeling\", \"plotting\" description \"Learn about optimisation and gradient descent with help from our visuals and automatic checks. We use these new skill to fit parameters of an epidemic model to match reality.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI md\" homework 8, version 3 \" md\"\"\" Homework 8 Epidemic modeling II `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" In the past couple homeworks, we've messed around with probability distributions and Monte Carlo simulations. This time, we'll build a more mathematical model, and we'll use optimization to fit it to given data. Models have parameters , such as the rate of recovery from infection. Where do the parameter values come from? Ideally we would like to extract them from data. The goal of this homework is to do this by fitting a model to data. For simplicity, we will use data that generated from a spatial model one that simulates the movement of a population in space instead of real world data, and we will fit the simplest SIR model. But the same ideas apply more generally. There are many ways to fit a function to data, but all must involve some form of optimization , usually minimization of a particular function, a loss function this is the basis of the vast field of machine learning . The loss function is a function of the model parameters it measures how far the model output is from the data, for the given values of the parameters. We emphasise that this material is pedagogical there is no suggestion that these specific techniques should be used actual calculations rather, it is the underlying ideas that are important. \"\"\" md\"\"\" Exercise 1 Calculus without calculus \"\"\" md\"\"\" Before we jump in to simulating the SIR equations, let's experiment with a simple 1D function. In calculus, we learn techniques for differentiating and integrating symbolic equations, e.g. ``\\frac d dx x^n nx^ n 1 ``. But in real applications, it is often impossible to apply these techniques, either because the problem is too complicated to solve symbolically, or because our problem has no symbolic expression, like when working with experimental results. Instead, as you will recall from lecture, we use ✨ computers ✨ to approximate derivatives and integrals. Instead of applying rules to symbolic expressions, we use much simpler strategies that only use the output values of our function . One such example is the finite difference method for approximating the derivative of a function. It is inspired by the analytical definition of the derivative f' a \\lim h \\rightarrow 0 \\frac f a h f a h . The finite difference method simply fixes a small value for h , say h 10^ 3 , and then approximates the derivative as f' a \\simeq \\frac f a h f a h . \"\"\" md\"\"\" Exercise 1.1 tangent line 👉 Write a function `finite difference slope` that takes a function `f` and numbers `a` and `h`. It returns the slope ``f' a ``, approximated using the finite difference formula above. \"\"\" function finite difference slope f Function, a, h 1e 3 return missing end finite difference slope sqrt, 4.0, 5.0 md\"\"\" 👉 Write a function `tangent line` that takes the same arguments `f`, `a` and `g`, but it returns a function . This function ``\\mathbb R \\rightarrow \\mathbb R `` is the tangent line with slope ``f' a `` computed using `finite difference slope` that passes through `` a, f a ``. \"\"\" function tangent line f, a, h return missing end this is our test function wavy x .1x^3 1.6x^2 7x 3 md\"\"\" The slider below controls ``h`` using a log scale . In the mathematical definition of the derivative, we take ``\\lim h \\rightarrow 0 ``. This corresponds to moving the slider to the left. Notice that, as you decrease ``h``, the tangent line gets more accurate, but what happens if you make ``h`` too small? \"\"\" bind log h Slider 16 0.01 .5, default .5 h finite diff 10.0^log h zeroten LinRange 0.0, 10.0, 300 bind a finite diff Slider zeroten, default 4 let p plot zeroten, wavy, label \"f x \" scatter p, a finite diff , wavy a finite diff , label \"a\", color \"red\" vline p, a finite diff , label nothing, color \"red\", linestyle dash scatter p, a finite diff h finite diff , wavy a finite diff h finite diff , label \"a h\", color \"green\" try result tangent line wavy, a finite diff, h finite diff plot p, zeroten, result, label \"tangent\", color \"purple\" catch end plot p, xlim 0, 10 , ylim 2, 8 end | as svg md\"\"\" html\" span id theslopeequation span \" Exercise 1.2 antiderivative In the finite differences method, we approximated the derivative of a function f' a \\simeq \\frac f a h f a h We can do something very similar to approximate the 'antiderivate' of a function. Finding the antiderivative means that we use the slope ``f'`` to compute ``f`` numerically This antiderivative problem is illustrated below. The only information that we have is the slope at any point ``a \\in \\mathbb R ``, and we have one initial value , ``f 1 ``. \"\"\" in this exercise, only the derivative is given wavy deriv x .3x^2 3.2x 7 bind a euler Slider zeroten, default 1 let slope wavy deriv a euler p plot LinRange 1.0 0.1, 1.0 0.1, 2 , wavy, label nothing, lw 3 scatter p, 1 , wavy, label \"f 1 \", color \"blue\", lw 3 p plot x a euler 0.2,a euler 0.2 for y in 4 10 plot p, x, slope . x . a euler . y, label nothing, color \"purple\", opacity .6 end vline p, a euler , color \"red\", label \"a\", linestyle dash plot p, xlim 0, 10 , ylim 2, 8 end | as svg md\"\"\" Using only this information, we want to reconstruct ``f``. By rearranging the equation above theslopeequation , we get the Euler method f a h \\simeq hf' a f a Using this formula, we only need to know the value ``f a `` and the slope ``f' a `` of a function at ``a`` to get the value at ``a h``. Doing this repeatedly can give us the value at ``a 2h``, at ``a 3h``, etc., all from one initial value ``f a ``. 👉 Write a function `euler integrate step` that applies this formula to a known function ``f'`` at ``a``, with step size ``h`` and the initial value ``f a ``. It returns the next value, ``f a h ``. \"\"\" function euler integrate step fprime Function, fa Number, a Number, h Number return missing end md\"\"\" 👉 Write a function `euler integrate` that takes takes a known function ``f'``, the initial value ``f a `` and a range `T` with `a first T ` and `h step T `. It applies the function `euler integrate step` repeatedly, once per entry in `T`, to produce the sequence of values ``f a h ``, ``f a 2h ``, etc. \"\"\" function euler integrate fprime Function, fa Number, T AbstractRange a0 T 1 h step T return missing end md\"\"\" Let's try it out on ``f' x 3x^2`` and `T` ranging from ``0`` to ``10``. We already know the analytical solution ``f x x^3``, so the result should be an array going from approximately `0.0` to `1000.0`. \"\"\" euler test let fprime x 3x^2 T 0 0.1 10 euler integrate fprime, 0, T end bind N euler Slider 2 40 let a 1 h .3 history euler integrate wavy deriv, wavy a , range a step h, length N euler slope wavy deriv a euler p plot zeroten, wavy, label \"exact solution\", lw 3, opacity .1, color \"gray\" p plot last a a N euler 1 h vline p, last a , color \"red\", label \"a\", linestyle dash try plot p, a . h . 1 N euler , history, color \"blue\", label nothing scatter p, a . h . 1 N euler , history, color \"blue\", label \"appromixation\", markersize 2, markerstrokewidth 0 plot p, 0,10 , 0,10 . last a h . wavy deriv last a h . history end , label \"tangent\", color \"purple\" catch end plot p, xlim 0, 10 , ylim 2, 8 end | as svg md\"\"\" You see that our numerical antiderivate is not very accurate, but we can get a smaller error by choosing a smaller step size. Try it out There are also alternative integration methods that are more accurate with the same step size. Some methods also use the second derivative, other methods use multiple steps at once, etc. This is the study of Numerical Methods. \"\"\" md\"\"\" Exercise 2 Simulating the SIR differential equations We will look at a type of mathematical model for epidemic outbreaks called the SIR model . In this model, we consider three functions of time t the fraction of the population s t that is susceptible to the disease, the fraction i t that is infected , and the fraction r t that has recovered . We've already used these three quantities in a simulation before, namely in the Monte Carlo simulation from last homework It turns out that, given some assumptions about how people from the three groups interact over time, we can model the behavior of the three quantities in terms of their derivatives with respect to time t . We use two parameters \\beta and \\gamma , and with them, we can form a system of ordinary differential equations ODEs for the SIR model. They are as follows \\begin align \\dot s & \\beta s \\, i \\\\ \\dot i & \\beta s \\, i \\gamma i \\\\ \\dot r & \\gamma i \\end align where ``\\dot s \\frac ds dt `` is the derivative of s with respect to time. Recall that s denotes the proportion fraction of the population that is susceptible, a number between 0 and 1 . We will use the simplest possible method to simulate these, namely the Euler method . The Euler method is not always a good method to solve ODEs accurately, but for our purposes it is good enough. In the previous exercise, we introduced the Euler method for a 1D function, which you can see as an ODE that only depends on time. For the SIR equations, we have an ODE that only depends on the previous value , not on time, and we have 3 equations instead of 1. The solution is quite simple, we apply the Euler method to each of the differential equations within a single time step to get new values for each of s , i and r at the end of the time step in terms of the values at the start of the time step. The Euler discretised equations are \\begin align s t h & s t h\\,\\cdot\\beta s t \\, i t \\\\ i t h & i t h\\,\\cdot \\beta s t \\, i t \\gamma i t \\\\ r t h & r t h\\,\\cdot \\gamma i t \\end align 👉 Implement a function `euler SIR step β, γ, sir 0, h ` that performs a single Euler step for these equations with the given parameter values and initial values, with a step size h . `sir 0` is a 3 element vector, and you should return a new 3 element vector with the values after the timestep. \"\"\" function euler SIR step β, γ, sir 0 Vector, h Number s, i, r sir 0 return missing, missing, missing, end euler SIR step 0.1, 0.05, 0.99, 0.01, 0.00 , 0.1 md\"\"\" 👉 Implement a function `euler SIR β, γ, sir 0, T ` that applies the previously defined function over a time range T . You should return a vector of vectors a 3 element vector for each point in time. \"\"\" function euler SIR β, γ, sir 0 Vector, T AbstractRange T is a range, you get the step size and number of steps like so h step T num steps length T return missing end sir T 0 0.1 60.0 sir results euler SIR 0.3, 0.15, 0.99, 0.01, 0.00 , sir T md\"\"\" Let's plot s , i and r as a function of time. \"\"\" function plot sir p, T, results label \"\", kwargs... s getindex. results, 1 i getindex. results, 2 r getindex. results, 3 plot p, T, s color 1, label label \" S\", lw 3, kwargs... plot p, T, i color 2, label label \" I\", lw 3, kwargs... plot p, T, r color 3, label label \" R\", lw 3, kwargs... p end plot sir plot , sir T, sir results md\"\"\" 👉 Do you see an epidemic outbreak i.e. a rapid growth in number of infected individuals, followed by a decline ? What happens after a long time? Does everybody get infected? \"\"\" default SIR parameters observation md\"\"\" your answer here \"\"\" md\"\"\" 👉 Make an interactive visualization, similar to the above plot, in which you vary \\beta and \\gamma via sliders. What relation should \\beta and \\gamma have for an epidemic outbreak to occur? \"\"\" md\"\"\" Exercise 3 Numerical gradient For fitting we need optimization, and for optimization we will use derivatives rates of change . In Exercise 1, we wrote a function `finite difference slope f, a ` to approximate ``f' a ``. In this exercise we will write a function to compute partial derivatives . \"\"\" md\"\"\" Exercise 3.1 👉 Write functions `∂x f, a, b ` and `∂y f, a, b ` that calculate the partial derivatives \\frac \\partial f \\partial x and \\frac \\partial f \\partial y at a, b of a function f \\mathbb R ^2 \\to \\mathbb R i.e. a function that takes two real numbers and returns one real . Recall that \\frac \\partial f \\partial x is the derivative of the single variable function g x f x, b obtained by fixing the value of y to b . You should use anonymous functions for this. These have the form `x x^2`, meaning \"the function that sends x to x^2 \". \"\"\" function ∂x f Function, a, b return missing end ∂x x, y 7x^2 y, 3, 7 function ∂y f Function, a, b return missing end ∂y x, y 7x^2 y, 3, 7 md\"\"\" Exercise 3.2 👉 Write a function `gradient f, a, b ` that calculates the gradient of a function f at the point a, b , given by the vector \\nabla f a, b \\frac \\partial f \\partial x a, b , \\frac \\partial f \\partial y a, b . \"\"\" function gradient f Function, a, b return missing end gradient x, y 7x^2 y, 3, 7 md\"\"\" Exercise 4 Minimisation using gradient descent In this exercise we will use gradient descent to find local minima of smooth enough functions, revisiting what we saw on the lectures. As a refresher, we'll want to think of a function as a hill. To find a minimum we should \"roll down the hill\". Exercise 4.1 We want to minimize a 1D function, i.e. a function f \\mathbb R \\to \\mathbb R . To do so we notice that the derivative tells us the direction in which the function increases . Positive slope means that the minimum is to the left, negative slope means to the right. So our gradient descent method is to take steps in the opposite direction, of a small size \\eta \\cdot f' x 0 . 👉 Write a function `gradient descent 1d step f, x0 ` that performs a single gradient descent step, from the point `x0` and using your function `finite difference slope` to approximate the derivative. The result should be the next guess for ``x``. \"\"\" function gradient descent 1d step f, x0 η 0.01 return missing end let f x x^2 the minimum is at 0, so we should take a small step to the left gradient descent 1d step f, 5 end bind N gradient 1d Slider 0 20 md\" ``x 0 `` bind x0 gradient 1d Slider 3 .01 1.5, default 1, show value true \" function gradient 1d viz N gradient 1d, x0 f x x^4 3x^3 3x 5. x LinRange 3, 1.5, 200 history accumulate 1 N gradient 1d, init x0 do old, gradient descent 1d step f, old, η .025 end all x0, history... slope wavy deriv a euler p plot x, f, label \"f x \", lw 3, opacity .6, color \"gray\" p plot plot p, all, f, color \"blue\", opacity range .5,step .2,length length all , label nothing scatter p, all, f, color \"blue\", label \"gradient descent\", markersize 3, markerstrokewidth 0 as svg p end gradient 1d viz N gradient 1d, x0 gradient 1d md\"\"\" 👉 Write a function `gradient descent 1d f, x0 ` that repeatedly applies the previous function `N steps` times , starting from the point `x0`, like in the vizualisation above. The result should be the final guess for ``x``. \"\"\" function gradient descent 1d f, x0 η 0.01, N steps 1000 return missing end let f x x 5 ^2 3 minimum should be at x 5 gradient descent 1d f, 0.0 end md\"\"\" Right now we take a fixed number of steps, even if the minimum is found quickly. What would be a better way to decide when to end the function? \"\"\" better stopping idea md\"\"\" your answer here \"\"\" md\"\"\" Exericse 4.2 Multivariable calculus tells us that the gradient \\nabla f a, b at a point a, b is the direction in which the function increases the fastest. So again we should take a small step in the opposite direction. Note that the gradient is a vector which tells us which direction to move in the plane a, b . We multiply this vector with the scalar ``\\eta`` to control the step size. 👉 Write functions `gradient descent 2d step f, x0, y0 ` and `gradient descent 2d f, x0, y0 ` that do the same for functions f x, y of two variables. \"\"\" function gradient descent 2d step f, x0, y0 η 0.01 return missing end function gradient descent 2d f, x0, y0 η 0.01 return missing end bind N gradient 2d Slider 0 20 md\" ``x 0 `` bind x0 gradient 2d Slider 4 .01 4, default 0, show value true \" md\" ``y 0 `` bind y0 gradient 2d Slider 4 .01 4, default 0, show value true \" himmelbau x, y x^2 y 11 ^2 x y^2 7 ^2 gradient descent 2d himmelbau, 0, 0 md\"\"\" We also prepared a 3D visualisation if you like It's a bit slow... \"\"\" run 3d visualisation false function gradient 2d viz 3d N gradient 2d, x0, y0 history accumulate 1 N gradient 2d, init x0, y0 do old, gradient descent 2d step himmelbau, old... end all x0, y0 , history... p surface 4 0.4 5, 4 0.4 4, himmelbau trace himmelbau s... for s in all plot p, first. all , last. all , trace, color \"blue\", opacity range .5,step .2,length length all , label nothing scatter p, first. all , last. all , trace, color \"blue\", label \"gradient descent\", markersize 3, markerstrokewidth 0 as svg p end if run 3d visualisation let we temporarily change the plotting backend to an interactive one plotly we dont use the sliders because this plot is quite slow x0 0.5 N 20 y0 3 p gradient 2d viz 3d N, x0, y0 gr p end end function gradient 2d viz 2d N gradient 2d, x0, y0 history accumulate 1 N gradient 2d, init x0, y0 do old, gradient descent 2d step himmelbau, old... end all x0, y0 , history... p heatmap 4 0.4 5, 4 0.4 4, himmelbau plot p, first. all , last. all , color \"blue\", opacity range .5,step .2,length length all , label nothing scatter p, first. all , last. all , color \"blue\", label \"gradient descent\", markersize 3, markerstrokewidth 0 as svg p end gradient 2d viz 2d N gradient 2d, x0 gradient 2d, y0 gradient 2d md\"\"\" 👉 Can you find different minima? \"\"\" md\"\"\" Exercise 5 Learning parameter values In this exercise we will apply gradient descent to fit a simple function y f \\alpha, \\beta x to some data given as pairs x i, y i . Here \\alpha and \\beta are parameters that appear in the form of the function f . We want to find the parameters that provide the best fit , i.e. the version f \\alpha, \\beta of the function that is closest to the data when we vary \\alpha and \\beta . To do so we need to define what \"best\" means. We will define a measure of the distance between the function and the data, given by a loss function , which itself depends on the values of \\alpha and \\beta . Then we will minimize the loss function over \\alpha and \\beta to find those values that minimize this distance, and hence are \"best\" in this precise sense. The iterative procedure by which we gradually adjust the parameter values to improve the loss function is often called machine learning or just learning , since the computer is \"discovering\" information in a gradual way, which is supposed to remind us of how humans learn. Hint This is not how humans learn. Exercise 5.1 🎲 frequencies We generate a small dataset by throwing 10 dice, and counting the sum. We repeat this experiment many times, giving us a frequency distribution in a familiar shape. \"\"\" import Statistics function dice frequencies N dice, N experiments experiment let sum of rolls sum rand 1 6, N dice end results experiment for in 1 N experiments x N dice N dice 6 y map x do total sum isequal total , results end . N experiments x, y end dice x, dice y dice frequencies 10, 20 000 md\"\"\" Let's try to fit a gaussian normal distribution. Its PDF with mean \\mu and standard deviation \\sigma is f \\mu, \\sigma x \\frac 1 \\sigma \\sqrt 2 \\pi \\exp \\left \\frac x \\mu ^2 2 \\sigma^2 \\right 👉 Not graded Manually fit a Gaussian distribution to our data by adjusting ``\\mu`` and ``\\sigma`` until you find a good fit. \"\"\" md\"μ bind guess μ Slider 1 0.1 last dice x default last dice x 0.4, show value true \" md\"σ bind guess σ Slider 0.1 0.1 last dice x 2 default 12, show value true \" md\"Show manual fit bind show manual fit CheckBox \" function gauss x, μ, σ 1 sqrt 2π σ exp x μ ^2 σ^2 2 end md\"\"\" What we just did was adjusting the function parameters until we found the best possible fit. Let's automate this process To do so, we need to quantify how good or bad a fit is. 👉 Define a loss function to measure the \"distance\" between the actual data and the function. It will depend on the values of \\mu and \\sigma that you choose \\mathcal L \\mu, \\sigma \\sum i f \\mu, \\sigma x i y i ^2 \"\"\" function loss dice μ, σ return missing end loss dice guess μ 3, guess σ loss dice guess μ, guess σ md\"\"\" 👉 Use your `gradient descent 2d` function to find a local minimum of \\mathcal L , starting with initial values \\mu 30 and \\sigma 1 . Call the found parameters `found μ` and `found σ`. \"\"\" found μ, found σ let your code here missing, missing end let p plot dice x, dice y, size 600, 200 , label \"data\" if show manual fit plot p, dice x, gauss. dice x, guess μ , guess σ , label \"manual fit\" end try plot p, dice x, gauss. dice x, found μ , found σ , label \"optimized fit\" catch end p end md\"\"\" Go back to the graph to see your optimized gaussian curve If your fit is close, then probability theory tells us that the found parameter ``\\mu`` should be close to the weighted mean of our data, and ``\\sigma`` should approximate the sample standard deviation . We have already computed these values, and we check how close they are \"\"\" stats μ sum dice x . dice y abs stats μ found μ stats σ sqrt sum dice x.^2 . dice y stats μ .^ 2 abs stats σ found σ md\"\"\" Exercise 6 Putting it all together — fitting an SIR model to data In this exercise we will fit the non spatial SIR ODE model from Exercise 2 to some data generated from a different, spatial epidemic model, as we mentioned at the beginning. If we are able to find a good fit, that would suggest that the spatial aspect \"does not matter\" too much for the dynamics of these models. If the fit is not so good, perhaps there is an important effect of space. As usual in statistics, and indeed in modelling in general, we should be very cautious of making claims of this nature. This fitting procedure will be different from that in Exercise 4, however we no longer have an explicit form for the function that we are fitting. Rather, it is simply the output of an ODE So what should we do? We will try to find the parameters \\beta and \\gamma for which the output of the ODEs when we simulate it with those parameters best matches the data Exercise 6.1 Below is the result from the spatial model. These are the average S, I, R fractions from running 20 simulations. Click on it \"\"\" hw4 results transposed S 0.99, 0.9895, 0.9895, 0.989, 0.9885, 0.9885, 0.9885, 0.988, 0.9865, 0.986, 0.9855, 0.9855, 0.9855, 0.9845, 0.9845, 0.9845, 0.984, 0.984, 0.984, 0.9835, 0.9835, 0.982, 0.982, 0.982, 0.982, 0.9815, 0.981, 0.9805, 0.98, 0.98, 0.98, 0.98, 0.9795, 0.9795, 0.979, 0.979, 0.979, 0.978, 0.9775, 0.9775, 0.977, 0.976, 0.9755, 0.9745, 0.9735, 0.9735, 0.973, 0.972, 0.972, 0.972, 0.971, 0.971, 0.9695, 0.968, 0.9675, 0.967, 0.9665, 0.966, 0.9655, 0.9645, 0.9645, 0.9645, 0.964, 0.9615, 0.9595, 0.959, 0.958, 0.9575, 0.9575, 0.9575, 0.9565, 0.956, 0.954, 0.9535, 0.9535, 0.951, 0.95, 0.949, 0.9485, 0.948, 0.947, 0.9465, 0.9465, 0.9455, 0.945, 0.9425, 0.9415, 0.9405, 0.9395, 0.9385, 0.938, 0.937, 0.9355, 0.9355, 0.935, 0.9325, 0.932, 0.93, 0.929, 0.927, 0.9265, 0.926, 0.9245, 0.924, 0.9235, 0.923, 0.9225, 0.9225, 0.922, 0.9215, 0.9195, 0.918, 0.915, 0.9125, 0.911, 0.907, 0.9055, 0.9045, 0.9025, 0.9005, 0.899, 0.898, 0.8965, 0.8955, 0.893, 0.892, 0.89, 0.8875, 0.885, 0.8835, 0.882, 0.8805, 0.8785, 0.8755, 0.8735, 0.869, 0.868, 0.868, 0.8645, 0.8625, 0.8605, 0.8575, 0.8545, 0.8505, 0.847, 0.8455, 0.8435, 0.8415, 0.84, 0.8385, 0.8365, 0.8345, 0.829, 0.826, 0.824, 0.822, 0.817, 0.8145, 0.814, 0.811, 0.8095, 0.8075, 0.805, 0.8005, 0.7985, 0.7965, 0.793, 0.7895, 0.7865, 0.785, 0.7815, 0.779, 0.776, 0.769, 0.7655, 0.764, 0.7625, 0.7595, 0.7575, 0.754, 0.751, 0.7485, 0.743, 0.7395, 0.736, 0.7355, 0.732, 0.728, 0.725, 0.7235, 0.7215, 0.718, 0.7165, 0.7135, 0.7095, 0.709, 0.705, 0.7015, 0.699, 0.697, 0.694, 0.69, 0.685, 0.6835, 0.6805, 0.6795, 0.6765, 0.6745, 0.6735, 0.6705, 0.6665, 0.6625, 0.66, 0.656, 0.653, 0.65, 0.6465, 0.641, 0.639, 0.6365, 0.6335, 0.632, 0.6285, 0.6265, 0.623, 0.6195, 0.617, 0.6125, 0.609, 0.607, 0.6045, 0.601, 0.596, 0.591, 0.5905, 0.5885, 0.5825, 0.579, 0.576, 0.574, 0.5705, 0.569, 0.563, 0.559, 0.5565, 0.555, 0.5515, 0.546, 0.5455, 0.5435, 0.5395, 0.538, 0.535, 0.5315, 0.528, 0.525, 0.523, 0.519, 0.516, 0.513, 0.511, 0.509, 0.5065, 0.5045, 0.5015, 0.497, 0.4925, 0.488, 0.485, 0.4795, 0.473, 0.47, 0.465, 0.4595, 0.457, 0.454, 0.45, 0.447, 0.444, 0.4405, 0.4385, 0.4345, 0.431, 0.4275, 0.4245, 0.422, 0.421, 0.418, 0.414, 0.411, 0.4075, 0.404, 0.4015, 0.398, 0.3945, 0.3915, 0.39, 0.3875, 0.385, 0.3825, 0.379, 0.3765, 0.3725, 0.3685, 0.3655, 0.364, 0.359, 0.3555, 0.3555, 0.354, 0.351, 0.3495, 0.347, 0.345, 0.343, 0.341, 0.3365, 0.3325, 0.3325, 0.3275, 0.3255, 0.3225, 0.3205, 0.3195, 0.3175, 0.316, 0.313, 0.3125, 0.31, 0.308, 0.3055, 0.302, 0.301, 0.299, 0.297, 0.294, 0.29, 0.2895, 0.2855, 0.283, 0.2825, 0.279, 0.276, 0.273, 0.2695, 0.2665, 0.2655, 0.2635, 0.261, 0.2595, 0.258, 0.257, 0.254, 0.2535, 0.251, 0.25, 0.2465, 0.245, 0.2435, 0.2415, 0.2405, 0.238, 0.2365, 0.235, 0.2335, 0.2325, 0.23, 0.2275, 0.226, 0.2245, 0.2235, 0.223, 0.22, 0.218, 0.2165, 0.2135, 0.2135, 0.2095, 0.2075, 0.206, 0.205, 0.2045, 0.2025, 0.201, 0.2, 0.1985, 0.1985, 0.196, 0.1945, 0.1915, 0.1895, 0.188, 0.185, 0.184, 0.1835, 0.1835, 0.183, 0.183, 0.182, 0.182, 0.1805, 0.179, 0.1785, 0.177, 0.176, 0.1745, 0.174, 0.1735, 0.173, 0.1715, 0.171, 0.1685, 0.167, 0.1665, 0.165, 0.1645, 0.163, 0.162, 0.162, 0.1605, 0.1605, 0.16, 0.159, 0.158, 0.1555, 0.155, 0.1545, 0.1545, 0.152, 0.1505, 0.15, 0.149, 0.1475, 0.1465, 0.1445, 0.1435, 0.142, 0.1405, 0.14, 0.14, 0.139, 0.139, 0.1375, 0.137, 0.136, 0.1355, 0.135, 0.134, 0.1335, 0.133, 0.133, 0.1325, 0.1315, 0.1305, 0.13, 0.1295, 0.1295, 0.1275, 0.1265, 0.126, 0.126, 0.125, 0.124, 0.124, 0.1235, 0.1225, 0.1225, 0.1225, 0.122, 0.121, 0.1205, 0.1205, 0.1195, 0.1185, 0.117, 0.1145, 0.1135, 0.113, 0.113, 0.113, 0.112, 0.1105, 0.11, 0.109, 0.1085, 0.1055, 0.1055, 0.105, 0.105, 0.105, 0.105, 0.1035, 0.103, 0.1025, 0.102, 0.101, 0.1005, 0.0995, 0.099, 0.0975, 0.0965, 0.096, 0.096, 0.095, 0.095, 0.095, 0.0945, 0.0935, 0.0935, 0.0925, 0.091, 0.091, 0.0905, 0.09, 0.09, 0.09, 0.09, 0.0895, 0.0895, 0.0895, 0.088, 0.087, 0.0865, 0.0865, 0.086, 0.085, 0.085, 0.0845, 0.0845, 0.084, 0.084, 0.084, 0.084, 0.0835, 0.0825, 0.0825, 0.082, 0.082, 0.0815, 0.081, 0.081, 0.0805, 0.0795, 0.0795, 0.0795, 0.0795, 0.0795, 0.079, 0.0785, 0.078, 0.0775, 0.077, 0.076, 0.076, 0.076, 0.076, 0.076, 0.0755, 0.0755, 0.0755, 0.0755, 0.0755, 0.075, 0.075, 0.075, 0.075, 0.074, 0.074, 0.074, 0.0735, 0.0735, 0.0735, 0.0735, 0.073, 0.072, 0.072, 0.072, 0.072, 0.0715, 0.0715, 0.0715, 0.0705, 0.0705, 0.0695, 0.0695, 0.0695, 0.0695, 0.0695, 0.0695, 0.069, 0.069, 0.0685, 0.0685, 0.0685, 0.0685, 0.0685, 0.0685, 0.068, 0.0675, 0.0665, 0.0665, 0.0665, 0.0665, 0.0665, 0.066, 0.066, 0.066, 0.0655, 0.065, 0.065, 0.065, 0.0645, 0.0645, 0.0645, 0.0645, 0.064, 0.064, 0.0635, 0.0635, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.0615, 0.0615, 0.0615, 0.0615, 0.0615, 0.0615, 0.061, 0.0605, 0.0605, 0.0605, 0.06, 0.0595, 0.0595, 0.0595, 0.059, 0.059, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.058, 0.058, 0.058, 0.058, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.056, 0.056, 0.056, 0.056, 0.056, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.055, 0.055, 0.0545, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.051, 0.051, 0.051, 0.051, 0.051, 0.051, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05 , I 0.01, 0.0105, 0.0105, 0.011, 0.0115, 0.0115, 0.0115, 0.0115, 0.013, 0.0135, 0.014, 0.014, 0.014, 0.015, 0.015, 0.015, 0.0155, 0.0155, 0.0155, 0.016, 0.016, 0.0175, 0.0175, 0.0175, 0.017, 0.0175, 0.018, 0.0185, 0.019, 0.019, 0.019, 0.019, 0.0195, 0.0195, 0.02, 0.02, 0.0195, 0.0205, 0.0205, 0.0205, 0.021, 0.022, 0.022, 0.023, 0.024, 0.024, 0.0245, 0.0255, 0.0255, 0.0255, 0.0265, 0.0265, 0.028, 0.0295, 0.03, 0.0305, 0.031, 0.0315, 0.032, 0.033, 0.033, 0.033, 0.0335, 0.036, 0.037, 0.0375, 0.0385, 0.039, 0.039, 0.039, 0.0395, 0.0395, 0.0415, 0.042, 0.0415, 0.0435, 0.0445, 0.0455, 0.0455, 0.046, 0.047, 0.0475, 0.0475, 0.0485, 0.0485, 0.051, 0.052, 0.053, 0.053, 0.054, 0.0545, 0.0555, 0.057, 0.057, 0.0575, 0.06, 0.0605, 0.0625, 0.0635, 0.0645, 0.065, 0.0655, 0.067, 0.0675, 0.068, 0.0685, 0.069, 0.069, 0.0695, 0.07, 0.072, 0.0735, 0.076, 0.0785, 0.0795, 0.0835, 0.0845, 0.0855, 0.0875, 0.0895, 0.091, 0.0915, 0.093, 0.0935, 0.096, 0.097, 0.0985, 0.101, 0.1035, 0.105, 0.1065, 0.108, 0.1095, 0.1125, 0.1135, 0.1175, 0.1185, 0.1185, 0.122, 0.124, 0.126, 0.1285, 0.1315, 0.1355, 0.1385, 0.14, 0.1415, 0.1435, 0.1445, 0.144, 0.146, 0.148, 0.1535, 0.1565, 0.1585, 0.1605, 0.1655, 0.1675, 0.168, 0.171, 0.172, 0.174, 0.176, 0.1795, 0.181, 0.1815, 0.1845, 0.188, 0.191, 0.192, 0.1955, 0.1975, 0.199, 0.206, 0.209, 0.21, 0.2105, 0.213, 0.215, 0.218, 0.2205, 0.2225, 0.2275, 0.2305, 0.2335, 0.2335, 0.2365, 0.24, 0.242, 0.242, 0.244, 0.2465, 0.2475, 0.2505, 0.2535, 0.2525, 0.256, 0.259, 0.261, 0.2625, 0.2645, 0.268, 0.273, 0.273, 0.2745, 0.2755, 0.278, 0.28, 0.281, 0.2835, 0.2865, 0.2905, 0.292, 0.295, 0.2975, 0.3, 0.303, 0.308, 0.3095, 0.3115, 0.313, 0.3145, 0.318, 0.319, 0.3225, 0.326, 0.328, 0.332, 0.333, 0.334, 0.3355, 0.3385, 0.3435, 0.348, 0.3475, 0.347, 0.353, 0.356, 0.3585, 0.36, 0.3635, 0.365, 0.371, 0.3745, 0.376, 0.3765, 0.379, 0.3835, 0.384, 0.386, 0.3875, 0.3875, 0.3895, 0.3915, 0.395, 0.397, 0.3975, 0.3995, 0.402, 0.4035, 0.4055, 0.4075, 0.409, 0.41, 0.4115, 0.416, 0.42, 0.424, 0.4255, 0.4305, 0.4355, 0.4375, 0.441, 0.445, 0.4465, 0.4475, 0.4505, 0.4535, 0.4565, 0.4595, 0.4605, 0.4635, 0.4665, 0.4695, 0.472, 0.474, 0.4745, 0.4775, 0.48, 0.483, 0.4835, 0.4865, 0.489, 0.491, 0.4935, 0.495, 0.4945, 0.4965, 0.4985, 0.5, 0.502, 0.5045, 0.508, 0.51, 0.5125, 0.514, 0.518, 0.52, 0.5185, 0.519, 0.5215, 0.522, 0.5225, 0.5235, 0.5245, 0.525, 0.529, 0.5315, 0.5315, 0.5345, 0.5355, 0.5375, 0.5385, 0.538, 0.54, 0.541, 0.5435, 0.5425, 0.5445, 0.545, 0.547, 0.549, 0.549, 0.55, 0.551, 0.553, 0.5565, 0.557, 0.56, 0.562, 0.5615, 0.564, 0.5655, 0.5655, 0.5665, 0.569, 0.567, 0.5665, 0.569, 0.568, 0.568, 0.5675, 0.5695, 0.569, 0.5705, 0.5695, 0.572, 0.5725, 0.574, 0.575, 0.5755, 0.5775, 0.5775, 0.5785, 0.5785, 0.579, 0.5805, 0.582, 0.583, 0.583, 0.583, 0.5815, 0.5825, 0.584, 0.585, 0.5855, 0.584, 0.5865, 0.587, 0.5875, 0.5875, 0.5865, 0.587, 0.587, 0.5875, 0.588, 0.5865, 0.588, 0.5885, 0.5905, 0.5915, 0.5925, 0.5945, 0.5935, 0.592, 0.5905, 0.5905, 0.5905, 0.5905, 0.59, 0.59, 0.5905, 0.589, 0.589, 0.5885, 0.5875, 0.5875, 0.585, 0.584, 0.584, 0.583, 0.5835, 0.583, 0.582, 0.5825, 0.582, 0.5825, 0.582, 0.581, 0.582, 0.581, 0.579, 0.5785, 0.5775, 0.5795, 0.58, 0.579, 0.576, 0.578, 0.579, 0.579, 0.578, 0.5795, 0.5795, 0.5805, 0.58, 0.5805, 0.5815, 0.5805, 0.5795, 0.5795, 0.5775, 0.5765, 0.576, 0.5765, 0.576, 0.5755, 0.5755, 0.5755, 0.575, 0.5745, 0.5745, 0.574, 0.573, 0.573, 0.5715, 0.568, 0.5695, 0.5705, 0.571, 0.5705, 0.5695, 0.5695, 0.569, 0.569, 0.569, 0.5685, 0.566, 0.565, 0.5655, 0.565, 0.564, 0.564, 0.563, 0.563, 0.565, 0.564, 0.564, 0.563, 0.563, 0.5635, 0.564, 0.563, 0.5635, 0.563, 0.5655, 0.564, 0.5635, 0.562, 0.562, 0.5605, 0.5605, 0.5595, 0.559, 0.5595, 0.5595, 0.559, 0.558, 0.557, 0.5575, 0.5575, 0.556, 0.5555, 0.5545, 0.5525, 0.5495, 0.549, 0.549, 0.5475, 0.5475, 0.548, 0.548, 0.548, 0.547, 0.5445, 0.5425, 0.5425, 0.5425, 0.5415, 0.541, 0.5415, 0.541, 0.5405, 0.5395, 0.5395, 0.54, 0.539, 0.539, 0.5375, 0.5365, 0.5355, 0.535, 0.5345, 0.534, 0.5345, 0.534, 0.533, 0.5325, 0.5315, 0.532, 0.5315, 0.5315, 0.5315, 0.531, 0.53, 0.5295, 0.528, 0.526, 0.5255, 0.5235, 0.524, 0.523, 0.5215, 0.521, 0.521, 0.5205, 0.5205, 0.5205, 0.52, 0.519, 0.5185, 0.517, 0.5165, 0.5145, 0.5135, 0.513, 0.513, 0.513, 0.513, 0.5135, 0.5125, 0.5125, 0.511, 0.5115, 0.5125, 0.5115, 0.511, 0.511, 0.51, 0.5095, 0.509, 0.509, 0.5065, 0.5065, 0.506, 0.5045, 0.504, 0.502, 0.4995, 0.4995, 0.4995, 0.4995, 0.4985, 0.4965, 0.493, 0.492, 0.492, 0.491, 0.49, 0.4905, 0.4905, 0.4905, 0.4905, 0.4895, 0.489, 0.488, 0.4875, 0.488, 0.486, 0.4855, 0.4855, 0.485, 0.484, 0.483, 0.4825, 0.483, 0.482, 0.4815, 0.481, 0.4815, 0.4805, 0.4785, 0.4775, 0.477, 0.475, 0.4735, 0.473, 0.4725, 0.471, 0.4705, 0.47, 0.469, 0.4675, 0.4675, 0.466, 0.466, 0.4645, 0.4635, 0.4625, 0.4615, 0.461, 0.46, 0.4595, 0.458, 0.458, 0.457, 0.457, 0.4565, 0.4545, 0.454, 0.453, 0.452, 0.451, 0.4495, 0.449, 0.448, 0.4465, 0.4465, 0.4455, 0.4455, 0.4455, 0.4455, 0.4455, 0.445, 0.444, 0.444, 0.4425, 0.4415, 0.44, 0.4385, 0.438, 0.437, 0.436, 0.4355, 0.435, 0.4345, 0.432, 0.4295, 0.4295, 0.428, 0.427, 0.426, 0.4255, 0.4245, 0.4215, 0.4215, 0.4205, 0.4205, 0.42, 0.4195, 0.4195, 0.4185, 0.4165, 0.4155, 0.4135, 0.412, 0.4125, 0.4105, 0.41, 0.409, 0.4075, 0.407, 0.407, 0.4065, 0.405, 0.4045, 0.404, 0.4035, 0.404, 0.4015, 0.4, 0.3995, 0.398, 0.398, 0.397, 0.397, 0.397, 0.396, 0.396, 0.395, 0.3945, 0.395, 0.394, 0.393, 0.393, 0.393, 0.392, 0.391, 0.3905, 0.39, 0.389, 0.3885, 0.388, 0.387, 0.386, 0.385, 0.384, 0.384, 0.3835, 0.3825, 0.3825, 0.3825, 0.3815, 0.3815, 0.38, 0.38, 0.3795, 0.379, 0.379, 0.378, 0.377, 0.3765, 0.376, 0.3755, 0.3735, 0.3715, 0.371, 0.3715, 0.371, 0.3705, 0.37, 0.37, 0.369, 0.369, 0.3675, 0.367, 0.367, 0.3655, 0.364, 0.3625, 0.3615, 0.361, 0.3605, 0.3605, 0.36, 0.3595, 0.359, 0.3575, 0.3575, 0.357, 0.3565, 0.355, 0.355, 0.354, 0.3535, 0.353, 0.352, 0.352, 0.3515, 0.351, 0.3495, 0.3495, 0.349, 0.348, 0.3475, 0.345, 0.344, 0.3435, 0.3425, 0.3425, 0.341, 0.3395, 0.3395, 0.339, 0.3375, 0.337, 0.336, 0.336, 0.3345, 0.3345, 0.334, 0.3335, 0.3335, 0.3325, 0.3325, 0.3315, 0.3295, 0.3285, 0.328, 0.328, 0.326, 0.326, 0.326, 0.326, 0.326, 0.3255, 0.325, 0.3235, 0.3225, 0.3215, 0.3215, 0.3215, 0.321, 0.3195, 0.318, 0.3175, 0.317, 0.3165, 0.316, 0.3155, 0.3155, 0.315, 0.3145, 0.3135, 0.3135, 0.313, 0.3105, 0.31, 0.31, 0.308, 0.3075, 0.3075, 0.3075, 0.307, 0.305, 0.3045, 0.3035, 0.3035, 0.3015, 0.3015, 0.3015, 0.3015, 0.301, 0.3, 0.2995, 0.2995, 0.299, 0.299, 0.298, 0.298, 0.297, 0.2965, 0.2945, 0.294, 0.293, 0.293, 0.2915, 0.2915, 0.291, 0.2905, 0.29, 0.289, 0.289, 0.288, 0.288, 0.288, 0.288, 0.288, 0.2865, 0.2855, 0.2855, 0.2845, 0.2835, 0.283, 0.282, 0.2815, 0.281, 0.281, 0.2805, 0.281, 0.2805, 0.2795, 0.2785, 0.278, 0.278, 0.2785, 0.2785, 0.277, 0.2765, 0.275, 0.2745, 0.2745, 0.2745, 0.274, 0.273, 0.273, 0.273, 0.2715, 0.27, 0.2695, 0.2695, 0.269, 0.2685, 0.268, 0.267, 0.266, 0.266, 0.265, 0.2645, 0.2645, 0.2635, 0.263, 0.2625, 0.2625, 0.262, 0.262, 0.2605, 0.26, 0.259, 0.2575, 0.2565, 0.2555, 0.255, 0.254, 0.253, 0.2525, 0.251, 0.2505, 0.2505, 0.2505, 0.25, 0.2495, 0.2495, 0.2495, 0.249, 0.249, 0.249, 0.2485, 0.2475, 0.246, 0.246, 0.245, 0.245, 0.2445, 0.2445, 0.244, 0.244, 0.2435, 0.2425, 0.242, 0.242, 0.2415, 0.2405, 0.239, 0.238, 0.2375, 0.2365, 0.2365, 0.2365, 0.236, 0.236, 0.2365, 0.2365, 0.2365, 0.2365, 0.236, 0.235, 0.2345, 0.234, 0.234, 0.2335, 0.2335, 0.233, 0.233, 0.2325, 0.232, 0.2315, 0.231, 0.2295, 0.2285 , R 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.0015, 0.0015, 0.002, 0.002, 0.002, 0.002, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0035, 0.0035, 0.0035, 0.0035, 0.0035, 0.0035, 0.004, 0.0045, 0.0045, 0.0045, 0.005, 0.0055, 0.0055, 0.0055, 0.006, 0.006, 0.006, 0.006, 0.006, 0.006, 0.0065, 0.0065, 0.0065, 0.0065, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.009, 0.009, 0.0095, 0.0095, 0.01, 0.01, 0.01, 0.01, 0.01, 0.0105, 0.0105, 0.011, 0.011, 0.011, 0.0115, 0.0115, 0.0115, 0.0115, 0.0115, 0.0115, 0.012, 0.012, 0.013, 0.0135, 0.0135, 0.0135, 0.0135, 0.0135, 0.0135, 0.014, 0.014, 0.014, 0.0145, 0.0145, 0.015, 0.015, 0.0155, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.018, 0.018, 0.018, 0.0185, 0.0185, 0.019, 0.02, 0.0205, 0.022, 0.0225, 0.0225, 0.0225, 0.023, 0.023, 0.0235, 0.025, 0.025, 0.0255, 0.026, 0.027, 0.0275, 0.0275, 0.028, 0.0285, 0.029, 0.0295, 0.03, 0.0305, 0.031, 0.0315, 0.032, 0.033, 0.0345, 0.0345, 0.0355, 0.036, 0.036, 0.037, 0.0385, 0.039, 0.0395, 0.04, 0.0405, 0.0415, 0.042, 0.042, 0.0435, 0.045, 0.045, 0.0455, 0.0455, 0.0455, 0.046, 0.047, 0.047, 0.048, 0.049, 0.0495, 0.05, 0.0505, 0.051, 0.0515, 0.052, 0.0535, 0.0535, 0.0535, 0.0545, 0.0545, 0.0545, 0.055, 0.0555, 0.058, 0.059, 0.06, 0.0605, 0.0605, 0.061, 0.062, 0.0645, 0.0645, 0.065, 0.0655, 0.066, 0.066, 0.066, 0.066, 0.0665, 0.0675, 0.0685, 0.0695, 0.0705, 0.0705, 0.0705, 0.073, 0.0745, 0.0755, 0.077, 0.077, 0.078, 0.0795, 0.0815, 0.082, 0.0835, 0.0835, 0.0835, 0.0845, 0.0855, 0.087, 0.087, 0.0875, 0.088, 0.0895, 0.09, 0.0915, 0.0925, 0.094, 0.0955, 0.0965, 0.0985, 0.0995, 0.0995, 0.0995, 0.1, 0.101, 0.102, 0.1025, 0.103, 0.1035, 0.104, 0.1045, 0.1045, 0.106, 0.106, 0.109, 0.1095, 0.1095, 0.111, 0.112, 0.1135, 0.1155, 0.116, 0.1165, 0.1175, 0.119, 0.119, 0.1195, 0.1215, 0.122, 0.122, 0.123, 0.1245, 0.126, 0.127, 0.1275, 0.1285, 0.1305, 0.1315, 0.1325, 0.134, 0.1345, 0.136, 0.136, 0.138, 0.139, 0.14, 0.141, 0.1425, 0.1425, 0.143, 0.1435, 0.145, 0.1455, 0.147, 0.1475, 0.149, 0.15, 0.151, 0.152, 0.153, 0.1535, 0.1535, 0.1545, 0.155, 0.156, 0.157, 0.1585, 0.1615, 0.164, 0.1645, 0.1675, 0.17, 0.17, 0.1725, 0.174, 0.1755, 0.1765, 0.1775, 0.1785, 0.1805, 0.1815, 0.1825, 0.1825, 0.1835, 0.184, 0.1845, 0.186, 0.1865, 0.188, 0.1885, 0.1895, 0.1905, 0.191, 0.1925, 0.1935, 0.1955, 0.1975, 0.198, 0.1985, 0.201, 0.2025, 0.204, 0.2055, 0.2065, 0.2075, 0.209, 0.2105, 0.212, 0.2125, 0.2135, 0.215, 0.216, 0.217, 0.218, 0.219, 0.2195, 0.2205, 0.2225, 0.2245, 0.226, 0.2265, 0.2265, 0.2275, 0.228, 0.2295, 0.2305, 0.2325, 0.234, 0.2355, 0.238, 0.2385, 0.2415, 0.243, 0.2445, 0.246, 0.248, 0.25, 0.2515, 0.2525, 0.2535, 0.2545, 0.256, 0.257, 0.2575, 0.2585, 0.261, 0.2625, 0.2645, 0.265, 0.265, 0.2665, 0.2695, 0.27, 0.2705, 0.271, 0.273, 0.273, 0.274, 0.275, 0.2765, 0.2775, 0.278, 0.2795, 0.2805, 0.2815, 0.2835, 0.286, 0.287, 0.2875, 0.2885, 0.2895, 0.2905, 0.291, 0.292, 0.2925, 0.293, 0.2945, 0.2965, 0.297, 0.299, 0.3025, 0.303, 0.303, 0.303, 0.3035, 0.3055, 0.3065, 0.307, 0.3075, 0.3085, 0.309, 0.3115, 0.313, 0.3135, 0.3145, 0.3155, 0.3165, 0.3185, 0.32, 0.3205, 0.3225, 0.323, 0.324, 0.324, 0.3245, 0.3255, 0.327, 0.3275, 0.3285, 0.329, 0.3305, 0.3315, 0.333, 0.333, 0.3345, 0.336, 0.3375, 0.3385, 0.3385, 0.3395, 0.3405, 0.3425, 0.344, 0.345, 0.346, 0.348, 0.3485, 0.3505, 0.3525, 0.3555, 0.3565, 0.3575, 0.359, 0.36, 0.361, 0.361, 0.3615, 0.363, 0.3655, 0.3675, 0.3675, 0.368, 0.369, 0.3695, 0.3705, 0.372, 0.373, 0.374, 0.3745, 0.375, 0.376, 0.3765, 0.378, 0.3795, 0.3805, 0.381, 0.3815, 0.3825, 0.383, 0.3835, 0.385, 0.3855, 0.387, 0.387, 0.3875, 0.388, 0.389, 0.3895, 0.3905, 0.391, 0.3925, 0.395, 0.396, 0.3985, 0.3985, 0.4, 0.4025, 0.403, 0.403, 0.4035, 0.4035, 0.404, 0.4045, 0.4055, 0.406, 0.4075, 0.4085, 0.4105, 0.4115, 0.412, 0.413, 0.413, 0.413, 0.413, 0.414, 0.414, 0.4155, 0.4155, 0.4155, 0.4165, 0.417, 0.417, 0.4185, 0.419, 0.4195, 0.4205, 0.423, 0.424, 0.4245, 0.426, 0.4265, 0.4285, 0.431, 0.4315, 0.4315, 0.432, 0.433, 0.435, 0.4385, 0.4395, 0.4395, 0.441, 0.4425, 0.443, 0.443, 0.443, 0.443, 0.444, 0.445, 0.446, 0.4465, 0.4465, 0.449, 0.4495, 0.4495, 0.4505, 0.4515, 0.4525, 0.453, 0.453, 0.454, 0.455, 0.4555, 0.456, 0.457, 0.459, 0.46, 0.4605, 0.4625, 0.464, 0.4645, 0.465, 0.4665, 0.467, 0.4675, 0.4685, 0.47, 0.47, 0.4715, 0.472, 0.4735, 0.4745, 0.4755, 0.4765, 0.477, 0.478, 0.4785, 0.48, 0.48, 0.481, 0.4815, 0.482, 0.484, 0.4845, 0.4855, 0.4865, 0.488, 0.49, 0.4905, 0.4915, 0.4935, 0.494, 0.495, 0.495, 0.4955, 0.4955, 0.496, 0.4965, 0.4975, 0.4975, 0.499, 0.5, 0.5015, 0.503, 0.5035, 0.5045, 0.5055, 0.506, 0.5065, 0.5075, 0.51, 0.5125, 0.5125, 0.5145, 0.5155, 0.5165, 0.517, 0.518, 0.521, 0.521, 0.522, 0.522, 0.5225, 0.523, 0.523, 0.524, 0.526, 0.527, 0.529, 0.5305, 0.5305, 0.5325, 0.533, 0.534, 0.5355, 0.536, 0.536, 0.5365, 0.538, 0.5385, 0.539, 0.5395, 0.5395, 0.542, 0.5435, 0.544, 0.5455, 0.5455, 0.5465, 0.5465, 0.5465, 0.5475, 0.5475, 0.5485, 0.549, 0.549, 0.55, 0.551, 0.551, 0.551, 0.5525, 0.5535, 0.554, 0.5545, 0.5555, 0.556, 0.5565, 0.5575, 0.5585, 0.5595, 0.5605, 0.561, 0.5615, 0.563, 0.5635, 0.5635, 0.5645, 0.5645, 0.566, 0.566, 0.5665, 0.567, 0.567, 0.568, 0.569, 0.5695, 0.57, 0.5705, 0.5725, 0.5745, 0.575, 0.5755, 0.576, 0.5765, 0.577, 0.577, 0.578, 0.578, 0.5795, 0.58, 0.58, 0.5815, 0.583, 0.5845, 0.5855, 0.586, 0.5865, 0.5865, 0.587, 0.5875, 0.588, 0.5895, 0.5895, 0.59, 0.5905, 0.592, 0.592, 0.593, 0.5935, 0.594, 0.595, 0.595, 0.596, 0.5965, 0.598, 0.598, 0.5985, 0.5995, 0.6, 0.6025, 0.6035, 0.604, 0.605, 0.605, 0.6065, 0.608, 0.608, 0.6085, 0.61, 0.6105, 0.6115, 0.6115, 0.613, 0.613, 0.6135, 0.614, 0.614, 0.615, 0.615, 0.616, 0.618, 0.619, 0.6195, 0.6195, 0.6215, 0.6215, 0.6215, 0.6215, 0.6215, 0.622, 0.6225, 0.624, 0.625, 0.626, 0.6265, 0.6265, 0.627, 0.6285, 0.63, 0.6305, 0.631, 0.6315, 0.632, 0.6325, 0.6325, 0.633, 0.6335, 0.6345, 0.6345, 0.635, 0.6375, 0.638, 0.638, 0.64, 0.6405, 0.6405, 0.6405, 0.641, 0.643, 0.6435, 0.6445, 0.6445, 0.6465, 0.6465, 0.6465, 0.6465, 0.647, 0.648, 0.6485, 0.6485, 0.649, 0.649, 0.65, 0.65, 0.651, 0.6515, 0.6535, 0.654, 0.655, 0.655, 0.6565, 0.6565, 0.657, 0.6575, 0.658, 0.659, 0.659, 0.6605, 0.6605, 0.6605, 0.6605, 0.6605, 0.662, 0.663, 0.663, 0.664, 0.665, 0.6655, 0.6665, 0.667, 0.6675, 0.6675, 0.668, 0.668, 0.6685, 0.6695, 0.6705, 0.671, 0.671, 0.671, 0.671, 0.6725, 0.673, 0.6745, 0.675, 0.675, 0.675, 0.6755, 0.6765, 0.6765, 0.6765, 0.678, 0.6795, 0.68, 0.68, 0.6805, 0.681, 0.6815, 0.6825, 0.6835, 0.6835, 0.6845, 0.685, 0.685, 0.686, 0.6865, 0.687, 0.687, 0.6875, 0.6875, 0.689, 0.6895, 0.6905, 0.692, 0.693, 0.694, 0.6945, 0.6955, 0.6965, 0.697, 0.6985, 0.699, 0.699, 0.699, 0.6995, 0.7, 0.7, 0.7, 0.7005, 0.7005, 0.7005, 0.701, 0.702, 0.7035, 0.7035, 0.7045, 0.7045, 0.705, 0.705, 0.7055, 0.7055, 0.706, 0.707, 0.7075, 0.7075, 0.708, 0.709, 0.7105, 0.7115, 0.712, 0.713, 0.713, 0.713, 0.7135, 0.7135, 0.7135, 0.7135, 0.7135, 0.7135, 0.714, 0.715, 0.7155, 0.716, 0.716, 0.7165, 0.7165, 0.717, 0.717, 0.7175, 0.718, 0.7185, 0.719, 0.7205, 0.7215 spatial results collect. zip hw4 results transposed... spatial T 1 length spatial results md\"\"\" 👉 Not graded Manually fit the SIR curves to our data by adjusting ``\\beta`` and ``\\gamma`` until you find a good fit. This will use the `euler SIR` function from Exercise 2. \"\"\" md\"β bind guess β Slider 0.00 0.0001 0.1 default 0.05, show value true \" md\"γ bind guess γ Slider 0.00 0.0001 0.01 default 0.005, show value true \" md\"Show manual fit bind show manual sir fit CheckBox \" md\"\"\" 👉 To do this automatically, we will again need to define a loss function \\mathcal L \\beta, \\gamma . This will compare the solution of the SIR equations with parameters \\beta and \\gamma with your data. This time, instead of comparing two vectors of numbers, we need to compare two vectors of vectors , the S, I, R values. \"\"\" function loss sir β, γ return missing end loss sir guess β, guess γ md\"\"\" 👉 Use this loss function to find the optimal parameters ``\\beta`` and ``\\gamma``. \"\"\" found β, found γ let your code here missing, missing end let p plot plot sir p, spatial T, spatial results, label \"spatial\", opacity .7 if show manual sir fit guess results euler SIR guess β, guess γ, 0.99, 0.01, 0.00 , spatial T plot sir p, spatial T, guess results, label \"manual\", linestyle dash, lw 2 end try assert found β isa Missing && found γ isa Missing found results euler SIR found β, found γ, 0.99, 0.01, 0.00 , spatial T plot sir p, spatial T, found results, label \"optimized\", linestyle dot, lw 2 catch end as svg p end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"\"\" Remember that functions are objects https www.youtube.com watch?v O HBDZMLrM For example, here is a function that returns the square root function ```julia function the square root function f x sqrt x return f end ``` \"\"\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let result euler integrate step x x^2, 10, 11, 12 if result isa Missing still missing elseif result isa Number keep working md\"Make sure that you return a number.\" else if result ≈ 6358 correct elseif result ≈ 1462 almost md\"Use ``f' a h ``, not ``f' a ``.\" else keep working end end end if euler test isa Missing still missing elseif euler test isa Vector || abs length euler test 101 1 keep working md\"Make sure that you return a vector of numbers, of the same size as `T`.\" else if abs euler test 1 0 1 keep working elseif abs euler test 50 5^3 20 keep working elseif abs euler test end 10^3 100 keep working else correct end end let result1 gradient descent 1d step x x^2, 10 η 1 result2 gradient descent 1d step x x^2, 10 η 2 if result1 isa Missing still missing elseif result1 isa Real keep working md\"You need to return a number.\" else if result2 result1 10.0 correct else keep working end end end let result gradient descent 1d 10 do x x 5pi ^ 2 10 end if result isa Missing still missing elseif result isa Real keep working md\"You need to return a number.\" else error abs result 5pi if error 5.0 almost md\"It's not accurate enough yet. Maybe you need to increase the number of steps?\" elseif error 0.02 keep working else correct end end end let if found β isa Missing || found γ isa Missing still missing else if isnan found β || isnan found γ hint md\"The found parameters are `NaN`, which means that floating point errors led to an invalid value. Try setting ``\\eta`` much lower, try `1e 6`, `1e 7`, etc.\" else diffb abs found β 0.019 diffc abs found γ 0.0026 if diffb .1 || diffc .01 almost md\"Try using initial values that are closer to the expected result. For example, the values that you found using the sliders. \" elseif diffb .01 || diffc .001 almost md\"Try using initial values that are closer to the expected result. For example, the values that you found using the sliders. You can also experiment with a different loss function. Are you using the absolute error, instead of the square of the error? A parabolic loss function is 'easier to optimize' using gradient descent than a cone shaped one.\" else correct md\"\"\" If you made it this far, congratulations you have just taken your first step into the exciting world of scientific machine learning \"\"\" end end end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined finite difference slope not defined finite difference slope else let result finite difference slope sqrt, 4.0, 5.0 if result isa Missing still missing elseif result isa Real keep working md\"Make sure that you return a number.\" else if result ≈ 0.2 correct else keep working end end end end if isdefined tangent line not defined tangent line else let result tangent line sqrt, 4.0, 5.0 if result isa Missing still missing elseif result isa Function keep working md\"Make sure that you return a function.\" else if finite difference slope result, 14.0, 15.0 ≈ 0.2 if result 4.0 ≈ 2.0 correct else almost md\"The tangent line should pass through a, f a .\" end else keep working end end end end if isdefined ∂x not defined ∂x else let result ∂x x, y 2x^2 3y^2, 6, 7 if result isa Missing still missing elseif result isa Number keep working md\"Make sure that you return a number.\" else if abs result 24 1.0 correct else keep working end end end end if isdefined ∂y not defined ∂y else let result ∂y x, y 2x^2 3y^2, 6, 7 if result isa Missing still missing elseif result isa Number keep working md\"Make sure that you return a number.\" else if abs result 42 1.0 correct else keep working end end end end if isdefined gradient not defined gradient else let result gradient x, y 2x^2 3y^2, 6, 7 if result isa Missing still missing elseif result isa Vector keep working md\"Make sure that you return a 2 element vector.\" else if abs result 1 24 1 && abs result 2 42 1 correct else keep working end end end end if isdefined found μ not defined found μ elseif isdefined found σ not defined found σ else let if found μ isa Missing || found σ isa Missing still missing else diff μ abs stats μ found μ diff σ abs stats σ found σ if diff μ 1 || diff σ 1 keep working elseif diff μ .2 || diff σ .2 almost md\"The fit is close, but we can do better. Try increasing ``\\eta`` \" else correct end end end end TODO html\" span style 'display inline font size 2em color purple font weight 900 ' TODO span \" "},{"url":"homework/hw9/","title":"Epidemic modeling III","tags":["homework","module3","epidemiology","track_julia","plotting","agent based model","statistics","random","modeling","monte carlo","track_climate"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 section 1.5 order 1.5 homework number 9 title \"Epidemic modeling III\" layout \"layout.jlhtml\" tags \"homework\", \"module3\", \"epidemiology\", \"track julia\", \"plotting\", \"agent based model\", \"statistics\", \"random\", \"modeling\", \"monte carlo\", \"track climate\" description \"In this problem set, we will look at a simple spatial agent based epidemic model agents can interact only with other agents that are nearby.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, LinearAlgebra md\" homework 9, version 3 \" hello there md\"\"\" Homework 9 Epidemic modeling III `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\" Let's create a package environment \" md\"\"\" In this problem set, we will look at a simple spatial agent based epidemic model agents can interact only with other agents that are nearby . In Homework 7, any agent could interact with any other, which is not realistic. A simple approach is to use discrete space each agent lives in one cell of a square grid. For simplicity we will allow no more than one agent in each cell, but this requires some care to design the rules of the model to respect this. We will adapt some functionality from Homework 7. You should copy and paste your code from that homework into this notebook. \"\"\" md\"\"\" Exercise 1 Wandering at random in 2D In this exercise we will implement a random walk on a 2D lattice grid . At each time step, a walker jumps to a neighbouring position at random i.e. chosen with uniform probability from the available adjacent positions . \"\"\" md\"\"\" Exercise 1.1 We define a struct type `Coordinate` that contains integers `x` and `y`. \"\"\" md\"\"\" 👉 Construct a `Coordinate` located at the origin. \"\"\" origin missing md\"\"\" 👉 Write a function `make tuple` that takes an object of type `Coordinate` and returns the corresponding tuple ` x, y `. Boring, but useful later \"\"\" function make tuple c missing end md\"\"\" Exercise 1.2 In Julia, operations like ` ` and ` ` are just functions, and they are treated like any other function in the language. The only special property you can use the infix notation you can write ```julia 1 2 ``` instead of ```julia 1, 2 ``` There are lots of special 'infixable' function names https github.com JuliaLang julia blob master src julia parser.scm L23 L24 that you can use for your own functions When you call it with the prefix notation, it becomes clear that it really is 'just another function', with lots of predefined methods. \"\"\" 1, 2 md\"\"\" Extending in the wild Because it is a function, we can add our own methods to it This feature is super useful in general languages like Julia and Python, because it lets you use familiar syntax `a b c` on objects that are not necessarily numbers One example we've see before is the `RGB` type in the first lectures. You are able to do ```julia 0.5 RGB 0.1, 0.7, 0.6 ``` to multiply each color channel by 0.5 . This is possible because `Images.jl` wrote a method https github.com JuliaGraphics ColorVectorSpace.jl blob 06d70b8a28f5c263f52e414745c2066ccb72b518 src ColorVectorSpace.jl L207 ```julia Real, AbstractRGB AbstractRGB ``` 👉 Implement addition on two `Coordinate` structs by adding a method to `Base. ` \"\"\" function Base. a TYPE, b TYPE return missing end Coordinate 3,4 Coordinate 10,10 uncomment to check works md\"\"\" Pluto has some trouble here, you need to manually re run the cell above \"\"\" md\"\"\" Exercise 1.3 In our model, agents will be able to walk in 4 directions up, down, left and right. We can define these directions as `Coordinate`s. \"\"\" uncomment this possible moves Coordinate 1, 0 , Coordinate 0, 1 , Coordinate 1, 0 , Coordinate 0, 1 , md\"\"\" 👉 `rand possible moves ` gives a random possible move. Add this to the coordinate `Coordinate 4,5 ` and see that it moves to a valid neighbor. \"\"\" md\"\"\" We are able to make a `Coordinate` perform one random step, by adding a move to it. Great 👉 Write a function `trajectory` that calculates a trajectory of a `Wanderer` `w` when performing `n` steps., i.e. the sequence of positions that the walker finds itself in. Possible steps Use `rand possible moves, n ` to generate a vector of `n` random moves. Each possible move will be equally likely. To compute the trajectory you can use either of the following two approaches 1. 🆒 Use the function `accumulate` see the live docs for `accumulate` . Use ` ` as the function passed to `accumulate` and the `w` as the starting value `init` keyword argument . 1. Use a `for` loop calling ` `. \"\"\" function trajectory w Coordinate, n Int return missing end test trajectory trajectory Coordinate 4,4 , 30 uncomment to test function plot trajectory p Plots.Plot, trajectory Vector kwargs... plot p, make tuple. trajectory label nothing, linewidth 2, linealpha LinRange 1.0, 0.2, length trajectory , kwargs... end try p plot ratio 1, size 650, 200 plot trajectory p, test trajectory color \"black\", showaxis false, axis nothing, linewidth 4 p catch end let long trajectory trajectory Coordinate 4,4 , 1000 p plot ratio 1 plot trajectory p, long trajectory p end ^ uncomment to visualize a trajectory md\"\"\" Exercise 1.4 👉 Plot 10 trajectories of length 1000 on a single figure, all starting at the origin. Use the function `plot trajectory ` as demonstrated above. Remember from last week that you can compose plots like this ```julia let Create a new plot with aspect ratio 1 1 p plot ratio 1 plot trajectory p, test trajectory plot one trajectory plot trajectory p, another trajectory plot the second one ... p end ``` \"\"\" md\"\"\" Exercise 1.5 Agents live in a box of side length 2L , centered at the origin. We need to decide i.e. model what happens when they reach the walls of the box boundaries , in other words what kind of boundary conditions to use. One relatively simple boundary condition is a collision boundary Each wall of the box is a wall, modelled using \"collision\" if the walker tries to jump beyond the wall, it ends up at the position inside the box that is closest to the goal. 👉 Write a function `collide boundary` which takes a `Coordinate` `c` and a size L , and returns a new coordinate that lies inside the box i.e. `` L,L \\times L,L `` , but is closest to `c`. This is similar to `extend mat` from Homework 1. \"\"\" function collide boundary c Coordinate, L Number return missing end collide boundary Coordinate 12,4 , 10 uncomment to test md\"\"\" Exercise 1.6 👉 Implement a 3 argument method of `trajectory` where the third argument is a size. The trajectory returned should be within the boundary use `collide boundary` from above . You can still use `accumulate` with an anonymous function that makes a move and then reflects the resulting coordinate, or use a for loop. \"\"\" function trajectory c Coordinate, n Int, L Number return missing end md\"\"\" Exercise 2 Wanderering Agents In this exercise we will create Agents which have a location as well as some infection state information. Let's define a type `Agent`. `Agent` contains a `position` of type `Coordinate` , as well as a `status` of type `InfectionStatus` as in Homework 4 . For simplicity we will not use a `num infected` field, but feel free to do so \"\"\" enum InfectionStatus S I R define agent struct here md\"\"\" Exercise 2.1 👉 Write a function `initialize` that takes parameters N and L , where N is the number of agents and 2L is the side length of the square box where the agents live. It returns a `Vector` of `N` randomly generated `Agent`s. Their coordinates are randomly sampled in the `` L,L \\times L,L `` box, and the agents are all susceptible, except one, chosen at random, which is infectious. \"\"\" function initialize N Number, L Number return missing end initialize 3, 10 Color based on infection status color s InfectionStatus if s S \"blue\" elseif s I \"red\" else \"green\" end position a Agent a.position uncomment this line color a Agent color a.status uncomment this line md\"\"\" Exercise 2.2 👉 Write a function `visualize` that takes in a collection of agents as argument, and the box size `L`. It should plot a point for each agent at its location, coloured according to its status. You can use the keyword argument `c color. agents ` inside your call to the plotting function make the point colors correspond to the infection statuses. Don't forget to use `ratio 1`. \"\"\" function visualize agents Vector, L return missing end let N 20 L 10 visualize initialize N, L , L uncomment this line end md\"\"\" Exercise 3 Spatial epidemic model Dynamics Last week we wrote a function `interact ` that takes two agents, `agent` and `source`, and an infection of type `InfectionRecovery`, which models the interaction between two agent, and possibly modifies `agent` with a new status. This week, we define a new infection type, `CollisionInfectionRecovery`, and a new method that is the same as last week, except it only infects `agent` if `agents.position source.position` . \"\"\" abstract type AbstractInfection end struct CollisionInfectionRecovery AbstractInfection p infection Float64 p recovery Float64 end md\"\"\" Write a function `interact ` that takes two `Agent`s and a `CollisionInfectionRecovery`, and If the agents are at the same spot, causes a susceptible agent to communicate the disease from an infectious one with the correct probability. if the first agent is infectious, it recovers with some probability \"\"\" function interact agent Agent, source Agent, infection CollisionInfectionRecovery missing end md\"\"\" Exercise 3.1 Your turn 👉 Write a function `step ` that takes a vector of `Agent`s, a box size `L` and an `infection`. This that does one step of the dynamics on a vector of agents. Choose an Agent `source` at random. Move the `source` one step, and use `collide boundary` to ensure that our agent stays within the box. For all other agents, call `interact other agent, source, infection `. return the array `agents` again. \"\"\" function step agents Vector, L Number, infection AbstractInfection return missing end md\"\"\" Exercise 3.2 If we call `step ` `N` times, then every agent will have made one step, on average. Let's call this one sweep of the simulation. 👉 Create a before and after plot of ``k sweeps 1000`` sweeps. Initialize a new vector of agents `N 50`, `L 40`, `infection` is given as `pandemic` below . Plot the state using `visualize`, and save the plot as a variable `plot before`. Run `k sweeps` sweeps. Plot the state again, and store as `plot after`. Combine the two plots into a single figure using ```julia plot plot before, plot after ``` \"\"\" pandemic CollisionInfectionRecovery 0.5, 0.00001 bind k sweeps Slider 1 10000, default 1000 let N 50 L 40 plot before plot 1 3 replace with your code plot after plot 1 3 plot plot before, plot after end md\"\"\" Exercise 3.3 Every time that you move the slider, a completely new simulation is created an run. This makes it hard to view the progress of a single simulation over time. So in this exercise, we we look at a single simulation, and plot the S, I and R curves. 👉 Plot the SIR curves of a single simulation, with the same parameters as in the previous exercise. Use `k sweep max 10000` as the total number of sweeps. \"\"\" k sweep max 10000 let N 50 L 30 agents initialize N, L compute k sweep max number of sweeps and plot the SIR end md\"\"\" Exercise 3.4 optional Let's make our plot come alive There are two options to make our visualization dynamic 👉1️⃣ Precompute one simulation run and save its intermediate states using `deepcopy`. You can then write an interactive visualization that shows both the state at time t using `visualize` and the history of S , I and R from time 0 up to time t . t is controlled by a slider. 👉2️⃣ Use ` gif` from Plots.jl to turn a sequence of plots into an animation. Be careful to skip about 50 sweeps between each animation frame, otherwise the GIF becomes too large. This an optional exercise, and our solution to 2️⃣ is given below. \"\"\" let N 50 L 30 missing end md\"\"\" Exercise 3.5 👉 Using L 20 and N 100 , experiment with the infection and recovery probabilities until you find an epidemic outbreak. Take the recovery probability quite small. Modify the two infections below to match your observations. \"\"\" causes outbreak CollisionInfectionRecovery 0.5, 0.001 does not cause outbreak CollisionInfectionRecovery 0.5, 0.001 md\"\"\" Exercise 3.6 👉 With the parameters of Exercise 3.2, run 50 simulations. Plot S , I and R as a function of time for each of them with transparency . This should look qualitatively similar to what you saw in the previous homework. You probably need different `p infection` and `p recovery` values from last week. Why? \"\"\" need different parameters because md\"\"\" i say so \"\"\" md\"\"\" Exercise 4 Optional Effect of socialization In this exercise we'll modify the simple mixing model. Instead of a constant mixing probability, i.e. a constant probability that any pair of people interact on a given day, we will have a variable probability associated with each agent, modelling the fact that some people are more or less social or contagious than others. \"\"\" md\"\"\" Exercise 4.1 We create a new agent type `SocialAgent` with fields `position`, `status`, `num infected`, and `social score`. The attribute `social score` represents an agent's probability of interacting with any other agent in the population. \"\"\" struct SocialAgent here... md\"\"\"define the `position` and `color` methods for `SocialAgent` as we did for `Agent`. This will allow the `visualize` function to work. on both kinds of Agents\"\"\" begin position a SocialAgent ... color a SocialAgent ... end md\"\"\" 👉 Create a function `initialize social` that takes `N` and `L`, and creates N agents within a 2L x 2L box, with `social score`s chosen from 10 equally spaced between 0.1 and 0.5. see LinRange \"\"\" function initialize social N, L return missing end md\"\"\" Now that we have 2 agent types 1. let's create an AbstractAgent type 2. Go back in the notebook and make the agent types a subtype of AbstractAgent. \"\"\" md\"\"\" Exercise 4.2 Not all two agents who end up in the same grid point may actually interact in an infectious way they may just be passing by and do not create enough exposure for communicating the disease. 👉 Write a new `interact ` method on `SocialAgent` which adds together the social scores for two agents and uses that as the probability that they interact in a risky way. Only if they interact in a risky way, the infection is transmitted with the usual probability. \"\"\" function interact agent SocialAgent, source SocialAgent, infection CollisionInfectionRecovery your code here end md\"\"\" Make sure `step `, `position`, `color`, work on the type `SocialAgent`. If `step ` takes an untyped first argument, it should work for both Agent and SocialAgent types without any changes. We actually only need to specialize `interact ` on SocialAgent. Exercise 4.3 👉 Plot the SIR curves of the resulting simulation. N 50 L 40 number of steps 200 In each step call `step ` 50N times. \"\"\" let N 50 L 40 global social agents initialize social N, L Ss, Is, Rs , , Tmax 200 gif for t in 1 Tmax 1. Step a lot 2. Count S, I and R, push them to Ss Is Rs 3. call visualize on the agents, 4. place the SIR plot next to visualize. plot left, right, size 600,300 final plot end end md\"\"\" Exercise 4.4 👉 Make a scatter plot showing each agent's `social score` on one axis, and the `num infected` from the simulation in the other axis. Run this simulation several times and comment on the results. \"\"\" md\"\"\" 👉 Run a simulation for 100 steps, and then apply a \"lockdown\" where every agent's social score gets multiplied by 0.25, and then run a second simulation which runs on that same population from there. What do you notice? How does changing this factor form 0.25 to other numbers affect things? \"\"\" md\"\"\" Exercise 5 Optional Effect of distancing We can use a variant of the above model to investigate the effect of the mis named \"social distancing\" we want people to be socially close, but physically distant . In this variant, we separate out the two effects \"infection\" and \"movement\" an infected agent chooses a neighbouring site, and if it finds a susceptible there then it infects it with probability p I . For simplicity we can ignore recovery. Separately, an agent chooses a neighbouring site to move to, and moves there with probability p M if the site is vacant. Otherwise it stays where it is. When p M 0 , the agents cannot move, and hence are completely quarantined in their original locations. 👉 How does the disease spread in this case? \"\"\" md\"\"\" 👉 Run the dynamics repeatedly, and plot the sites which become infected. \"\"\" md\"\"\" 👉 How does this change as you increase the density \\rho N L^2 of agents? Start with a small density. This is basically the site percolation https en.wikipedia.org wiki Percolation theory model. When we increase p M , we allow some local motion via random walks. \"\"\" md\"\"\" 👉 Investigate how this leaky quarantine affects the infection dynamics with different densities. \"\"\" if student.name \"Jazzy Doe\" md\"\"\" danger \"Before you submit\" Remember to fill in your name and Kerberos ID at the top of this notebook. \"\"\" end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"\"\" After every sweep, count the values S , I and R and push them to 3 arrays. \"\"\" hint md\"\"\" ```julia let N 50 L 40 x initialize N, L initialize to empty arrays Ss, Is, Rs Int , Int , Int Tmax 200 gif for t in 1 Tmax for i in 1 50N step x, L, pandemic end ... track S, I, R in Ss Is and Rs left visualize x, L right plot xlim 1,Tmax , ylim 1,N , size 600,300 plot right, 1 t, Ss, color color S , label \"S\" plot right, 1 t, Is, color color I , label \"I\" plot right, 1 t, Rs, color color R , label \"R\" plot left, right end end ``` \"\"\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let we need to call Base. instead of to make Pluto understand what's going on oops if isdefined Coordinate result Base. Coordinate 3,4 , Coordinate 10,10 if result isa Missing still missing elseif result isa Coordinate keep working md\"Make sure that your return a `Coordinate`. 🧭\" elseif result.x 13 || result.y 14 keep working else correct end end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined origin not defined origin else let if origin isa Missing still missing elseif origin isa Coordinate keep working md\"Make sure that `origin` is a `Coordinate`.\" else if origin Coordinate 0,0 correct else keep working end end end end if isdefined make tuple not defined make tuple else let result make tuple Coordinate 2,1 if result isa Missing still missing elseif result isa Tuple keep working md\"Make sure that you return a `Tuple`, like so `return 1, 2 `.\" else if result 2, 1 correct else keep working end end end end if isdefined trajectory not defined trajectory else let c Coordinate 8,8 t trajectory c, 100 if t isa Missing still missing elseif t isa Vector keep working md\"Make sure that you return a `Vector`.\" elseif all x isa x, Coordinate , t keep working md\"Make sure that you return a `Vector` of `Coordinate`s.\" else if length t 100 almost md\"Make sure that you return `n` elements.\" elseif 1 length Set t 90 correct else keep working md\"Are you sure that you chose each step randomly?\" end end end end if isdefined initialize not defined initialize else let N 200 result initialize N, 1 if result isa Missing still missing elseif result isa Vector || length result N keep working md\"Make sure that you return a `Vector` of length `N`.\" elseif any e e isa Agent , result keep working md\"Make sure that you return a `Vector` of `Agent`s.\" elseif length Set result N keep working md\"Make sure that you create `N` new `Agent`s. Do not repeat the same agent multiple times.\" elseif sum a a.status S, result N 1 && sum a a.status I, result 1 if 8 length Set a.position for a in result 9 correct else keep working md\"The coordinates are not correctly sampled within the box.\" end else keep working md\"`N 1` agents should be Susceptible, 1 should be Infectious.\" end end end bigbreak html\" br br br br \" bigbreak bigbreak bigbreak bigbreak bigbreak "},{"url":"images_abstractions/abstraction/","title":"Abstraction","tags":["lecture","module1","track_julia","track_math"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v 3zTO3LEY cM\" image \"\" section 2 order 2 title \"Abstraction\" layout \"layout.jlhtml\" youtube id \"3zTO3LEY cM\" description \"\" tags \"lecture\", \"module1\", \"track julia\", \"track math\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using PlutoUI using ImageShow, FileIO, ImageIO end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\" Introduction The goal of this section is to introduce you to the notion of abstraction. You can think of abstraction as an opposite to specialization. We will illustrate this by looking at the following example. What is one ? Before we get lost talking about the foundations of number theory, I will present you with a few examples that represent one to me. \" md\"Each of the items in this list is a specific, or specialized representation of one 1. as an integer 1. as a float 1. as a string 1. as a rational number 1. as a cute picture 1. as an 2x2 identity matrix 1. as a singular dog Of course, these are just a few examples of one . People have been representing one for ages in different languages, scripts, artistic expression, etc. The difference between these ones to me is clear. In fact, I just articulated it to you. Now, let's turn to how a computer sees one differently based on what I type. \" md\"So to a computer, all of these are different types.\" md\" What is a collection of one s? Now, I want to make a collection of ones for some reason. Below is a way for you to experiment building this collection with different one s. As you do this experiment, I want you to look at what stays in the same in the Julia output, and what doesn't.\" md\"\"\" Before we even look at the output, I am amazed that this code even ran. Are you telling me that the computer doesn't care which one I am using in my array? Yes That's exactly what abstraction is. By stepping back, we can now think and operate at a level that doesn't care about which specific one I am using. This is what we mean by abstraction is the opposite of specialization . The information that Julia gives back is quite informative. Here is an example of the first line of the output for a few different types ``` array 3x4 Array Int64, 2 array 3x4 Array Float64, 2 array 3x4 Array Rational Int64 , 2 ``` Notice that for all of these, we have the same `3x4 Array , 2 `. \"\"\" md\"\"\" First Taste of Abstraction Now, I want to do something to a collection of ones, that doesn't care about which one I'm using. So I'm going to write a function that takes in my collection, and add a corgi whereever I desire. \"\"\" function insert new, A, i, j B copy A B i,j new return B end begin A fill 0, 3, 4 md\"\"\" bind i Slider 1 size A,1 , show value true bind j Slider 1 size A,2 , show value true \"\"\" end begin one number array fill 1,3,4 insert 5, one number array, i, j end md\"This is still amazing. I wrote one function that just cares about how to insert an object into an array, without knowing anything about what's inside, and it worked for two completely different arrays, collections of ones of any kind .\" md\" Conclusion The key idea here is that a computer language should allow you to do operations that make sense. Often times, an operation can make sense for many different objects. So we can abstract away the specifics of the object in our implementation. It should let you step back from there. \" md\"\"\" Appendix \"\"\" begin oneimage load download \"https user images.githubusercontent.com 6933510 199281680 5ac1e8ea c68c 4fb5 a3bf b47ad28057d1.png\" corgi load download \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" nothing end one 1, 1.0, \"one\", 1 1, oneimage, 1 0 0 1 , corgi, computer ones typeof. one begin one keys \"1\", \"1.0\", \"one\", \"1 1\", \"Cute One\", \"2x2 Identity\", \"One Corgi\" selections one keys . one keys lookup element Dict one keys . one md\" bind element key Select selections \" end your chosen one element lookup element element key its type typeof element a 3x4 array of this one. array fill element,3,4 begin one image array fill oneimage,3,4 insert corgi, one image array, i, j end "},{"url":"images_abstractions/dynamicprograms/","title":"Dynamic Programming","tags":["lecture","module1","optimization","programming","track_julia","track_math","interactive","discrete","lazy"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v KyBXJV1zFlo\" image \"https user images.githubusercontent.com 6933510 136196599 c6ae60f0 9269 4134 bb0d 5bcab928bd2b.gif\" section 7 order 7 title \"Dynamic Programming\" layout \"layout.jlhtml\" youtube id \"KyBXJV1zFlo\" description \"\" tags \"lecture\", \"module1\", \"optimization\", \"programming\", \"track julia\", \"track math\", \"interactive\", \"discrete\", \"lazy\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import ImageIO using Plots, PlutoUI, Colors, Images end md\"\"\" What is dynamic programming? The word \"programming\" here is a rather archaic word but still in use for an optimization problem , as used, for example, in the phrase \"linear programming.\" Probably the word \"programming\" should be abandoned in this context, but no doubt it is too late. \"\"\" md\"\"\" Summing over paths problem \"\"\" md\"\"\" Let's start by looking at the following problem. Given a matrix, we follow a path from top to bottom. The paths start at one of the square on the top, and can only go downwards, either South East , South , or South West . We will add up the numbers visited along each path. Our goal is to find the path that has the smallest sum . This is indeed an optimization problem in the set of all possible paths, we want to find a path that minimizes the sum of visited squares. \"\"\" md\"\"\" Try it yourself \"\"\" md\"\"\" Board size \"\"\" const n 8 M rand 0 9, n, n md\"\"\" One way to solve this problem is the naive algorithm where we enumerate all the paths, calculate the sum for each, and take the minimum. However, as the matrix gets larger the total number of paths grows exponentially . \"\"\" md\"\"\" Possible research problem Investigate the statistics of the sums over all possible paths. \"\"\" md\"\"\" Fixing a single point on a path \"\"\" md\"\"\" Let's fix a given point i, j and focus only on all those paths that pass through i, j . \"\"\" md\"\"\" i bind fixi Scrubbable 1 n j bind fixj Scrubbable 1 n \"\"\" md\"\"\" Suppose we fix the point on the penultimate row last but one . When we look at the paths below the fixed value, we're doing the same calculation over and over again. It doesn't seem sensible to keep re doing these calculations. The same holds as we move the fixed point further upwards. So instead of calculating by working \"forwards\", for each box we look at the minimum below it. \"\"\" md\"\"\" The idea of overlapping subproblems \"\"\" md\"\"\" The key point in this problem is that there are overlapping subproblems there are calculations that we don't need to repeat. The idea of dynamic programming is to remember the solution of those subproblems to get an exponential speed up in the calculation speed. \"\"\" begin struct Paths m Int n Int end Base.iterate p Paths fill 1,p.m , fill 1,p.m start the iteration with 1's Base.IteratorSize Type Paths Base.SizeUnknown function Base.iterate p Paths, state if state ≠ fill p.n,p.m end when each row has an n newstate next state,p.n return newstate, newstate end end function next path,n k length path start from the end and find the first element that can be updated by adding 1 while k≥2 && path k n || path k 1 path k 1 1 k 1 end path k 1 add the one then reset the following elements for j k 1 length path path j max path j 1 1,1 end return path end function allpaths m,n Vector Int copy p for p in Paths m,n end end begin paths allpaths n,n numpaths length paths md\"There are numpaths paths to check.\" end md\"\"\" Path bind whichpath Slider 1 numpaths show value true \"\"\" fixedpaths p for p∈paths if p fixi fixj begin number of fixedpaths length fixedpaths md\"Number of fixed paths number of fixedpaths\" end md\"\"\" Path bind whichfixedpath Slider 1 number of fixedpaths show value true \"\"\" md\"\"\" Summing Paths Demo \"\"\" begin winnernum argmin sum M i,p i for i 1 n for p∈paths winner paths winnernum winnertotal sum M i,winner i for i 1 n end md\"\"\" Our goal is to add the numbers on a path and find the minimal path. The winner is number winnernum. \"\"\" md\"\"\" Appendix \"\"\" function setup board M n size M,1 rectangle w, h, x, y Shape x . 0,w,w,0 , y . 0,0,h,h p plot the board for i 1 n, j 1 n if rem i j,2 0 plot p, rectangle 1,1,i,j , opacity .2, color red end end for i 1 n, j 1 n annotate p, j .5 ,n 2 i .5 , M i,j end plot p, legend false, aspectratio 1, xlims 1,n 1 , ylims 1,n 1 , axis nothing return p end rectangle w, h, x, y Shape x . 0,w,w,0 , y . 0,0,h,h function draw path p, path, indices 1 n kwargs... draw the line plot p, path indices . .5, n 1 . indices . .5 linewidth 4, kwargs... add little squares as background for the number in a square for j in n 1 . indices i path 1 n j plot p, rectangle .4,.4,i .3,j .3 , opacity 1, color RGB 0,1,0 , linewidth 0,fillcolor ifelse rem i j,2 0, RGBA 1,.85,.85,.2 , white end p end path text values \" join values, \" \" sum values \" let path fixedpaths whichfixedpath values M i,path i for i 1 n p setup board M annotate p, fixj .5 ,n 2 fixi .5 , M fixi,fixj , red draw path p, path, 1 fixi color black draw path p, path, fixi n color blue plot p title path text values end function viz whichpath path paths whichpath values M i,path i for i 1 n nv length values rectangle w, h, x, y Shape x . 0,w,w,0 , y . 0,0,h,h p setup board M The winning path draw path p, winner color RGB 1,.6,.6 , your path draw path p, path color black, text plot p, title path text values plot p, xlabel path text M i,winner i for i 1 n , xguidefontcolor RGB 1,.5,.5 p end viz whichpath "},{"url":"images_abstractions/images/","title":"Images as Data and Arrays","tags":["lecture","module1","philip","track_julia","matrix","image"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v 3zTO3LEY cM\" image \"https user images.githubusercontent.com 6933510 136196634 2294d0a7 e79a 40d0 bbb8 81da70f4d398.png\" section 1 order 1 title \"Images as Data and Arrays\" layout \"layout.jlhtml\" youtube id \"3zTO3LEY cM\" description \"\" tags \"lecture\", \"module1\", \"philip\", \"track julia\", \"matrix\", \"image\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using PlutoUI using PlutoTeachingTools using HypertextLiteral end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Images as examples of data all around us Welcome to the Computational Thinking using Julia for Real World Problems, at MIT in Spring 2021 The aim of this course is to bring together concepts from computer science and applied math with coding in the modern Julia language , and to see how to apply these techniques to study interesting applications and of course to have fun . We would be pleased if students who have been interested in computer science now become interested in computational science and those interested in scientific applications learn computer science they may not see elsewhere. ... and for all students, we wish to share the value of the Julia language as the best of both worlds. \"\"\" md\"\"\" Alan's Essay Are all programming languages the same? Superficially, many programming languages are very similar. \"Showoffs\" will compare functional programming vs imperative programming. Others will compare compiled languages vs dynamic languages. I will avoid such fancy terms in this little essay, preferring to provide this course's pedagogical viewpoint. Generally speaking beginning programmers should learn to create \"arrays\" write \"for loops\", \"conditionals\", \"comparisons\", express mathematical formulas, etc. So why Julia at a time when Python seems to be the language of teaching, and Java and C so prominent in the corporate world? As you might imagine, we believe Julia is special. Oh you will still have the nitty gritty of when to use a bracket and a comma. You might have strong opinions as to whether arrays should begin with 0 or 1 joke some say it's time to compromise and use ½. Getting past these irrelevant issues, you will begin to experience one by one what makes Julia so very special. For starters, a language that runs fast is more fun. We can have you try things that would just be so slow in other languages it would be boring. We also think you will start to notice how natural Julia is, how it feels like the mathematics, and how flexible it can be. Getting to see the true value of fancy terms like multiple dispatch, strong typing, generic programming, and composable software will take a little longer, but stick with us, and you too will see why Julia is so very special. \"\"\" md\"\"\" Computer Science and Computational Science Working Together \"\"\" md\"\"\" Applications of computer science in the real world use data , i.e. information that we can measure in some way. Data take many different forms, for example Numbers that change over time time series Stock price each second minute day Weekly number of infections Earth's global average temperature Video The view from a window of a self driving car A hurricane monitoring station Ultrasound e.g. prenatal Images Diseased versus healthy tissue in a medical scan Pictures of your favourite dog \"\"\" md\"\"\" Exercise Think of another two examples in each category. Can you think of other categories of data? \"\"\" md\"\"\" Computational science can be summed up by a simplified workflow \"\"\" md\"\"\" data ⟶ input ⟶ process ⟶ model ⟶ visualize ⟶ output \"\"\" md\"\"\" html\" br \" To use any data source, we need to input the data of interest, for example by downloading it, reading in the resulting file, and converting it into a form that we can use in the computer. Then we need to process it in some way to extract information of interest. We usually want to visualize the results, and we may want to output them, for example by saving to disc or putting them on a website. We often want to make a mathematical or computational model that can help us to understand and predict the behavior of the system of interest. In this course we aim to show how programming, computer science and applied math combine to help us with these goals. \"\"\" md\"\"\" Data Images as an example of data Let's start off by looking at images and how we can process them. Our goal is to process the data contained in an image in some way, which we will do by developing and coding certain algorithms . Here is the the Fall 2020 version of this lecture small variations by 3 Blue 1 Brown Grant Sanderson for your reference. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid DGojI9xcCfg params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" If we open an image on our computer or the web and zoom in enough, we will see that it consists of many tiny squares, or pixels \"picture elements\" . Each pixel is a block of one single colour, and the pixels are arranged in a two dimensional square grid. You probably already know that these pixels are stored in a computer numerically perhaps in some form of RGB red,green,blue format. This is the computer's representation of the data. Note that an image is already an approximation of the real world it is a two dimensional, discrete representation of a three dimensional reality. \"\"\" md\"\"\" Input and Visualize loading and viewing an Image in Julia \"\"\" md\"\"\" Let's use Julia to load actual images and play around with them. We can download images from the internet, your own file, or your own webcam. \"\"\" md\"\"\" Downloading an image from the internet or a local file We can use the `Images.jl` package to load an image file in three steps. \"\"\" md\"\"\" Step 1 from internet we specify the URL web address to download from html\" br \" note that Pluto places results before commands because some people believe output is more interesting than code. This takes some getting used to. \"\"\" url \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" md\"\"\" Step 2 Now we use the aptly named `download` function to download the image file to our own computer. Philip is Prof. Edelman's corgi. \"\"\" philip filename download url download to a local file. The filename is returned md\"\"\" Step 3 Using the `Images.jl` package loaded at the start of this notebook scroll up and take a look. we can load the file, which automatically converts it into usable data. We'll store the result in a variable. Remember the code is after the output. \"\"\" philip load philip filename md\" Hi there Philip \" md\"\"\" We see that the Pluto notebook has recognised that we created an object representing an image, and automatically displayed the resulting image of Philip, the cute Welsh Pembroke corgi and co professor of this course. Poor Philip will undergo quite a few transformations as we go along \"\"\" md\"\"\" 👉 Exercise change the url to use another image from the web. \"\"\" md\"\"\" Capturing an Image from your own camera \"\"\" md\"\"\" Even more fun is to use your own webcam. Try pressing the enable button below. Then press the camera to capture an image. Kind of fun to keep pressing the button as you move your hand etc. \"\"\" bind myface1 PlutoUI.WebcamInput help false, max size 150 myface1 myface1 , end 1 1 myface1 end 1 1, myface1 end 1 1, end 1 1 md\"\"\" Inspecting your data \"\"\" md\"\"\" Image size \"\"\" md\"\"\" The first thing we might want to know is the size of the image \"\"\" philip size size philip md\"\"\" Julia returns a pair of two numbers. Comparing these with the picture of the image, we see that the first number is the height, i.e. the vertical number of pixels, and the second is the width. \"\"\" philip height philip size 1 philip width philip size 2 md\"\"\" Locations in an image Indexing Now suppose that we want to examine a piece of the image in more detail. We need some way of specifying which piece of the image we want. Thinking of the image as a grid of pixels, we need a way to tell the computer which pixel or group of pixels we want to refer to. Since the image is a two dimensional grid, we can use two integers whole numbers to give the coordinates of a single pixel. Specifying coordinates like this is called indexing think of the index of a book, which tells you on which page an idea is discussed. In Julia we use square brackets, ` ` and ` ` for indexing \"\"\" a pixel philip 200, 100 md\"\"\" We see that Julia knows to draw our pixel object for us a block of the relevant color. When we index into an image like this, the first number indicates the row in the image, starting from the top, and the second the column , starting from the left. In Julia, the first row and column are numbered starting from 1, not from 0 as in some other programming languages. \"\"\" md\"\"\" We can also use variables as indices... \"\"\" md\"\"\" ...and these variables can be controlled by sliders \"\"\" bind row i Slider 1 size philip 1 , show value true bind col i Slider 1 size philip 2 , show value true row i,col i philip row i, col i md\"\"\" Locations in an image Range indexing We saw that we can use the row number and column number to index a single pixel of our image. Next, we will use a range of numbers to index multiple rows or columns at once, returning a subarray \"\"\" philip 550 650, 1 philip width md\"\"\" Here, we use `a b` to mean \" all numbers between `a` and `b` \". For example \"\"\" collect 1 10 md\"\"\" You can also use a ` ` without start and end to mean \" every index \" \"\"\" philip 550 650, md\"\"\" Let's get a single row of pixels \"\"\" philip 550, philip head philip 470 800, 140 410 md\"\"\" Scroll in on Philip's nose Use the widgets below slide left and right sides . \"\"\" bind range rows RangeSlider 1 size philip head 1 bind range cols RangeSlider 1 size philip head 2 nose philip head range rows, range cols md\"\"\" Process Modifying an image Now that we have access to image data, we can start to process that data to extract information and or modify it in some way. We might want to detect what type of objects are in the image, say to detect whether a patient has a certain disease. To achieve a high level goal like this, we will need to perform mid level operations, such as detecting edges that separate different objects based on their color. And, in turn, to carry that out we will need to do low level operations like comparing colors of neighboring pixels and somehow deciding if they are \"different\". \"\"\" md\"\"\" Representing colors We can use indexing to modify a pixel's color. To do so, we need a way to specify a new color. Color turns out to be a complicated concept, having to do with the interaction of the physical properties of light with the physiological mechanisms and mental processes by which we detect it We will ignore this complexity by using a standard method of representing colours in the computer as an RGB triple , i.e. a triple of three numbers r, g, b , giving the amount of red, of green and of blue in a colour, respectively. These are numbers between 0 none and 1 full . The final colour that we perceive is the result of \"adding\" the corresponding amount of light of each colour the details are fascinating, but beyond the scope of this course \"\"\" md\"\"\" We can create a new color in Julia as follows \"\"\" RGB 1.0, 0.0, 0.0 begin md\"\"\" A pixel with bind test r Scrubbable 0 0.1 1 default 0.1 red, bind test g Scrubbable 0 0.1 1 default 0.5 green and bind test b Scrubbable 0 0.1 1 default 1.0 blue looks like \"\"\" end RGB test r, test g, test b md\"\"\" Exercise 2.5 👉 Write a function `invert` that inverts a color, i.e. sends r, g, b to 1 r, 1 g, 1 b . \"\"\" function invert color AbstractRGB return missing end md\"Let's invert some colors \" color black RGB 0.0, 0.0, 0.0 invert color black color red RGB 0.8, 0.1, 0.1 invert color red md\"Can you invert the picture of Philip?\" philip inverted missing md\"\"\" Modifying a pixel Let's start by seeing how to modify an image, e.g. in order to hide sensitive information. We do this by assigning a new value to the color of a pixel \"\"\" let temp copy philip head temp 100, 200 RGB 1.0, 0.0, 0.0 temp end md\"\"\" Groups of pixels We probably want to examine and modify several pixels at once. For example, we can extract a horizontal strip 1 pixel tall \"\"\" philip head 50, 50 100 md\"\"\" Here, Julia is showing the strip as a collection of rectangles in a row. \"\"\" md\"\"\" And then modify it \"\"\" let temp copy philip head temp 50, 50 100 . RGB 1.0, 0.0, 0.0 temp end md\"\"\" Similarly we can modify a whole rectangular block of pixels \"\"\" let temp copy philip head temp 50 100, 50 100 . RGB 1.0, 0.0, 0.0 temp end md\"\"\" Exercise 1.2 👉 Generate a vector of 100 zeros. Change the center 20 elements to 1. \"\"\" function create bar return missing end if isdefined create bar not defined create bar else let result create bar if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa Vector || length result 100 keep working md\"The result should be a `Vector` with 100 elements.\" elseif result 1,50,100 0,1,0 keep working else correct end end end md\"\"\" Reducing the size of an image \"\"\" md\"\"\" Maybe we would also like to reduce the size of this image, since it's rather large. For example, we could take every 10th row and every 10th column and make a new image from the result \"\"\" reduced image philip 1 10 end, 1 10 end md\"\"\" Note that the resulting image doesn't look very good, since we seem to have lost too much detail. Exercise Think about what we might do to reduce the size of an image without losing so much detail. \"\"\" md\"\"\" Model Creating synthetic images Think about your favorite Pixar movie e.g. Monsters Inc. Movie frames are images that are generated from complicated mathematical models. Ray tracing which may be covered in this class is a method for making images feel realistic. \"\"\" md\"\"\" Output Saving an image to a file Finally, we want to be able to save our new creation to a file. To do so, you can right click on a displayed image, or you can write it to a file. Fill in a path below \"\"\" save \"reduced phil.png\", reduced image md\"\"\" html\" br \" \"\"\" md\"\"\" Computer science Arrays An image is a concrete example of a fundamental concept in computer science, namely an array . Just as an image is a rectangular grid, where each grid cell contains a single color, an array is a rectangular grid for storing data. Data is stored and retrieved using indexing, just as in the image examples each cell in the grid can store a single \"piece of data\" of a given type. Dimension of an array An array can be one dimensional, like the strip of pixels above, two dimensional, three dimensional, and so on. The dimension tells us the number of indices that we need to specify a unique location in the grid. The array object also needs to know the length of the data in each dimension. Names for different types of array One dimensional arrays are often called vectors or, in some other languages, \"lists\" and two dimensional arrays are matrices . Higher dimensional arrays are tensors . Arrays as data structures An array is an example of a data structure , i.e. a way of arranging data such that we can access it. A key theme in computer science is that of designing different data structures that represent data in different ways. Conceptually, we can think of an array as a block of data that has a position or location in space. This can be a useful way to arrange data if, for example, we want to represent the fact that values in nearby locations in array are somehow near to one another. Images are a good example of this neighbouring pixels often represent different pieces of the same object, for example the rug or floor, or Philip himself, in the photo. We thus expect neighbouring pixels to be of a similar color. On the other hand, if they are not, this is also useful information, since that may correspond to the edge of an object. \"\"\" md\"\"\" Julia constructing arrays Creating vectors and matrices Julia has strong support for arrays of any dimension. Vectors, or one dimensional arrays, are written using square brackets and commas \"\"\" 1, 20, \"hello\" RGB 1, 0, 0 , RGB 0, 1, 0 , RGB 0, 0, 1 md\"\"\" Matrices, or two dimensional arrays, also use square brackets, but with spaces and new lines instead of commas \"\"\" RGB 1, 0, 0 RGB 0, 1, 0 RGB 0, 0, 1 RGB 0.5, 0.5, 0.5 md\"\"\" Array comprehensions It's clear that if we want to create an array with more than a few elements, it will be very tedious to do so by hand like this. Rather, we want to automate the process of creating an array by following some pattern, for example to create a whole palette of colors Let's start with all the possible colors interpolating between black, `RGB 0, 0, 0 `, and red, `RGB 1, 0, 0 `. Since only one of the values is changing, we can represent this as a vector, i.e. a one dimensional array. A neat method to do this is an array comprehension . Again we use square brackets to create an array, but now we use a variable that varies over a given range values \"\"\" RGB x, 0, 0 for x in 0 0.1 1 md\"\"\" Here, `0 0.1 1` is a range the first and last numbers are the start and end values, and the middle number is the size of the step. \"\"\" md\"\"\" In a similar way we can create two dimensional matrices, by separating the two variables for each dimension with a comma `,` \"\"\" RGB i, j, 0 for i in 0 0.1 1, j in 0 0.1 1 md\"\"\" Joining matrices We often want to join vectors and matrices together. We can do so using an extension of the array creation syntax \"\"\" philip head philip head philip head reverse philip head, dims 2 reverse philip head, dims 1 rot180 philip head md\"\"\" Pluto Interactivity using sliders \"\"\" md\"\"\" Suppose we want to see the effect of changing the number of colors in our vector or matrix. We could, of course, do so by manually fiddling with the range. It would be nice if we could do so using a user interface , for example with a slider . Fortunately, the Pluto notebook allows us to do so \"\"\" md\"\"\" We can define a slider using \"\"\" bind number reds Slider 1 100, show value true md\"\"\" The `Slider` type is defined in the `PlutoUI.jl` package. \"\"\" md\"\"\" This creates a new variable called `number reds`, whose value is the value shown by the slider. When we move the slider, the value of the variable gets updated. Since Pluto is a reactive notebook, other cells which use the value of this variable will automatically be updated too \"\"\" md\"\"\" Let's use this to make a slider for our one dimensional collection of reds \"\"\" RGB red value number reds, 0, 0 for red value in 0 number reds md\"\"\" When you move the slider, you should see the number of red color patches change \"\"\" md\"\"\" What is going on here is that we are creating a vector in which `red value` takes each value in turn from the range from `0` up to the current value of `number reds`. If we change `number reds`, then we create a new vector with that new number of red patches. \"\"\" md\"\"\" Exercise Make three sliders with variables `r`, `g` and `b`. Then make a single color patch with the RGB color given by those values. \"\"\" md\"\"\" We can do the same to create different size matrices, by creating two sliders, one for reds and one for greens. Try it out \"\"\" md\"\"\" Summary \"\"\" md\"\"\" Let's summarize the main ideas from this notebook Images are arrays of colors We can inspect and modify arrays using indexing We can create arrays directly or using array comprehensions \"\"\" begin colored line x Vector Real Gray. Float64. hcat x ' colored line x Any nothing end colored line create bar bigbreak html\" br br br br br \" bigbreak "},{"url":"images_abstractions/linear_transformation/","title":"linear_transformation","tags":[],"text":" A Pluto.jl notebook v0.19.45 using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using PlutoUI using HypertextLiteral using LinearAlgebra end md\"Show grid lines bind show grid CheckBox default true \" let range 2 .1 2 md\"\"\" a bind a Slider range default 1.0, show value true b bind b Slider range default 0.0, show value true c bind c Slider range default 0.0, show value true d bind d Slider range default 1.0, show value true Re run this cell to reset to identity transformation \"\"\" end A a b c d md\" Det A a d c b det A \" md\"\"\" Appendix \"\"\" det A det A if det A 0 invA inv A end img sources \"https user images.githubusercontent.com 6933510 108605549 fb28e180 73b4 11eb 8520 7e29db0cc965.png\" \"Corgis\", \"https user images.githubusercontent.com 6933510 108883855 39690f80 7606 11eb 8eb1 e595c6c8d829.png\" \"Arrows\", \"https images.squarespace cdn.com content v1 5cb62a904d546e33119fa495 1589302981165 HHQ2A4JI07C43294HVPD ke17ZwdGBToddI8pDm48kA7bHnZXCqgRu4g0 U7hbNpZw zPPgdn4jUwVcJE1ZvWQUxwkmyExglNqGp0IvTJZamWLI2zvYWH8K3 s 4yszcp2ryTI0HqTOaaUohrI8PISCdr 3EAHMyS8K84wLA7X0UZoBreocI4zSJRMe1GOxcKMshLAGzx4R3EDFOm1kBS fluffy corgi?format 2500w\" \"Alan\" md\"\"\" Choose an image bind img source Select img sources \"\"\" img original load download img source typeof img original function trygetpixel img AbstractMatrix, x Float64, y Float64 rows, cols size img \"The linear map 1,1 ↦ 0,1 \" f t t 1.0 1.0 1.0 i floor Int, rows f y j floor Int, cols f x rows cols if 1 i ≤ rows && 1 j ≤ cols img i,j else zero eltype img end end function with gridlines img Array Any,2 n 8 sep i size img, 1 ÷ n sep j size img, 2 ÷ n result copy img stroke zero eltype img RGBA RGB 1,1,1 , 0.75 stroke RGBA 1, 1, 1, 0.75 result 1 sep i end, . stroke result , 1 sep j end . stroke a second time, to create a line 2 pixels wide result 2 sep i end, . stroke result , 2 sep j end . stroke return result end img if show grid with gridlines img original else img original end if det A 0 RGB 1.0, 1.0, 1.0 else in x, in y invA out x, out y trygetpixel img, in x, in y end for out y in LinRange 2, 2, 300 , out x in LinRange 2, 2, 300 "},{"url":"images_abstractions/newton_method/","title":"The Newton Method","tags":["lecture","module1","track_math","continuous","differentiation","automatic differentiation","ForwardDiff","interactive","Symbolics","transformation"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v Wjcx9sNSLP8\" image \"https user images.githubusercontent.com 6933510 136196605 b6119b9d 223c 44bc 97d5 ef7cfce66483.gif\" section 6 order 6 title \"The Newton Method\" layout \"layout.jlhtml\" youtube id \"Wjcx9sNSLP8\" description \"This lecture explains a method for finding the root of a function, but using code an illustrations instead of a chalkboard We will illustrate the Newton method using the ForwardDiff.jl package to carry out automatic differentiation, but we will also try to understand what's going on \\\"under the hood\\\".\" tags \"lecture\", \"module1\", \"track math\", \"continuous\", \"differentiation\", \"automatic differentiation\", \"ForwardDiff\", \"interactive\", \"Symbolics\", \"transformation\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Symbolics, ForwardDiff, Plots, PlutoUI, LaTeXStrings using ForwardDiff jacobian end md\"\"\" Solving equations and finding inverse transformations using the Newton method \"\"\" md\"\"\" In science and engineering we often need to solve systems of equations . If the equations are linear then linear algebra tells us a general method to solve them these are now routinely applied to solve systems of millions of linear equations. If the equations are non linear then things are less obvious. The main solution methods we know work by... reducing the nonlinear equations to a sequence of linear equations They do this by approximating the function by a linear function and solving that to get a better solution, then repeating this operation as many times as necessary to get a sequence of increasingly better solutions. This is an example of an iterative algorithm . A well known and elegant method, which can be used in many different contexts, is the Newton method . It does, however, have the disadvantage that it requires derivatives of the function. This can be overcome using automatic differentiation techniques. We will illustrate the Newton method using the `ForwardDiff.jl` package to carry out automatic differentiation, but we will also try to understand what's going on \"under the hood\". \"\"\" md\"\"\" The Newton method in 1D We would like to solve equations like f x g x . We rewrite that by moving all the terms to one side of the equation so that we can write h x 0 , with h x f x g x . A point x^ such that h x^ 0 is called a root or zero of h . The Newton method finds zeros, and hence solves the original equation. \"\"\" md\"\"\" The idea of the Newton method is to follow the direction in which the function is pointing We do this by building a tangent line at the current position and following that instead, until it hits the x axis. Let's look at that visually first \"\"\" md\"\"\" n bind n2 Slider 0 10, show value true, default 0 \"\"\" md\"\"\" x₀ bind x02 Slider 10 10, show value true, default 6 \"\"\" md\"\"\" n bind n Slider 0 10, show value true, default 0 \"\"\" md\"\"\" x₀ bind x0 Slider 10 10, show value true, default 6 \"\"\" md\"\"\" Using symbolic calculations to understand derivatives and nonlinear maps \"\"\" md\"\"\" We can use Julia's new symbolic capabilities to understand what's going on with a nonlinear polynomial function. Let's see what happens if we perturb a function f around a point z by a small amount \\eta . \"\"\" variables z, η md\"\"\" m bind m Slider 1 6, show value true \"\"\" f x x^m 2 f′ x ForwardDiff.derivative f, x f z f z η md\"\"\" When \\eta is small, \\eta^2 is very small, so we can ignore it. We are left with terms that either don't contain \\eta constants , or multiply \\eta linear . The part that multiplies \\eta is the derivative \"\"\" f′ z f z η f′ z md\"\"\" The derivative gives the \" linear part\" of the function. `ForwardDiff.jl`, and forward mode automatic differentiation in general, effectively uses this although not symbolically in this sense to just propagate the linear part of each function through a calculation. \"\"\" md\"\"\" Mathematics of the Newton method \"\"\" md\"\"\" We can convert the idea of \"following the tangent line\" into equations as follows. You can also do so by just looking at the geometry in 1D, but that does not help in 2D. \"\"\" md\"\"\" Suppose we have a guess x 0 for the root and we want to find a hopefully better guess x 1 . Let's set x 1 x 0 \\delta , where x 1 and \\delta are still unknown. We want x 1 to be a root, so \"\"\" md\"\"\" f x 1 f x 0 \\delta \\simeq 0 \"\"\" md\"\"\" If we are already \"quite close\" to the root then \\delta should be small, so we can approximate f using the tangent line f x 0 \\delta \\, f' x 0 \\simeq 0 and hence \\delta \\simeq \\frac f x 0 f' x 0 so that x 1 x 0 \\frac f x 0 f' x 0 Now we can repeat so that x 2 x 1 \\frac f x 1 f' x 1 and in general x n 1 x n \\frac f x n f' x n . This is the Newton method in 1D. \"\"\" md\"\"\" Implementation in 1D \"\"\" function newton1D f, x0 f′ x ForwardDiff.derivative f, x \\prime TAB x0 37.0 starting point sequence x0 x x0 for i in 1 10 x f x f′ x end return x end newton1D x x^2 2, 37.0 sqrt 2 md\"\"\" Symbolic derivative in 2D \"\"\" md\"\"\" Let's see what happens when we perturb by small amounts \\delta in the x direction and \\epsilon in the y direction around the point a, b \"\"\" md\"\"\" p bind p Slider 0 0.01 1, show value true \"\"\" variables a, b, δ, ϵ md\"\"\" Newton for transformations in 2 dimensions T \\mathbb R ^2 \\to \\mathbb R ^2 \"\"\" md\"\"\" We want to find the inverse T^ 1 y , i.e. to solve the equation T x y for x . We use the same idea as in 1D, but now in 2D \"\"\" md\"\"\" T x 0 \\delta \\simeq 0 T x 0 J \\cdot \\delta \\simeq 0, where J DT x 0 is the Jacobian matrix of T at x 0 , i.e. the best linear approximation of T near to x 0 . \"\"\" md\"\"\" Hence \\delta is the solution of the system of linear equations \"\"\" md\"\"\" J \\cdot \\delta T x 0 Then we again construct the new approximation x 1 as x 1 x 0 \\delta . \"\"\" md\"\"\" In 2D we have an explicit formula for the inverse of the matrix. \"\"\" md\"\"\" Implementation in 2D \"\"\" function newton2D step T, x J ForwardDiff.jacobian T, x should use StaticVectors δ J \\ T x J^ 1 T x return x δ end \"Looks for x such that T x 0\" function newton2D T, x0, n 10 x x0 for i in 1 n x newton2D step T, x end return x end md\"\"\" Remember that Newton is designed to look for roots , i.e. places where T x 0 . We want T x y , so we need another layer \"\"\" \"Looks for x such that f x y, i.e. f x y 0\" function inverse f, y, x0 0, 0 return newton2D x f x y, x0 end inverse f y inverse f, y straight x0, y0, x, m y0 m x x0 function standard Newton f, n, x range, x0, ymin 10, ymax 10 f′ x ForwardDiff.derivative f, x p plot f, x range, lw 3, ylim ymin, ymax , legend false, size 400, 300 hline 0.0 , c \"magenta\", lw 3, ls dash scatter x0 , 0 , c \"green\", ann x0, 5, L\"x 0\", 10 for i in 1 n plot x0, x0 , 0, f x0 , c gray, alpha 0.5 scatter x0 , f x0 , c red m f′ x0 plot x range, straight x0, f x0 , x, m for x in x range , c blue, alpha 0.5, ls dash, lw 2 x1 x0 f x0 m scatter x1 , 0 , c \"green\", ann x1, 5, L\"x % i\", 10 x0 x1 end p | as svg end let f x x^2 2 standard Newton f, n2, 1 0.01 10, x02, 10, 70 end let f x 0.2x^3 4x 1 standard Newton f, n, 10 0.01 10, x0, 10, 70 end T α x, y , x α y^2, y α x^2 jacobian T p , a, b .| Text jacobian T p , a, b δ, ϵ md\"\"\" α bind α Slider 0.0 0.01 1.0, show value true \"\"\" T α 0.3, 0.4 inverse T α 0.3, 0.4 T α ∘ inverse T α 0.3, 0.4 md\"\"\" Appendix \"\"\" expand ex simplify ex, polynorm true expand f z η expand f z η f z η f′ z image expand. T p a δ , b ϵ image T p a, b simplify. expand. image T p a, b jacobian T p , a, b δ, ϵ "},{"url":"images_abstractions/seamcarving/","title":"Seam Carving","tags":["lecture","module1","image","matrix","track_julia","track_climate","track_data","optimization","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v KyBXJV1zFlo\" image \"https user images.githubusercontent.com 6933510 136196584 b3c806a8 aa61 48d9 9e73 30583fcc38bf.gif\" section 8 order 8 title \"Seam Carving\" layout \"layout.jlhtml\" youtube id \"KyBXJV1zFlo\" description \"\" tags \"lecture\", \"module1\", \"image\", \"matrix\", \"track julia\", \"track climate\", \"track data\", \"optimization\", \"interactive\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using ImageMagick using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using ImageFiltering using Plots, PlutoUI Standard libraries using Statistics, LinearAlgebra end md\"\"\" Scroll through the images in this notebook. The idea of seam carving is to shrink an image by removing the \"least interesting\" parts of the image, but without resizing the objects within the image. We want to remove the \"dead space\" within the image. We try to find a \"seam\", i.e. a connected path of pixels from top to bottom of the image, which consists of the \"least important\" pixels, by some measure. We then remove the pixels in that seam to give an image that is one pixel narrower. In order to do this, we need to decide how to measure which pixels are \"important\". \"\"\" md\"\"\" Fall 2020 MIT Class Video from Grant Sanderson Here is Grant Sanderson 3Blue1Brown explaining seam carving using this notebook from the Fall 2020 edition of this class. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid rpB6zQNsbQU params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" The seam carving algorithm We need to specify a notion of importance of pixels. The seam will then sum up the importance of pixels over the seam and pick the seam which minimizes this total importance. We will assign importance as \"the extent to which a pixel sits inside an edge\". So we need to calculate the \"edgeness\" of each pixel. \"\"\" md\"\"\" 1. We will use convolution with Sobel filters for edge detection. 2. Then we will use that to write an algorithm that removes \"uninteresting\" bits of an image in order to shrink it. \"\"\" image urls \"https wisetoast.com wp content uploads 2015 10 The Persistence of Memory salvador deli painting.jpg\", \"https upload.wikimedia.org wikipedia commons thumb 1 17 Gustave Caillebotte Paris Street%3B Rainy Day Google Art Project.jpg 1014px Gustave Caillebotte Paris Street%3B Rainy Day Google Art Project.jpg\", \"https upload.wikimedia.org wikipedia commons thumb 1 17 Gustave Caillebotte Paris Street%3B Rainy Day Google Art Project.jpg 1014px Gustave Caillebotte Paris Street%3B Rainy Day Google Art Project.jpg\", \"https upload.wikimedia.org wikipedia commons thumb c cc Grant Wood American Gothic Google Art Project.jpg 480px Grant Wood American Gothic Google Art Project.jpg\", \"https wisetoast.com wp content uploads 2015 10 The Persistence of Memory salvador deli painting.jpg\", \"https upload.wikimedia.org wikipedia commons thumb 7 7d A Sunday on La Grande Jatte%2C Georges Seurat%2C 1884.jpg 640px A Sunday on La Grande Jatte%2C Georges Seurat%2C 1884.jpg\", \"https upload.wikimedia.org wikipedia commons thumb e ea Van Gogh Starry Night Google Art Project.jpg 758px Van Gogh Starry Night Google Art Project.jpg\", \"https web.mit.edu facilities photos construction Projects stata 1 large.jpg\", image url image urls 1 img load download image url arbitrarily choose the brightness of a pixel as mean of rgb brightness c AbstractRGB mean c.r, c.g, c.b Use a weighted sum of rgb giving more weight to colors we perceive as 'brighter' Based on https www.tutorialspoint.com dip grayscale to rgb conversion.htm brightness c AbstractRGB 0.3 c.r 0.59 c.g 0.11 c.b Gray. brightness. img md\"\"\" Edge detection filter Spoiler alert We use the Sobel edge detection filter we created in our Homework. ```math \\begin align G x & \\begin bmatrix 1 & 0 & 1 \\\\ 2 & 0 & 2 \\\\ 1 & 0 & 1 \\\\ \\end bmatrix \\star A\\\\ 10pt G y & \\begin bmatrix 1 & 2 & 1 \\\\ 0 & 0 & 0 \\\\ 1 & 2 & 1 \\\\ \\end bmatrix \\star A \\end align ``` Here, \\star denotes convolution. Here A is the array corresponding to your image. We can think of G x and G y as calculating discretized derivatives in the x and y directions. Then we combine them by finding the magnitude of the discretized gradient , in the sense of multivariate calculus, by defining G \\text total \\sqrt G x^2 G y^2 . \"\"\" md\"\"\" Here are the Sobel kernels for the derivatives in each direction \"\"\" Sy, Sx Kernel.sobel collect Int. 8 . Sx , collect Int. 8 . Sy plotly surface brightness. img md\"\"\" blue shows positive values red shows negative values \"\"\" function shrink image image, ratio 5 height, width size image new height height ÷ ratio 1 new width width ÷ ratio 1 list mean image ratio i ratio i 1 , ratio j ratio j 1 , for j in 1 new width for i in 1 new height reshape list, new height, new width end function convolve M, kernel height, width size kernel half height height ÷ 2 half width width ÷ 2 new image similar M i, j loop over the original image m, n size M inbounds for i in 1 m for j in 1 n k, l loop over the neighbouring pixels accumulator 0 M 1, 1 for k in half height half height height 1 for l in half width half width width 1 Mi i k Mj j l First index into M if Mi 1 Mi 1 elseif Mi m Mi m end Second index into M if Mj 1 Mj 1 elseif Mj n Mj n end accumulator kernel k, l M Mi, Mj end end new image i, j accumulator end end return new image end function edgeness img Sy, Sx Kernel.sobel b brightness. img ∇y convolve b, Sy ∇x convolve b, Sx sqrt. ∇x.^2 ∇y.^2 end function show colored array array pos color RGB 0.36, 0.82, 0.8 neg color RGB 0.99, 0.18, 0.13 to rgb x max x, 0 pos color max x, 0 neg color to rgb. array maximum abs. array end show colored array Sx , show colored array Sy let ∇y convolve brightness. img , Sy ∇x convolve brightness. img , Sx data md\"``G x``\", md\"``G y``\", zoom in on the clock img 300 end, 1 300 , img 300 end, 1 300 , show colored array. ∇x 300 end, 1 300 , ∇y 300 end, 1 300 ... avoid collating the images into one big matrix todisplay permutedims reshape data, 2,3 , 2,1 assert length todisplay 10 PlutoUI.ExperimentalLayout.grid todisplay end md\"\"\" Seam carving idea The idea of seam carving is to find a path from the top of the image to the bottom of the image where the path minimizes the edgeness. In other words, this path minimizes the number of edges in the image that it crosses . We will call the edgeness the energy . \"\"\" md\"\"\" At every step in going down, the path is allowed to go south west, south or south east. We want to find a connected path, or seam , with the minimum possible sum of \"energies\" along the path. We start by writing a `least edgy` function which takes a matrix of energies and returns a new matrix. The new matrix has entries M i, j which gives the minimum possible energy when starting from the pixel i, j and going from there down to a pixel in the bottom row. \"\"\" e x,y ↙ ↓ ↘ pick the next path which gives the least overall energy e x 1,y 1 e x,y 1 e x 1,y 1 Basic calculation e x,y min e x 1,y 1 , e x,y , e x 1,y `dirs` records which direction we take from 1 SW, 0 S, 1 SE function least edgy E least E zeros size E dirs zeros Int, size E least E end, . E end, the minimum energy on the last row is the energy itself m, n size E Go from the last row up, finding the minimum energy for i in m 1 1 1 for j in 1 n j1, j2 max 1, j 1 , min j 1, n e, dir findmin least E i 1, j1 j2 least E i,j e least E i,j E i,j dirs i, j 1, 0, 1 dir j 1 end end return least E, dirs end The bright areas are screaming \"AVOID ME \" least e, dirs least edgy edgeness img show colored array least e md\"\"\" Here are the directions that we should take at each step \"\"\" direction the path should take at every pixel. reduce x, y x y \"\\n\", reduce , getindex. \" \", \"↙\", \"↓\", \"↘\" , , dirs 1 25, 1 60 . 3 , dims 2, init \"\" , init \"\" | Text md\" Remove seams\" md\"\"\" We now compress an image horizontally by successively removing a number of seams of lowest energy. \"\"\" function get seam at dirs, j m size dirs, 1 js fill 0, m js 1 j for i 2 m js i js i 1 dirs i 1, js i 1 end return tuple. 1 m, js end get seam at dirs, 2 function mark path img, path img′ copy img m size img, 2 for i, j in path To make it easier to see, we'll color not just the pixels of the seam, but also those adjacent to it for j′ in j 1 j 1 img′ i, clamp j′, 1, m RGB 1,0,1 end end return img′ end md\"\"\" In the visualization below, the slider specifies which column we start with at the top. The pink seam is the best least total energy that will be snipped out. \"\"\" bind start column Slider 1 size img, 2 , show value true path get seam at dirs, start column function pencil X f x RGB 1 x,1 x,1 x map f, X . maximum X end e edgeness img md\"\"\" Lowest energy path We can use `findmin` to find the path with the least energy \"\"\" function rm path img, path img′ img , 1 end 1 one less column for i, j in path img′ i, 1 j 1 . img i, 1 j 1 img′ i, j end . img i, j 1 end end img′ end function shrink n img, n imgs marked imgs e edgeness img for i 1 n least E, dirs least edgy e , min j findmin view least E 1, seam get seam at dirs, min j img rm path img, seam Recompute the energy for the new image Note, this currently involves rerunning the convolution on the whole image, but in principle the only values that need recomputation are those adjacent to the seam, so there is room for a meanintful speedup here. e edgeness img e rm path e, seam push imgs, img push marked imgs, mark path img, seam end imgs, marked imgs end n examples min 200, size img, 2 returns two vectors of n successively smaller images The second images have markings where the seam is cut out carved, marked carved shrink n img, n examples md\"\"\" Seam carving in action \"\"\" md\"\"\" Here is the algorithm in action. Now the slider tells us on which step of the algorithm we are, having removed each least energy seam at each step \"\"\" md\"\"\" Shrink by bind n Slider 1 length carved show value true \"\"\" md\"\"\" Appendix \"\"\" function downsample img maxheight 100 h,w size img if h maxheight img else img floor. Int,LinRange 1,h,maxheight , floor. Int,LinRange 1,w,floor Int, maxheight w h end end downsample img maxheight 40 function hbox x, y, gap 16 sy size y , sx size x w, h max sx 1 , sy 1 , gap sx 2 sy 2 slate fill RGB 1,1,1 , w,h slate 1 size x,1 , 1 size x,2 . RGB. x slate 1 size y,1 , size x,2 gap . 1 size y,2 . RGB. y slate end function hbox imgs... maxheight 200 g x PlutoUI.ExperimentalLayout.Div downsample x maxheight style Dict \"display\" \"flex\", \"flex\" \"1 0 auto\" PlutoUI.ExperimentalLayout.Div collect map g, imgs style Dict \"display\" \"flex\", \"flex direction\" \"row\", \"aspect ratio\" length imgs size imgs 1 ,2 size imgs 2 ,1 end begin img brightness brightness. img ∇x convolve img brightness, Sx ∇y convolve img brightness, Sy hbox show colored array ∇x , show colored array ∇y end begin edged edgeness img hbox img, pencil edged hbox img, Gray. edgeness img maximum abs. edged end hbox mark path img, path , mark path show colored array least e , path hbox mark path img, path , mark path pencil e , path let least energy path of them all , k findmin least e 1, path get seam at dirs, k hbox mark path img, path , mark path show colored array least e , path end hbox img, marked carved n vbox x... PlutoUI.ExperimentalLayout.vbox collect x size img size carved n "},{"url":"images_abstractions/structure/","title":"Taking Advantage of Structure","tags":["lecture","module1","track_julia","type","programming","matrix","track_data","structure"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v wZrVxbmX218\" image \"https user images.githubusercontent.com 6933510 136196581 ffdf4a3b f35c 4eb8 85a7 f07560bd421b.png\" section 9 order 9 title \"Taking Advantage of Structure\" layout \"layout.jlhtml\" youtube id \"wZrVxbmX218\" description \"\" tags \"lecture\", \"module1\", \"track julia\", \"type\", \"programming\", \"matrix\", \"track data\", \"structure\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using ImageShow.ImageCore using ColorSchemes using InteractiveUtils, PlutoUI using LinearAlgebra, SparseArrays, Statistics end PlutoUI.TableOfContents aside true myonehatvector 0, 1, 0, 0, 0, 0 also one \"cold\" 1 . myonehatvector struct OneHot AbstractVector Int n Int k Int end Base.size x OneHot x.n, Base.getindex x OneHot, i Int Int x.k i md\"\"\" One hot vectors This example comes from machine learning. \"\"\" md\"\"\" A one hot vector has a single \"hot\" element, i.e. a single 1 in a sea of zeros. For example \"\"\" md\"\"\"How much \"information\" numbers do you need to represent a one hot vector? Is it n or is it two? \"\"\" md\"\"\" There are also \"1 cold\" vectors \"\"\" md\"\"\" Julia `structs` creating a new type in Julia \"\"\" md\"\"\" We can create our own new types in Julia. Let's create a new type to represent one hot vectors. It will be a subtype of `AbstractVector`, meaning that it behaves like a vector. \"\"\" md\"\"\" We need to specify how long the vector is \"\"\" md\"\"\" and how to extract the i th component \"\"\" md\"\"\" Note that `x.k i` returns a Boolean value, `true` or `false`, which we are then converting to an `Int`. \"\"\" myonehotvector OneHot 6,2 myonehotvector 3 myonehotvector 2 md\"\"\" This behaves as if it were the original vector, but we are storing only 2 integers. This is an example of taking advantage of structure. \"\"\" md\"\"\" Julia `dump` \"\"\" md\"\"\" `dump` shows the internal data stored inside a given object \"\"\" dump myonehotvector md\"\"\" When used inside our code, however, it behaves and looks exactly like a vector. Pluto also displays it as a vector, with its individual elements. \"\"\" myonehotvector md\"\"\" Visualizing a one hot vector \"\"\" md\"\"\" n bind nn Slider 1 20, show value true \"\"\" md\"\"\" k bind kk Slider 1 nn, default 1, show value true \"\"\" x OneHot nn, kk md\" Diagonal matrices\" md\"Another example is diagonal matrices. Here's how you might see them in high school \" denseD 5 0 0 0 6 0 0 0 10 md\"Julia has a better way of representing them \" D Diagonal denseD md\"\"\" It even displays nicely, with dots instead of zeros. We can also create a diagonal matrix from the values on the diagonal \"\"\" Diagonal 5, 6, 10 md\"How much information is stored for each representation? We can use Julia's `dump` function to find out \" dump denseD dump D md\"\"\" We see that `Diagonal` stores only the diagonal entries, not the zeros \"\"\" md\"\"\"We should always look for structure where it exists \"\"\" md\" Sparse matrices\" md\"A sparse matrix is a matrix that has many zeros, and is hence worth storing in a sparse representation \" denseM 0 0 9 0 0 0 12 0 4 md\"\"\" The above displays a sparse matrix in so called ` i, j, value ` form. We could store sparse matrices in this way \"\"\" M sparse denseM md\"\"\" Although it looks like it's stored like this, in fact the actual storage format is different. In the Julia `SparseArrays.jl` package, the storage format is compressed sparse column CSC format, which is generally considered favorable for arithmetic, matrix vector products and column slicing. Of course, for specific matrices, other formats might be better. `nzval` contains the nonzero matrix entries `rowval` is the \"i\" or row entry for the corresponding value in nzval length rowval length nzval `colptr j ` points into nzval and tells you the first nonzero in or after column j The last entry of colptr points beyond the end of nzval to indicate no more columns. length colptr number of columns 1 \"\"\" dump sparse M md\"\"\" An example where CSC may not be a great choice is the following. The reason is that `colptr` must have an entry in each column \"\"\" M2 sparse 1, 2, 10^6 , 4, 9, 10^6 , 7, 8, 9 dump M2 M3 1 0 2 0 10 0 3 4 0 9 0 0 0 5 8 0 0 0 0 7 M4 M3 . 0 dump sparse M4 sparse M4 md\"\"\" Random vectors \"\"\" md\"How much structure is there in a random vector?\" v rand 1 9, 1 000 000 md\"\"\"You might guess that there is \"no structure\". But you can actually think of randomness itself as a structure. For example, take the mean and standard deviation some would say that's the structure. \"\"\" mean v , std v , 5, sqrt 10 2 3 md\"\"\" If you repeat the calculation, to 3 or 4 digits the mean and standard deviation don't change, and are approximately equal to the theoretical values on the right. \"\"\" md\"\"\" We can also count how many times each digit occurs in the data set \"\"\" sum v . i for i in 1 9 md\"\"\" We see that each number occurs roughly the same number of times. \"\"\" md\"Statisticians and professors who've just graded exams might say that under certain circumstances the mean and the variance give you the necessary structure, and the rest can be thrown away.\" m sum v length v mean σ² sum v . m .^ 2 length v 1 σ sqrt σ² std v md\"Sometimes the summary statistics are all you want. But sometimes not. \" md\" Multiplication tables\" outer v, w x y for x ∈ v, y ∈ w just a multiplication table outer 1 10, 1 10 md\"Did you memorize this in third grade?\" bind k Slider 1 14, show value true outer 1 k, 1 k outer 2,4,6 , 10,100,1000 md\"\"\" A multiplication table is clearly a structure, but it's not sparse there are no zeros. Nonetheless you need much less information to reconstruct the matrix. \"\"\" md\"In the context of 1 k times 1 k, just one number k is needed.\" md\"If you look at the following matrix? Does it have any structure? It's certainly more hidden.\" outer rand 3 , rand 4 but it's just a multiplication table md\"\"\" You might guess by visualizing the matrix that it is a multiplication table \"\"\" md\"We can factor out a multiplication table, if it's there \" md\"\"\" Julia Exceptions are thrown generated using `error` An exception is anything that can interrupt a program, e.g. invalid input data. \"\"\" function factor mult table v mult table , 1 w mult table 1, if v 1 ≠ 0 w v 1 end Good code has a check if outer v, w ≈ mult table return v, w else error \"Input is not a multiplication table\" end end factor outer 1, 2, 3 , 2, 2, 2 md\"\"\" A random 2x2 matrix is not a multiplication table. Most matrices are not given by multiplication tables. \"\"\" factor rand 2,2 md\" Let's add two or more multiplication tables \" A sum outer rand 3 ,rand 3 for i 1 2 md\"Is it possible, given the matrix, to find the structure? E.g. to show that a matrix is a sum of outer products multiplication table .\" md\"The answer is yes The Singular Value Decomposition SVD from algebra can find the structure \" md\"\"\" Let's take the SVD and calculate the sum of two outer products \"\"\" begin U, Σ, V svd A outer U , 1 , V , 1 Σ 1 outer U , 2 , V , 2 Σ 2 end md\"\"\" We see that we reconstruct the original matrix \" \"\"\" B rand 3,3 begin UU, ΣΣ, VV svd B outer UU ,1 , VV ,1 ΣΣ 1 outer UU ,2 , VV ,2 ΣΣ 2 end md\"and it can approximate too \" flag outer 1,1,1,2,2,2,1,1,1 , 1,1,1,1,1,1,1,1,1 cs distinguishable colors 100 cs flag cs flag flag' outer 1,1,1,2,2,2,1,1,1 , 1,1,1,1,1,1,1,1,1 outer 1,1,1,1,1,1,1,1,1 , 1,1,1,2,2,2,1,1,1 cs outer 1,1,1,2,2,2,1,1,1 , 1,1,1,1,1,1,1,1,1 outer 1,1,1,1,1,1,1,1,1 , 1,1,1,2,2,2,1,1,1 md\"\"\" Singular Value Decomposition SVD A tool to find structure \"\"\" tree url \"https user images.githubusercontent.com 6933510 110924885 d7f1b200 8322 11eb 9df7 7abf29c8db7d.png\" image load download tree url picture Float64. channelview image size picture pr, pg, pb eachslice picture, dims 1 RGB. pr, 0, 0 RGB. 0, pg, 0 RGB. 0, 0, pb begin Ur, Σr, Vr svd pr Ug, Σg, Vg svd pg Ub, Σb, Vb svd pb end bind n Slider 1 200, show value true RGB. sum outer Ur ,i , Vr ,i . Σr i for i in 1 n , sum outer Ug ,i , Vg ,i . Σg i for i in 1 n , sum outer Ub ,i , Vb ,i . Σb i for i in 1 n md\" Appendix\" md\" Syntax Learned\" md\"\"\" Syntax to be learned A `struct` is a great way to embody structure. `dump` to see what's inside a data structure. `Diagonal`, `sparse` `error` throws an exception `svd` Singular Value Decomposition \"\"\" dump, Diagonal, error, svd begin show image M get. ColorSchemes.rainbow , M . maximum M show image x AbstractVector show image x' end show image x show image outer rand 10 , rand 10 "},{"url":"images_abstractions/transformations2/","title":"Transformations II: Composability, Linearity and Nonlinearity","tags":["lecture","module1","transformation","track_math","track_julia","inverse","composition","matrix","linear algebra","nonlinear","linear"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v VDPf3RjoCpY\" image \"https user images.githubusercontent.com 6933510 136196619 0750544f cd6d 4ae3 ace7 60c24443d721.png\" section 5 order 5 title \"Transformations II Composability, Linearity and Nonlinearity\" layout \"layout.jlhtml\" youtube id \"VDPf3RjoCpY\" description \"Let's see what mathematical transformations, inverses, composition and non linearity look like in practice. i.e. applied to philip.jpg \" tags \"lecture\", \"module1\", \"transformation\", \"track math\", \"track julia\", \"inverse\", \"composition\", \"matrix\", \"linear algebra\", \"nonlinear\", \"linear\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using PlutoUI using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using PlutoUI using LinearAlgebra using ForwardDiff using NonlinearSolve using StaticArrays end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" urls corgis \"https user images.githubusercontent.com 6933510 108605549 fb28e180 73b4 11eb 8520 7e29db0cc965.png\", longcorgi \"https user images.githubusercontent.com 6933510 110868198 713faa80 82c8 11eb 8264 d69df4509f49.png\", theteam \"https news.mit.edu sites default files styles news article image gallery public images 202004 edelman%2520philip%2520sanders.png?itok ZcYu9NFeg\", img original load download urls.longcorgi md\"\"\" The fun stuff playing with transforms \"\"\" let range 1.5 .1 1.5 md\"\"\" This is a \"scrubbable\" matrix click on the number and drag to change A `` `` bind a Scrubbable range default 1.0 bind b Scrubbable range default 0.0 `` `` `` `` bind c Scrubbable range default 0.0 bind d Scrubbable range default 1.0 `` `` \"\"\" end md\"\"\" Grab a linear a0afe3ae 76b9 11eb 2301 cde7260ddd7f or nonlinear a290d5e2 7a02 11eb 37db 41bf86b1f3b3 transform, or make up your own \"\"\" md\"\"\" zoom bind z Scrubbable .1 .1 3, default 1 \"\"\" md\"\"\" pan bind panx Scrubbable 1 .1 1, default 0 , bind pany Scrubbable 1 .1 1, default 0 \"\"\" md\"\"\" α bind α Slider 30 .1 30, show value true, default 0 β bind β Slider 10 .1 10, show value true, default 5 h bind h Slider .1 .1 10, show value true, default 5 \"\"\" 1 1 md\"\"\" pixels bind pixels Slider 1 1000, default 800, show value true \"\"\" md\"\"\" Show grid lines bind show grid CheckBox default true ngrid bind ngrid Slider 5 5 20, show value true, default 10 \"\"\" md\"\"\" Circular Frame bind circular CheckBox default true radius bind r Slider .1 .1 1, show value true, default 1 \"\"\" md\"\"\" Above The original image is placed in a 1,1 x 1 1 box and transformed. \"\"\" A a b c d md\"\"\" Pedagogical note Why the Module 1 application image processing Image processing is a great way to learn Julia, some linear algebra, and some nonlinear mathematics. We don't presume the audience will become professional image processors, but we do believe that the principles learned transcend so many applications... and everybody loves playing with their own images \"\"\" md\"\"\" Last Lecture Leftovers \"\"\" md\"\"\" Interesting question about linear transformations If a transformation takes lines into lines and preserves the origin, does it have to be linear? Answer no The example of a perspective map takes all lines into lines, but parallelograms generally do not become parallelograms. \"\"\" md\"\"\" A nice interactive demo of perspective maps https www.khanacademy.org humanities renaissance reformation early renaissance1 beginners renaissance florence a linear perspective interactive from Khan academy. \"\"\" Resource \"https cdn.kastatic.org ka perseus images 1b351a3653c1a12f713ec24f443a95516f916136.jpg\" md\"\"\" Challenge exercise Rewrite this using Julia and Pluto \"\"\" md\"\"\" Julia style a little advanced Reminder about defining vector valued functions \"\"\" md\"\"\" Many people find it hard to read ```julia f v v 1 v 2 , v 1 v 2 or f v v 1 v 2 , v 1 v 2 ``` and instead prefer ```julia f x,y x y , x y or f x,y , x y , x y ``` All four of these will take a 2 vector to a 2 vector in the same way for the purposes of this lecture, i.e. `f 1,2 ` can be defined by any of the four forms. The forms with the ` ` are anonymous functions. They are still considered anonymous, even though we then name them `f`. \"\"\" md\"\"\" Functions with parameters The anonymous form comes in handy when one wants a function to depend on a parameter . For example `f α x,y , x αy, x αy ` allows you to apply the `f 7 ` function to the input vector ` 1, 2 ` by running `f 7 1, 2 ` . \"\"\" md\"\"\" Linear transformations a collection \"\"\" md\"\"\" Here are a few useful linear transformations \"\"\" begin id x, y SA x, y scalex α x, y , SA α x, y scaley α x, y , SA x, α y scale α x, y , SA α x, α y swap x, y SA y, x flipy x, y SA x, y rotate θ x, y , SA cos θ x sin θ y, sin θ x cos θ y shear α x, y , SA x α y, y end T⁻¹ id T⁻¹ rotate α T⁻¹ shear α T⁻¹ lin A uses the scrubbable T⁻¹ shear α ∘ shear α T⁻¹ nonlin shear α T⁻¹ inverse nonlin shear α T⁻¹ nonlin shear α T⁻¹ xy T⁻¹ warp α T⁻¹ x,y , x α y^2,y α x^2 may be non invertible T⁻¹ x,y , x,y^2 T⁻¹ flipy ∘ x,y , β x α y β y , h y β y md\"\"\" In fact we can write down the most general linear transformation in one of two ways \"\"\" begin lin a, b, c, d x, y , a x b y, c x d y lin A v A v... linear algebra version using matrix multiplication end md\"\"\" The second version uses the matrix multiplication notation from linear algebra, which means exactly the same as the first version when A \\begin bmatrix a & b \\\\ c & d \\end bmatrix \"\"\" md\"\"\" Nonlinear transformations a collection \"\"\" translate α,β x, y , SA x α, y β affine, but not linear nonlin shear α x, y , SA x, y α x^2 warp α x, y , rotate α √ x^2 y^2 SA x, y xy r, θ SA r cos θ , r sin θ rθ x SA norm x , atan x 2 ,x 1 exponentialish x,y , log x 1.2 , log y 1.2 merc x,y , log x^2 y^2 2 , atan y,x reim log complex y,x md\"\"\" Composition \"\"\" let x rand these two are the same a sin ∘ cos x b sin cos x a b end md\"\"\" Composing functions in mathematics Wikipedia math https en.wikipedia.org wiki Function composition In math we talk about composing two functions to create a new function the function that takes x to \\sin \\cos x is the composition of the sine function and the cosine function. We humans tend to blur the distinction between the sine function and the value of \\sin x at some point x . The sine function is a mathematical object by itself. It's a thing that can be evaluated at as many x 's as you like. If you look at the two sides of ` sin ∘ cos x ≈ sin cos x ` and see that they are exactly the same, it's time to ask yourself what's a key difference? On the left a function is built ` sin ∘ cos ` which is then evaluated at `x`. On the right, that function is never built. \"\"\" md\"\"\" Composing functions in computer science wikipedia cs https en.wikipedia.org wiki Function composition computer science A key issue is a programming language is whether it's easy to name the composition in that language. In Julia one can create the function `sin ∘ cos` and one can readily check that ` sin ∘ cos x ` always yields the same value as `sin cos x `. \"\"\" md\"\"\" Composing functions in Julia Julia's `∘` operator https docs.julialang.org en v1 manual functions Function composition and piping follows the mathematical typography https en.wikipedia.org wiki Function composition Typography convention, as was shown in the `sin ∘ cos` example above. We can type this symbol as `\\circ TAB `. \"\"\" md\"\"\" Composition of software at a higher level The trend these days is to have higher order composition of functionalities. A good example would be that an optimization can wrap a highly complicated program which might include all kinds of solvers, and still run successfully. This might require the ability of the outer software to have some awareness of the inner software. It can be quite magical when two very different pieces of software \"compose\", i.e. work together. Julia's language construction encourages composability. We will discuss this more in a future lecture. \"\"\" md\"\"\" Find your own examples Take some of the Linear and Nonlinear Transformations see the Table of Contents and find some inverses by placing them in the `T ` section of \"The fun stuff\" at the top of this notebook. \"\"\" md\"\"\" Linear transformations can be written in math using matrix multiplication notation as \\begin pmatrix a & b \\\\ c & d \\end pmatrix \\begin pmatrix x \\\\ y \\end pmatrix . \"\"\" md\"\"\" By contrast, here are a few fun functions that cannot be written as matrix times vector. What characterizes the matrix ones from the non matrix ones? \"\"\" md\"\"\" This may be a little fancy, but we see that warp is a rotation, but the rotation depends on the point where it is applied. \"\"\" begin warp₂ α,x,y rotate α √ x^2 y^2 warp₂ α x, y , warp₂ α,x,y x,y end warp3 α x, y , rotate α √ x^2 y^2 x,y warp3 1 1,2 warp 1 5,6 warp₂ 1.0 5.0,6.0 md\"\"\" Linear transformations See a matrix, think beyond number arrays \"\"\" md\"\"\" Software writers and beginning linear algebra students see a matrix and see a lowly table of numbers. We want you to see a linear transformation that's what professional mathematicians see. \"\"\" md\"\"\" What defines a linear transformation? There are a few equivalent ways of giving a definition. \"\"\" md\"\"\" Linear transformation definitions \"\"\" md\"\"\" The intuitive definition The rectangles gridlines in the transformed image above e0b657ce 7a03 11eb 1f9d f32168cb5394 always become a lattice of congruent parallelograms. The easy operational but devoid of intuition definition A transformation is linear if it is defined by v \\mapsto A v matrix times vector for some fixed matrix A . The scaling and adding definition 1. If you scale and then transform or if you transform and then scale, the result is always the same T cv c \\, T v v is any vector, and c any number. 2. If you add and then transform or vice versa the result is the same T v 1 v 2 T v 1 T v 2 . v 1,v 2 are any vectors. The mathematician's definition A consolidation of the above definition. T is linear if T c 1 v 1 c 2 v 2 c 1 T v 1 c 2 T v 2 for all numbers c 1,c 2 and vectors v 1,v 2 . This can be extended to beyond 2 terms. \"\"\" md\"\"\" The Matrix \"\"\" Resource \"https upload.wikimedia.org wikipedia en c c1 The Matrix Poster.jpg\" md\"\"\" No, not that matrix \"\"\" md\"\"\" The matrix for a linear transformation T is easy to write down The first column is T 1, 0 and the second is T 0,1 . That's it \"\"\" md\"\"\" Once we have those, the linearity relation T x,y x \\, T 1,0 y \\, T 0,1 x \\, \\mathrm column\\ 1 y \\, \\mathrm column\\ 2 is exactly the definition of matrix times vector. Try it. \"\"\" md\"\"\" Matrix multiply You know how to do it, but why? \"\"\" md\"\"\" Did you ever ask yourself why matrix multiply has that somewhat complicated multiplying and adding going on? \"\"\" let A randn 2,2 B randn 2,2 v rand 2 lin A ∘ lin B v , lin A B v end md\"\"\" Important The composition of the linear transformation is the linear transformation of the multiplied matrices There is only one definition of matmul matrix multiply that realizes this fact. To see what it is exactly, remember the first column of `lin A ∘ lin B ` should be the result of computing the two matrix times vectors y A 1,0 then z A y , and the second column is the same for 0,1 . This is worth writing out if you have never done this. \"\"\" md\"\"\" Let's try doing that with random matrices \"\"\" begin P randn 2, 2 Q randn 2, 2 T₁ lin P ∘ lin Q T₂ lin P Q lin P Q 1, 0 , lin P ∘lin Q 1, 0 end test img load download urls.corgis test pixels 300 md\"\"\" `lin P Q ` \"\"\" md\"\"\" `lin P ∘lin Q ` \"\"\" md\"\"\" Coordinate transformations vs object transformations \"\"\" md\"\"\" If you want to move an object to the right, the first thing you might think of is adding 1 to the x coordinate of every point. The other thing you could do is to subtract one from the first coordinate of the coordinate system. The latter is an example of a coordinate transform. \"\"\" md\"\"\" Coordinate transform of an array i, j vs points x, y \"\"\" md\"\"\" The original image has 1,1 in the upper left corner as an array but is thought of as existing in the entire plane. \"\"\" translate 400,400 1,1 md\"\"\" Inverses \"\"\" md\"\"\" If f is a function from 2 vectors to 2 vectors say , we define the inverse of f , denoted f^ 1 , to have the property that it \" undoes \" the effect of f , i.e. f f^ 1 v v and f^ 1 f v v . This equation might be true for all v or for some v in a region. \"\"\" md\"\"\" Example Scaling up and down \"\"\" let v rand 2 T rotate 30 ∘rotate 30 T v , v end let T scale 0.5 ∘ scale 2 v rand 2 T v .≈ v end md\"\"\" We observe numerically that `scale 2 ` and `scale .5 ` are mutually inverse transformations, i.e. each is the inverse of the other. \"\"\" md\"\"\" Inverses Solving equations \"\"\" md\"\"\" What does an inverse really do? Let's think about scaling again. Suppose we scale an input vector \\mathbf x by 2 to get an output vector \\mathbf x \\mathbf y 2 \\mathbf x Now suppose that you want to go backwards. If you are given \\mathbf y , how do you find \\mathbf x ? In this particular case we see that \\mathbf x \\frac 1 2 \\mathbf y . If we have a linear transformation, we can write \\mathbf y A \\, \\mathbf x with a matrix A . If we are given \\mathbf y and want to go backwards to find the \\mathbf x from that, we need to solve a system of linear equations . Usually , but not always , we can solve these equations to find a new matrix B such that \\mathbf x B \\, \\mathbf y , i.e. B undoes the effect of A . Then we have \\mathbf x B \\, A \\mathbf x , so that B A must be the identity matrix. We call B the matrix inverse of A , and write B A^ 1 . For 2 \\times 2 matrices we can write down an explicit formula for the matrix inverse, but in general we will need a computer to run an algorithm to find the inverse. \"\"\" md\"\"\" Inverting Linear Transformations \"\"\" let v rand 2 A randn 2,2 lin inv A ∘ lin A v , v end let A randn 2,2 B randn 2,2 inv A B ≈ inv B inv A end md\"\"\" ``A^ 1 \\begin pmatrix d & b \\\\ c & a \\end pmatrix ad bc \\quad `` if ``\\ A \\ \\begin pmatrix a & b \\\\ c & d \\end pmatrix . `` \"\"\" md\"\"\" Inverting nonlinear transformations \"\"\" md\"\"\" What about if we have a nonlinear transformation T can we invert it? In other words, if \\mathbf y T \\mathbf x , can we solve this to find \\mathbf x in terms of \\mathbf y ? In general this is a difficult question Sometimes we can do so analytically, but usually we cannot. Nonetheless, there are numerical methods that can sometimes solve these equations, for example the Newton method https en.wikipedia.org wiki Newton%27s method . There are several implementations of such methods in Julia, e.g. in the NonlinearSolve.jl package https github.com JuliaComputing NonlinearSolve.jl . We have used that to write a function `inverse` that tries to invert nonlinear transformations of our images. \"\"\" md\"\"\" The Big Diagram of Transforming Images \"\"\" Resource \"https raw.githubusercontent.com mitmath 18S191 Spring21 notebooks week3 comm2.png\" md\"\"\" Note that we are defining the map with the inverse of T so we can go pixel by pixel in the result. \"\"\" md\"\"\" Collisions \"\"\" begin function inverse f, y, u0 SVector 0.0, 0.0 prob NonlinearProblem false u, p f u, p . y, u0 solver solve prob, NewtonRaphson , tol 1e 4 return solver.u end inverse f y inverse u, p f SVector u... , y end md\"\"\" Check out Linear Map Wikipedia https en.wikipedia.org wiki Linear map Transformation Matrix Wikipedia https en.wikipedia.org wiki Transformation matrix \"\"\" md\"\"\" Why are we doing this backwards? If one moves the colors forward rather than backwards you have trouble dealing with the discrete pixels. You may have gaps. You may have multiple colors going to the same pixel. An interpolation scheme or a newton scheme could work for going forwards, but very likely care would be neeeded for a satisfying general result. \"\"\" md\"\"\" Appendix \"\"\" det A det A begin white c RGB RGB 1,1,1 white c RGBA RGBA 1,1,1,0.75 black c RGB RGB 0,0,0 black c RGBA RGBA 0,0,0,0.75 end begin function transform xy to ij img AbstractMatrix, x Float64, y Float64 convert coordinate system xy to ij center image, and use \"white\" when out of the boundary rows, cols size img m max cols, rows function to take xy to ij xy to ij translate rows 2, cols 2 ∘ swap ∘ flipy ∘ scale m 2 apply the function and \"snap to grid\" i, j floor. Int, xy to ij x, y end function getpixel img,i Int,j Int circular Bool false, r Real 200 grab image color or place default rows, cols size img m max cols,rows if circular c i rows 2 ^2 j cols 2 ^2 ≤ r m^2 4 else c true end if 1 i ≤ rows && 1 j ≤ cols && c img i, j else white img 1, 1 black img 1,1 end end function getpixel img,x Float64,y Float64 i,j transform xy to ij img,x,y getpixel img,i,j end function transform ij to xy i Int,j Int,pixels ij to xy scale 2 pixels ∘ flipy ∘ swap ∘ translate pixels 2, pixels 2 ij to xy i,j end end begin begin x, y transform ij to xy i,j, test pixels X, Y T₁ x,y i, j transform xy to ij test img,X,Y getpixel test img,i,j end for i 1 test pixels, j 1 test pixels end begin begin x, y transform ij to xy i,j, test pixels X, Y T₂ x,y i, j transform xy to ij test img,X,Y getpixel test img,i,j end for i 1 test pixels, j 1 test pixels end transform ij to xy 1,1,400 function with gridlines img Array Any,2 n 10 n 2n 1 rows, cols size img result copy img stroke zero eltype img RGBA RGB 1,1,1 , 0.75 stroke RGBA 1, 1, 1, 0.75 result floor. Int, LinRange 1, rows, n , . stroke result ceil. Int,LinRange 1, rows, n , . stroke result , floor. Int, LinRange 1, cols, n . stroke result , ceil. Int,LinRange 1, cols, n . stroke result rows ÷2 , . RGBA 0,1,0,1 result 1 rows ÷2 , . RGBA 0,1,0,1 result , cols ÷2 , . RGBA 1,0,0,1 result , 1 cols ÷2 , . RGBA 1,0,0,1 return result end img if show grid with gridlines img original n ngrid else img original end let x, y transform ij to xy i,j, pixels X, Y translate panx, pany x,y X, Y T⁻¹∘scale 1 z ∘translate panx, pany x,y i, j transform xy to ij img,X,Y getpixel img,i,j circular circular, r r end for i 1 pixels, j 1 pixels transform xy to ij img,0.0,0.0 img size img img 50,56 white background x PlutoUI.ExperimentalLayout.Div x style \"background white\" Resource \"https raw.githubusercontent.com mitmath 18S191 Spring21 notebooks week3 coord transform.png\" | white background Resource \"https raw.githubusercontent.com mitmath 18S191 Spring21 notebooks week3 collide.png\" | white background "},{"url":"images_abstractions/transformations_and_autodiff/","title":"Automatic Differentiation","tags":["lecture","module1","track_julia","track_math","programming","function","transformation","automatic differentiation","continuous","derivative"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v AAREeuaKCic\" image \"https user images.githubusercontent.com 6933510 136196632 ad67cb84 a4c9 410e ab72 f4fcfc26f69a.png\" section 3 order 3 title \"Automatic Differentiation\" layout \"layout.jlhtml\" youtube id \"AAREeuaKCic\" description \"We use the package ForwardDiff.jl to automatically differentiate functions, on scalar and vector domains.\" tags \"lecture\", \"module1\", \"track julia\", \"track math\", \"programming\", \"function\", \"transformation\", \"automatic differentiation\", \"continuous\", \"derivative\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using PlutoUI using HypertextLiteral using LinearAlgebra using ForwardDiff end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Last time, recall we defined linear combinations of images. Remember we scaled an image by multiplying by a constant combined images by adding the colors in each pixel possibly saturating In general if we perform both of these operations, we get a linear combination. \"\"\" md\"\"\" Functions in Math and Julia \"\"\" md\"\"\" Univariate Functions \"\"\" md\"\"\" In high school you learned about univariate functions e.g. f₁ x x^2 f₂ x \\sin x f₃ x x^\\alpha In Julia, functions can be written in short form, anonymous form, or long form. \"\"\" short form f₁ x x^2 subscript unicode \\ 1 tab f₁ 5 anonymous form x sin x x sin x π 2 long form function f₃ x,α 3 default parameter return x^α the \"return\" is optional end f₃ 5 f₃ 5,2 md\"\"\" Keyword arguments After the regular positional arguments, a function definition can also accept keyword arguments. In the definition, these come after a semicolon ` ` \"\"\" f₄ x α, β x β ^α md\"\"\" When calling a function with keyword arguments, you use the key \"\"\" f₄ 3, α 5, β 1 the order of keyword arguments does not matter f₄ 3, β 1, α 5 md\"\"\" See Julia's function documentation https docs.julialang.org en v1 manual functions for more. \"\"\" md\"\"\" Automatic Differentiation of Univariates \"\"\" md\"\"\" Automatic differentiation is a key enabling technology for machine learning and so much of scientific computing. It derives the answer in a manner that is interestingly different from the symbolic differentiation of elementary calculus classes and the numerical differentiation of simple finite differences. See the video at the end of this lecture. \"\"\" ForwardDiff.derivative f₁, 5 ForwardDiff.derivative x f₃ x,3 , 5 md\"\"\" Notice the use of anonymous functions to fix the parameter α 3 \"\"\" md\"\"\" In case you have forgotten what a derivative is, we remind you with a simple finite difference approximation \"\"\" begin md\"\"\" bind e Slider 6 1, default 1, show value true \"\"\" end ϵ 10.0^e sin 1 ϵ sin 1 ϵ , cos 1 , ForwardDiff.derivative sin,1 md\"\"\" Scalar Valued Multivariate Functions \"\"\" md\"\"\" Sometimes we are interested in scalar valued functions of more than 1 variable. This can be written in Julia as a function of many variables or a function of a vector. e.g. f 5 x 5\\sin x 1 x 2 2x 2 4x 3 \"\"\" begin f₅ v 5sin v 1 v 2 2 v 2 4v 3 f₅ x,y,z 5sin x y 2 y 4z end f₅ 1,2,3 , f₅ 1,2,3 md\"\"\" Better yet if you must write it the two ways you probably won't need to, but if you must , don't copy code , reuse code so if it changes in one place it changes everywhere. \"\"\" begin f₆ x,y,z 5sin x y 2 y 4z f₆ v f₆ v 1 ,v 2 ,v 3 end f₆ 1,2,3 , f₆ 1,2,3 md\"\"\" There's one other julia idea that is a trick to make vector code more readable. If you give a tuple argument, the function works directly on vectors but is defined with readable letters. \"\"\" f₇ x,y,z 5sin x y 2 y 4z more readable then 5sin v 1 v 2 2 v 2 4v 3 f₇ 1,2,3 this works with vector arguments, but not scalars f₇ 1,2,3 errros md\"\"\" You can see that the functions f 5 and f 6 has two julia methods, one with one variable, and one with three variables. \"\"\" methods f₅ md\"\"\" Automatic Differentiation Scalar valued multivariate functions \"\"\" md\"\"\" In many applications, including machine learning, one needs to take derivatives of the function in every argument direction. This is known as the gradient . Automatic differentiation works again \"\"\" ForwardDiff.gradient f₅, 1,2,3 md\"\"\" Remember f 5 x 5\\sin x 1 x 2 2x 2 4x 3 \"\"\" md\"\"\" One can check numerically by adding a small change to each of the arguments.m \"\"\" begin ∂f₅∂x f₅ 1 ϵ, 2, 3 f₅ 1, 2, 3 ϵ ∂f₅∂y f₅ 1, 2 ϵ, 3 f₅ 1, 2, 3 ϵ ∂f₅∂z f₅ 1, 2, 3 ϵ f₅ 1, 2, 3 ϵ ∇f ∂f₅∂x , ∂f₅∂y, ∂f₅∂z end md\"\"\" Whether you are an expert at multivariable calculus, or you have never seen this before, I hope seeing it numerically makes the idea intuitive. \"\"\" md\"\"\" Important Remark In machine learning, and other optimization contexts, we want to minimize a scalar function of many parameters known as a \"loss function.\" Following the negative gradient is a standard technique for minimizing functions especially when there are many variables. When there are only a few variables, there are better techniques. \"\"\" md\"\"\" Transformations Vector Valued Multivariate Functions \"\"\" md\"\"\" While scalar functions might technically be called a transformation, it is more common to use the term when both the input and output are multidimensional. \"\"\" md\"\"\" Let us consider a few functions that take in a vector of size 2 and returns a vector of size 2. \"\"\" begin idy x,y x,y lin1 x,y 2x 3y, 5x 4x scalex α x,y , α x, y scaley α x,y , x, α y rot θ x,y , cos θ x sin θ y, sin θ x cos θ y shear α x,y , x α y,y genlin a,b,c,d x,y , a x b y c x d y end rot π 2 4,5 md\"\"\" We bet you have noticed that these functions could all have been defined with matrices. Indeed the general case can be written \"\"\" a b c d x,y md\"\"\" or in math `` \\begin pmatrix a & b \\\\ c & d \\end pmatrix \\begin pmatrix x \\\\ y \\end pmatrix `` . \"\"\" md\"\"\" By contrast here are a few fun functions that can not be written as matrix times vector. What characterizes the matrix ones from the non matrix ones? \"\"\" begin function warp α x,y , begin r √ x^2 y^2 θ α r rot θ x,y end end rθ x norm x , atan x 2 ,x 1 maybe vectors are more readable here? xy r,θ r cos θ , r sin θ end md\"\"\" This may be a little fancy, but we see that warp is a rotation, but the rotation depends on the point where it is applied. \"\"\" begin warp₂ α,x,y rot α √ x^2 y^2 warp₂ α x,y , warp₂ α,x,y x,y end warp 1 5,6 warp₂ 1.0 5.0,6.0 md\"\"\" Automatic Differentiation of Transformations \"\"\" ForwardDiff.jacobian warp 3.0 , 4,5 md\"\"\" What is this thing? \"\"\" begin ∂w∂x warp 3.0 4 ϵ, 5 warp 3.0 4,5 ϵ This is a vector, right? ∂w∂y warp 3.0 4, 5 ϵ warp 3.0 4,5 ϵ This too ∂w∂x ∂w∂y end md\"\"\" But what is a transformation, really? You have very likely learned how to multiply matrices times vectors. I'll bet you think of a matrix as a table of numbers, and a vector as a column of numbers, and if you are well practiced, you know just when to multiply and just when to add. Congratulations, you now can do what computers excel at. \"\"\" md\"\"\" Matrices are often thought of as containers of numbers in a rectangular array, and hence one thinks of manipulating these tables like a spreadsheet, but actually the deeper meaning is that it is a transformation. \"\"\" md\"\"\" α bind α Slider .1 .1 3, show value true \"\"\" let range 1.5 .1 1.5 md\"\"\" This is a \"scrubbable matrix\" click on the number and drag to change. `` `` bind a Scrubbable range default 1.0 bind b Scrubbable range default 0.0 `` `` `` `` bind c Scrubbable range default 0.0 bind d Scrubbable range default 1.0 `` `` Re run this cell to reset to identity transformation \"\"\" end T shear 1 Pick a transformation T genlin a,b,c,d md\"\"\" center zoom bind z Slider .1 .1 3, show value true, default 1 \"\"\" md\"\"\" top left zoom bind f Slider .1 1 3, show value true, default 1 \"\"\" md\"Show grid lines bind show grid CheckBox default true \" A a b c d md\" Det A a d c b det A \" md\"\"\" We never seem to see this in linear algebra classes do we? Check out Linear Map Wikipedia https en.wikipedia.org wiki Linear map Transformation Matrix Wikipedia https en.wikipedia.org wiki Transformation matrix \"\"\" md\"\"\" Geometry of determinant, how areas scale. \"\"\" md\"\"\" Why are we doing this backwards? \"\"\" md\"\"\" Computer Science Solving 2 equations in 2 unknowns, and higher dimensional analogs. The top 500 supercomputers, and how many equations in how many unknowns are being solved today. \"\"\" md\"\"\" Automatic Differentiation in 10 minutes okay, 11 \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid vAp6nUMrKYg params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" What about more than 3 dimensions? Oh I've heard it all, there is no such thing as more than 3 dimensions. The fourth dimension is time, isn't it? I have trouble visualizing 3 dimensions, let alone contemplating 4. ... but mathematicians have no trouble with n spatial dimensions, and they do not usually worry about silly questions like whether these exist. \"\"\" md\"\"\" Appendix \"\"\" det A det A img sources \"https user images.githubusercontent.com 6933510 108605549 fb28e180 73b4 11eb 8520 7e29db0cc965.png\" \"Corgis\", \"https user images.githubusercontent.com 6933510 108883855 39690f80 7606 11eb 8eb1 e595c6c8d829.png\" \"Arrows\", \"https images.squarespace cdn.com content v1 5cb62a904d546e33119fa495 1589302981165 HHQ2A4JI07C43294HVPD ke17ZwdGBToddI8pDm48kA7bHnZXCqgRu4g0 U7hbNpZw zPPgdn4jUwVcJE1ZvWQUxwkmyExglNqGp0IvTJZamWLI2zvYWH8K3 s 4yszcp2ryTI0HqTOaaUohrI8PISCdr 3EAHMyS8K84wLA7X0UZoBreocI4zSJRMe1GOxcKMshLAGzx4R3EDFOm1kBS fluffy corgi?format 2500w\" \"Long Corgi\" md\"\"\" Choose an image bind img source Select img sources \"\"\" img original load download img source typeof img original begin white c RGB RGB 1,1,1 white c RGBA RGBA 1,1,1,0.75 end function trygetpixel img AbstractMatrix, x Float64, y Float64 rows, cols size img \"The linear map 1,1 ↦ 0,1 \" f t t 1.0 1.0 1.0 i floor Int, rows f y z j floor Int, cols f x rows cols z if 1 i ≤ rows && 1 j ≤ cols img i,j else white img 1,1 end end function with gridlines img Array Any,2 n 16 sep i size img, 1 ÷ n sep j size img, 2 ÷ n result copy img stroke zero eltype img RGBA RGB 1,1,1 , 0.75 stroke RGBA 1, 1, 1, 0.75 result 1 sep i end, . stroke result , 1 sep j end . stroke a second time, to create a line 2 pixels wide result 2 sep i end, . stroke result , 2 sep j end . stroke result sep i n ÷2 . 1,2 , . RGBA 0,1,0,1 result , sep j n ÷2 . 1,2 , . RGBA 1,0,0,1 return result end img if show grid with gridlines img original else img original end if det A 0 RGB 1.0, 1.0, 1.0 else in x, in y A \\ out x, out y in x, in y xy out x, out y in x, in y T out x, out y trygetpixel img, in x, in y end for out y in LinRange f, f, 500 , out x in LinRange f, f, 500 "},{"url":"images_abstractions/transforming_images/","title":"Transformations with Images","tags":["lecture","module1","image","convolution","track_math","matrix","philip"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v uZYVjDDZW9A\" image \"https user images.githubusercontent.com 6933510 136196626 194e81c9 00f7 49f6 90c3 09945723b6a3.png\" section 4 order 4 title \"Transformations with Images\" layout \"layout.jlhtml\" youtube id \"uZYVjDDZW9A\" description \"\" tags \"lecture\", \"module1\", \"image\", \"convolution\", \"track math\", \"matrix\", \"philip\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using PlutoUI using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using Unitful using ImageFiltering using OffsetArrays using Plots end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" rotabook PlutoUI.Resource \"https i.gr assets.com images S compressed.photo.goodreads.com books 1348902666l 1646354.jpg\" md\"\"\" Never run overtime a microcentury with UnitFul Running overtime is the one unforgivable error a lecturer can make. After fifty minutes one microcentury as von Neumann used to say everybody's attention will turn elsewhere even if we are trying to prove the Riemann hypothesis. One minute overtime can destroy the best of lectures. from \"Indiscrete Thoughts\" by Rota, Chpt 18, 10 Lessons I Wish I Had Been Taught \"\"\" century 100u\"yr\" a u\"yr\" is a special kind of string denoting a unit of a year century 2 century 200 century^2 g 9.8u\"m\" u\"s\"^2 uconvert u\"minute\", century 1e 6 convert into minutes the value of a microcentury PotentialEnergy 10u\"kg\" g 50u\"m\" uconvert u\"J\",PotentialEnergy md\"\"\" Adding units to numbers just works in Julia, and furthermore, does not slow down execution. We are sneaking in an example of the power of generic programming and Julia's type system, some of the underlying technology that makes us love working with Julia. More on this later in the book. Meanwhile if this helps you do your problem sets in some other class, go for it. \"\"\" md\"\"\" info \"Reminder\" Try your own pictures everywhere \"\"\" md\"\"\" Transforming Images \"\"\" md\"\"\" 2.1. Downsampling Upsampling \"\"\" md\"\"\" How can we pixelate a corgi? Found this cute picture online, but we'll pixelate a real corgi. \"\"\" pixelated corgi load download \"https i.redd.it 99lhfbnwpgd31.png\" philip load download \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" bind r Slider 1 40, show value true, default 40 downsample philip philip 1 r end, 1 r end upsample philip kron downsample philip, fill 1,r,r md\"\"\" Note the use of kron and fill. See Wikipedia Kron https en.wikipedia.org wiki Kronecker product \"\"\" md\"\"\" Exercise Use the nose selection tool from Section 1.1 to pixelate a rectangle of an image. Warning you'll have to worry about sizes if not exact multiples. \"\"\" md\"\"\" 2.2 Linear Combinations Combining Images \"\"\" md\"\"\" One big idea in mathematics is the linear combination https en.wikipedia.org wiki Linear combination . The idea combines scaling an object combining two or more objects by combining scaled versions of multiple objects. \"\"\" md\"\"\" Let's scale some corgis. \"\"\" corgis load download \"https user images.githubusercontent.com 6933510 108605549 fb28e180 73b4 11eb 8520 7e29db0cc965.png\" bind c Slider 0 .1 3, show value true, default 1 c . corgis scaling the corgis changes intensity md\"\"\" You might wonder about the dot times or pointwise times . You can delete the dot, but it is recommended for clarity and performance. The dot emphasizes that the multiplication by c is happening pixel by pixel or that the scalar is being \"broadcast\" to every pixel. \"\"\" md\"\"\" Scaling too far saturates the image. Any r,g,b ≥ 1, saturates at 1. \"\"\" md\"\"\" We need another image. We could grab one from somewhere or we can just transform the one we have. Let's do the latter and turn the corgis upsidedown. \"\"\" upsidedown corgis corgis end 1 1 , md\"\"\" Now let's scaled version of the two images to see what that does. \"\"\" .5 upsidedown corgis . .5 corgis md\"\"\" Convex Combinations \"\"\" md\"\"\" If all the coefficients are positive and add to 1, we say we have a convex combination . Let's take α and 1 α as the two coefficients adding to 1, and scale the two corgi pictures with different α's, thereby giving different weights to the rightside up and upside down corgis. \"\"\" bind α Slider 0 .01 1 , show value true, default 1.0 α . corgis . 1 α . upsidedown corgis md\"\"\" The moment I did this with α .5, I noticed my brain's tendency to see the rightsisde up corgis even though both have equal weight. For me maybe around α .39 which gives weight .61 to the upside down corgis \"feels\" balanced to me. I think this is what the field of psychology called psychometrics tries to measure perhaps someone can tell me if there are studies of the brain's tendency to use world experience to prefer rightside up corgis, and in particular to put a numerical value to this tendency. \"\"\" md\"\"\" 10 seconds with google and I found there is a thing about faces The Face Inversion effect https en.wikipedia.org wiki Face inversion effect ~ text The%20face%20inversion%20effect%20is,same%20for%20non%2Dfacial%20objects.&text The%20most%20supported%20explanation%20for,is%20the%20configural%20information%20hypothesis and also the Thatcher Effect https en.wikipedia.org wiki Thatcher effect ~ text The%20Thatcher%20effect%20or%20Thatcher,obvious%20in%20an%20upright%20face seems related. ... the article suggests objects don't suffer in the same way as faces, so I put forth that the phenomenon applies to corgi faces as much as human faces, suggesting maybe that corgi faces are processed in the face processing part of the brain,not the object processing part of the brain. Corgis are human, after all, right? Note, this is 5 minutes of armchair science, not a professional opinion. \"\"\" md\"\"\" 2.3 Fun with Photoshop What does \"filter\" mean in this context? \"\"\" md\"\"\" Photshop Filter Reference https helpx.adobe.com photoshop using filter effects reference.html \"\"\" md\"\"\" Let's play with photoshop if for no other reason, let's see what image transformations are available considered useful by the pros. Some worth emphasizing are 1. Blur 2. Sharpen 3. Stylize Find Edges 3. Pixelate 4. Distort Some of these transformations e.g. Blur, Sharpen, Find Edges are examples of convolutions which are very efficient, and show up these days in machine learning particularly in image recognition. \"\"\" md\"\"\" 2.4 Image Filtering convolutions \"\"\" md\"\"\" Last semester Grant Sanderson 3Blue1Brown lectured in this course. This lecture on convolutions in image processing was popular. Let's watch an excerpt from 1 04 to 2 48 . We pick a few exercepts, but we wouldn't blame you if you just wanted to watch the whole video. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid 8rrHTtUzyZA params \"modestbranding 1&rel 0&start 64&end 168\" lite youtube \"\"\" md\"\"\" Definition of convolutions and kernels \"\"\" md\"\"\" Wikipedia Page on Kernels https en.wikipedia.org wiki Kernel image processing Details \"\"\" md\"\"\" Computer Science Complexity The number of multiplications Number of Pixels in the Image Number of Cells in the kernel \"\"\" md\"\"\" Thought Problem Why are small kernels better than large kernels from a complexity viewpoint? \"\"\" md\"\"\" Computer Science Architectures GPUs or Graphical Processing Units Some important computations can be greatly accelerated through the use of specialized hardware such as the GPU processors that were originally designed as image renderers but it has turned out that these processors can be quite fast at other very regular computations. Convolutions is a very GPU friendly operation due to its regular structure. \"\"\" kernelize M OffsetArray M, 1 1, 1 1 begin identity 0 0 0 0 1 0 0 0 0 edge detect 0 1 0 1 4 1 0 1 0 sharpen identity . edge detect Superposition box blur 1 1 1 1 1 1 1 1 1 9 ∇x 1 0 1 1 0 1 1 0 1 2 centered deriv in x ∇y ∇x' kernels identity, edge detect, sharpen, box blur, ∇x, ∇y kernel keys \"identity\", \"edge detect\", \"sharpen\", \"box blur\", \"∇x\", \"∇y\" selections kernel keys . kernel keys kernel matrix Dict kernel keys . kernels md\" bind kernel name Select selections \" end kernel matrix kernel name imfilter corgis, kernelize kernel matrix kernel name Gray. 1.5 . abs. imfilter corgis, kernelize kernel matrix kernel name md\"\"\" Gaussian Filter \"\"\" md\"\"\" In our next Grant Sanderson segment from Fall 2020 4 35 to 7 00 , we hear about convolving images with a Gaussian kernel. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid 8rrHTtUzyZA params \"modestbranding 1&rel 0&start 275&end 420\" lite youtube \"\"\" round. Kernel.gaussian 1 , digits 3 md\"\"\" We could have defined this ourselves with calls to the exponential function. \"\"\" begin G exp i^2 j^2 2 for i 2 2, j 2 2 round. G . sum G , digits 3 end md\"\"\" This is often known as Gaussian blur to emphasize the result of this operation. Adobe on Gaussian blur https www.adobe.com creativecloud photography discover gaussian blur.html . \"\"\" md\"\"\" Focus around 5 23 \"\"\" bind gparam Slider 0 9, show value true, default 1 kernel Kernel.gaussian gparam plotly surface kernel md\"\"\" Note black lines are contours \"\"\" md\"\"\" Computer Science Data Structure Offset Arrays \"\"\" kernel 0,0 kernel 2,2 M 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Z OffsetArray M, 1 1, 2 2 the indices c.I for c ∈ CartesianIndices Z Z 1, 2 md\"\"\" 2.5. Discrete vs Continuous \"\"\" md\"\"\" Some folks only like discrete objects, others continuous. The computer makes clear what many mathematicians already know, that while different language has evolved to describe discrete objects vs continuous objects, often the underlying conceptual idea is similar or the same. Here is one analogy \"\"\" md\"\"\" Blurring Kernels Integrals ≡ Sharpening Kernels Derivatives \"\"\" md\"\"\" Think about integrals vs derivatives in one dimension. If you replace f x with g x ∫ f t dt for x r ≤ t ≤ x r, that will blur or smooth out the features of f. However if you take the derivative,you will emphasize the changes, i.e., you will sharpen or \"edge detect.\" \"\"\" md\"\"\" 2.6 Respect my Boundaries \"\"\" md\"\"\" Applying the convolution on a boundary requires special thought because it is literally an edge case . Once again Grant said this so very well 2 53 4 19 \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid 8rrHTtUzyZA params \"modestbranding 1&rel 0&start 173&end 259\" lite youtube \"\"\" "},{"url":"week0/basic_syntax/","title":"basic_syntax","tags":[],"text":" A Pluto.jl notebook v0.19.45 using Markdown using InteractiveUtils md\" Get started with Julia live Before being able to run this notebook successfully locally, you will need to set up Julia and Pluto. Spring21 installation \" md\" Variables We can define a variable using ` ` assignment . Then we can use its value in other expressions \" x 3 y 2x md\"By default Julia displays the output of the last operation. You can suppress the output by adding ` ` a semicolon at the end. \" md\"We can ask what type a variable has using `typeof` \" typeof y md\" Functions\" md\"We can use a short form, one line function definition for simple functions \" f x 2 x md\"Typing the function's name gives information about the function. To call it we must use parentheses \" f f 10 md\"For longer functions we use the following syntax with the `function` keyword and `end` \" function g x, y z x y return z^2 end g 1, 2 md\" For loops\" md\"Use `for` to loop through a pre determined set of values \" let s 0 for i in 1 10 s i Equivalent to s s i end s end md\"Here, `1 10` is a range representing the numbers from 1 to 10 \" typeof 1 10 md\"Above we used a `let` block to define a new local variable `s`. But blocks of code like this are usually better inside functions, so that they can be reused. For example, we could rewrite the above as follows \" function mysum n s 0 for i in 1 n s i end return s end mysum 100 md\" Conditionals `if`\" md\"We can evaluate whether a condition is true or not by simply writing the condition \" a 3 a 5 md\"We see that conditions have a Boolean `true` or `false` value. We can then use `if` to control what we do based on that value \" if a 5 \"small\" else \"big\" end md\"\"\"Note that the `if` also returns the last value that was evaluated, in this case the string `\"small\"` or `\"big\"`, Since Pluto is reactive, changing the definition of `a` above will automatically cause this to be reevaluated \"\"\" md\" Arrays\" md\" 1D arrays `Vector`s \" md\"We can make a `Vector` 1 dimensional, or 1D array using square brackets \" v 1, 2, 3 typeof v md\"The `1` in the type shows that this is a 1D array. We access elements also using square brackets \" v 2 v 2 10 md\"Note that Pluto does not automatically update cells when you modify elements of an array, but the value does change.\" md\"A nice way to create `Vector`s following a certain pattern is to use an array comprehension \" v2 i^2 for i in 1 10 md\" 2D arrays matrices \" md\"We can make small matrices 2D arrays with square brackets too \" M 1 2 3 4 typeof M md\"The `2` in the type confirms that this is a 2D array.\" md\"This won't work for larger matrices, though. For that we can use e.g.\" zeros 5, 5 md\"Note that `zeros` gives `Float64`s by default. We can also specify a type for the elements \" zeros Int, 4, 5 md\"We can then fill in the values we want by manipulating the elements, e.g. with a `for` loop.\" md\"A nice alternative syntax to create matrices following a certain pattern is an array comprehension with a double `for` loop \" i j for i in 1 5, j in 1 6 "},{"url":"week0/plots_tutorial/","title":"plots_tutorial","tags":[],"text":" A Pluto.jl notebook v0.19.45 using Markdown using InteractiveUtils using Plots using SpecialFunctions using VegaLite, VegaDatasets using DataFrames using PlutoUI md\"\"\" Resources Plots Docs Useful Tips https docs.juliaplots.org latest basics Useful Tips Plots Docs Examples https docs.juliaplots.org latest generated gr Plot Attributes http docs.juliaplots.org latest generated attributes plot Axis Attributes http docs.juliaplots.org latest generated attributes axis Axis Color Names http juliagraphics.github.io Colors.jl stable namedcolors \"\"\" md\"\"\" Loading Time Plots loading time sucks big time. This is a known problem. We know, we know, Julia is fast, just grab some coffee .... ☕☕☕☕☕☕☕☕☕ \"\"\" md\"\"\" Hello World \"\"\" plot rand 4 md\"\"\" Adding Stuff \"\"\" begin plot rand 4 scatter rand 4 end md\"\"\" Removing Stuff \"\"\" plot legend false, axis false, grid false, ticks false md\"\"\" Lines and points \"\"\" md\"\"\" Square Aspect Ratio \"\"\" begin v rand 4 plot v, ratio 1, legend false scatter v end md\"\"\" Matrices with color heatmap yflip true places the 2,1 entry where you want it, since you want to interchange xy with ij and i runs down and y runs up. \"\"\" A 1 1000 1 1 1 1 1 1 1 heatmap A, ratio 1, yflip true, legend false, axis false, grid false, ticks false heatmap rand 10,10 , clim 0,1 , ratio 1, legend false, axis false, ticks false begin MM 0 1 0 0 0 0 1 0 0 whiteblack RGBA 1,1,1,0 , RGB 0,0,0 heatmap c whiteblack, MM, aspect ratio 1, ticks .5 3.5, lims .5,3.5 , gridalpha 1, legend false, axis false, ylabel \"i\", xlabel \"j\" end begin p plot 1 4,guideposition top end begin p.attr foreground color RGB 1,0,0 p.attr foreground color end p begin q p.series list 1 .plotattributes q.explicit linecolor green end p md\"\"\" Colors \"\"\" Colors.color names mycolors colorant\"lightslateblue\",colorant\"limegreen\",colorant\"red\" begin AA i for i 50 300, j 1 100 heatmap AA, c mycolors, clim 1,300 end md\"\"\" Area under curves \"\"\" begin y rand 10 plot y, fillrange y. 0 . .5, label \"above below 1 2\", legend topleft end begin x LinRange 0,2,100 y1 exp. x y2 exp. 1.3 . x plot x, y1, fillrange y2, fillalpha 0.35, c 1, label \"Confidence band\", legend topleft end let x 3 .01 3 areaplot x, exp. x.^2 2 √ 2π ,alpha .25,legend false end begin M 1 2 3 7 8 9 4 5 6 0 .5 1.5 areaplot 1 3, M, seriescolor red green blue , fillalpha 0.2 0.3 0.4 end let f x exp x^2 2 √ 2π δ .01 plot x √2 . erfinv. 2 . δ 2 δ 1 . 1 areaplot x, f. x , seriescolor red, blue , legend false plot f, x,c black end md\"\"\" Shapes \"\"\" begin rectangle w, h, x, y Shape x . 0,w,w,0 , y . 0,0,h,h circle r,x,y θ LinRange 0,2π,500 x. r. cos. θ , y. r. sin. θ end begin plot circle 5,0,0 , ratio 1, c red, fill true plot rectangle 5 √2,5 √2, 2.5 √2, 2.5 √2 ,c white,fill true,legend false end md\"\"\" Editing Plots manually \"\"\" pl plot 1 4, 1, 4, 9, 16 pl.attr pl.series list 1 pl size 300,200 pl md\"\"\" VegaLite \"\"\" vlplot point, rand 10 , rand 10 begin dataset \"zipcodes\" | vlplot circle, width 500, height 300, transform calculate \"substring datum.zip code, 0, 1 \", as digit , projection type albersUsa , longitude longitude, latitude latitude, size value 1 , color \"digit n\" end d DataFrame dataset \"zipcodes\" scatter d , longitude , d , latitude , m ., ms 1, xlim 150, 50 , ylim 20,55 begin struct RankOneMatrix T v AbstractVector T w AbstractVector T end end RankOneMatrix rand 3 , rand 3 methods RankOneMatrix begin xx .1 .1 1 plot xx.^2, xaxis log, yaxis log end begin plot exp. x , yaxis log end TableOfContents "}] \ No newline at end of file +[{"url":"cheatsheets/","title":"Cheatsheets","tags":["welcome"],"text":"Cheatsheets Getting Started with Julia - live . Fastrack to Julia cheatsheet. MATLAB-Julia-Python comparative cheatsheet by QuantEcon group Plots.jl cheatsheet"},{"url":"clips/","title":"clips","tags":[],"text":"Submit A Short Clip Did you have a favorite (or many favorite) 1-2 minute clips from one of the lectures in this course? Consider sending them to us and that way we can share them on Twitter so more people can get exposure to computational thinking. Use this form to upload clips."},{"url":".","title":"index","tags":["homepage"],"text":""},{"url":"installation/","title":"Software installation","tags":["welcome"],"text":"First-time setup: Install Julia & Pluto Video version: Text and pictures version: Step 1: Install Julia 1.10.4 Go to https://julialang.org/downloads and download the current stable release, Julia 1.10.4 , using the correct version for your operating system (Linux x86, Mac, Windows, etc). Step 2: Run Julia After installing, make sure that you can run Julia . On some systems, this means searching for the “Julia 1.10.4 ” program installed on your computer; in others, it means running the command julia in a terminal. Make sure that you can execute 1 + 1 : Make sure that you are able to launch Julia and calculate 1+1 before proceeding! Step 3: Install Pluto Next we will install the Pluto , the notebook environment that we will be using during the course. Pluto is a Julia programming environment designed for interactivity and quick experiments. Open the Julia REPL . This is the command-line interface to Julia, similar to the previous screenshot. Here you type Julia commands , and when you press ENTER, it runs, and you see the result. To install Pluto, we want to run a package manager command . To switch from Julia mode to Pkg mode, type ] (closing square bracket) at the julia> prompt: \njulia> ]\n\n(@v 1.10 ) pkg>\n The line turns blue and the prompt changes to pkg> , telling you that you are now in package manager mode . This mode allows you to do operations on packages (also called libraries). To install Pluto, run the following (case sensitive) command to add (install) the package to your system by downloading it from the internet.\nYou should only need to do this once for each installation of Julia: \n(@v 1.10 ) pkg> add Pluto\n This might take a couple of minutes, so you can go get yourself a cup of tea! You can now close the terminal. Step 4: Use a modern browser: Mozilla Firefox or Google Chrome We need a modern browser to view Pluto notebooks with. Firefox and Chrome work best. Second time: Running Pluto & opening a notebook Repeat the following steps whenever you want to work on a project or homework assignment. Step 1: Start Pluto Start the Julia REPL, like you did during the setup. In the REPL, type: julia> using Pluto\n\njulia> Pluto.run()\n The terminal tells us to go to http://localhost:1234/ (or a similar URL). Let’s open Firefox or Chrome and type that into the address bar. If you’re curious about what a Pluto notebook looks like, have a look at the Featured Notebooks . These notebooks are useful for learning some basics of Julia programming. If you want to hear the story behind Pluto, have a look a the JuliaCon presentation . If nothing happens in the browser the first time, close Julia and try again. And please let us know! Step 2a: Opening a notebook from the web This is the main menu - here you can create new notebooks, or open existing ones. Our homework assignments will always be based on a template notebook , available in this GitHub repository. To start from a template notebook on the web, you can paste the URL into the blue box and press ENTER. For example, homework 0 is available here . Go to this page, and on the top right, click on the button that says “Edit or run this notebook”. From these instructions, copy the notebook link, and paste it into the box. Press ENTER, and select OK in the confirmation box. The first thing we will want to do is to save the notebook somewhere on our own computer; see below. Step 2b: Opening an existing notebook file When you launch Pluto for the second time, your recent notebooks will appear in the main menu. You can click on them to continue where you left off. If you want to run a local notebook file that you have not opened before, then you need to enter its full path into the blue box in the main menu. More on finding full paths in step 3. Step 3: Saving a notebook We first need a folder to save our homework in. Open your file explorer and create one. Next, we need to know the absolute path of that folder. Here’s how you do that in Windows , MacOS and Ubuntu . For example, you might have: C:\\Users\\fons\\Documents\\18S191_assignments\\ on Windows /Users/fons/Documents/18S191_assignments/ on MacOS /home/fons/Documents/18S191_assignments/ on Ubuntu Now that we know the absolute path, go back to your Pluto notebook, and at the top of the page, click on “Save notebook…” . This is where you type the new path+filename for your notebook : Click Choose . Step 4: Sharing a notebook After working on your notebook (your code is autosaved when you run it), you will find your notebook file in the folder we created in step 3. This the file that you can share with others, or submit as your homework assignment to Canvas. \nconst run = f => f();\nrun(async () => {\nconst versions = await (await fetch(`https://julialang-s3.julialang.org/bin/versions.json`)).json()\nconst sortby = v => v.split(\"-\")[0].split(\".\").map(parseFloat).reduce((a,b) => a*10000 + b)\nconst version_names = Object.keys(versions).sort((a,b) => sortby(a) - sortby(b)).reverse()\nconst stable = version_names.find(v => versions[v].stable)\nconsole.log({stable})\nconst pkg_stable = /\\d+\\.\\d+/.exec(stable)[0]\ndocument.querySelectorAll(\"auto-julia-version\").forEach(el => {\n console.log(el)\n el.innerText = el.getAttribute(\"short\") == null ? stable : pkg_stable\n})\n});"},{"url":"logistics/","title":"Class logistics","tags":["welcome"],"text":"main a img {\n width: 5rem;\n margin: 1rem;\n}\n Course logistics MIT Students Only Suggestion : Bookmark this page for easy access to everything you need to follow this class! Counts as 6.0001 and 6.0002 credit, or can be taken in addition. Counts as a course\n6 elective. Course materials will be published on this website on Monday 1:00pm . Each week is a new chapter , which includes: Asynchronous video lectures (total 60 minutes) Interactive visualizations Exercises Live lectures On Monday 1:00pm - 2:30pm , after the material is published, there will also be: Q&A on Discord Live overview lecture (30 minutes) On Wednesday 1:00pm 2:30pm (MIT students only) , you will meet with fellow students and your TA to: Review the lecture Work on problem sets in small groups or individually, with the opportunity to ask questions to your TA Start date: February 16, 2021 Discussion forum and homework submission Discord : discussion (we encourage you to hang out here during class!) MIT Only: Canvas : Homework submissions, Piazza Link. If you’re a non-MIT student, don’t worry, the homework has built-in answers checks , or you can find a partner to cross-grade homeworks via Discord. Evaluation The final grade is 80% problem sets, and 20% MITx quick questions . Problem sets are released on Friday and due the following Friday by 11:59pm EST. They have equal weight; your lowest score will be dropped. MITx exercises ( quick questions ) are due before Wednesday (11:59pm), but are best done on Monday, during or right after the lectures."},{"url":"reviews/","title":"Class reviews","tags":["welcome"],"text":"Student feedback Applications Feedback\n Spring 2020\n The Introduction to Computational Thinking with Julia class is a welcome departure from the paradigm of teaching the canonical computer science examples such as sorting algorithms and graph search that are often overused in introductory curricula. The class delves into real-world applications from the very beginning in a way that gives students an opportunity to be excited about the possibilities of computer science and mathematical modelling all while learning how to harness the power and elegance of the modern language of Julia. Class Feedback\n Spring 2020\n This is one of the best classes I have ever taken. I like how the content is divided into four main real-world applications of computational thinking, which made learning very enjoyable and also made working with Julia easier for me. I also found it amazing that this class provided applications of topics in differential equations and linear algebra classes and added a new way to view them. Unfortunately, I haven't found a similar class on computational thinking for the spring as the skills I gain are very practical and needed in research (I was impressed by the number of MISTI research opportunities that asked for experience with agent-based modeling or modeling in general.) Website Feedback\n Spring 2020\n The class website made the class lectures and homework easy to find in addition to the GitHub page and canvas. - The Discord channel and Piazza also made it easier to ask questions and see other students' questions and comments. - The feedback for assignments was very clear, and the instructors were willing to explain my mistakes further by email. Lecture Feedback\n Spring 2020\n The synchronous lectures were very comfortable, and the instructors encouraged us to ask questions. What other people are saying MIT's Free Online Course to Learn Julia — The Rising Star — Review of 18.S191 by Towards AI Newsletter the course is exceptional🎇🎇🎇 — YT Cai (@Yitao_CAI) December 15, 2020 I cannot recommend this class enough, it’s awesome. https://t.co/4BsBEygeLd — James Doss-Gollin (@jdossgollin) December 16, 2020"},{"url":"search/","title":"Search results","tags":[],"text":"window.init_search(); Search Results \nLoading..."},{"url":"semesters/","title":"Previous semesters","tags":["welcome"],"text":"This is the Fall 2024 edition For previous versions of this class, see: Fall 2023 Fall 2022 Spring 2021 Fall 2020 Spring 2020 Fall 2019"},{"url":"sidebar data/","title":"sidebar data","tags":[],"text":"Dict \"main\" \"welcome\" collections \"welcome\" .pages, \"Module 1 Images, Transformations, Abstractions\" collections \"module1\" .pages, \"Module 2 Social Science & Data Science\" collections \"module2\" .pages, \"Module 3 Climate Science\" collections \"module3\" .pages, , "},{"url":"structure/","title":"structure","tags":[],"text":""},{"url":"climate_science/2d_advection_diffusion/","title":"Advection and diffusion in 2D","tags":["lecture","module3","PDE","differential equation","track_math","track_climate","structure","modeling","climate","simulation","fluid simulation","continuous","advection–diffusion"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v DdTWgBlDgr0\" image \"https user images.githubusercontent.com 6933510 136200688 e3c6d6ee 808c 433f 8252 af6ad278fb4d.gif\" section 9 order 9 title \"Advection and diffusion in 2D\" layout \"layout.jlhtml\" youtube id \"DdTWgBlDgr0\" description \"\" tags \"lecture\", \"module3\", \"PDE\", \"differential equation\", \"track math\", \"track climate\", \"structure\", \"modeling\", \"climate\", \"simulation\", \"fluid simulation\", \"continuous\", \"advection–diffusion\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Statistics using Plots using PlutoUI using OffsetArrays end TableOfContents md\"\"\" PDEs in 2D Heat transport by ocean currents \"\"\" html\"\"\" iframe width \"700\" height \"394\" src \"https www.youtube nocookie.com embed 6 GQuVopmUM?start 15\" frameborder \"0\" allow \"accelerometer autoplay clipboard write encrypted media gyroscope picture in picture\" allowfullscreen iframe \"\"\" md\"\"\" One dimensional advection diffusion Let's recall from the previous lecture that we derived the advection diffusion equation with one spatial dimension \\frac \\partial T x,t \\partial t U \\frac \\partial T \\partial x \\kappa \\frac \\partial^ 2 T \\partial x^ 2 , where T x, t is the temperature, U is a constant advective velocity and \\kappa is the heat diffusivity . Two dimensional advection diffusion The two dimensional advection diffusion equation adds advection and diffusion operators acting in a second spatial dimension y that is perpendicular to x . The temperature field T is then a function T T x, y, t , and the PDE becomes \\frac \\partial T x,y,t \\partial t u x,y \\frac \\partial T \\partial x v x,y \\frac \\partial T \\partial y \\kappa \\left \\frac \\partial^ 2 T \\partial x^ 2 \\frac \\partial^ 2 T \\partial y^ 2 \\right , where \\vec u x,y u, v u\\,\\mathbf \\hat x v\\,\\mathbf \\hat y is a velocity vector field, and each partial derivative is also a function of x , y and t . We will think of x as being the longitudinal direction positive from west to east and y as the latitudinal direction positive from south to north . \"\"\" begin reviewBox bind show review CheckBox default false md\"\"\" Review of multivariable calculus identities and notation Check the box for a review reviewBox \"\"\" end if show review md\"\"\" The two dimensional advection diffusion equation is often written more succinctly as \\frac \\partial T x,y,t \\partial t \\vec u \\cdot \\nabla T \\kappa \\nabla^ 2 T, using the following shorthand notation. The gradient operator is defined as \\nabla \\equiv \\left \\frac \\partial \\partial x , \\frac \\partial \\partial y \\right such that \\nabla T \\left \\frac \\partial T \\partial x , \\frac \\partial T \\partial y \\right and \\vec u \\cdot \\nabla T u, v \\cdot \\left \\frac \\partial T \\partial x , \\frac \\partial T \\partial y \\right u \\frac \\partial T \\partial x v\\frac \\partial T \\partial y . The Laplacian operator \\nabla^ 2 , sometimes denoted \\Delta , is defined as \\nabla^ 2 \\frac \\partial^ 2 \\partial x^ 2 \\frac \\partial^ 2 \\partial y^ 2 such that \\nabla^ 2 T \\frac \\partial^ 2 T \\partial x^ 2 \\frac \\partial^ 2 T \\partial y^ 2 . The divergence operator is defined as \\nabla \\cdot \\quad , such that \\nabla \\cdot \\vec u \\left \\frac \\partial \\partial x , \\frac \\partial \\partial x \\right \\cdot u,v \\frac \\partial u \\partial x \\frac \\partial v \\partial y . Note Since seawater is largely incompressible , we have \\nabla \\cdot \\vec u \\frac \\partial u \\partial x \\frac \\partial v \\partial y 0 , i.e. ocean currents are approximately a non divergent flow . Among other implications, this allows us to write \\begin align \\vec u \\cdot \\nabla T& u\\frac \\partial T x,y,t \\partial x v\\frac \\partial T x,y,t \\partial y \\newline & u\\frac \\partial T \\partial x v\\frac \\partial T \\partial y T\\left \\frac \\partial u \\partial x \\frac \\partial v \\partial y \\right \\newline & \\left u\\frac \\partial T \\partial x T\\frac \\partial u \\partial x \\right \\left v\\frac \\partial T \\partial y \\frac \\partial v \\partial y \\right \\newline & \\frac \\partial uT \\partial x \\frac \\partial vT \\partial x \\newline & \\nabla \\cdot \\vec u T \\end align using the product rule separately in both x and y . The flux form two dimensional advection diffusion equation This lets us finally re write the two dimensional advection diffusion equation as \\frac \\partial T \\partial t \\nabla \\cdot \\vec u T \\kappa \\nabla^ 2 T, which is the form we will use in our numerical algorithm below. \"\"\" end md\"\"\" Numerical solution of the 2D equation Discretizing advection in two dimensions In Lecture XX we saw that in one dimension we can discretize a first partial derivative in space using the centered finite difference \\frac \\partial T x i , t n \\partial x \\approx \\frac T i 1 ^ n T i 1 ^ n 2 \\Delta x . In two dimensions, we discretize the partial derivative the exact same way, except that we also need to keep track of the cell index j in the y dimension \\frac \\partial T x i , y j , t n \\partial x \\approx \\frac T i 1,\\, j ^ n T i 1,\\,j ^ n 2 \\Delta x . The x gradient kernel below, implemented using the `OffsetArray` type, is shown below, and is reminiscent of the edge detection or sharpening kernels used in image processing and machine learning \"\"\" md\"\"\" The first order partial derivative in y is similarly discretized as \\frac \\partial T x i , y j , t n \\partial y \\approx \\frac T i,\\, j 1 ^ n T i,\\,j 1 ^ n 2 \\Delta y . Its kernel is shown below. \"\"\" md\"\"\" Now that we have discretized the two derivate terms, we can write out the advective tendency for computing T i, j, n 1 as u\\frac \\partial T \\partial x v\\frac \\partial T \\partial y \\approx u i,\\, j ^ n \\frac T i,\\, j 1 ^ n T i,\\,j 1 ^ n 2 \\Delta y v i,\\, j ^ n \\frac T i,\\, j 1 ^ n T i,\\,j 1 ^ n 2 \\Delta y . We implement this in julia as a series of methods for the `advect` function. The first method computes the advective tendency as a single `Float64` type for the i,j grid cell while the second method returns an array of the tendencies for each grid cell using two nested for loops. \"\"\" md\"\"\" Discretizing diffusion in two dimensions Just as with advection, the process for discretizing the diffusion operators effectively consists of repeating the one dimensional process for x in a second dimension y , separately \\kappa \\left \\frac \\partial^ 2 T \\partial x^ 2 \\frac \\partial^ 2 T \\partial y^ 2 \\right \\kappa \\left \\frac T i 1,\\ j ^ n 2T i,\\ j ^ n T i 1,\\ j ^ n \\left \\Delta x \\right ^ 2 \\frac T i,\\ j 1 ^ n 2T i,\\ j ^ n T i,\\ j 1 ^ n \\left \\Delta y \\right ^ 2 \\right The corresponding x and y second derivative kernels are shown below \"\"\" md\"\"\" Just as we did with advection, we implement a `diffuse` function using multiple dispatch \"\"\" md\"\"\" No flux boundary conditions We want to impose the no flux boundary conditions, which states that u\\frac \\partial T \\partial x \\kappa \\frac \\partial T \\partial x 0 at x boundaries and v\\frac \\partial T \\partial y \\kappa \\frac \\partial T \\partial y 0 at the y boundaries. To impose this, we treat i 1 and i N x as ghost cells , which do not do anything expect help us impose these boundaries conditions. Discretely, the boundary fluxes between i 1 and i 2 vanish if \\dfrac T 2,\\,j ^ n T 1,\\,j ^ n \\Delta x 0 or T 1,\\,j ^ n T 2,\\,j ^ n . Thus, we can implement the boundary conditions by updating the temperature of the ghost cells to match their interior point neighbors \"\"\" function update ghostcells A Array Float64,2 option \"no flux\" Atmp view A , if option \"no flux\" A 1, Atmp 2, Atmp end, Atmp end 1, A , 1 Atmp , 2 Atmp , end Atmp , end 1 end end md\"Let's get a feel for what this is actually doing\" begin A rand Float64, 6,6 heatmap A, size 200, 200 end | as svg begin Acopy copy A update ghostcells Acopy heatmap Acopy, size 200, 200 end | as svg md\"\"\" Timestepping \"\"\" md\"\"\" Data structures \"\"\" begin struct Grid N Int64 L Float64 Δx Float64 Δy Float64 x Array Float64,2 y Array Float64,2 Nx Int64 Ny Int64 function Grid N, L Δx L N m Δy L N m x 0. Δx 2. Δx L Δx 2. x reshape x, 1, size x,1 y L Δy 2. Δy L Δy 2. y reshape y, size y,1 , 1 Nx, Ny size x, 2 , size y, 1 return new N, L, Δx, Δy, x, y, Nx, Ny end end Base.zeros G Grid zeros G.Ny, G.Nx end struct Parameters κ Float64 end abstract type ClimateModel end begin struct OceanModel ClimateModel G Grid P Parameters u Array Float64,2 v Array Float64,2 end OceanModel G, P OceanModel G, P, zeros G , zeros G OceanModel G OceanModel G, Parameters 1.e4 , zeros G , zeros G end begin mutable struct ClimateModelSimulation ModelType ClimateModel model ModelType T Array Float64,2 Δt Float64 iteration Int64 end ClimateModelSimulation C ModelType, T, Δt where ModelType ClimateModelSimulation ModelType C, T, Δt, 0 end md\"\"\" Simulating heat transport by advective & diffusive ocean currents \"\"\" md\" Simulation controls \" begin quiverBox bind show quiver CheckBox default false anomalyBox bind show anomaly CheckBox default false md\"\"\" Click to show the velocity field quiverBox or to show temperature anomalies instead of absolute values anomalyBox \"\"\" end begin U ex Slider bind U ex Slider 4 1 8, default 0, show value false md\"\"\" U ex Slider \"\"\" end begin md\" Vary the current speed U 2. ^U ex × reference \" end begin κ ex Slider bind κ ex Slider 0. 1.e3 1.e5, default 1.e4, show value true md\"\"\" Vary the diffusivity κ κ ex Slider m² s \"\"\" end bind go ex Clock 0.1 md\"\"\" Some unit tests for verification \"\"\" function CFL adv sim ClimateModelSimulation maximum sqrt. sim.model.u.^2 sim.model.v.^2 sim.Δt sim.model.G.Δx end md\"\"\" Appendix \"\"\" Nvec 1 25 function plot state sim ClimateModelSimulation clims 1.1, 1.1 , show quiver true, show anomaly false, IC nothing model sim.model grid sim.model.G p plot xlabel \"longitudinal distance km \", ylabel \"latitudinal distance km \", clabel \"Temperature\", yticks grid.L 1000e3 grid.L , Int64. 1e 3 grid.L 1000e3 grid.L , xticks 0 1000e3 grid.L , Int64. 1e 3 0 1000e3 grid.L , xlims 0., grid.L , ylims grid.L, grid.L , X repeat grid.x, grid.Ny, 1 Y repeat grid.y, 1, grid.Nx if show anomaly arrow col black maxdiff maximum abs. sim.T . IC heatmap p, grid.x , grid.y , sim.T . IC, clims 1.1, 1.1 , color balance, colorbar title \"Temperature anomaly °C \", linewidth 0., size 400, 530 else arrow col white heatmap p, grid.x , grid.y , sim.T, color thermal, levels clims 1 clims 2 clims 1 21. clims 2 , colorbar title \"Temperature °C \", clims clims, linewidth 0., size 400, 520 end annotate p, 50e3, 6170e3, text string \"t \", Int64 round sim.iteration sim.Δt 60 60 24 , \" days\" , color black, left, 9 if show quiver Nq grid.N ÷ 5 quiver p, X Nq 1 ÷2 Nq end , Y Nq 1 ÷2 Nq end , quiver grid.L 4 . model.u Nq 1 ÷2 Nq end , model.v Nq 1 ÷2 Nq end , color arrow col, alpha 0.7 end as png p end md\"\"\" Computing the velocity field for a single circular vortex \"\"\" begin ∂x ϕ, Δx ϕ ,2 end ϕ ,1 end 1 Δx ∂y ϕ, Δy ϕ 2 end, ϕ 1 end 1, Δy xpad ϕ hcat zeros size ϕ,1 , ϕ, zeros size ϕ,1 ypad ϕ vcat zeros size ϕ,2 ', ϕ, zeros size ϕ,2 ' xitp ϕ 0.5 ϕ ,2 end ϕ ,1 end 1 yitp ϕ 0.5 ϕ 2 end, ϕ 1 end 1, function diagnose velocities ψ, G u xitp ∂y ψ, G.Δy G.L v yitp ∂x ψ, G.Δx G.L return u, v end end function impose no flux u, v u 1, . 0. v 1, . 0. u end, . 0. v end, . 0. u ,1 . 0. v ,1 . 0. u ,end . 0. v ,end . 0. end function PointVortex G Ω 1., a 0.2, x0 0.5, y0 0. x reshape 0. G.Δx G.L G.Δx G.L 1. G.Δx G.L , 1, G.Nx 1 y reshape 1. G.Δy G.L G.Δy G.L 1. G.Δy G.L , G.Ny 1, 1 function ψ̂ x,y r sqrt. y . y0 .^2 . x . x0 .^2 stream Ω 4 r.^2 stream r . a Ω a^2 4 1. . 2 log. r r . a a return stream end u, v diagnose velocities ψ̂ x, y , G impose no flux u, v return u, v end md\"\"\" Computing a quasi realistic ocean velocity field \\vec u u, v Our velocity field is given by an analytical solution to the classic wind driven gyre problem, which is given by solving the fourth order partial differential equation \\epsilon M \\hat \\nabla ^ 4 \\hat \\Psi \\frac \\partial \\hat \\Psi \\partial \\hat x \\nabla \\times \\hat \\tau \\mathbf z , where the hats denote that all of the variables have been non dimensionalized and all of their constant coefficients have been bundles into the single parameter \\epsilon M \\equiv \\dfrac \\nu \\beta L^3 . The solution makes use of an advanced asymptotic method valid in the limit that \\epsilon \\ll 1 known as boundary layer analysis see MIT course 18.305 to learn more . \"\"\" function DoubleGyre G β 2e 11, τ₀ 0.1, ρ₀ 1.e3, ν 1.e5, κ 1.e5, H 1000. ϵM ν β G.L^3 ϵ ϵM^ 1 3. x reshape 0. G.Δx G.L G.Δx G.L 1. G.Δx G.L , 1, G.Nx 1 y reshape 1. G.Δy G.L G.Δy G.L 1. G.Δy G.L , G.Ny 1, 1 ψ̂ x,y π sin. π y 1 . x exp. x 2 ϵ . cos. √3 x 2 ϵ . 1. √3 sin. √3 x 2 ϵ . ϵ exp. x . 1. ϵ u, v τ₀ ρ₀ β G.L H . diagnose velocities ψ̂ x, y , G impose no flux u, v return u, v end md\"\"\" Some simple initial temperature fields \"\"\" linearT G 0.5 1. . y G.L for y in G.y , 1 , x in G.x 1, function InitBox G value 1., nx 2, ny 2, xspan false, yspan false T zeros G T G.Ny÷2 ny G.Ny÷2 ny, G.Nx÷2 nx G.Nx÷2 nx . value if xspan T G.Ny÷2 ny G.Ny÷2 ny, . value end if yspan T , G.Nx÷2 nx G.Nx÷2 nx . value end return T end begin G Grid 10, 6.e6 P Parameters κ ex u, v zeros G , zeros G u, v PointVortex G, Ω 0.5 u, v DoubleGyre G IC InitBox G IC InitBox G, xspan true IC linearT G model OceanModel G, P, u 2. ^U ex, v 2. ^U ex Δt 12 60 60 ocean sim ClimateModelSimulation model, copy IC , Δt end begin heat capacity 51. total heat content sum heat capacity ocean sim.T ocean sim.model.G.Δx ocean sim.model.G.Δy 1e 15 mean temp mean ocean sim.T end begin go ex md\"\"\" Let's make sure our model conserves energy. We have not added any energy to the system advection and diffusion just move the energy around. The total heat content is round total heat content, digits 3 peta Joules and the average temperature is round mean temp, digits 2 °C. \"\"\" end md\" The CFL condition The CFL condition is defined by \\text CFL \\dfrac \\max\\left \\sqrt u² v² \\right Δt Δx round CFL adv ocean sim , digits 2 \" plot kernel A heatmap collect A , color bluesreds, clims maximum abs. A , maximum abs. A , colorbar false, xticks false, yticks false, size 30 30 size A, 2 , 30 30 size A, 1 , xaxis false, yaxis false begin xgrad kernel OffsetArray reshape 1., 0, 1. , 1, 3 , 0 0, 1 1 plot kernel xgrad kernel end begin ygrad kernel OffsetArray reshape 1., 0, 1. , 3, 1 , 1 1, 0 0 plot kernel ygrad kernel end begin function advect T, u, v, Δy, Δx, j, i return . u j, i . sum xgrad kernel 0, 1 1 . T j, i 1 i 1 2Δx . v j, i . sum ygrad kernel 1 1, 0 . T j 1 j 1, i 2Δy end advect T, u, v, Δy, Δx advect T, u, v, Δy, Δx, j, i for j 2 size T, 1 1, i 2 size T, 2 1 advect T, O OceanModel advect T, O.u, O.v, O.G.Δy, O.G.Δx end begin xdiff kernel OffsetArray reshape 1., 2., 1. , 1, 3 , 0 0, 1 1 ydiff kernel OffsetArray reshape 1., 2., 1. , 3, 1 , 1 1, 0 0 plot kernel xdiff kernel , plot kernel ydiff kernel end begin function diffuse T, κ, Δy, Δx, j, i return κ. sum xdiff kernel 0, 1 1 . T j, i 1 i 1 Δx^2 sum ydiff kernel 1 1, 0 . T j 1 j 1, i Δy^2 end diffuse T, κ, Δy, Δx diffuse T, κ, Δy, Δx, j, i for j 2 size T, 1 1, i 2 size T, 2 1 diffuse T, O OceanModel diffuse T, O.P.κ, O.G.Δy, O.G.Δx end function timestep sim ClimateModelSimulation OceanModel update ghostcells sim.T tendencies advect sim.T, sim.model . diffuse sim.T, sim.model sim.T 2 end 1, 2 end 1 . sim.Δt tendencies sim.iteration 1 end for i in 1 50 timestep ocean sim end let go ex if ocean sim.iteration 0 timestep ocean sim else for i in 1 50 timestep ocean sim end end plot state ocean sim, clims 0.1, 1 , show quiver show quiver, show anomaly show anomaly, IC IC end tvec map Nvec do Npower G Grid 8 Npower, 6.e6 P Parameters κ ex u, v DoubleGyre G u, v PointVortex G, Ω 0.5 u, v zeros G , zeros G model OceanModel G, P, u, v IC InitBox G IC InitBox G, nx G.Nx÷2 1 IC linearT G Δt 6 60 60 S ClimateModelSimulation model, copy IC , Δt return elapsed timestep S end begin plot 8 Nvec, tvec, xlabel \"Number of Grid Cells in x direction \", ylabel \"elapsed time per timestep s \" end | as svg "},{"url":"climate_science/advection_and_diffusion/","title":"Advection and diffusion in 1D","tags":["lecture","module3","advection–diffusion","climate","modelling","PDE","differential equation","track_math","track_climate","simulation","plotting","fluid simulation","continuous"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v Xb iUwXI78A\" image \"https user images.githubusercontent.com 6933510 136200627 8211654f 7730 4f4a 8933 5b64164806c3.gif\" section 7 order 7 title \"Advection and diffusion in 1D\" layout \"layout.jlhtml\" youtube id \"Xb iUwXI78A\" description \"\" tags \"lecture\", \"module3\", \"advection–diffusion\", \"climate\", \"modelling\", \"PDE\", \"differential equation\", \"track math\", \"track climate\", \"simulation\", \"plotting\", \"fluid simulation\", \"continuous\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, LinearAlgebra TableOfContents depth 4 md\"\"\" Evolution in time and space Advection and diffusion in 1D \"\"\" md\"\"\" So far we have been looking at dynamics in time, for example how does the temperature of the Earth change over time. But the Earth does not have a single, uniform temperature rather, at a particular moment in time, different places on Earth are at different temperatures, and those different temperatures change over time due to several mechanisms. In this notebook we will look at two fundamental mechanisms advection and diffusion . Let's think about the temperature in the ocean. Since the ocean is a fluid that is in motion, a warm \"parcel\" of water can flow move to a new location due to the physical motion of the water itself this is advection . And even if the water doesn't move, temperature or a higher concentration of some substance dissolved in the fluid can spread out due to molecular mechanisms this is diffusion . \"\"\" md\"\"\" In this notebook we will restrict ourselves to one spatial dimension 1D . So we will think about the temperature T , for example, being a function T t, x of two independent variables time, t space, x We want to calculate a value of the temperature T for each possible pair of values t, x , i.e. for all time 0 and all positions. The temperature at a given point will change due to different physical processes. We need to model this by writing down equations describing each physical process and how it affects the temperature. Since there are now two independent variables, t and x , we can expect to end up with derivatives with respect to both of these variables, so that the rate of change of temperature in time at a given point depends also on gradients of the temperature in space . This will lead to a partial differential equation that relates partial derivatives of T . In the context of climate modelling, we can think of x as being the latitude , supposing that the temperature is the same at all points with the same latitude. In this way we could model the fact that the poles are cold and the equator is warm, and we could model how heat flows from hot to cold. However, we clearly cannot model actual ocean currents like this, which would require two, or even three, spatial dimensions. \"\"\" md\"\"\" Visualising advection diffusion \"\"\" md\"\"\" Here is a visualisation of the physical processes of advection and diffusion in one dimension that we will discuss and build during this notebook. \"\"\" md\"\"\" U bind UU Slider 1 0.01 1, show value true, default 0 D bind DD Slider 0.2 0.001 0.2, show value true, default 0 \"\"\" md\"\"\" Temperature profiles and discretization \"\"\" md\"\"\" An ordinary differential equation needs an initial value for each variable. Similarly, we will need an initial function T 0 x that gives us the temperature at each position x . Let's suppose that the position is restricted to the interval 0, L x . As usual, to represent the continuous function T 0 x on a computer, we will need to discretise it in some way, i.e. approximate the continuous function by a finite set of numbers in the computer. The simplest but far from the only discretisation method is to sample the function at discrete grid points or nodes x i , for i 1, \\dots, N x . For simplicity we will take these equally spaced, with spacing x i 1 x i \\delta x L x N x . \"\"\" md\"\"\" For example, let's consider the following initial temperature profile \"\"\" T₀ x sin 2π x ^2 0.5 T₀ x sin 2π x 2 cos 4π x 0.2 md\"\"\" and define the grid points as follows \"\"\" begin Nₓ 20 Lₓ 1.0 δx Lₓ Nₓ xs δx 2 δx Lₓ end xs md\"\"\" It turns out to be a good idea to take the grid points at the centre of each interval, so that we have N x intervals and N x grid points, starting at x 1 \\delta x 2 and finishing at x N L x \\delta x 2 . \"\"\" md\"\"\" We call such a function of x at a given time a temperature profile . Let's draw it both as a function and as a heatmap \"\"\" md\"\"\" We will denote by T^0 i the initial temperature at grid point number i . \"\"\" md\"\"\" A useful way to think about T^n i is as some kind of spatial average of T t n, x over the interval of positions between neighbouring grid points, so T i is the average over the interval between x i \\frac \\delta x 2 and x i \\frac \\delta x 2 . We can thus think of the following piecewise constant approximation to the original continuous function \"\"\" let δx xs 2 xs 1 p plot 0 0.001 Lₓ, T₀, label \"T₀\", lw 1, ls dash scatter xs, T₀. xs , label \"sampled\" scatter xs, zero. xs , label \"x nodes\", alpha 0.5, ms 3, lw 2 for i in 1 length xs plot xs i δx 2, T₀ xs i , xs i δx 2, T₀ xs i , c green, lw 4, lab false plot xs i δx 2, 0 , xs i δx 2, T₀ xs i , xs i δx 2, T₀ xs i , xs i δx 2, 0 , c green, lw 1, lab false, ls dash, alpha 0.3 end xlabel \"x\" ylabel \"T₀ x \" end md\"\"\" Advection \"\"\" md\"\"\" Now let's think of this profile as representing the temperature in each small volume, or \"parcel\", of fluid. Let's suppose that the fluid is moving to the right with a constant, uniform speed U . Uniform here means that the speed is the same in all parts of the fluid. Then the temperature profile should also move with the fluid We call a quantity, such as the temperature, that is carried along with the fluid a tracer . If we fix our attention at a single, fixed point in space, say the grid point x i , the temperature there will vary over time, due to the fact that the fluid is moving past it. How it varies in time depends on the values at neighbouring grid points, since they determine how much heat will be transported into and out of the current cell. The point of view where we fix our attention at one point in space is called Eulerian . The alternative is to follow a parcel of fluid as it moves along in space this is called Lagrangian . \"\"\" md\"\"\" Visualising fluxes in a fluid \"\"\" md\"\"\" Let's visualise what happens as the fluid moves past a grid point, or rather the cell centered at a grid point. We will visualise tracer particles moving inside the fluid \"\"\" N 5000 begin xx abs. 2 . 4 . rand N .^ 2 . 1.5 yy rand N end δ 0.8 md\"\"\" show particles entering and leaving in \\delta t bind show particles CheckBox \"\"\" md\"\"\" t bind t Slider 0 0.001 2, show value true, default 0 \"\"\" md\"\"\" Time stepping \"\"\" md\"\"\" We want to model how the temperature profile changes in time due to the flow of the fluid. We'll do so by looking at each cell and asking how much heat enters and leaves the cell in a given time step, of duration \\delta t . \"\"\" md\"\"\" Let's call T^n i the approximate unknown average value of T in the cell at position x i and at the n th time step t n , i.e. an approximation of T t n, x i , where t n n \\, \\delta t . Then T^ n 1 i \\simeq T t n \\delta t, x i and T^ n i 1 \\simeq T t n, x i \\delta x . Note that the superscript n in these algorithms does not mean a power it's just a label for the time step. We could write T i^ n instead, but that is annoying to both write and read, so we omit the parentheses. \"\"\" md\"\"\" Suppose the fluid is moving to the right with speed U . During a time step of duration \\delta t , the temperature T^n i at cell i changes for two reasons some heat enters cell i some heat leaves cell i Note that most of the fluid that starts within cell i remains within that cell during the time step if the time step is short enough , as we see from the visualisation above. To calculate how much heat enters and leaves, note that only heat in the region of fluid within a distance U \\, \\delta t from the boundary of the cell will cross into that cell. So a proportion U \\, \\delta t \\delta x of the amount in cell i crosses the boundary. We will blur the distinction between \"amount of heat\" and temperature. Hence, roughly an amount T^n i U \\delta t \\delta x will leave cell number i and cross into cell i 1 the cell to the right . Similarly, an amount T^n i 1 U \\delta t \\delta x will enter cell i from the neighbouring cell i 1 on the left. Hence we arrive at the following T^ n 1 i T^ n i T^n i 1 T^n i \\, U \\, \\delta t \\delta x. Note that on the right hand side we have quantities at the time step n , and on the left at time step n 1 . So this tells us how to update our quantities from slice n to slice n 1 . \"\"\" md\"\"\" Continuous limit Advection equation PDE \"\"\" md\"\"\" Rearranging the previous equation we get \\frac T^ n 1 i T^ n i \\delta t \\frac T^n i 1 T^n i \\delta x \\, U. \"\"\" md\"\"\" Taking the continuum limit when \\delta t \\to 0 and \\delta x \\to 0 , we recognise the definition of partial derivatives with respect to time and space variables from multivariable calculus. Note the different indices that change on the two sides of the equation. Denoting these partial derivatives using \\partial , we arrive at the advection equation \\frac \\partial T t, x \\partial t U \\frac \\partial T t, x \\partial x , or for short \\frac \\partial T \\partial t U \\frac \\partial T \\partial x . Since T is a function of both x and t , and this equation involves partial derivatives with respect to both of the independent variables, this is a partial differential equation PDE . It describes how the function T t, x changes continuously as a function both of time and space. Although there are some analytical methods to solve PDEs, often it's necessary to use numerical methods. Here we'll look at simple numerical methods to solve such equations. \"\"\" md\"\"\" Numerics for the advection equation \"\"\" md\"\"\" Let's return to the version of the equation in which the value at the following time step is isolated T^ n 1 i T^ n i \\left U \\frac \\delta t \\delta x \\right T^n i T^n i 1 . In the last term on the right hand side, we see that we require combinations of values of T at the same time step from different places, with certain coefficients. \"\"\" md\"\"\" There are many approaches to implementing this numerically. The simplest is to directly transcribe the equation for the i th entry of the vector. Calling `T` the current vector, i.e. \\mathbf T ^n T^n i i 1, \\ldots, N x , and `T′` the new vector at the next time step, we have the following basic expression T′ i T i δt U T i 1 T i δx But now we realise a problem What should we do when i 1 ? This will try to access the index 0 of the vector `T`, which does not exist \"\"\" md\"\"\" Boundary conditions This illustrates the necessity of choosing boundary conditions that specify what happens at the edge of the domain. For simplicity we will choose to use periodic boundary conditions . This is a convenient mathematical fiction that allows us to treat all cells as being on the same footing, by wrapping the system around a torus, so that cells i 1 and i N x are neighbours. \"\"\" md\"\"\" We can then write this as follows, where we separate out the case i 1 \"\"\" function advection T, δt, δx, U N length T T′ similar T create new vector of the same length bulk cells for i in 2 N T′ i T i δt U T i T i 1 δx end boundary cells T′ 1 T 1 δt U T 1 T N δx periodic return T′ end md\"\"\" This performs a single time step of the advection equation it takes in the current vector of T s and returns the new T s after the step. Note that this is just like a step of the Euler method for solving ODEs, but where many spatial coordinates are updated at the same time. Effectively we are solving a system of coupled ODEs \"\"\" δt 0.001 U 0.2 assign colours to particles cs map xx do x if U δ x 0 1 elseif 1 U δ x 1 2 else 0 end end begin plot 0, 1, 1, 0, 0 , 0, 0, 1, 1, 0 , series shape, alpha 0.5, fill true, ratio 1, label false, leg false new xx xx . U . t scatter xx . U . t, yy, ms 1.5, alpha 0.1, c gray if show particles scatter new xx cs . 0 , yy cs . 0 , ms 1.5, alpha 0.5, c cs cs . 0 end plot 1.5, 2 , 0, 0 , c black plot 1.5, 2 , 1, 1 , c black xlims 2, 2 ylims 0.1, 1.1 as svg plot axis true, yticks 0, 1 end md\"\"\" Unfortunately this does not behave as we expect instead of preserving the shape of the profile over time, it is decaying. This is due to the way we are approximating. A better way to discretize the spatial derivative is using the following centered difference \\frac \\partial T t n, x i \\partial x \\simeq \\frac T^n i 1 T^n i 1 2 \\delta x \"\"\" function advection2 T, δt, δx, U N length T T′ similar T create new vector of the same length for i in 2 N 1 T′ i T i δt U T i 1 T i 1 2δx end periodic boundary T′ 1 T 1 δt U T 2 T N 2δx T′ N T N δt U T 1 T N 1 2δx return T′ end md\"\"\" Diffusion \"\"\" md\"\"\" Another key physical process is diffusion . This models how temperature or mass spreads out from hot or high concentration regions towards regions where it is cold or where there is a low concentration. Physical mechanism Random walks The physical mechanism behind this is random motion this is the continuous limit of equations describing the evolution of the probability distribution in space and time of a cloud of random walkers. This is the same process that we studied in lecture 2.6. Using our current notation, there we showed that the probability distribution of a cloud of random walkers satisfies the following time evolution p^ n 1 i \\frac 1 2 p^n i 1 p^n i 1 If now we say that the walkers jump only with a certain probability, with a large probability to stay in the same place, and that these random walkers are the carriers of heat, then we get T^ n 1 i \\kappa T^n i 1 2 T^n i T^n i 1 . Watch this video https www.youtube.com watch?v a3V0BJLIo c from last semester's class to see Grant Sanderson explaining this. \"\"\" md\"\"\" Continuous limit Heat equation PDE \"\"\" md\"\"\" Introducing \\delta x as the spatial discretisation, and \\delta t as the time step, we get T^ n 1 i \\kappa \\frac \\delta t \\delta x^2 T^n i 1 2 T^n i T^n i 1 . \"\"\" md\"\"\" The continuous limit is the following heat equation or diffusion equation \"\"\" md\"\"\" \\frac \\partial T \\partial t \\kappa \\frac \\partial^2 T \\partial x^2 . \"\"\" md\"\"\" Here, \\kappa is the heat diffusivity , which says how quickly heat spreads out. In the context of diffusion of mass the equivalent is the diffusion coefficient , D . \"\"\" md\"\"\" To obtain a numerical method to solve this equation, we again need to discretise this, in particular the second derivative. One possible discretisation is \\frac \\partial^2 T \\partial x^2 t n, x i \\simeq \\frac T^n i 1 2 T^n i T^n i 1 \\delta x^2 . \"\"\" md\"\"\" This may again be transcribed directly into code \"\"\" function diffusion T, δt, δx, D N length T T′ similar T create new vector of the same length for i in 2 N 1 T′ i T i δt D T i 1 2T i T i 1 δx^2 end periodic boundary T′ 1 T 1 δt D T 2 2T 1 T N δx^2 T′ N T N δt D T 1 2T N T N 1 δx^2 return T′ end md\"\"\" The advection diffusion PDE \"\"\" md\"\"\" Finally we can combine both mechanisms, to describe a tracer that is both being advected at a constant speed and diffusing. This basically utilises the composition of the advection and diffusion functions \"\"\" function advection diffusion T, δt, δx, U, D temp advection2 T, δt, δx, U return diffusion temp, δt, δx, D end md\"\"\" Function library \"\"\" function temperature heatmap x, T p heatmap x, 0. , collect T' , clims 1., 1. , cbar false, xticks nothing, yticks nothing return p end begin p1 plot 0 0.001 Lₓ, T₀, label \"T₀\", lw 3 scatter xs, T₀. xs , label \"sampled\" scatter xs, zero. xs , label \"x nodes\", alpha 0.5, ms 3 xlabel \"x\" ylabel \"T₀\" for x in xs plot x, 0 , x, T₀ x , ls dash, c black, label \"\", alpha 0.5 end hline 0 , ls dash, lab false p2 temperature heatmap xs, T₀. xs plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 end function evolve method, xs, δt, U, t final 10.0, f₀ T₀ T f₀. xs δx xs 2 xs 1 t 0.0 ts t results T while t t final T′ method T, δt, δx, U new push results, T′ t δt push ts, t T copy T′ end return ts, results end tt, results evolve advection diffusion, xs, δt, UU, DD md\"\"\" time step bind n6 Slider 1 length results , show value true \"\"\" ts, evolution evolve advection, xs, δt, U bind n Slider 1 length evolution , show value true let p1 plot xs, evolution n , m o, xlim 0, 1 , ylim 1.1, 1.1 , title \"t round ts n , digits 2 \", leg false p2 temperature heatmap xs, evolution n plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 end ts2, evolution2 evolve advection2, xs, δt, 0.1 n2 slider bind n2 Slider 1 length evolution2 , show value true let p1 plot xs, evolution2 n2 , m o, xlim 0, 1 , ylim 3.1, 3.1 , title \"t round ts2 n2 , digits 2 \", leg false p2 temperature heatmap xs, evolution2 n2 plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 end ts3, evolution3 evolve diffusion, xs, δt, 0.01 bind n3 Slider 1 length evolution3 , show value true let p1 plot xs, results n6 , m o, xlim 0, 1 , ylim 3.1, 3.1 , title \"t round ts3 n3 , digits 2 \", leg false p2 temperature heatmap xs, results n6 plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 , clim 1, 1 end let p1 plot xs, evolution3 n3 , m o, xlim 0, 1 , ylim 3.1, 3.1 , title \"t round ts3 n3 , digits 2 \", leg false p2 temperature heatmap xs, evolution3 n3 plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 , clim 1, 1 end ts5, evolution5 evolve advection diffusion, xs, δt, 1.0, 0.01 bind n5 Slider 1 length evolution5 , show value true let p1 plot xs, evolution5 n5 , m o, xlim 0, 1 , ylim 1.1, 1.1 , title \"t round ts3 n5 , digits 2 \", leg false p2 temperature heatmap xs, evolution5 n5 plot p1, p2, layout grid 2, 1, heights 0.9, 0.1 , clim 1, 1 end "},{"url":"climate_science/climate2_snowball_earth/","title":"Snowball Earth and hysteresis","tags":["lecture","module3","dynamics","track_math","track_climate","hysteresis","equilibrium","climate","modeling","differential equation","ODE","plotting","feedback","runaway","nonlinear","snowball earth"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v cdIgr 2nUvI\" image \"https user images.githubusercontent.com 6933510 136199703 5edb4791 f9c0 4872 b0a7 7c9b1b6502d5.png\" section 6 order 6 title \"Snowball Earth and hysteresis\" layout \"layout.jlhtml\" youtube id \"cdIgr 2nUvI\" description \"\" tags \"lecture\", \"module3\", \"dynamics\", \"track math\", \"track climate\", \"hysteresis\", \"equilibrium\", \"climate\", \"modeling\", \"differential equation\", \"ODE\", \"plotting\", \"feedback\", \"runaway\", \"nonlinear\", \"snowball earth\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using DifferentialEquations, Plots, PlutoUI, LinearAlgebra end TableOfContents depth 4 md\"\"\" Julia Concepts `sign x ` \"\"\" md\"\"\" The sign or signum latin for \"sign\" function \"\"\" md\"\"\" `sign x ` returns 0 if x is 0, or ±1 for positive negative x. \"\"\" begin scatter sign, 5 .1 5, legend false, m c, ms 3, size 600, 300 title \"The sign function is discontinuous at 0\" xlabel \"x\" ylabel \"sign x \" end sign Inf sign Inf md\"\"\" Mathematics multiple equilibria. Using computation to explore hysteresis. \"\"\" md\"\"\" The function f y,a \\rm sign y a y can be written ``f y,a \\left\\ \\begin array l 1 a y \\ \\ \\rm if \\ y 0 \\ \\ \\textrm root at a 1 \\textrm if a 1 \\\\ \\ \\ \\ 1 a y \\ \\ \\rm if \\ y 0 \\ \\ \\textrm root at a 1 \\textrm if a 1 \\end array \\right.`` we will ignore y 0 . Notice that for 1 a 1 there are two roots. \"\"\" begin f y,a sign y a y f y,a,t f y,a just for the difeq solver end md\"\"\" The graph of `z f y,a ` consists of two parallel half planes. On the left below we intersect that graph with planes of constant `a`. On the right, we have the intersection with `z 0`. \"\"\" md\"\"\" a bind a Slider 5 .1 5, show value true, default 0 \"\"\" begin plot y f y,a, 0 , 9, eps , xlabel \"y\" , ylabel \"sign y y a\", lw 3,color blue plot y f y,a, 0 , eps ,9, lw 3, color blue ylims 5,10 xlims 9,9 hline 0 , ls dash, color pink, legend false if a 1 annotate 2.2 a, 4, text round 1 a,digits 1 , position right,11, red scatter 1 a , 0 ,m circle,ms 10,color red end if a 1 annotate 1 a, 4, text round 1 a,digits 1 , position right,11, blue scatter 1 a , 0 ,m circle,ms 10,color blue end vline 1 a , ls dash, color gray vline 1 a , ls dash, color gray vline 0 ,ls dash, color pink P1 title \" Plot of f y,a a vs y\" begin plot a a 1, 5 1, lw 2, c blue annotate a, 3 a, \"a a\" plot a a 1, 1 5, lw 2, c blue, legend false xlabel \"a\" ylabel \"y\" title \"Solution to f y,a 0\" if a 1 annotate 4, .7 a, text round 1 a,digits 1 , position center,11, red hline 1 a ,c gray,ls dash scatter a , 1 a ,m circle,ms 10,color red end if a 1 annotate 4, 1.3 a, text round 1 a,digits 1 , position center,11, blue hline 1 a ,c gray,ls dash scatter a , 1 a ,m circle,ms 10,color blue end ylims 5,5 P2 annotate 0, 4.7, text \"Two roots if 1 a 1\",6, blue vline a ,c gray,ls dash plot P1 ,P2,layout 1, 2 end end md\"\"\" The above are two section views of the surface z f y,a . Left constant a. Right z 0. \"\"\" begin s1 surface 5 .1 .1, 4 .1 4, f, legend false s2 surface .00001 .1 4.1, 4 .1 4, f, legend false surface 6 .05 4, 5 .05 4, f, legend false, alpha .5 plot 0, 6 , 1, 5 , 0,0 , c red plot 0,5 , 1, 4 , 0,0 , c red xlabel \"y\" ylabel \"a\" end gr md\"\"\" Solution to y' f y,a with y 0 y₀ \"\"\" md\"\"\" y₀ bind y₀ Slider 6 .1 6, show value true, default 2.0 \"\"\" sol solve ODEProblem f, y₀, 0, 10.0 , a function plotit y₀, a sol solve ODEProblem f, y₀, 0, 10.0 , a p plot sol , legend false, background color inside black , ylims 7, 7 , lw 3, c red plot direction field xs Float64 ys Float64 lrx LinRange xlims p ..., 30 for x in lrx for y in LinRange ylims p ..., 30 v 1, f y, a ,x v . 100 lrx 2 lrx 1 push xs, x v 1 , x v 1 , NaN push ys, y v 2 , y v 2 , NaN end end if a 1 hline 1 a ,c white,ls dash end if a 1 hline 1 a ,c white,ls dash end hline 0 ,c pink,ls dash plot xs, ys, alpha 0.7, c yellow ylabel \"y\" annotate .5,y₀,text \"y₀\",color red if a 1 annotate 5,1 a,text round 1 a,digits 3 ,color white end if a 1 annotate 5, 1 a .4,text round 1 a,digits 3 ,color white end title \"Solution to y' f y,a \" return p end plotit y₀, a md\"\"\" Hysteresis Increasing then decreasing ``a`` Let's increase a by .25 from 4 to 4 then decrease from 4 to 4. Every time we change a, we let 10 units of time evolve, enough to reach the equilibriumf for that a, and watch the y values. We see that when 1 a 1 it's possible to be at the \"negative\" equilibrium or the \"positive\" equilibrium, depending on how you got there. \"\"\" let Δt 10 t 0.0 y₀ 5 a 4 sol solve ODEProblem f, y₀, t, t Δt , a p plot sol annotate 10, 5,text \"a \",7 for i 1 32 a .25 t Δt y₀ sol t sol solve ODEProblem f, y₀, t, t Δt , a if 1≤a ≤1 p plot sol,xlims 0, t , legend false, fillrange 5,fillcolor gray,alpha .5 else p plot sol,xlims 0, t , legend false end if i%4 0 annotate t, 5,text round a,digits 1 , 7, blue end end for i 1 32 a .25 t Δt y₀ sol t sol solve ODEProblem f, y₀, t, t Δt , a if 1≤a≤1 p plot sol,xlims 0, t , legend false, fillrange 5,fillcolor gray,alpha .5 else p plot sol,xlims 0, t , legend false end if i%4 0 annotate t, 5,text round a,digits 1 , 7, red end end ylabel \"y\" as svg plot end md\"\"\" The dependence of the state of a system on its history, what we observe above, is known as hysteresis Greek ὑστέρησις for lagging behind . \"\"\" md\"\"\" Application to Snowball Earth, the ice albedo feedback from Henri Drake's lecture Review of the last climate lecture. Recall from Lecture 20 Part I https www.youtube.com watch?v Gi4ZZVS2GLA&t 15s that the the zero dimensional energy balance equation is \\begin gather \\color brown C \\frac dT dt \\ \\color black \\ \\color orange \\frac 1 α S 4 \\ \\color black \\ \\color blue A BT \\ \\color black \\ \\color grey a \\ln \\left \\frac \\text CO ₂ \\text CO ₂ \\text PI \\right , \\end gather \"\"\" html\"\"\" img src \"https raw.githubusercontent.com hdrake hdrake.github.io master figures planetary energy balance.png\" height 230 \"\"\" md\"\"\" Today, we will ignore changes in CO₂, so that \\ln \\left \\frac \\text CO ₂ \\text PI \\text CO ₂ \\text PI \\right \\ln 1 0 and the model simplifies to \\begin gather \\color brown C \\frac dT dt \\ \\color black \\ \\color orange \\frac 1 α S 4 \\ \\color black \\ \\color blue A BT . \\end gather The dynamics of this Ordinary Differential Equation ODE are quite simple because it is linear we can rewrite it in the form \\dot T f T t where f x \\alpha x \\beta is a linear function of x. A linear ODE permits only one stable solution, \\dot T 0 , which in Lecture 20 we found was Earth's pre industrial temperature T 0 14 °C. In this lecture, we show how a small modification that makes one term in our simple climate model non linear completely changes its dynamics, allowing us to explain the existence of both \"Snowball Earth\" and the relatively warm pre industrial climate that allowed humans to thrive. \"\"\" md\"\"\" 1 Background Snowball Earth Geological evidence shows that the Neoproterozoic Era 550 to 1000 million years ago is marked by two global glaciation events, in which Earth's surface was covered in ice and snow from the Equator to the poles see review by Pierrehumbert et al. 2011 https www.annualreviews.org doi full 10.1146 annurev earth 040809 152447 . \"\"\" html\"\"\" img src \"https news.cnrs.fr sites default files styles asset image full public assets images frise earths glaciations 72dpi.jpg?itok MgKrHlIV\" height 500 \"\"\" html\"\"\" img src \"https upload.wikimedia.org wikipedia commons d df Ice albedo feedback.jpg\" height 350 \"\"\" md\"\"\" We can represent the ice albedo feedback crudely in our energy balance model by allowing the albedo to depend on temperature \\alpha T \\begin cases \\alpha i & \\mbox if \\ \\ T \\leq 10\\text °C &\\text completely frozen \\\\ \\alpha i \\alpha 0 \\alpha i \\frac T 10 20 & \\mbox if \\ \\ 10\\text °C \\leq T \\leq 10\\text °C &\\text partially frozen \\\\ \\alpha 0 &\\mbox if \\ \\ T \\geq 10\\text °C &\\text no ice \\end cases \"\"\" md\"\"\" 1.2 Adding the ice albedo feedback to our simple climate model First, we program albedo as a function of temperature. \"\"\" md\"\"\" To add this function into our energy balance model from Lecture 20 Part I https www.youtube.com watch?v Gi4ZZVS2GLA&t 15s which we've copied into the cell below , all we have to do is overwrite the definition of the `timestep ` method to specify that the temperature dependent albedo should be updated based on the current state \"\"\" module Model const S 1368 solar insolation W m^2 energy per unit time per unit area const α 0.3 albedo, or planetary reflectivity unitless const B 1.3 climate feedback parameter W m^2 °C , const T0 14. preindustrial temperature °C absorbed solar radiation α α, S S S 1 α 4 W m^2 outgoing thermal radiation T A A, B B A B T const A S 1. α 4 B T0 W m^2 . greenhouse effect CO2 a a, CO2 PI CO2 PI a log CO2 CO2 PI const a 5.0 CO2 forcing coefficient W m^2 const CO2 PI 280. preindustrial CO2 concentration parts per million ppm CO2 const t CO2 PI constant CO2 concentrations const C 51. atmosphere and upper ocean heat capacity J m^2 °C function timestep ebm append ebm.T, ebm.T end ebm.Δt tendency ebm append ebm.t, ebm.t end ebm.Δt end tendency ebm 1. ebm.C absorbed solar radiation α ebm.α, S ebm.S outgoing thermal radiation ebm.T end , A ebm.A, B ebm.B greenhouse effect ebm.CO2 ebm.t end , a ebm.a, CO2 PI ebm.CO2 PI begin mutable struct EBM T Array Float64,1 t Array Float64,1 Δt Float64 CO2 Function C Float64 a Float64 A Float64 B Float64 CO2 PI Float64 α Float64 S Float64 end Make constant parameters optional kwargs EBM T Array Float64,1 , t Array Float64,1 , Δt Float64, CO2 Function C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S EBM T, t, Δt, CO2, C, a, A, B, CO2 PI, α, S Construct from float inputs for convenience EBM T0 Float64, t0 Float64, Δt Float64, CO2 Function C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S EBM T0 , t0 , Δt, CO2 C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S end begin function run ebm EBM, end year Real while ebm.t end end year timestep ebm end end run ebm run ebm, 200. run for 200 years by default end CO2 hist t CO2 PI 1 . fractional increase t fractional increase t t . 1850. 220 .^3 begin hist EBM T0, 1850., 1., CO2 hist, C C, B B, A A run hist, 2020. end begin CO2 RCP26 t CO2 PI 1 . fractional increase t . min. 1., exp. t . 1850. . 170 100 RCP26 EBM T0, 1850., 1., CO2 RCP26 run RCP26, 2100. CO2 RCP85 t CO2 PI 1 . fractional increase t . max. 1., exp. t . 1850. . 170 100 RCP85 EBM T0, 1850., 1., CO2 RCP85 run RCP85, 2100. end end md\"\"\" 1.1 The ice albedo feedback In Lecture 20, we used a constant value α Model.hist.α for Earth's planetary albedo, which is a reasonable thing to do for small climate variations relative to the present such as the difference between the present day and preindustrial climates . In the case of large variations, however, this approximation is not very reliable. While oceans are dark and absorbant, α ocean \\approx 0.05 , ice and snow are bright and reflective \\alpha ice,\\,snow \\approx 0.5 to 0.9 . Thus, if much of the ocean's surface freezes over, we expect Earth's albedo to rise dramatically, causing more sunlight to be reflected to space, which in turn causes even more cooling and more of the ocean to freeze, etc. This non linear positive feedback effect is referred to as the ice albedo feedback see illustration below . \"\"\" function α T α0 Model.α, αi 0.5, ΔT 10. if T ΔT return αi elseif ΔT T ΔT return αi α0 αi T ΔT 2ΔT elseif T ΔT return α0 end end begin T example 20. 1. 20. plot size 600, 400 , ylims 0.2, 0.6 plot 20, 10 , 0.2, 0.2 , fillrange 0.6, 0.6 , color lightblue, alpha 0.2, label nothing plot 10, 20 , 0.2, 0.2 , fillrange 0.6, 0.6 , color red, alpha 0.12, label nothing plot T example, α. T example , lw 3., label \"α T \", color black plot ylabel \"albedo α\\n planetary reflectivity \", xlabel \"Temperature °C \" annotate 15.5, 0.252, text \"completely\\nfrozen\", 10, darkblue annotate 15.5, 0.252, text \"no ice\", 10, darkred annotate 0.3, 0.252, text \"partially frozen\", 10, darkgrey end function Model.timestep ebm ebm.α α ebm.T end Added this line append ebm.T, ebm.T end ebm.Δt Model.tendency ebm append ebm.t, ebm.t end ebm.Δt end md\"\"\" 2 Multiple Equilibria OR the existence of \"alternate Earths\" Human civilization flourished over the last several thousand years in part because Earth's global climate has been remarkably stable and forgiving. The preindustrial combination of natural greenhouse effect and incoming solar radiation yielded temperatures between the freezing and boiling points of water across most of the planet, allowing ecoystems based on liquid water to thrive. The climate system, however, is rife with non linear effects like the ice albedo effect , which reveal just how fragile our habitable planet is and just how unique our stable pre industrial climate was. We learned in Lecture 20 that in response to temperature fluctuations, net negative feedbacks act to restore Earth's temperature back towards a single equilibrium state in which absorbed solar radiation is balanced by outgoing thermal radiation. Here, we explore how non linear positive feedbacks can temporarily result in a net positive feedback and modify Earth's state space. \"\"\" md\"\"\" 2.1 Exploring the non linear ice albedo feedback In Lecture 20 Part II https www.youtube.com watch?v D3jpfeQCISU , we learned how introducing non linear terms in ordinary differential equations can lead to complex state spaces that allow for multiple fixed points e.g. for \\dot x \\mu x^ 2 . Let's explore how this plays out with the non linear ice albedo feedback by varying the initial condition T 0 \\equiv T t 0 and allowing the system to evolve for 200 years. \"\"\" begin T0 slider bind T0 interact Slider 60. 0.0025 30., default 24., show value true md\"\"\" T₀ T0 slider °C\"\"\" end begin ebm interact Model.EBM Float64 T0 interact , 0., 1., Model.CO2 const Model.run ebm interact, 200 end md\"We can get an overview of the behavior by drawing a set of these curves all on the same graph \" begin p equil plot xlabel \"year\", ylabel \"temperature °C \", legend bottomright, xlims 0, 205 , ylims 60, 30. plot 0, 200 , 60, 60 , fillrange 10., 10. , fillalpha 0.3, c lightblue, label nothing annotate 120, 20, text \"completely frozen\", 10, darkblue plot 0, 200 , 10, 10 , fillrange 30., 30. , fillalpha 0.09, c red, lw 0., label nothing annotate 120, 25, text \"no ice\", 10, darkred for T0 sample in 60. 5. 30. ebm Model.EBM T0 sample, 0., 1., Model.CO2 const Model.run ebm, 200 plot p equil, ebm.t, ebm.T, label nothing end T un 7.5472 for δT in 1.e 2 2, 1., 0., 1., 2. ebm un Model.EBM T un δT, 0., 1., Model.CO2 const Model.run ebm un, 200 plot p equil, ebm un.t, ebm un.T, label nothing, linestyle dash end plot p equil, 200 , Model.T0 , markershape circle, label \"Our pre industrial climate stable ''warm'' branch \", color orange, markersize 8 plot p equil, 200 , 38.3 , markershape circle, label \"Alternate universe pre industrial climate stable ''cold'' branch \", color aqua, markersize 8 plot p equil, 200 , T un , markershape diamond, label \"Impossible alternate climate unstable branch \", color lightgrey, markersize 8, markerstrokecolor white, alpha 1. p equil end md\"We see that for T₀ ⪆ round T un, digits 2 °C, all of the curves seem to converge on the T 14°C equilibrium or fixed point that we saw in Lecture 20. Curves that start below this value warm up and while curves that start above this value will cool down. For T₀ ⪅ round T un, digits 2 °C, however, the temperatures converge on a much colder equilibrium around T 40°C. This is the Snowball Earth equilibrium. These two states are referred to as stable equilibria because even if the state gets temporarily pushed slightly away from its equilibrium, it will eventually converge right back to its equilibrium. So what happens is T₀ ≈ round T un, digits 2 °C? For some exact temperature near there, there is indeed an equilibrim state if you start with that temperature you will stay there forever. However, if the temperature starts off even one one hundredth of a degree above or below this exact value, we see that temperatures eventually converge to one of the other two equilibria. Thus, we call this intermediate equilibrium an unstable equilibrium , because any infinitesimal push away will cause it to careen away towards another state. \" md\"\"\" 2.2 Radiative stability analysis We can understand why our model has two stable equilibria and one unstable equilibrium by applying concepts from dynamical systems theory. Recall that, with fixed CO₂ concentrations, our energy balance model differential equation can be expressed as C\\frac d\\,T d\\,t \\text ASR T \\text OTR T , where now the Absorbed Solar Radiation ASR is also temperature dependent because the albedo α T is. In particular, by plotting the right hand side tendency terms as a function of the state variable T , we can plot a stability diagram for our system that tells us whether the planet will warm C \\frac d\\,T d\\,t 0 or cool C \\frac d\\,T d\\,t 0 . \"\"\" begin T samples 60. 1. 30. OTR Model.outgoing thermal radiation. T samples ASR Model.absorbed solar radiation. α α T sample for T sample in T samples imbalance ASR . OTR end begin p1 stability plot legend topleft, ylabel \"energy flux W m² \", xlabel \"temperature °C \" plot p1 stability, T samples, OTR, label \"Outgoing Thermal Radiation\", color blue, lw 2. plot p1 stability, T samples, ASR, label \"Absorbed Solar Radiation\", color orange, lw 2. p2 stability plot ylims 50, 45 , ylabel \"energy flux W m² \", xlabel \"temperature °C \" plot 60., 30. , 100, 100 , fillrange 0, 0 , color blue, alpha 0.1, label nothing plot 60., 30. , 100, 100 , fillrange 0, 0 , color red, alpha 0.1, label nothing annotate 58, 40, text \"cooling\", left, darkblue annotate 58, 38, text \"warming\", left, darkred plot p2 stability, T samples, imbalance, label \"Radiative Imbalance\\n ASR OTR \", color black, lw 2. plot 7.542 , 0 , markershape diamond, markersize 6, color lightgrey, markerstrokecolor darkgrey, label nothing plot Model.T0 , 0 , markershape circle, markersize 6, color orange, markerstrokecolor black, label nothing plot 38.3 , 0 , markershape circle, markersize 6, color aqua, markerstrokecolor black, label nothing p stability plot p1 stability, p2 stability, layout 1, 2 , size 680, 300 end md\" 3 Transitioning to and from Snowball Earth 3.1 Turning up the Sun Over the entire history of the Earth, the Sun is thought to have brightened by about 40%.\" html\"\"\" img src \"https rainbow.ldeo.columbia.edu courses s4021 ec98 fys.gif\" alt \"Plot showing increasing solar luminosity over time\" height 300 \"\"\" md\"In the Neoproterozoic ~700 million years ago , the Sun was 93% as bright as it is today, such that the incoming solar radiation was S 1272 W m², Earth's average temperature plunged to T 50 °C, and Earth's ice covered surface had a high albedo reflectivity of α i 0.5 . 3.2 Did the increasing brightness of the Sun melt the Snowball? If we start out in the Neoproterozoic climate and all we do is increase solar insolation to today's value of S 1368 W m², can we warm the planet up to the pre industrial temperature of T 14 °C? \" md\"\"\" Extend upper limit of insolation bind extend S CheckBox default false \"\"\" if extend S md\"\"\" \"Cold\" branch bind show cold CheckBox default false | \"Warm\" branch bind show warm CheckBox default false | Unstable branch bind show unstable CheckBox default false \"\"\" else show cold true nothing end if extend S md\"\"\" Abrupt climate transitions In this model, temperature variations are fairly smooth unless temperatures rise above 10°C or fall below 10°C, in which case the ice albedo positive feedback kicks in and causes an abrupt climate transition . While this is just a simple hypothetical model, these kinds of abrupt climate transitions show up all the time in the paleoclimate record and in more realistic climate model simulations. https www.pnas.org content pnas 115 52 13288 F1.large.jpg This simulation teaches us that we should not take the stability of our climate for granted and that pushing our present climate outside of its historical regime of stability could trigger catastrophic abrupt climate transitions. \"\"\" end md\"\"\" 3.3 If not the Sun, how did Snowball Earth melt? The leading theory is that a slow but steady outgassing of CO₂ from volcanoes eventually caused a strong enough greenhouse gas effect to offset the cooling effect of the frozen surface's high albedo and raise temperatures above the melting point 10 °C. \"\"\" html\"\"\" img src \"https hartm242.files.wordpress.com 2011 03 snowball earth.jpg\" width 680 \"\"\" md\"\"\" In Homework 9 , you will extend the above model to include the effect of CO₂ and determine how much CO2 would need to be added to the snowball for it to melt. \"\"\" md\"\"\" 4 Towards realistic climate modelling In this simple model, the preindustrial climate of T 14 °C is so warm that there is no ice anywhere on the planet. Indeed, the only two valid stable climates are one with no ice or one with ice everywhere . So how did Earth's preindustrial climate, which was relatively stable for thousands of years, have substantial ice caps at the poles? \"\"\" md\"\"\" The \"Aquaplanet\", a simple three dimensional ocean world climate model An \"Aquaplanet\" is a three dimensional global climate simulation of a hypothetical planet covered entirely by a single global Ocean. While this is of course very different from Earth, where 27% of the planet is covered in land, the \"Aquaplanet\" exhibits many of the same characteristics as Earth and is much more realistic than our zero dimensional climate model above. The video below shows that the Aquaplanet simulation exhibits a third equilibrium state, with a mostly liquid ocean but ice caps at the poles , in addition to the two we found in our zero dimensional model. In Homework 10 2020 class , you will build a simple two dimensional version of the aqua planet and explore its stability. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid lYm IyBHMUs params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" Appendix \"\"\" md\" Pluto Magic\" function ingredients path String this is from the Julia source code evalfile in base loading.jl but with the modification that it returns the module instead of the last object name Symbol basename path m Module name Core.eval m, Expr toplevel, eval x Expr core, eval name, x , include x Expr top, include name, x , include mapexpr Function, x Expr top, include mapexpr, name, x , include path m end md\"\"\" Bifurcation diagram helper functions \"\"\" function restart ebm ebm ebm.T ebm.T end ebm.t ebm.t 1 end function plot trajectory p, x, y lw 8 n size x,1 plot x, y, color \"black\", alpha collect 1 n 1 n 1. , linewidth collect 0. lw n lw , label nothing plot x end , y end , color \"black\", marker c, markersize lw 2 1.2, label nothing, markerstrokecolor nothing, markerstrokewidth 0. return p end begin Smin 1200 Smax 1800 Smax limited 1650 Svec Smin 1. Smax Svec vcat Svec, reverse Svec Tvec zeros size Svec local T restart 100. for i, S enumerate Svec ebm Model.EBM T restart, 0., 5., Model.CO2 const ebm.S S Model.run ebm, 400. T restart ebm.T end Tvec i deepcopy T restart end md\" Data structures for storing warm & cool branch climates \" end md\"\"\" For insolations S between Smin W m² and Smax limited W m², temperatures always remain below 10°C and the planet remains completely frozen. What if we extend the upper limit on insolation so that the Sun becomes bright enough to start melting ice? \"\"\" begin T unstable branch S, A, B, αi, α0 A B S 4B 1 0.5 αi α0 1 S αi α0 80B S unstable Smin 2 Smax T unstable T unstable branch. S unstable, Model.A, Model.B, 0.5, 0.3 T unstable .~ 0.3 . α. T unstable . 0.5 . NaN md\" Unstable branch solution \" end function add labels p plot p, xlabel \"year\", ylabel \"temperature °C \", legend bottomright, xlims 5, 205 , ylims 60, 30. plot p, 5, 200 , 60, 60 , fillrange 10., 10. , fillalpha 0.3, c lightblue, label nothing annotate 120, 20, text \"completely frozen\", 10, darkblue plot p, 5, 200 , 10, 10 , fillrange 30., 30. , fillalpha 0.09, c red, lw 0., label nothing annotate p, 120, 25, text \"no ice\", 10, darkred end begin p interact plot ebm interact.t, ebm interact.T, label nothing, lw 3 plot 0. , T0 interact , label nothing, markersize 4, markershape circle add labels p interact end | as svg begin Sneo Model.S 0.93 Tneo 48. md\" Initial conditions \" end begin if extend S solarSlider bind S Slider Smin 2. Smax, default Sneo md\"\"\" Smin W m² solarSlider Smax W m²\"\"\" else solarSlider bind S Slider Smin 2. Smax limited, default Sneo md\"\"\" Smin W m² solarSlider Smax limited W m²\"\"\" end end begin md\"\"\" Move the slider below to change the brightness of the Sun solar insolation S S W m² \"\"\" end begin ebm Model.EBM Tneo, 0., 5., Model.CO2 const ebm.S Sneo ntraj 10 Ttraj repeat NaN , ntraj Straj repeat NaN , ntraj md\" Data structures for storing trajectories of recent climates \" end begin S warming mask 1 size Svec 1 . size Svec . 2 p plot xlims Smin, Smax limited , ylims 55, 75 , title \"Earth's solar insolation bifurcation diagram\" plot Model.S, Model.S , 55, 75 , color yellow, alpha 0.3, lw 8, label \"Pre industrial present insolation\" if extend S plot p, xlims Smin, Smax if show cold plot Svec warming mask , Tvec warming mask , color blue,lw 3., alpha 0.5, label \"cool branch\" end if show warm plot Svec . warming mask , Tvec . warming mask , color \"red\", lw 3., alpha 0.5, label \"warm branch\" end if show unstable plot S unstable, T unstable, color darkgray, lw 3., alpha 0.4, ls dash, label \"unstable branch\" end end plot legend topleft plot xlabel \"solar insolation S W m² \", ylabel \"Global temperature T °C \" plot Model.S , Model.T0 , markershape circle, label \"Our preindustrial climate\", color orange, markersize 8 plot Model.S , 38.3 , markershape circle, label \"Alternate preindustrial climate\", color aqua, markersize 8 plot Sneo , Tneo , markershape circle, label \"neoproterozoic 700 Mya \", color lightblue, markersize 8 plot trajectory p, reverse Straj , reverse Ttraj , lw 9 plot Smin, Smax , 60, 60 , fillrange 10., 10. , fillalpha 0.3, c lightblue, label nothing annotate Smin 12, 19, text \"completely\\nfrozen\", 10, darkblue, left plot Smin, Smax , 10, 10 , fillrange 80., 80. , fillalpha 0.09, c red, lw 0., label nothing annotate Smin 12, 15, text \"no ice\", 10, darkred, left end | as svg begin S restart ebm ebm ebm.S S Model.run ebm, 500 insert Straj, 1, copy S pop Straj insert Ttraj, 1, copy ebm.T end pop Ttraj end "},{"url":"climate_science/how_to_collaborate_on_software/","title":"GitHub & Open Source Software","tags":["lecture","module3","track_julia","track_data","track_climate","github","open source","collaboration","pull request","fork","software development"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v 7N9Vvc8amGM\" image \"https user images.githubusercontent.com 6933510 136199704 ba6d0586 34bf 490c 8fd0 6959ab42cd23.png\" section 5 order 5 title \"GitHub & Open Source Software\" layout \"layout.jlhtml\" youtube id \"7N9Vvc8amGM\" description \"\" tags \"lecture\", \"module3\", \"track julia\", \"track data\", \"track climate\", \"github\", \"open source\", \"collaboration\", \"pull request\", \"fork\", \"software development\" using Markdown using InteractiveUtils begin using HypertextLiteral using PlutoTest using PlutoUI end md\"\"\" How to collaborate on software What is the simplest way to collaborate on project online? You can email files to each other This works for small projects, but at some point, you will need something more suited to the task. For example Encyclopedia a wiki system School essay Google Drive Software project GitHub For software projects, people of use GitHub, or similar git based tools to collaborate on their work. Why is that? \"\"\" TableOfContents aside false, depth 1 md\"\"\" Why not Google Drive? \"\"\" md\"\"\" Google Drive or Dropbox, Nextcloud, etc is a platform for realtime collaboration on files. Besides synchronizing single files like the video above , Google Drive also allows you to synchronize entire folders between multiple computers and the cloud. That's awesome So do people use it for collaborative software projects? Well, generally not. Software projects have some unique properties that require different tools. As today's introduction into git , let's look at the differences between a software project and a non software project , and why platforms like Google Drive don't work well for collaborating on code. Note that we will take a different perspective that other introductions to git . Let's start out with a realtime synchronization system like Google Drive as our default, and think about why we might want to change its functionality to make it more suitable for software. \"\"\" md\"\"\" Reason 1 sensitive to change Reason 1 In software projects, small changes often make a big difference. Changes to one file affect the entire program. For example, here is what Pluto.jl looks like after adding a single character to the source code \"\"\" md\"\"\" While a single character can mean disaster, this sensitivity also means that programming languages are quite powerful A small change will often do the trick. \"\"\" md\"\"\" Synchronizing every change This sensitivity to changes makes realtime synchronization unsuitable for software projects. To see why, let's say that I want to change `sqrt` to `log` in the following code \"\"\" md\"\"\" If you would synchronize every change while making that edit removing the letters `s`, `q`, `r`, `t`, typing `l`, `o`, `g` , then you are publishing broken code to your online code repository. If you were sharing changes in realtime like Google Drive , then other people would get those broken states while working on their features. \"\"\" md\"\"\" Synchronizing every keystroke \"\"\" md\"\"\" However, if we only publish code manually , we can make sure that we never publish broken code. This means that my collaborators, who might be working on the project at the same time, will not have to deal with errors that are coming from my work in progress changes. \"\"\" md\"\"\" Synchronizing manually \"\"\" md\"\"\" Even larger chunks In the previous example, we found that we can avoid publishing invalid syntax by not synchronizing on every keystroke like Google Docs, but by synchronizing manually. We also don't want to synchronize the file every time it is saved , because you often try multiple things before finding something that works. \"\"\" md\"\"\" Synchronizing every save \"\"\" md\"\"\" Synchronizing manually \"\"\" pass2 html\" span style 'opacity .3' ✅ span \" pass \"✅\" | HTML fail \"❌\" | HTML md\"\"\" Reason 2 branches and forks Reason 2 It should be easy to split a project into multiple branches copies of the codebase that can be worked on independently. And after some time, we want to compare branches and merge the new additions into one. Something that you may have done create a copy of your `presentation.ppt` called `presentation with pictures.ppt` before adding your pictures, and if it didn't work, you can go back to yesterday's backup. Now that's great, but what if your teammate also made a copy `presentation with titlepage.ppt` where they also made a change? In this situation, you have created two branches of the same file in parallel, and it will be difficult to combine those changes back into a single `presentation this one really.ppt` file. \"\"\" md\"\"\" Reason 3 automation Reason 3 Git is a platform for automation , and can be a mechanism to automatically test and review changes, release weekly versions, etc etc. Programmers love to automate things, and git allows all sorts of automation. At first, this can be intimidating , and it will take years of experience to learn all the possible tricks. Someone who does 'git magic' as a profession is called a DevOps https en.wikipedia.org wiki DevOps engineer . Don't worry about this too much when getting started with GitHub, and ask others for help if necessary. \"\"\" md\"\"\" Automatic testing While we don't recommend learning about git automation right away, one thing you will probably encounter when contributing to an open source project is automatic testing . More on this later \"\"\" bigbreak html\"\"\" br br br br br br br br \"\"\" bigbreak bigbreak md\"\"\" Our first contribution documentation Today will be a kick start into contributing to open source projects The easiest way to make a great contribution to packages is to contribute a documentation improvement . For our example today, we will look at the package ClimateMARGO.jl https github.com ClimateMARGO ClimateMARGO.jl , a climate economic model. This package is hosted on GitHub, which means that all code and previous versions of code is available at github.com ClimateMARGO ClimateMARGO.jl https github.com ClimateMARGO ClimateMARGO.jl . The authors also use the same GitHub site to manage their TO DOs — called Issues https github.com ClimateMARGO ClimateMARGO.jl issues — and to accept contributions from others — called Pull Requests https github.com ClimateMARGO ClimateMARGO.jl pulls . Today, we are going to make a Pull Request \"\"\" md\"\"\" What is a Pull Request? Julia packages are open source , which means that you are allowed and encouraged to make your own copy of the source code we call this a fork and modify it. If you made some good changes on your fork then you can suggest to changes to the original project by submitting your code as a Pull Request PR . This is a feature built in to GitHub, and this is how most code collaboration happens online. \"\"\" md\"\"\" Video tutorial will be available here after the live lecture. \"\"\" md\"\"\" After the Pull Request Submitting your Pull Request is a great feeling, but it's not the last step After your submission, the PR will be reviewed by the project authors, and they will leave their feedback. It is common that a PR is not merged accepted directly, but that you are asked to make some small changes. This is possible because a Pull Request is not static — you can make changes to the PR after submitting it Remember that a PR is from one branch to another . You can make changes to your PR simply by adding additional commits to the from branch. \"\"\" md\"\"\" As an example of a Pull Request in the wild, let's look at this PR to `JuliaLang julia` github.com JuliaLang julia pull 40596 https github.com JuliaLang julia pull 40596 . The code change is not so important to us right now, but pay attention to the communication around the code change. \"\"\" md\"\"\" How to use GitHub Enough talk, let's create our first repository \"\"\" md\"\"\" Requirements To follow this introduction, you will need Create an account at github.com github.com . Add a friendly profile picture For Windows & MacOS users, download GitHub Desktop https desktop.github.com , a graphical program to easily manage git repositories on your disk. Linux users can use the slightly more advanced program GitKraken https www.gitkraken.com . A code editor. If you don't have a code editor yet, or if you are using Notepad, gedit or Notepad , we highly recommend VS Code https code.visualstudio.com , an open source, beginner friendly programming environment. It may seem like something new and difficult to learn, but it is easy to use, and because it understands the code that you are writing, it will help you as a beginner programmer. \"\"\" md\"\"\" Video tutorial will be available here after the live lecture. \"\"\" md\"\"\" ⛑ Git trouble Sooner or later, you will find yourself in git trouble . Unlike Google Drive, it can happen that synchronization requires manual intervention, for example, git might say `🤐 merge conflict` Which means that someone changed a file right before you were going to commit. \"\"\" md\"\"\" Fonsi's Git Trouble Fix™ Many git problems have an 'official' solution, and it will take time to learn all of these techniques. In the meantime, here is my fool proof way of solving many tricky git situations \"\"\" md\"\"\" Step 1 Use GitHub Desktop to find out which files you have changed. Are the changes important? Step 2 Take any files that you want to keep, and copy them to a different folder, e.g. your desktop. Step 3 Remove your local clone of the repository, and move any leftover files to the trash. Step 4 Clone the repository again, and copy your files back to their original place, overwriting the cloned files. \"\"\" bigbreak md\"\"\" How to fork and contribute code \"\"\" md\"\"\" Video tutorial will be available here after the live lecture. \"\"\" md\"\"\" Tests People often write tests for their code As part of the codebase, there will be a `test` folder containing scripts that will import the project and run some basic checks. \"\"\" function double x x x end test double 3 6 test double 0 0 md\"\"\" One reason to write tests is to pin specific behaviour, protecting yourself from accidentally changing it later. For example, if you fix a bug that `double 2 ` returns `40` instead of `4`, you would also add a test for `double 2 4`. Months later, when you are changing `double` for another use case, the old test insures that you are not accidentally breaking something that was once fixed. \"\"\" md\"\"\" Running tests You can run the tests of any Julia package by opening the Julia REPL, and typing ```julia julia v1.6 pkg test Example ``` As mentioned before, many project use github magic to automatically run their tests on a server for every change, and you can view the test results online. An important application is running tests for every Pull Request. This means that package authors can quickly review your changes, knowing that it does not break anything. \"\"\" bigbreak md\"\"\" Extra tips \"\"\" md\"\"\" Contributing does not only mean contributing code https opensource.guide how to contribute https opensource.guide how to contribute \"\"\" md\"\"\" Create a \"test\" repository Create multiple \"\"\" md\"\"\" Appendix \"\"\" function ingredients path String this is from the Julia source code evalfile in base loading.jl but with the modification that it returns the module instead of the last object name Symbol basename path m Module name Core.eval m, Expr toplevel, eval x Expr core, eval name, x , include x Expr top, include name, x , include mapexpr Function, x Expr top, include mapexpr, name, x , include path m end Layout ingredients download \"https raw.githubusercontent.com fonsp disorganised mess 5a59ad7cff1e760b997a54ffa0f8fa202ac16db3 Layout.jl\" stackrows x permutedims hcat x... , 2, 1 flex x Union AbstractVector,Base.Generator kwargs... flex x... kwargs... begin Base. kwdef struct Div contents style Dict end Div x Div contents x function Base.show io IO, m MIME\"text html\", d Div h htl \"\"\" div style d.style d.contents div \"\"\" show io, m, h end end outline x Div x, Dict \"border\" \"3px solid rgba 0,0,0,.3 \", \"border radius\" \"3px\", \"padding\" \"5px\", Div md\"\"\" ```julia function height p c1 sqrt p c2 end ``` \"\"\", md\"to\", md\"\"\" ```julia function height p c1 log p c2 end ``` \"\"\" , Dict \"display\" \"flex\", \"justify content\" \"space evenly\", \"align items\" \"center\", function flex args... kwargs... Div contents collect args , style Dict \"display\" \"flex\", \"flex \" String k string v for k, v in kwargs ... end flex md\"\"\" Before Schermafbeelding 2021 05 04 om 18 51 27 https user images.githubusercontent.com 6933510 117040056 c10b8280 ad09 11eb 9384 d211156440b1.png \"\"\", md\"\"\" After a single change Schermafbeelding 2021 05 04 om 18 50 00 https user images.githubusercontent.com 6933510 117039958 a46f4a80 ad09 11eb 90fa c1264d896648.png \"\"\" | outline function grid items AbstractMatrix fill width Bool true Div contents Div. vec permutedims items, 2,1 , style Dict \"display\" fill width ? \"grid\" \"inline grid\", \"grid template columns\" \"repeat size items,2 , auto \", \"column gap\" \"1em\", , end grid nothing md\"`sqrt`\" md\"`sq`\" md\"` `\" md\"`lo`\" md\"`log`\" \"Your computer local \" pass fail fail fail pass \"Online remote \" pass fail fail fail pass grid nothing md\"`sqrt`\" md\"`sq`\" md\"` `\" md\"`lo`\" md\"`log`\" md\"Your computer local \" pass fail fail fail pass md\"Online remote \" pass nothing nothing nothing pass grid nothing md\"`sqrt`\" md\"Let's try `sin`\" md\"Let's try `cos`\" md\"Try `log`\" \"Your computer local \" pass fail fail pass \"Online remote \" pass fail fail pass grid nothing md\"`sqrt`\" md\"Let's try `sin`\" md\"Let's try `cos`\" md\"Try `log`\" \"Your computer local \" pass fail fail pass \"Online remote \" pass nothing nothing pass vocabulary x grid stackrows htl \" span style 'font size 1.2rem font weight 700 ' code k code span \" , v for k, v in x fill width false htl \" em remote em \" md\"The version that is on the internet, you browse the remote on github.com.\", htl \" em local em \" md\"What's on your computer. Use `pull`, `commit` and `push` to synchronize this with the remote. Google Drive does this all the time.\" | vocabulary \"fetch\" md\"Make your local git aware of any changes online. Do this often \", \"pull\" md\"Apply any changes on the remote version to your local copy. This will get the two in sync . Do this often \", \"commit\" md\"Create a collection of changes to files, ready to be `push`ed.\", \"push\" md\"Publish any local `commit`s to the remote version.\", | vocabulary function aside x htl \"\"\" style media min width calc 700px 30px 300px aside.plutoui aside wrapper position absolute right 11px width 0px transform translate 0, 40% aside.plutoui aside wrapper div width 300px style aside class \"plutoui aside wrapper\" div x div aside \"\"\" end html\"\"\" p If you have not used Google Drive before, here is a small demonstration p video src \"https user images.githubusercontent.com 6933510 117038375 d8497080 ad07 11eb 8260 34e96414131a.mov\" data canonical src \"https user images.githubusercontent.com 6933510 117038375 d8497080 ad07 11eb 8260 34e96414131a.mov\" controls \"controls\" muted \"muted\" class \"d block rounded bottom 2 width fit\" style \"max height 640px \" video \"\"\" | aside md\"\"\" ^sidenote Language design side note There are languages that are designed to be robust against small changes, and you can actually use realtime collaboration with those Examples are glitch.com glitch.com for collaborative HTML and CSS editing these languages can ignore syntax errors and continue , and the more experimental language Dark https darklang.com which uses a special editor that does not allow you to type errors . \"\"\" | aside md\"\"\" ^1 If you are working on a fork , then by creating a PR, you also give the original project authors access to make changes to the from branch of the PR. This is a useful feature, allowing you to work together on the same codebase. \"\"\" | aside md\"\"\" Test driven design Some people like to first write their tests, which will fail initially, and then write the code to solve the problem. While working on the code, they keep re running the tests, until all checks are green This can be an effective and rewarding way to work on a software problem If you are following this course, then you will already be familiar with this concept The homework exercises were all designed with the test driven principle. \"\"\" | aside "},{"url":"climate_science/inverse_climate_model/","title":"Climate Economics","tags":["lecture","module3","climate","modeling","track_climate","track_math","plotting","track_julia","economics","optimization","JuMP","simulation"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v nm86 hDwYTU\" image \"https user images.githubusercontent.com 6933510 136199660 315b045b f144 4009 9282 7fe6d1f5d41b.gif\" section 10 order 10 title \"Climate Economics\" layout \"layout.jlhtml\" youtube id \"nm86 hDwYTU\" description \"\" tags \"lecture\", \"module3\", \"climate\", \"modeling\", \"track climate\", \"track math\", \"plotting\", \"track julia\", \"economics\", \"optimization\", \"JuMP\", \"simulation\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import Pkg ENV \"JULIA MARGO LOAD PYPLOT\" \"no thank you\" Pkg.activate mktempdir Pkg.add Pkg.PackageSpec name \"Plots\", version \"1\" , Pkg.PackageSpec name \"ClimateMARGO\", version v\"0.3.2\" , Pkg.PackageSpec name \"PlutoUI\", version \"0.7\" , Pkg.PackageSpec name \"AbstractPlutoDingetjes\" , Pkg.PackageSpec name \"HypertextLiteral\", version \"0.9\" , Pkg.PackageSpec name \"Underscores\", version \"2\" , using Plots using Plots.Colors using ClimateMARGO using ClimateMARGO.Models using ClimateMARGO.Optimization using ClimateMARGO.Diagnostics using PlutoUI using AbstractPlutoDingetjes using HypertextLiteral using Underscores Plots.default linewidth 5 html\"\"\" style loading message display none style \"\"\" end TableOfContents html\" div id 'loading message' style 'font size 2rem font style italic font family system ui font weight 700 margin 2rem ' Loading packages, please wait... div \" const initial 1 Dict \"M\" 2090, 6 const initial 2 Dict \"M\" 2080, .7 const initial 3 Dict \"M\" 2080, .7 const initial 4 Dict \"M\" 2080, 0.7 , \"R\" 2120, 0.2 , HTML \"\"\" p When editing this notebook inside Pluto, use the img src \"https cdn.jsdelivr.net gh ionic team ionicons 5.5.1 src svg eye outline.svg\" style \"width 1em height 1em margin bottom .2em \" icon next to cells to view their code. p \"\"\" blob el, color \"red\" htl \"\"\" div style \" background color padding 1em 1em 1em 1em border radius 2em \" el div \"\"\" function infeasablewarning x htl \"\"\" margo infeasible x margo infeasible label No solution found margo infeasible label margo infeasible style margo infeasible opacity .1 margo infeasible margo infeasible label opacity .7 display block position absolute transform translate 50%, 50% font family \"Vollkorn\", system ui font style italic font size 40px top 50% left 50% white space nowrap style \"\"\" end status ok x x ∈ \"OPTIMAL\", \"LOCALLY SOLVED\", \"ALMOST OPTIMAL\", \"ALMOST LOCALLY SOLVED\" feasibility overlay x status ok x.status ? as html infeasablewarning begin hidecloack name HTML \"\"\" style plj cloack. name opacity 0 display block style \"\"\" \"A trick to hide a cell without creating a variable dependency, to make it simpler for PlutoSliderServer.jl.\" cloak name x htl \" plj cloack class name x plj cloak \" end colors baseline colorant\" dddddd\", baseline emissions colorant\" dddddd\", baseline concentrations colorant\" dddddd\", baseline temperature colorant\" dddddd\", baseline damages colorant\" dddddd\", temperature colorant\" edc949\", above paris colorant\" e1575910\", M colorant\" 4E79A7\", R colorant\" F28E2C\", A colorant\" 59A14F\", G colorant\" E15759\", T max colorant\" 00000080\", controls colorant\" af7aa1\", damages colorant\" e15759\", avoided damages colorant\" e49734\", benefits colorant\" 7abf5e\", emissions colorant\"brown\", emissions 1 colorant\" 4E79A7\", concentrations colorant\"brown\", names baseline \"Baseline\", baseline emissions \"Baseline\", baseline concentrations \"Baseline\", baseline temperature \"Baseline\", baseline damages \"Baseline\", temperature \"Temperature\", above paris \"Above Paris\", M \"Mitigation\", R \"Removal\", A \"Adaptation\", G \"Geo engineering\", T max \"Goal temperature\", controls \"Controls\", damages \"Damages\", avoided damages \"Avoided Damages\", benefits \"Benefits\", emissions \"Emissions\", emissions 1 \"Emissions\", concentrations \"Concentrations\", assert keys colors keys names begin struct BondDefault x default end Base.get bd BondDefault bd.default Base.show io IO, m MIME\"text html\", bd BondDefault Base.show io, m, bd.x BondDefault end bind which graph 2 Select \"Emissions\" \"Concentrations\" \"Temperature\" bind which graph 4 Select \"Emissions\" \"Concentrations\" \"Temperature\" \"Costs and benefits\" Tmax 9 slider bind Tmax 9 Slider 0 0.1 5 default 2 allow overshoot 9 cb bind allow overshoot 9 CheckBox function multiplier unit Real, factor Real 2, suffix String \"%\" h htl \"\"\" script const unit unit const factor factor const suffix suffix const input html` input type range min 1 max 1 step .01 value 0 ` const output html` input disabled style \"width 1.8em display inline block overflow x hidden \" input ` const output html`` const left Math.round 100 factor \"%\" const right Math.round 100 factor \"%\" const reset html` a href \" \" title \"Reset\" style 'padding left .5em' img width \"14\" src \"https cdn.jsdelivr.net gh ionic team ionicons 5.0.0 src svg arrow undo sharp.svg\" img a ` const span html` div style \"margin left 2em \" \\ left \\ input \\ right \\ reset div ` const on slider output.value Math.round 100 Math.pow factor, input.value input.title Math.round 100 Math.pow factor, input.value \"%\" reset.style.opacity input.valueAsNumber 0 ? \"0\" \"1\" input.oninput on slider on slider const on box input.value output.value reset.style.opacity input.valueAsNumber 100 ? \"0\" \"1\" output.oninput on box reset.onclick e input.value 0 on slider e.preventDefault span.dispatchEvent new CustomEvent \"input\", Object.defineProperty span, \"value\", get unit Math.pow factor, input.value , set val input.value Math.log2 val unit Math.log2 factor on slider return span script \"\"\" BondDefault h, unit end function Carousel elementsList wraparound Bool false, peek Bool true, assert peek carouselHTML map elementsList do element htl \"\"\" div class \"carousel slide\" element div \"\"\" end h htl \"\"\" div style .carousel box width 100% overflow hidden .carousel container top 0 left 0 display flex width 100% flex flow row nowrap transform translate 10%, 0px transition transform 200ms ease in out .carousel controls display flex justify content center align items center .carousel controls button margin 8px width 6em .carousel slide min width 80% style script const div currentScript.parentElement const buttons div.querySelectorAll \"button\" const max length elementsList let count 0 const mod n, m n % m m % m const clamp x, a, b Math.max Math.min x, b , a const update ui count buttons 0 .disabled wraparound && count 0 buttons 1 .disabled wraparound && count max 1 div.querySelector \".carousel container\" .style `transform translate \\ 10 count 80 %, 0px ` const onclick e const new count count parseInt e.target.dataset.value if wraparound count mod new count, max else count clamp new count, 0, max 1 div.value count 1 div.dispatchEvent new CustomEvent \"input\" update ui div.value 1 e.preventDefault buttons.forEach button button.addEventListener \"click\", onclick div.value count 1 update ui div.value 1 script div class \"carousel box\" div class \"carousel container\" carouselHTML div div div class \"carousel controls\" button data value \" 1\" Previous button button data value \"1\" Next button div div \"\"\" BondDefault h,1 end preindustrial concentrations 280 end of oil 2150 cannot mitigate when fossil fuels are depleted pp k, color c, label n for k, c, n in zip keys names , colors, names ... gauss stdev 30 begin default usage error error \"Example usage \\n\\n intially 1,2 bind x f x \\n\" macro initially Any default usage error end macro initially default, bind expr Expr if bind expr.head macrocall || bind expr.args 1 Symbol \" bind\" return default usage error end warn if the first argument is a bind if default isa Expr && default.head macrocall && default.args 1 Symbol \" bind\" return default usage error end esc intially function default, bind expr end function intially function default, bind expr sym bind expr.args 3 gensym setval bond quote if isdefined sym sym default end setval sym bond bind sym bind expr.args 4 PlutoRunner.Bond if sym isa Missing sym setval end bond end end end html\"\"\" style margo knob display block cursor pointer width 32px height 32px transform translate 8px, 16px background red border radius 100% border width 5px border style solid border color rgb 255 255 255 43% border opacity .2 position absolute top 0px left 0px margo knob label transform translate 32px, 8px display block position absolute left 0 top 0 white space nowrap background d6eccb font family system ui padding .4em border radius 11px font weight 600 pointer events none opacity 0 .wiggle margo knob animation wiggle 5s ease in out animation delay 600ms .wiggle margo knob label animation fadeout 1s ease in out animation delay 3s animation fill mode both keyframes fadeout from opacity 1 to opactiy 0 keyframes wiggle 0% transform translate 8px, 16px 2% transform translate 8px, 16px 5% transform translate 24px, 16px 10% transform translate 8px, 16px 15% transform translate 8px, 16px 17% transform translate 8px, 0px 20% transform translate 8px, 32px 25% transform translate 8px, 16px style \"\"\" initial mrga 1 Dict \"M\" 2070, 0.7 , \"R\" 2100, 0.4 , \"G\" 2170, 0.3 , \"A\" 2110, 0.1 , initially initial mrga 1 bind input 8 begin controls 8 MRGA gaussish input 8 \"M\" ... , gaussish input 8 \"R\" ... , gaussish input 8 \"G\" ... , gaussish input 8 \"A\" ... , plotclicktracker2 plot controls controls 8 , initial mrga 1, end function plotclicktracker p Plots.Plot draggable Bool false we need to render the plot before its dimensions are available plot render repr MIME\"image svg xml\" , p plot render repr MIME\"image svg xml\" , p these are the bounding boxes of our plot big bbox p.layout small plotarea p 1 the axis limits xl xlims p yl ylims p with this information, we can form the linear transformation from screen coordinate plot coordinate this is done on the JS side, to avoid one step in the Julia side we send the linear coefficients r x offset xl 1 xl 2 xl 1 small.x0 1 small.a 1 , x scale big.a 1 small.a 1 xl 2 xl 1 , y offset yl 2 yl 1 small.x0 2 small.a 2 yl 2 yl 1 yl 1 , y scale big.a 2 small.a 2 yl 2 yl 1 , x min xl 1 , TODO add margin x max xl 2 , y min yl 1 , y max yl 2 , HTML \"\"\" script id \"hello\" const body PlutoRunner.publish to js plot render const mime \"image svg xml\" const img this ?? document.createElement \"img\" let url URL.createObjectURL new Blob body , type mime img.type mime img.src url img.draggable false img.style.cursor \"pointer\" const clamp x,a,b Math.min Math.max x, a , b img.transform f clamp f 0 r.x scale r.x offset , r.x min , r.x max , clamp f 1 r.y scale r.y offset , r.y min , r.y max , img.fired false const val current undefined if this null Object.defineProperty img, \"value\", get val.current, set , const handle mouse e const svgrect img.getBoundingClientRect const f e.clientX svgrect.left svgrect.width, e.clientY svgrect.top svgrect.height if img.fired false img.fired true val.current img.transform f img.dispatchEvent new CustomEvent \"input\" , img.addEventListener \"click\", onclick img.addEventListener \"pointerdown\", e if draggable img.addEventListener \"pointermove\", handle mouse handle mouse e const mouseup e img.removeEventListener \"pointermove\", handle mouse document.addEventListener \"pointerup\", mouseup document.addEventListener \"pointerleave\", mouseup return img script \"\"\" end years 2020 12.0 2200 function gaussish mean, magnitude my stdev gauss stdev 1 magnitude map years do t clamp 1.5 magnitude 0.4 exp 1 t mean t mean 2 my stdev my stdev 1.0 0.4 , 0.0, 1.0 end end function default parameters ClimateModelParameters result deepcopy ClimateMARGO.IO.included configurations \"default\" result.domain years isa Domain ? years Domain step years , first years , last years result.economics.baseline emissions ramp emissions result.domain result.economics.extra CO₂ zeros size result.economics.baseline emissions return result end function opt controls temp model parameters default parameters opt parameters... model ClimateModel model parameters model optimizer optimize controls model opt parameters..., print raw status false result model, status ClimateMARGO.Optimization.JuMP.termination status model optimizer | string, return Dict model parameters model parameters, model results model ... end bigbreak html\"\"\" div style \"height 10em \" div \"\"\" bigbreak bigbreak bigbreak bigbreak function setfieldconvert value, name Symbol, x setfield value, name, convert typeof getfield value, name , x end function enforce maxslope controls dt step years , max slope Dict \"mitigate\" 1. 40., \"remove\" 1. 40., \"geoeng\" 1. 80., \"adapt\" 0. controls.mitigate 1 0.0 controls.remove 1 0.0 controls.geoeng 1 0.0 controls.adapt 1 0.0 for i in 2 length controls.mitigate controls.mitigate i clamp controls.mitigate i , controls.mitigate i 1 max slope \"mitigate\" dt, controls.mitigate i 1 max slope \"mitigate\" dt controls.remove i clamp controls.remove i , controls.remove i 1 max slope \"remove\" dt, controls.remove i 1 max slope \"remove\" dt controls.geoeng i clamp controls.geoeng i , controls.geoeng i 1 max slope \"geoeng\" dt, controls.geoeng i 1 max slope \"geoeng\" dt controls.adapt i clamp controls.adapt i , controls.adapt i 1 max slope \"adapt\" dt, controls.adapt i 1 max slope \"adapt\" dt end end function costs dict costs, model Dict discounted costs, total discounted sum costs . model.domain.dt , end model results model ClimateModel Dict controls model.controls, computed Dict temperatures Dict baseline T adapt model , M T adapt model M true , MR T adapt model M true, R true , MRG T adapt model M true, R true, G true , MRGA T adapt model M true, R true, G true, A true , , emissions Dict baseline effective emissions model , M effective emissions model M true , MRGA effective emissions model M true, R true , , concentrations Dict baseline c model , M c model M true , MRGA c model M true, R true , , damages Dict baseline costs dict damage model discounting true , model , MRGA costs dict damage model M true, R true, G true, A true, discounting true , model , , costs Dict M costs dict cost model M true, discounting true , model , R costs dict cost model R true, discounting true , model , G costs dict cost model G true, discounting true , model , A costs dict cost model A true, discounting true , model , MRGA costs dict cost model M true, R true, G true, A true, discounting true , model , , , const cost bars scale 70 total discounted costs, model sum costs . model.domain.dt colors js Dict k,string \" \", hex v for k,v in pairs colors names js Dict pairs names function plotclicktracker2 p Plots.Plot, initial Dict draggable Bool true we need to render the plot before its dimensions are available plot render repr MIME\"image svg xml\" , p plot render repr MIME\"image svg xml\" , p these are the bounding boxes of our plot big bbox p.layout small plotarea p 1 the axis limits xl xlims p yl ylims p with this information, we can form the linear transformation from screen coordinate plot coordinate this is done on the JS side, to avoid one step in the Julia side we send the linear coefficients r x offset xl 1 xl 2 xl 1 small.x0 1 small.a 1 , x scale big.a 1 small.a 1 xl 2 xl 1 , y offset yl 2 yl 1 small.x0 2 small.a 2 yl 2 yl 1 yl 1 , y scale big.a 2 small.a 2 yl 2 yl 1 , x min xl 1 , TODO add margin x max xl 2 , y min yl 1 , y max yl 2 , aspect ratio big.a 1 big.a 2 , htl \"\"\" script id \"hello\" const initial initial const colors colors js const names names js const body AbstractPlutoDingetjes.Display.published to js plot render const mime \"image svg xml\" const knob name const k html` margo knob id \\ name margo knob label 👈 Move me margo knob label margo knob ` k.style.backgroundColor colors name return k const aspect ratio r.aspect ratio const wrapper this ?? html` div style 'touch action none ' img style 'width 100% aspect ratio \\ aspect ratio background white ' \\ Object.keys initial .map knob div ` const img wrapper.firstElementChild let url URL.createObjectURL new Blob body , type mime invalidation.then URL.revokeObjectURL url Call `fetch` on the URL to trigger the browser to make it ready. let fetch promise fetch url Promise.race fetch promise, invalidation.then x null .then r if r null img.type mime img.src url img.draggable false const clamp x,a,b Math.min Math.max x, a , b wrapper.transform f clamp f 0 r.x scale r.x offset , r.x min , r.x max , clamp f 1 r.y scale r.y offset , r.y min , r.y max , wrapper.inversetransform f f 0 r.x offset r.x scale , f 1 r.y offset r.y scale , const set knob coord k, coord const svgrect img.getBoundingClientRect const r wrapper.inversetransform coord k.style.left `\\ r 0 svgrect.width px` k.style.top `\\ r 1 svgrect.height px` wrapper.fired already false wrapper.last render time Date.now If running for the first time if this null console.log \"Creating new plotclicktracker...\" will contain the currently dragging HTMLElement const dragging current undefined const value ...initial Object.defineProperty wrapper, \"value\", get value, set x console.log \"old\", value, \"new\", x Object.assign value, x Object.entries value .forEach k,v set knob coord wrapper.querySelector `margo knob \\ k ` , v , Event listener for pointer move const allow only one event per render false const on pointer move e if Object.keys initial .includes dragging.current.id const svgrect img.getBoundingClientRect const f e.clientX svgrect.left svgrect.width, e.clientY svgrect.top svgrect.height if allow only one event per render || wrapper.fired already false const new coord wrapper.transform f value dragging.current.id new coord set knob coord dragging.current, new coord wrapper.classList.toggle \"wiggle\", false wrapper.fired already true wrapper.dispatchEvent new CustomEvent \"input\" , Add the listeners wrapper.addEventListener \"pointerdown\", e window.getSelection .empty dragging.current e.target if draggable wrapper.addEventListener \"pointermove\", on pointer move on pointer move e const mouseup e wrapper.removeEventListener \"pointermove\", on pointer move document.addEventListener \"pointerup\", mouseup document.addEventListener \"pointerleave\", mouseup wrapper.onselectstart false Set knobs to initial positions, using the inverse transformation new Promise r img.onload r .then Array.from wrapper.querySelectorAll \"margo knob\" .forEach k set knob coord k, initial k.id Intersection observer to trigger to wiggle animation const observer new IntersectionObserver es es.forEach e if Date.now wrapper.last render time 500 wrapper.classList.toggle \"wiggle\", e.isIntersecting , rootMargin `20px`, threshold 1, observer.observe wrapper wrapper.classList.toggle \"wiggle\", true return wrapper script \"\"\" end begin mutable struct MRGA T M T R T G T A T end function MRGA M TM,R TR nothing,G TG nothing where TM,TR,TG MRGA Union TM,TR,TG,Nothing M,R,G,nothing end function MRGA M TM nothing,R TR nothing,G TG nothing,A TA nothing where TM,TR,TG,TA MRGA Union TM,TR,TG,TA M,R,G,A end MRGA x MRGA x,x,x,x splat mrga MRGA M mrga.M, R mrga.R, G mrga.G, A mrga.A Base.collect mrga MRGA mrga.M, mrga.R, mrga.G, mrga.A Base.getindex x MRGA, pos MRGA Bool collect x collect pos Base.getindex x MRGA, tech Symbol getfield x, tech Base.eachindex m MRGA M, R, G, A Base.enumerate mrga MRGA M, mrga.M , R, mrga.R , G, mrga.G , A, mrga.A Base.any m MRGA Bool m.M || m.R || m.G || m.A Base.all m MRGA Bool m.M && m.R && m.G && m.A MRGA end md\"\"\" Emissions mitigation and carbon dioxide removal to minimize climate suffering This interactive article lets you – the reader– run MARGO https github.com ClimateMARGO ClimateMARGO.jl , a simple climate model, to explore what it takes to avoid the catastrophic impacts of global warming. The code in this webpage is reactive , meaning that the graphs and numbers automatically update whenever you change the climate model's inputs. By Henri F. Drake https hdrake.github.io and Fons van der Plas Can you limit human caused global warming to \"well below 2ºC\"? \"\"\" md\"\"\" image https user images.githubusercontent.com 6933510 118835384 3ad36c80 b8c3 11eb 995d 70cba3b23846.png From ClimateMARGO.jl https github.com ClimateMARGO ClimateMARGO.jl \"\"\" md\"\"\" Overview of the climate problem from greenhouse gas emissions to climate suffering Human emissions of greenhouse gases, especially Carbon Dioxide CO₂ , increase the stock of greenhouse gases in the atmosphere. For every molecule of CO₂ emitted, about 50% are taken up by plants, soils, or the ocean within a few years, while the rest remains in the atmosphere. The effects of other greenhouse gases, such as Methane and CFCs, and other forcing agents, can approximately be converted into the \"CO₂ equivalent\"– or CO₂ₑ– concentrations that would lead to the same climate forcing . Greenhouse gases get their name because they trap invisible heat radiation emitted by Earth's surface and atmosphere from escaping to space, much like greenhouses trap hot air from rising when it is warmed by the Sun. This \"greenhouse effect\" causes the temperature to rise globally, although some places warm more and faster than others. Warmer temperatures exacerbate both the frequency and intensity of \"natural\" disasters, such as heat waves, coastal flooding from major hurricanes, and inland flooding from torrential rain. These climate impacts lead to enhanced climate suffering, which economics typically attempt to quantify suffering in terms of lost money or welfare. In the interactive article below, we invite you to explore the benefits of emissions mitigation and carbon dioxide removal in reducing climate suffering, and the trade offs with their costs. \"\"\" md\"\"\" Mitigating emissions Human greenhouse gas emissions are the result of fossil fuel burning e.g. for transportation, electricity generation, heating, industry , unsustainable agriculture, and land use changes. We refer to any actions or policies that reduce these emissions as mitigation . The MARGO model lumps all potential mitigation into a single number the percentage of baseline emissions that are mitigated in a given year. Baseline emissions are the emissions that would arise in a hypothetical future world absent of climate policy. In our hypothetical no policy world, we assume that emissions will go to zero by 2150 even without climate policy, perhaps because of public health concerns regarding other forms of air pollution, the development of new zero carbon technologies, or running out of extractable fossil fuels resources. In the plot below, drag the blue dot around to vary the amount and timing of mitigation, and observe how these changes affect key climate variables, using the drop box menu CO₂ₑ emissions, CO₂ₑ concentrations, and global temperature. \"\"\" md\"\"\" Cost & damages \"\"\" bind cost benefits narrative slide Carousel md\"\"\" 1. The costs of climate suffering In the absence of climate action, temperatures would rise over 4.5ºC above preindustrial levels 1800 to 1850 average , causing catastrophic climate impacts. MARGO attempts to quantify this suffering by translating the degree of warming into economic damages in \\ year . The curve below shows how climate damages rise over time, as a percentage of the World Gross Domestic Product WGDP in that year, due to uncontrolled temperature increases. \"\"\", md\"\"\" 2. Avoiding climate damages Emissions mitigation limits future warming and climate suffering Damages curve . The economic benefits of mitigation are given by the difference in damages relative to the no policy scenario Baseline curve minus Damages curve . In the figure below, drag around the blue dot to change the future mitigation strategy, and observe how the Avoided damages the grey area change \"\"\", md\"\"\" 3. Cost benefit analysis Unfortunately, mitigating CO₂ₑ emissions also carries a cost. In MARGO, the marginal cost of mitigation is proportional to the fraction of CO₂ₑ emissions that have been mitigated in a given year, increasing up to a maximum of 70 per metric ton of CO₂ₑ at 100% mitigation. This naturally leads to a cost benefit analysis . We search for the most beneficial, or optimal , scenario the one with the maximum net present benefits . In the figure below, try finding a mitigation strategy that optimizes these Net benefits . \"\"\" wraparound false if cost benefits narrative slide 1 hidecloack \"cost benefits narrative input\" end md\"\"\" Picking up the slack carbon dioxide removal While substantial emissions mitigations are necessary to reduce future climate suffering, they can not make up for the hundreds of billions of tons of CO₂ that humans have already emitted. However, both natural and technological methods for removing CO₂ from the atmosphere exist. Although they are presently miniscule compared to the tens of gigatons scale of global emissions, experts expect that they will play a key role in the future. In MARGO, we do not distinguish between different carbon dioxide removal methods, and further assume that the carbon is stored permanently. Drag the yellow dot in the figure below to modify the amount and timing of carbon dioxide removal . \"\"\" md\"\"\" MARGO's automated optimization In the above example, you manually adjusted the timing and amount of mitigation and carbon dioxide removal, but did not have much control on the shape of curves. Using a computer algorithm, we can do this optimization step automatically and faster , without having to assume anything about the shape of the mitigation and carbon dioxide removal curves. \"\"\" blob md\"\"\" Maximum temperature `0.0 °C` Tmax 9 slider `5.0 °C` Allow temperature overshoot allow overshoot 9 cb \"\"\", \" c5710014\" md\"\"\" Appendix This interactive page is a Pluto notebook https github.com fonsp Pluto.jl , which means that it contains its own source code, and we encourage you to take a look behind the scenes. Use the button \"Edit or run\" on the top right to modify the notebook and reproduce the result yourself. \"\"\" md\"\"\" Example of running the model \"\"\" md\"\"\" Plot functions \"\"\" plot controls c ClimateMARGO.Models.Controls plot controls MRGA c.mitigate, c.remove, c.geoeng, c.adapt yearticks collect 2020 20 2200 function finish p plot p xlim 2020,2201 , xticks yearticks, size 680,200 , grid false, end function plot costs result ClimateModel show baseline Bool true, show controls Bool true, show damages Bool true, title \"Control costs & climate damages\" p plot ylim 0,25.1 , left margin 2.25Plots.Measures.mm, ylabel \"trillion USD year\", title nothing || plot p title title baseline show baseline && plot p, years, damage result discounting true pp.baseline damages..., fillrange zero years , fillopacity .2, linestyle dash, control costs controlled damages damage result M true, R true, G true, A true, discounting true show controls && plot p, years, controlled damages . cost result M true, R true, G true, A true, discounting true fillrange controlled damages, fillopacity .2, pp.controls... controlled damages show damages && plot p, years, controlled damages fillrange zero years , fillopacity .2, pp.damages... finish p end function plot concentrations result ClimateModel relative to preindustrial Bool true Tmax 5 p relative to preindustrial ? plot ylim 0,4.5 , yticks 1,2,3,4 , yformatter x string Int x , \"×\" , title \"Atmospheric CO₂ₑ concentration, relative to 1800 1850\", plot ylim 0,1400 , ylabel \"ppm\", title \"Atmospheric CO₂ₑ concentration\", factor relative to preindustrial ? preindustrial concentrations 1 baseline plot p, years, c result . factor pp.baseline concentrations..., linestyle dash, controlled temperature plot p, years, c result M true, R true . factor pp.concentrations... finish p end function plot controls controls MRGA title nothing p plot ylim 0,1 , title nothing || plot p title title for tech, c in enumerate controls if c nothing which tech M ? years . end of oil eachindex years plot p, years which , c which pp tech ... end end finish p end function plot emissions result ClimateModel p plot ylim 3,11 , left margin 1.75Plots.Measures.mm, ylabel \"ppm year\", title \"Global CO₂ₑ emissions\", baseline plot p, years, effective emissions result pp.baseline emissions..., linestyle dash, controlled plot p, years, effective emissions result M true, R true fillrange zero years , fillopacity .2, pp.emissions... finish p end function plot emissions pretty result ClimateModel offset the x values so that framestyle origin will make the y axis pass through 2020 instead of 0. yuck R x x 2020 L x x 2020 Tmax 5 p plot ylim 3,11 , ylabel \"ppm year\", framestyle origin, xformatter string ∘ Int ∘ R, baseline plot p, L. years , effective emissions result pp.baseline emissions..., linestyle dash, controlled temperature plot p, L. years , effective emissions result M true, R true fillrange zero L. years , fillopacity .2, pp.emissions... finish p plot p xlim L. extrema years , xticks L. yearticks , end function plot temp result ClimateModel Tmax 5 setup p plot ylim 0,Tmax , yticks 0,1,2,3 , yformatter x string \" \", Int x , \" °C\" , title \"Global warming relative to 1800 1850\", shade dangerously high temperatures for a in 2,3 plot p, collect extrema years , a,a , linewidth 0, label nothing, fillrange Tmax,Tmax , fillcolor colors.above paris end baseline plot p, years, T adapt result splat MRGA false ... pp.baseline temperature..., linestyle dash, controlled temperature plot p, years, T adapt result splat MRGA true ... pp.temperature... finish p end md\"\"\" Plot dots input magic \"\"\" md\"\"\" Running the model \"\"\" function forward controls temp controls MRGA MRGA , model parameters default parameters model ClimateModel model parameters translations Dict M mitigate, R remove, G geoeng, A adapt, for k, v in enumerate controls if v nothing setfieldconvert model.controls, translations Symbol k , copy v end end enforce maxslope model.controls model return Dict model parameters model parameters, model results model ... end initially initial 1 bind input 1 begin local t input 1 \"M\" 1 local y input 1 \"M\" 2 controls 1 MRGA M gaussish t, clamp .07 10 y , 0, 1 , R gaussish t, clamp .07 10 y 0.25, 0, 1 , result 1 forward controls temp controls 1 plotclicktracker2 plot emissions result 1 , initial 1 end plot temp result 1 initially initial 2 bind input 2 begin controls 2 MRGA M gaussish input 2 \"M\" ... , result 2 forward controls temp controls 2 plotclicktracker2 plot controls controls 2 title \"Deployment of mitigation\" , initial 2 end if which graph 2 \"Emissions\" plot emissions result 2 elseif which graph 2 \"Concentrations\" plot concentrations result 2 relative to preindustrial true else plot temp result 2 end initially initial 3 bind input 3 begin controls 3 MRGA M gaussish input 3 \"M\" ... , result 3 forward controls temp controls 3 plotclicktracker2 plot controls controls 3 title \"Deployment of mitigation\" , initial 3 end | cloak \"cost benefits narrative input\" if cost benefits narrative slide 1 local uncontrolled ClimateModel default parameters plot costs uncontrolled show controls false, show baseline false elseif cost benefits narrative slide 2 plot costs result 3 show controls false else plot costs result 3 end initially initial 4 bind input 4 begin controls 4 MRGA M gaussish input 4 \"M\" ... , R gaussish input 4 \"R\" ... , result 4 forward controls temp controls 4 plotclicktracker2 plot controls controls 4 title \"Deployment of mitigation\" , initial 4 end if which graph 4 \"Emissions\" plot emissions result 4 elseif which graph 4 \"Concentrations\" plot concentrations result 4 relative to preindustrial true elseif which graph 4 \"Temperature\" plot temp result 4 else plot costs result 4 end shortname MRGA \"M\",\"R\",\"G\",\"A\" mediumname MRGA \"mitigate\", \"remove\", \"geoeng\", \"adapt\" longname MRGA \"Mitigation\", \"Removal\", \"Geo engineering\", \"Adaptation\" let default cost let e default parameters .economics MRGA e.mitigate cost, e.remove cost, e.geoeng cost, e.adapt cost end blob htl \"\"\" h4 Which controls? h4 style .controltable thead th, .controltable tbody td text align center .controltable input type range width 10em style table class \"controltable\" thead th th th Enabled? th th style \"text align center \" Cost multiplier th thead tbody tr th longname.M th td bind enable M 9 CheckBox default true td td bind cost M 9 multiplier default cost.M, 4 td tr tr th longname.R th td bind enable R 9 CheckBox default true td td bind cost R 9 multiplier default cost.R, 4 td tr tr th longname.G th td bind enable G 9 CheckBox default false td td bind cost G 9 multiplier default cost.G, 4 td tr tr th longname.A th td bind enable A 9 CheckBox default false td td bind cost A 9 multiplier default cost.A, 4 td tr tbody table \"\"\" , \" c500b40a\" end control enabled 9 MRGA enable M 9, enable R 9, enable G 9, enable A 9, control cost 9 MRGA Float64 cost M 9 , Float64 cost R 9 , Float64 cost G 9 , Float64 cost A 9 , output 9 let parameters default parameters parameters.economics.mitigate cost control cost 9.M parameters.economics.remove cost control cost 9.R parameters.economics.geoeng cost control cost 9.G parameters.economics.adapt cost control cost 9.A modify the parameters here opt controls temp parameters temp overshoot allow overshoot 9 ? 999.0 Tmax 9, temp goal Tmax 9, max deployment let e control enabled 9 Dict \"mitigate\" e.M ? 1.0 0.0, \"remove\" e.R ? 1.0 0.0, \"geoeng\" e.G ? 1.0 0.0, \"adapt\" e.A ? 0.4 0.0, end, end plot controls output 9.result.controls | feasibility overlay output 9 plot plot temp output 9.result , years, zero years . Tmax 9 lw 2, pp.T max... md\"\"\" Cost bars \"\"\" function avoided damages bars result td x total discounted x, result baseline damages td damage result discounting true controlled damages td damage result splat MRGA true ..., discounting true avoided damages baseline damages controlled damages costs td cost result splat MRGA true ..., discounting true htl \"\"\" script const colors colors js const names names js const baseline damages baseline damages const controlled damages controlled damages const avoided damages avoided damages const costs costs const scale cost bars scale const bar offset, width, color html` span style \"margin left \\ offset % width \\ width % opacity .7 display inline block background \\ color height 1.2em margin bottom .2em \" span ` return html` div \\ bar 0, controlled damages scale, colors.damages span style \"opacity .6 \" Controlled damages b \\ Math.ceil controlled damages trillion USD b . div div style \"border bottom 2px solid eee margin bottom 4px \" \\ bar 0, baseline damages scale, colors.baseline span style \"opacity .6 \" Baseline damages b \\ Math.ceil baseline damages trillion USD b . span div div style \"font style italic \" \\ bar controlled damages scale, avoided damages scale, colors.avoided damages span Avoided damages b \\ Math.ceil avoided damages trillion USD b . span div ` script \"\"\" end if cost benefits narrative slide 2 avoided damages bars result 3 end function cost bars result offset damages false td x total discounted x, result baseline damages td damage result discounting true controlled damages td damage result splat MRGA true ..., discounting true avoided damages baseline damages controlled damages costs td cost result splat MRGA true ..., discounting true htl \"\"\" script const colors colors js const names names js const baseline damages baseline damages const controlled damages controlled damages const avoided damages avoided damages const costs costs const scale cost bars scale const bar offset, width, color html` span style \"margin left \\ offset % width \\ width % opacity .7 display inline block background \\ color height 1.2em margin bottom .2em \" span ` div \\ bar 0, baseline damages scale, colors.baseline Baseline damages b \\ Math.ceil baseline damages trillion USD b . div const extra offset offset damages ? controlled damages scale 0 return html` div \\ bar extra offset, costs scale, colors.controls span style \"opacity .6 \" Control costs b \\ Math.ceil costs trillion USD b . span div div style \"border bottom 2px solid eee margin bottom 4px \" \\ bar extra offset, avoided damages scale, colors.avoided damages span style \"opacity .6 \" Avoided damages b \\ Math.ceil avoided damages trillion USD b . span div div style \"font style italic \" title \"Net benefits Avoided damages minus the cost of getting there.\" \\ bar extra offset costs scale, avoided damages costs scale, colors.benefits span Net benefits b \\ Math.ceil avoided damages costs trillion USD b . span div ` script \"\"\" end cost bars result 1 if cost benefits narrative slide 3 cost bars result 3 offset damages true else bigbreak end md\"\"\" MRGA struct \"\"\" function MR x T,y T where T MRGA T x, y, zero x , zero x end "},{"url":"climate_science/odes_and_parameterized_types/","title":"ODEs and parameterized types","tags":["lecture","module3","epidemiology","plotting","ODE","differential equation","track_julia","track_math","programming","DifferentialEquations","type","structure","track_climate"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v S71YIZ8e7MQ\" image \"https user images.githubusercontent.com 6933510 136199713 25eb2e90 c5cd 4e11 8463 6f6069a81a51.png\" section 2 order 2 title \"ODEs and parameterized types\" layout \"layout.jlhtml\" youtube id \"S71YIZ8e7MQ\" description \"\" tags \"lecture\", \"module3\", \"epidemiology\", \"plotting\", \"ODE\", \"differential equation\", \"track julia\", \"track math\", \"programming\", \"DifferentialEquations\", \"type\", \"structure\", \"track climate\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using DifferentialEquations, Plots, PlutoUI, MappedArrays TableOfContents md\"\"\" Julia concepts Using libraries Libraries are built around types and objects Plot recipes Defining how to plot our objects Making objects into callable functions Parameterized types \"\"\" md\"\"\" ODEs and parameterized types \"\"\" md\"\"\" In the previous lecture we started to look at Ordinary Differential Equations ODEs . These are equations relating a function to its derivatives, telling us how to move at each moment in time. For example, exponential decay is modelled by \\dot u p \\, u, in other words \\dot u t p \\, u t for all real t \\ge 0 , with initial condition u 0 u 0 where u t is the number of light bulbs still functioning at time t , the number of radioactive nuclei that have not yet decayed, or the number of people who are still infectious and have not yet recovered. The parameter p is the decay rate. We saw that time stepping methods provide a class of numerical methods for solving such initial value problems, of which the Euler method is the simplest example. Unfortunately the Euler method is not a good choice, since it is not accurate enough. In numerical analysis classes like 18.330 https github.com mitmath 18330 various better methods are derived. Fortunately those methods are available in a Julia library or package, `DifferentialEquations.jl`. \"\"\" md\"\"\" The `DifferentialEquations.jl` library \"\"\" md\"\"\" DifferentialEquations.jl https diffeq.sciml.ai v2.0 often abbreviated as DiffEq is a Julia library for solving many types of differential equations. It has become the best most complete and fastest general library for solving ordinary differential equations and many other types in any programming environment. It is also a very good example of the way that Julia libraries are often structured. We will see some basic usage examples and how Julia types are used both to control the library and to store the input and output to the solvers . \"\"\" md\"\"\" Basic usage of DifferentialEquations.jl \"\"\" md\"\"\" Let's try to solve the above equation that we derived in the previous lecture, \\dot u p \\, u . We are using u as the name of the dependent variable for consistency with the `DifferentialEquations.jl` documentation and internal structure. We write a general ODE in 1 variable as \\dot u f p t, u . The right hand side of the ODE is given by a function f , which may depend on a parameter p , i.e. a constant which does not change for any given run of the ODE. f may also depend explicitly on time. In our example we do indeed have a parameter p that we want to change, but f does not have an explicit time dependence, so we write \"\"\" f u, p, t p u md\"\"\" Note that you always need the three input arguments `u`, `p` and `t`, in that order, even if you don't use them. \"\"\" md\"\"\" Now we define our initial condition u 0 and the time span to integrate over, given as a tuple initial\\ time, final\\ time \"\"\" begin u0 100.0 time span 0.0, 10.0 end md\"\"\" To set up the problem instance we use a type `ODEProblem` that is defined in the `DifferentialEquations.jl` package, into which we pass all the information necessary to define the problem. The parameters must go in the following order \"\"\" md\"\"\" For more advanced use there are also some additional, optional, keyword arguments. Note that the displayed output does not currently include information about the function, nor the parameters. \"\"\" md\"\"\" To solve the ODE we call the `solve` function \"\"\" md\"\"\" What happened here? A suitable solver i.e. an algorithm to calculate the solution was chosen automatically , and it chose certain moments in time at which to output information about the approximate, but very accurate solution. In this particular case it chose to output data at only eight points in time between t 0 and t 10 . Let's try to plot the `solution` object \"\"\" md\"\"\" Plot recipes \"\"\" md\"\"\" Two surprising things happen. Firstly, there is no reason to expect this to have worked `solution` is some kind of Julia object, but somehow `Plots.jl` knows how to plot it. This is because `DifferentialEquations.jl` defines a plot recipe . This specifies a way to turn solution objects into plots Any package can do this relatively easily. \"\"\" md\"\"\" The second surprise is that the output looks like a smooth curve, rather than just 8 points. Let's see those points on top of the curve. We can extract the relevant data from the `solution` object \"\"\" md\"\"\" We see that the package in fact gives not only the value at those points, but it is in fact also capable of calculating an approximate solution at any intermediate point, using interpolation . In fact, we can access this by treating `solution` as if it were a function \"\"\" md\"\"\" For this particular ODE we know the analytical solution. Let's compare them as we vary the parameter p \"\"\" md\"\"\" p bind p Slider 0.0 0.1 2.0, show value true \"\"\" problem ODEProblem f, u0, time span, p solution solve problem plot solution, size 500, 300 , label \"solution\" scatter solution.t, solution.u, label \"discrete output\" begin tt 3.5 solution tt end scatter tt , solution tt , label \"t tt \", ms 5, m square let problem ODEProblem f, u0, time span, p solution solve problem plot solution, linewidth 3, xlabel \"t\", yaxis \"x t \", label \"numerical\", size 500, 300 plot t u0 exp p t , lw 3, ls dash, label \"exact\" ylims 0, 100 title \"p p\" end md\"\"\" We see that the numerical and exact solutions are to the eye indistinguishable, and that the package is fast enough to calculate the solution basically in real time. \"\"\" md\"\"\" Systems of ODEs \"\"\" md\"\"\" Now let's try to solve the SIR equations \\begin aligned \\dot s & \\beta s i \\\\ \\dot i & \\beta s i \\gamma i\\\\ \\dot r & \\gamma i \\end aligned \"\"\" md\"\"\" We need to think of the system in a vector form, \\dot \\mathbf x \\mathbf f \\mathbf p t, \\mathbf x , where the vector \\mathbf x is a vector of all the variables, \\mathbf x x 1, x 2, x 3 s, i, r the function \\mathbf f is a vector valued function \\mathbb R ^3 \\to \\mathbb R ^3 , where f k gives the right hand side of the equation for \\dot x k and the vector \\mathbf p \\beta, \\gamma is a vector of the parameters. \"\"\" x0 0.99, 0.01, 0.0 function SIR x, p, t s, i, r x unpack the vectors into scalar values β, γ p build a new vector to return return β s i, β s i γ i, γ i end md\"\"\" Now we see that the solverr has recognised that everything is a vector, and it returns a vector at each time stamp. Again we can plot \"\"\" md\"\"\" β bind β Slider 0.5 0.01 2.0, default 1.0, show value true γ bind γ Slider 0.5 0.01 2.0, default 0.1, show value true \"\"\" params β, γ SIR problem ODEProblem SIR, x0, 0.0, 50.0 , params sol solve SIR problem plot sol md\"\"\" It knows to plot each variable separately. We can instead plot combinations of variables in phase space or state space \"\"\" plot sol, idxs 1, 2 , xlabel \"s\", ylabel \"i\", arrow true, xlims 0.1, 1.0 , size 500, 300 md\"\"\" And even in 3D \"\"\" let plotly p plot sol, vars 1, 2, 3 , xlabel \"s\", ylabel \"i\", zlabel \"r\" gr p end md\"\"\" The ModelingToolkit.jl https mtk.sciml.ai stable tutorials ode modeling library provides ways to make creating ODE models more intuitive, using symbolic equation objects. \"\"\" md\"\"\" Making objects into callable functions \"\"\" md\"\"\" One of the interesting things we have seen is the way that the `solution` object can be called using the normal syntax for calling functions \"\"\" solution 3.5 md\"\"\" Not all types allow this. For example, if we define a matrix \"\"\" A rand 2, 2 md\"\"\" we cannot usually call it with a vector \"\"\" A rand 2 md\"\"\" If we wanted to do so, we could, however, using the following syntax \"\"\" A Matrix x A x md\"\"\" Note that this makes objects instances of the type callable, rather than the type itself, which we can already call as a constructor to create objects of that type \"\"\" Matrix 1 2 3 4 md\"\"\" For example, if we wanted to write our own type to store the output of the Euler method, we could do something like \"\"\" begin struct SimpleEulerOutput times Vector Float64 values Vector Float64 end function soln SimpleEulerOutput t use interpolation to calculate the value at time t using `soln.times` and `soln.values` end end md\"\"\" The definition of the function makes it callable. This says that \"the way to call an object called `soln`, of type `EulerOutput`, is the following \". Note that inside the function you will need to access the `times` and `values` from inside the `soln` object, using `soln.times` and `soln.values`. Note that at the time of writing, Pluto requires that you put this call definition in the same cell as the type definition. \"\"\" md\"\"\" Digging into objects \"\"\" md\"\"\" A key feature of the `DifferentialEquations.jl` library, and of most other libraries in Julia, is the way they define and use new types . As we have seen previously, we can define new types using `struct` which store data, and then define functions which act on those types. \"\"\" md\"\"\" Let's start by looking at the `ODEProblem` type \"\"\" problem md\"\"\" To see what fields, or attributes, the object contains we can use \"\"\" fieldnames typeof problem md\"\"\" In Pluto and other interactive environments we can get the same information using `problem. TAB `, i.e. by typing the TAB key after `problem.` \"\"\" md\"\"\" As usual we can extract the data contained in the object as usual using `.` \"\"\" problem.u0 problem.tspan md\"\"\" To see everything contained in the object, we can use `dump` \"\"\" dump problem md\"\"\" This seems much more complicated than you might expect. The library is clearly doing more than just storing the data that we provide in a type The main thing that has happened is that the function we passed in has been processed by wrapping it into yet another type, `ODEFunction`. This contains information such as how to calculate the derivative of the function that can be provided for more advanced usage of the package. \"\"\" md\"\"\" Similarly we can look inside the solution object \"\"\" fieldnames typeof solution dump solution md\"\"\" The solution is even more complicated, containing not only the data that was calculated, but also all information about which algorithms were used to solve the problem and tables of coefficients for interpolation. \"\"\" md\"\"\" Parameterized types \"\"\" md\"\"\" Let's look now at the type of the DiffEq objects \"\"\" typeof problem md\"\"\" We see that indeed `problem` is an object of type `ODEProblem`, but that it also has several type parameters , which are listed inside the curly braces ` ` and ` ` \"\"\" typeof problem .parameters... md\"\"\" For example, we see that the second type parameter is of type `Tuple Float64, Float64 `, which is the type of the variable `time span` \"\"\" typeof time span md\"\"\" One of the reasons for using parameterized types like this is for efficiency Julia is fastest when it knows the exact types of every variable, since then it can generate efficient machine code. \"\"\" md\"\"\" Making parameterized types \"\"\" md\"\"\" Let's see how and why we would make a parameterized type. Let's think again about the `EulerOutput` type that we started to write above. What happens if, as in the `DifferentialEquations.jl` library, we would like to handle both scalar ODEs one single real dependent variable , and vector ODEs? We can no longer specify that the output type is a `Vector Float64 `, so we would need to leave it unspecified \"\"\" struct SimpleEulerOutput2 times values end md\"\"\" However, it turns out that we then lose efficiency speed . What we would need is two different types one in which we specify the type of `values` as `Vector Float64 `, and the other as `Vector Vector Float64 `. Julia provides a mechanism to define both at once this is what parameterized types do \"\"\" md\"\"\" Let's define our own `MyODEProblem` type as a simple example. We specify type parameters for each variable \"\"\" struct MyODEProblem T,U t0 T tfinal T u0 U end md\"\"\" Note that the starting and final times have been specified to require the same type `T`. \"\"\" md\"\"\" Now if we create objects of this type, we see that Julia matches the types we pass in to the correct type parameters \"\"\" newprob MyODEProblem 3, 4, 100.0 newprob.t0 newprob.tfinal newprob.u0 md\"\"\" If we change the types of the inputs, the type parameters change \"\"\" MyODEProblem 3.0, 4.0, 100.0 md\"\"\" If we mix types for the first two arguments then we get an error, since Julia can't match to a single type `T` \"\"\" MyODEProblem 3.0, 5, 100.0 md\"\"\" We can use vectors \"\"\" MyODEProblem 3, 4, 17.0, 18.0 "},{"url":"climate_science/optimization_with_JuMP/","title":"Solving inverse problems","tags":["lecture","module3","optimization","JuMP","track_math","track_climate","track_julia","modeling","plotting"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v nm86 hDwYTU\" image \"https user images.githubusercontent.com 6933510 136200827 89647ae8 cb06 42ea a18d 5f64e9cc2b25.png\" section 11 order 11 title \"Solving inverse problems\" layout \"layout.jlhtml\" youtube id \"nm86 hDwYTU\" description \"\" tags \"lecture\", \"module3\", \"optimization\", \"JuMP\", \"track math\", \"track climate\", \"track julia\", \"modeling\", \"plotting\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using PlutoUI, Plots, Statistics, JuMP, Ipopt end TableOfContents md\"\"\" Solving inverse problems Optimization with JuMP \"\"\" md\"\"\" Forward and inverse problems \"\"\" md\"\"\" In a forward problem we provide inputs and calculate what the resulting output is using a model. In an inverse problem https en.wikipedia.org wiki Inverse problem we have a goal output and wish to find which inputs to the model will produce that goal. To do so we often need to solve an optimization problem . JuMP is a modeling language embedded in Julia. It allows us to write down optimization problems in a natural way these are then converted by JuMP into the correct input format to be sent to different solvers , i.e. software programs that choose which optimization algorithms to apply to solve the optimization problem. \"\"\" md\"\"\" Unconstrained optimization \"\"\" mm Model Ipopt.Optimizer md\"\"\" a bind a Slider 3 0.01 3, show value true, default 0 \"\"\" f x x^2 a x 2 min value let model Model Ipopt.Optimizer tell JuMP about our function register model, f, 1, f, autodiff true declare a variable variable model, 10 ≤ x ≤ 10 set the objective function to optimize NLobjective model, Min, f x optimize model value x end begin plot f, size 400, 300 , leg false plot x x^2 2, ls dash, alpha 0.5 scatter min value , f min value , xlims 5, 5 , ylims 10, 20 end md\"\"\" Constrained optimization \"\"\" md\"\"\" Often we need to add constraints , i.e. restrictions, to the problem. \"\"\" g x 3x 4 constraint x, y y x k x, y x^2 y^2 1 b slider bind b Slider 5 0.1 5, show value true, default 0 minx, miny let model Model Ipopt.Optimizer register model, k, 2, k, autodiff true register model, constraint, 2, constraint, autodiff true variable model, 10 ≤ x ≤ 10 variable model, 10 ≤ y ≤ 10 NLobjective model, Min, k x, y NLconstraint model, constraint x, y b optimize model x value x , y value y end md\"\"\" b b slider \"\"\" gr begin r 5 0.1 5 contour r, r, k, leg false contour r, r, constraint, levels b , ratio 1, lw 3 scatter minx , miny end plotly surface 5 0.1 5, 5 0.1 5, k, alpha 0.5 md\"\"\" y x b so y x b z x^2 y^2 x^2 x b ^2 \"\"\" begin xs 5 0.1 5 ys xs . b end b slider begin surface 5 0.1 5, 5 0.1 5, k, alpha 0.5 plot xs, ys, k. xs, ys , lw 3 scatter minx , miny , k minx, miny , zlim 10, 50 , xlim 5, 5 , ylim 5, 5 end "},{"url":"climate_science/our_first_climate_model/","title":"Our first climate model","tags":["lecture","module3","climate","modeling","track_climate","track_math","plotting","data","ODE","differential equation","CSV","dataframe","track_data"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v J1UsMa1cTeE\" image \"https user images.githubusercontent.com 6933510 136199705 7bdb6bb0 8698 43a1 87e6 c073ab102da5.png\" section 4 order 4 title \"Our first climate model\" layout \"layout.jlhtml\" youtube id \"J1UsMa1cTeE\" description \"\" tags \"lecture\", \"module3\", \"climate\", \"modeling\", \"track climate\", \"track math\", \"plotting\", \"data\", \"ODE\", \"differential equation\", \"CSV\", \"dataframe\", \"track data\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using DifferentialEquations using Plots, PlutoUI using LinearAlgebra using CSV, DataFrames end TableOfContents title \"📚 Table of Contents\", indent true, depth 4, aside true md\"\"\" This lecture is based on the fall 2020 \"Introduction to Climate Modeling\" lecture by Henri Drake. Henri's video https computationalthinking.mit.edu Fall20 lecture20 , Henri's original notebook https github.com mitmath 18S191 blob Fall20 lecture notebooks week11 01 energy balance model.jl where he describes the Energy Balance Model. \"\"\" md\"\"\" Pre Computational Thinking traditional ode \"analytic\" solutions \"\"\" md\"\"\" Notation ``y y t `` is a function of time t Inititally t 0 , y 0 y 0 We write ``y'`` for ``dy dt``. \"\"\" md\"\"\" ``y' `` constant ``y' a`` html\" br \" Solution ``y t at y 0`` \"\"\" md\"\"\" ``y' `` linear in y ``y' a by`` html\" br \" Solution ``y t \\frac a b e^ bt 1 y 0 e^ bt `` Comment Equilibrium obtained if b\\ne0 by solving y' 0 for y giving equilibrium y a b . Also can be obtained by letting t\\rightarrow \\infty in the solution killing the exponential terms. \"\"\" md\"\"\" ``y' `` linear in y \"forcing term\" in t ``y' a by f t `` html\" br \" Solution ``y t e^ bt \\left y 0 \\int 0^t e^ bu a f u du \\right `` \"\"\" md\"\"\" Computational Thinking \"\"\" md\"\"\" Review Solving an ODE y' f y in Julia 0. Define f y,p,t 1. ODEProblem f, f 0 , time span, parameters 2. solve 3. plot etc \"\"\" f y, a, b, forcing c ,t a b y forcing c t forcing c t c t md\"\"\" a bind a Slider 0 .01 10, show value true, default 0 b bind b Slider 0 .1 5, show value true, default 0 y₀ bind y₀ Slider 5 .1 15, show value true, default 2.0 c bind c Slider 0 .1 5, show value true, default 0.0 \"\"\" sol solve ODEProblem f, y₀, 0, 10.0 , a, b, forcing c y₀ begin p plot sol , legend false, background color inside black , ylims 0, 10 , lw 3, c red plot direction field xs Float64 ys Float64 lrx LinRange xlims p ..., 30 for x in lrx for y in LinRange ylims p ..., 30 v 1, a b y forcing c x v . 20 lrx 2 lrx 1 plot x, x v 1 , y, y v 2 , alpha 0.5, c gray push xs, x v 1 , x v 1 , NaN push ys, y v 2 , y v 2 , NaN end end hline b 0 ? 0 a b ,c white,ls dash plot xs, ys, alpha 0.7, c yellow ylabel \"y\" annotate .5,y₀,text \"y₀\",color red title \"Solution to y' t a by forcing c t \" end md\"\"\" Background climate physics The simplest climate model can be conceptualized as ```math \\begin align \\text \\color brown change in heat content & \\text \\color orange absorbed solar radiation energy from the Sun's rays \\newline & \\text \\color blue outgoing thermal radiation i.e. blackbody cooling to space \\newline & \\text \\color grey human caused greenhouse effect trapped outgoing radiation \\end align ``` where each of these is interpreted as an average over the entire globe hence \"zero dimensional\" . \"\"\" html\"\"\" img src \"https raw.githubusercontent.com hdrake hdrake.github.io master figures planetary energy balance.png\" height 225 \"\"\" md\"\"\" 1.1 Incoming 🌞 Absorbed solar radiation an example of \\mathrm temp ' constant \"\"\" md\"\"\" Heating the earth nonstop \"\"\" md\"\"\" At Earth's orbital distance from the Sun, the power of the Sun's rays that intercept the Earth is equal to \"\"\" S 1368 solar insolation W m^2 energy per unit time per unit area md\"A small fraction\" α 0.3 albedo, or planetary reflectivity unitless md\"\"\" In math we just write down a differential equation, but in the physical world there are physical variables to identify. In our baking the earth example, we will identify the following quantities Industrial Revolution Start 1850 Avg Temperature in 1850 14.0 °C Solar Insolation S 1368 W m^2 energy from the sun Albedo or plentary reflectivity α 0.3 atmosphere and upper ocean heat capacity C 51 J m^2 °C Earth Baking Formula html\" br \" `` C\\ \\mathrm temp ' t S 1 α 4 `` round S 1 α 4 sigdigits 4 \"\"\" md\"\"\" of this incoming solar radiation is reflected back out to space by reflective surfaces like white clouds, snow, and ice , with the remaining fraction 1 \\alpha being absorbed. Since the incoming solar rays are all approximately parallel this far from the Sun, the cross sectional area of the Earth that intercepts them is just a disc of area \\pi R^ 2 . Since all of the other terms we will consider act on the entire surface area 4\\pi R^ 2 of the spherical Earth, the absorbed solar radiation per unit surface area averaged over the entire globe is reduced by a factor of 4. https www.open.edu openlearn ocw pluginfile.php 101161 mod oucontent oucontent 890 639dcd57 ce3f1c3a s250 3 002i.jpg The absorbed solar radiation per unit area is thus \\textcolor orange \\text absorbed solar radiation \\equiv \\frac S 1 \\alpha 4 \"\"\" absorbed solar radiation S 1 α 4 W m^2 begin C 51. atmosphere and upper ocean heat capacity Wyr m^2 °C temp₀ 14.0 preindustrial temperature °C end p1 ODEProblem temp, p, t 1 C absorbed solar radiation, temp₀, 0.0, 170 begin plot solve p1 , legend false, background color inside black, xlabel \"years from 1850 \", ylabel \"Temperature °C\" hline temp₀,temp₀ ,c white,ls dash annotate 80, 25 temp₀, text \"Preindustrial Temperature temp₀ °C\",color white title \"Absorbing Solar Radiation only \" end md\"\"\"The heat content C temp is determined by the temperature temp in Kelvin and the heat capacity of the climate system. While we are interested in the temperature of the atmosphere, which has a very small heat capacity, its heat is closely coupled with that of the upper ocean, which has a much larger heat capacity of \"\"\" md\"\"\" The change in heat content over time is thus simply given by \\frac d CT dt . Since the heat capacity of sea water hardly changes with temperature, we can rewrite this in terms of the change in temperature with time as \\color brown \\text change in heat content \\ C \\frac dtemp dt \"\"\" md\"\"\" 1.2 Outgoing ♨ thermal radiation \"\"\" md\"\"\" The outgoing thermal radiation term \\mathcal G T or \"blackbody cooling to space\" represents the combined effects of negative feedbacks that dampen warming , such as blackbody radiation , and positive feedbacks that amplify warming , such as the water vapor feedback . Since these physics are too complicated to deal with here, we linearize the model combining the incoming and the outgoing. We assume that the preindustrial world was in energy balance, and thus the equilibrium temperature is the preindustrial temperature. Thus we assume temp' t B temp 0 temp t for some value of B . The minus sign in front of temp t indicating it restores equilibrium. \"\"\" md\"\"\" The value that has been chosen is \"\"\" B 1.3 climate feedback parameter W m^2 °C , md\"\"\" start\\ temp bind start temp Slider 0 28 show value true, default 14 \"\"\" p2 ODEProblem temp, p, t 1 C B temp₀ temp , start temp, 0.0, 170 begin plot solve p2 , legend false, background color inside black, xlabel \"years from start\", ylabel \"Temperature °C\", ylim 0, 30 hline temp₀,temp₀ ,c white,ls dash annotate 80, temp₀, text \"Preindustrial Temperature temp₀ °C\", bottom,color white title \"Energy Balance Model Healthy Earth \" end md\"\"\" 1.3 Greenhouse 🏭 Human caused greenhouse effect Empirically, the greenhouse effect is known to be a logarithmic function of gaseous carbon dioxide CO₂ concentrations \\color grey \\text human caused greenhouse effect \\ \\mbox forcing\\ coef \\ln \\left \\frac \\text CO ₂ \\text CO ₂ \\text PreIndust \\right , How this depends on time into the future depends on human behavior Time is not modelled in the above equation. \"\"\" md\"where\" forcing coef 5.0 CO2 forcing coefficient W m^2 CO₂ PreIndust 280. preindustrial CO2 concentration parts per million ppm greenhouse effect CO₂ forcing coef log CO₂ CO₂ PreIndust begin CO2 present 420. CO2 range 280 2 .^ range 1, stop 3,length 100 plot CO2 range, greenhouse effect. CO2 range , lw 2.5, label nothing, color black plot CO₂ PreIndust , greenhouse effect CO₂ PreIndust , marker circle, ms 6, linecolor white, color blue, lw 0, label \"pre industrial PI \" plot CO2 present , greenhouse effect CO2 present , marker circle, ms 6, color red, linecolor white, lw 0, label \"present day 2020 \" plot xticks 280, 280 2, 280 4, 280 8 , legend bottomright, size 400, 250 plot ylabel \"Radiative forcing W m² \", xlabel \"CO₂ concentration ppm \" end begin CO₂ t CO₂ PreIndust no emissions CO₂ t CO₂ PreIndust 1.01^t test model CO₂ t CO₂ PreIndust 1 t 220 ^3 end greenhouse effect CO₂ 15 p3 ODEProblem temp, p, t 1 C B temp₀ temp greenhouse effect CO₂ t , start temp, 0.0, 170 begin plot solve p3 , legend false, background color inside black, xlabel \"years from 1850\", ylabel \"Temperature °C\", ylim 10, 20 hline temp₀,temp₀ ,c white,ls dash annotate 80, temp₀, text \"Preindustrial Temperature temp₀ °C\", bottom,color white title \"Model with CO₂\" end begin years 1850 2030 plot years, CO₂. years. 1850 , lw 3, legend false xlabel \"year\" ylabel \"CO₂ ppm \" end md\"\"\" Observations from Mauna Loa Volcano Mauna Loa Volcano https i.pinimg.com originals df 1a e7 df1ae72cfd5e6d0d535c0ec99e708f6f.jpg information is available at https www.ncei.noaa.gov pub data paleo icecore antarctica law law2006.txt https www.ncei.noaa.gov pub data paleo icecore antarctica law law2006.txt . \"\"\" CO2 historical data url \"https gml.noaa.gov webdata ccgg trends co2 co2 mm mlo.csv\" CO2 historical path download CO2 historical data url begin offset findfirst startswith \" \" , readlines CO2 historical path CO2 historical data raw CSV.read CO2 historical path, DataFrame header offset, skipto offset 1, first CO2 historical data raw, 11 end md\"\"\" Data is in the column `\"average\"`. \"\"\" md\"\"\" Oh no, missing data 99.99 \"\"\" validrowsmask CO2 historical data raw , \"average\" . 0 CO2 historical data CO2 historical data raw validrowsmask, begin plot CO2 historical data , \"decimal date\" , CO2 historical data , \"average\" , label \"Mauna Loa CO₂ data Keeling curve \" plot years, CO₂. years. 1850 , lw 3 , label \"Cubic Fit\", legend topleft title \"CO₂ observations and fit\" end md\"\"\" We will use this fit to compare against historical temperatures. \"\"\" md\"\"\" Climate feedback BB bind BB Slider 0 .1 4, show value true, default B Ocean Heat Capacity CC bind CC Slider 10 .1 200, show value true, default C \"\"\" p4 ODEProblem temp, p, t 1 CC BB temp₀ temp greenhouse effect CO₂ t , start temp, 0.0, 170 md\"\"\" Best and worst case projections of future global warming \"\"\" md\"\"\"Consider two divergent hypothetical futures 1. a low emissions world in which emissions decrease such that CO2 concentrations stay below 500 ppm by 2100 known in climate circles as \"RCP2.6\" and 2. a high emissions world in which emissions continue increasing and CO2 concentrations soar upwards of 1200 ppm \"RCP8.5\" . \"\"\" md\"\"\" https raw.githubusercontent.com mitmath 18S191 Spring21 notebooks week12 predictthefuture.svg \"\"\" md\"\"\" In the low emissions scenario, the temperature increase stays below ΔT 2 °C by 2100, while in the high emissions scenario temperatures soar upwards of 3.5ºC above pre industrial levels. \"\"\" md\"Although the greenhouse effect due to human caused CO₂ emissions is the dominant forcing behind historical and future projected warming, modern climate modelling considers a fairly exhaustive list of other forcing factors aerosols, other greenhouse gases, ozone, land use changes, etc. . The video below shows a breakdown of these forcing factors in a state of the art climate model simulation of the historical period.\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid E7kMr2OYKSU params \"modestbranding 1&rel 0\" lite youtube \"\"\" solp4 solve p4 begin T url \"https data.giss.nasa.gov gistemp graphs graph data Global Mean Estimates based on Land and Ocean Data graph.txt\" T df CSV.read download T url ,DataFrame, header false, skipto 6,delim \" \" T df T df , 1,6 end begin plot years,solp4. years. 1850 ,lw 2,label \"Predicted Temperature from model\", legend topleft xlabel \"year\" ylabel \"Temp °C\" plot parse. Float64, T df ,1 , parse. Float64, T df ,2 . 14.15, color black, label \"NASA Observations\", legend topleft end "},{"url":"climate_science/predicting_the_weather/","title":"Why we can't predict the weather","tags":["lecture","module3","track_climate","track_math","bifurcation","nonlinear","ODE","differential equation","continuous","plotting","dynamics","climate","modeling","DifferentialEquations"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v M3udLzIHtsc\" image \"https user images.githubusercontent.com 6933510 136199708 af8acad2 4172 4fa7 911e e30300efb5ee.png\" section 3 order 3 title \"Why we can't predict the weather\" layout \"layout.jlhtml\" youtube id \"M3udLzIHtsc\" description \"\" tags \"lecture\", \"module3\", \"track climate\", \"track math\", \"bifurcation\", \"nonlinear\", \"ODE\", \"differential equation\", \"continuous\", \"plotting\", \"dynamics\", \"climate\", \"modeling\", \"DifferentialEquations\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Plots, PlutoUI, LaTeXStrings, Roots, DifferentialEquations using LinearAlgebra end TableOfContents md\"\"\" Why we can predict the climate but can't predict the weather \"\"\" md\"\"\" Weather vs. climate \"\"\" md\"\"\" Will it rain tomorrow? We should be able to predict that using observations about the current state of the atmosphere and mathematical models in the form of partial differential equations that predict how the wind will move the air around, what the temperature will be, and how much water can precipitate out. What about predicting rain next week? Next year? We know from experience that we can only predict the weather in the short term, up to a week or so and still only with a certain probability . Predicting even one month out doesn't seem possible. On the other hand, we can talk about the climate and how it behaves on much longer time scales, e.g. \"the average temperature in in Boston in April is 55°F\". The short term weather has a significant random or apparently random component, but becomes much more predictable when we look at statistical properties like averages and standard deviations this is then called climate . \"\"\" md\"\"\" Nonlinear dynamics stability and bifurcations \"\"\" md\"\"\" In this notebook we will see that the simplest possible model of the weather, a set of 3 coupled, nonlinear ODEs known as the Lorenz equations , can have unpredictable behaviour, known as deterministic chaos . We will build up to that by looking at two simpler models of dynamics, in 1D and 2D. \"\"\" md\"\"\" Reminder Ordinary Differential Equations ODEs \"\"\" md\"\"\" Recall that we are looking at differential equations , with the goal of modelling the evolution in time of the climate. The simplest such models are ordinary differential equations ODEs , where one or a few continuous variables evolve continuously in time, with a model that specifies their instantaneous rate of change as a function of their current values. The general form of an ODE that does not depend explicitly on time, i.e. that is autonomous , is \\frac dx t dt f x t , or \\dot x t f x t , with an initial condition x t 0 x 0 . Here \\dot x t denotes the derivative of the function t \\mapsto x t at time t . \"\"\" md\"\"\" We have seen that the simplest numerical method to solve such an equation is the forward Euler method , in which we convert this equation into an explicit time stepping routine we take a small time step of length h and approximate the derivative as \\frac dx t dt \\simeq \\frac x t h x t h , giving the numerical method x n 1 x n h f x n , where x n is the approximation of the true solution x t n at the n th time step t n . \"\"\" md\"\"\" 1D Modelling bacterial growth \"\"\" md\"\"\" Let's use this to simulate a simple nonlinear ODE describing the dynamics of a population of bacteria. The bacteria reproduce at a rate \\lambda provided that there is sufficient food, in which case we would have \\dot x \\lambda x . But the need for enough food will put a limit on the sustainable population to a value K , sometimes called the carrying capacity . The simplest model for the combined effect of growth and saturation is as follows \\dot x \\lambda \\, x \\, K x . When x is close to 0 , the growth rate is \\lambda , but that rate decreases as x increases. This is sometimes called the logistic https en.wikipedia.org wiki Logistic function Logistic differential equation differential equation, although that name does not seem particularly helpful. \"\"\" md\"\"\" Our goal is to use computational thinking, but we will actually not be interested so much in the exact dynamics in time, but rather in the qualitative features of the behaviour of the system. For example, at long times formally t \\to \\infty does the population get arbitrarily large? Or does it, for example, oscillate around a particular value? Or does it converge to a particular size? This forms the subject of nonlinear dynamics or dynamical systems theory. \"\"\" md\"\"\" Let's simulate the system using the Euler method to try to guess the answer to this question. Note that there are many much more sophisticated methods for solving ODEs collected in the DifferentialEquations.jl package. We should never use the Euler method in practice, but should use a tried and tested library instead, and algorithms that provide much better accuracy in the solutions, if we are interested in faithful numerical results. \"\"\" md\"\"\" It's useful to rescale the variables to the simplest form \\dot x x \\, 1 x . We can do so by defining new adimensional space and time variables x' and t' as follows x K \\, x' , giving K \\dot x' \\lambda K \\, x' K K x' , and then t' \\lambda K \\, t , giving dx' dt' x' 1 x' . \"\"\" md\"\"\" We define a function representing the right hand side of the ODE \"\"\" logistic x x 1 x md\"\"\" Let's simulate this with the Euler method and plot the trajectory x t as a function of time t \"\"\" md\"\"\" Normally we would not choose the Euler method to calculate actual trajectories, but it is enough for our purposes of understanding the qualitative behaviour of the equations. \"\"\" md\"\"\" We see that for this particular initial condition, the solution seems to settle down to a fixed value after some time, and then remains at that value thereafter. Such a value is called a fixed point , a stationary point , or a steady state of the ODE. \"\"\" md\"\"\" Qualitative behaviour Fixed points and their stability \"\"\" gr fmt png, dpi 150, size 300, 200 gr fmt svg gr fmt png, dpi 300, size 400, 300 md\"\"\" Let's see what happens for other initial conditions \"\"\" md\"\"\" x₀ bind x0 Slider 0.9 0.001 3.0, default 0.5, show value true \"\"\" md\"\"\" To get an overview of the behaviour we can draw all the results on a single graph \"\"\" md\"\"\" We see that all the curves starting near to x 0 1.0 seem to converge to 1 at long times. If the system starts exactly at 0 then it stays there forever. However, if it starts close to 0, on either side, then it moves away from 0 on that same side of 0 starting from a negative value x becomes ever more negative. Even though negative populations have no meaning in the original interpretation as the dynamics of a population, we can still ask study the dynamics of the equation with negative initial conditions, since it may model other systems too. The special values x^ 1 1 and x^ 2 0 are called stationary points or fixed points of the differential equation. If we start at x^ i , then the derivative there is f' x^ i 0 , and hence we cannot move away from x^ i The fixed points can be found as zeros or roots of the function f , i.e. values x^ such that f x^ 0 . \"\"\" md\"\"\" We see, though, that the two types of fixed points are qualitatively different trajectories that start close to x^ 1 1 move towards x^ 1 , whereas trajectories that start close to x^ 2 0 move away from it. We say that x^ 1 is a stable fixed point and x^ 2 is an unstable fixed point . In general it is not possible to find analytical formulas for the position and stability of fixed points instead, we can use numerical root finding algorithms , for example the Newton method. There are various Julia packages with implementations of these algorithms we will use `Roots.jl`, which works well for scalar valued functions. \"\"\" md\"\"\" State space Vector field and phase portrait \"\"\" md\"\"\" If we want to find the whole trajectory for a given initial condition then we need to solve the equations, either numerically or analytically. However, we may want less information about the system, for example the long time or asymptotic dynamics. It turns out that we can obtain some information about that without explicitly solving the ODE This is the qualitative approach to studying nonlinear systems. Instead of drawing trajectories x t as a function of time t , as we did above, let's use a different graphical representation, where we draw state space or phase space This is the set \"space\" of all possible values of the dependent variables \"states\" . For the above ODE there is only a single dependent variable, x , so the state space is the real line, \\mathbb R . At each possible value of x , the ODE gives us information about the rate of change of x t at that point. Let's draw an arrow at that point, pointing in the direction that a particle placed at that point would move to the right if \\dot x 0 and to the left if \\dot x 0 . \"\"\" md\"\"\" This vector field indeed gives us a qualitative picture of the dynamics. It does not tell us how fast the dynamics will occur in each region, but it indicates what the tendency is. We have coded the fixed points according to their stability this may be calculated using the derivative evaluated at the fixed point, f' x^ , since this derivative controls the behaviour of nearby initial conditions x^ \\delta x . The unstable fixed point is shown as a green square, and the stable fixed point as a grey circle we will use this convention throughout the notebook. \"\"\" md\"\"\" Bifurcations \"\"\" md\"\"\" Now suppose that there is a parameter \\mu in the system that can be varied. For each value of \\mu we have a different ODE \\dot x f \\mu x . For example, \\dot x \\mu x^2. Let's draw the state space for each different value of \\mu \"\"\" g μ, x μ x^2 md\"\"\" μ bind λ Slider 1.0 0.05 1, show value true \"\"\" md\"\"\" Now let's collect all the vector fields into a single plot. The horizontal axis now represents the different possible values of the parameter \\mu \"\"\" md\"\"\" We see that at the critical value \\mu c 0 there is a qualitative change in behaviour of the system for \\mu c 0 there are two fixed points, whereas for \\mu c 0 there are no fixed points at all. Such a qualitative change is called a bifurcation . In this particular case the two fixed points collide in a saddle node or fold bifurcation. \"\"\" md\"\"\" 1D Bistability and hysteresis \"\"\" md\"\"\" Now let's look at the dynamics of the following system \\dot x \\mu x x^3. \"\"\" h μ, x μ x x^3 md\"\"\" Let's plot the bifurcation diagram again \"\"\" md\"\"\" We see that there is a range of values of \\mu for which there are three coexisting fixed points , two stable and one unstable. Since there are two stable fixed points in which the system can remain, we say that the system is bistable . \"\"\" md\"\"\" Now that we understand what the plots mean and the dynamics, let's plot just the fixed points x^ \\mu as a function of \\mu . Such a plot is called a bifurcation diagram \"\"\" md\"\"\" The pieces of curve are called branches . \"\"\" md\"\"\" Hysteresis \"\"\" md\"\"\" Suppose we now think about slowly varying the parameter \\mu . If we change the parameter \\mu by a little, the system is no longer at a fixed point, since the position of the fixed point moves when \\mu changes. However, the system will then relax it will follow the dynamics at the new value of \\mu , and will rapidly converge to the new fixed point nearby for that new value of \\mu . For example, starting at \\mu 2 , the system will stay on the lower black stable branch until \\mu 0.4 or so. At that point, two fixed points collide and annihilate each other After that there is no longer a fixed point nearby. However, there is another fixed point much further up that will now attract all trajectories, so the system rapidly transitions to that fixed point. Now suppose we decrease the parameter again. The system will now track the upper branch until \\mu 0.4 or so, when again it will jump back down. For each parameter value \\mu in the interval 0.4, 0.4 there is bistability , i.e. coexistence of two fixed points with the same value of \\mu together with a third, unstable fixed point that is not observable . The fact that the system tracks different stable branches depending on where we started, i.e. on the history of the dynamics, is known as hysteresis . \"\"\" md\"\"\" Hysteretic behaviour like this is found in many scientific and engineering contexts, including switches in biology, for example genetic switches, and in the historical dynamics of the earth's climate. \"\"\" md\"\"\" Slow fast systems \"\"\" md\"\"\" What are we actually doing when we let the parameter \\mu vary? Effectively we now have a system of two coupled ODEs, for example \\dot x \\mu x x^3 \\dot \\mu \\epsilon, where \\mu varies at a slow speed \\epsilon . On a time scale much shorter than 1 \\epsilon , the dynamics of x \"does not know\" that \\mu is changing, so it will converge to a fixed point x^ \\mu for the current value of \\mu . An associated term is adiabatic approximation . However, in fact \\mu does gradually change, so the value of x will effectively \"slide along\" the curve x t \\simeq x^ \\mu t , tracking the curve of fixed points as \\mu changes. Once \\mu reaches a critical value \\mu c , however, there is no longer a nearby fixed point to move to, and the vector field will move the system to rapidly transition to the far away alternative fixed point. If we now reverse the dynamics of \\mu , we slide back along the upper branch. \"\"\" md\"\"\" 2D Oscillations in chemical reactions the Brusselator model \"\"\" md\"\"\" Bifurcations are not restricted to 1D systems indeed, the dynamics can be much richer in higher dimensions. For example, let's look at the Brusselator model https en.wikipedia.org wiki Brusselator . This models a chemical reaction that oscillates , known as a chemical clock https en.wikipedia.org wiki Chemical clock \"\"\" md\"\"\" \\begin aligned \\dot x & a x^2 y bx x \\\\ \\dot y & b x x^2 y \\end aligned \"\"\" function brusselator xx, p, t x, y xx a, b p return a x^2 y b x x, b x x^2 y end md\"\"\" a bind a Slider 0.0 0.1 5.0, show value true, default 1.0 b bind b Slider 0.0 0.1 5.0, show value true, default 1.5 \"\"\" begin u0 1, 1 tspan 0.0, 50.0 params a, b end begin prob ODEProblem brusselator, u0, tspan, params soln solve prob end gr dpi 300 let tspan 0.0, 10.0 params a, b p1 plot leg false, background color inside black for x in 0 1.0 5 for y in 0 1.0 5 u0 x, y prob ODEProblem brusselator, u0, tspan, params soln solve prob plot p1, soln, vars 1, 2 , xlims 0, 5 , ylims 0, 5 , ratio 1, lw 1.5 p2 plot soln plot p1, p2, ylims 0, 5 end end plot direction field xs Float64 ys Float64 for x in 0 0.1 5 for y in 0 0.1 5 v brusselator x, y , params, 0 v . norm v 30 plot x, x v 1 , y, y v 2 , alpha 0.5, c gray push xs, x v 1 , x v 1 , NaN push ys, y v 2 , y v 2 , NaN end end plot xs, ys, alpha 0.7, c gray as svg p1 md\"\"\" Trajectories in state space for the Brusselator model p1 \"\"\" end md\"\"\" We see that there is a critical value of b above which the fixed point becomes unstable and gives rise to an attracting periodic orbit. This is a Hopf bifurcation https en.wikipedia.org wiki Hopf bifurcation . \"\"\" md\"\"\" 3D Chaos in the Lorenz equations \"\"\" md\"\"\" The Lorenz equations https en.wikipedia.org wiki Lorenz system form a very simplified model of convection in a layer of fluid representing the atmosphere, first investigated in a famous paper by Edward Lorenz, published in 1963, about work done at MIT. They represent a pioneering numerical investigation of chaotic behaviour . The Lorenz equations are a set of three coupled ODEs. They have an apparently simple form, with only two nonlinear terms \"\"\" md\"\"\" \\begin align \\dot x & \\sigma y x \\\\ 6pt \\dot y & x \\rho z y \\\\ 6pt \\dot z & x y \\beta z \\end align \"\"\" md\"\"\" Nonetheless, we will see that they can exhibit remarkably complex behaviour. Indeed, most nonlinear ODEs with at least three variables can be expected to exhibit similarly complicated behaviour. \"\"\" md\"\"\" Let's solve the system using `DifferentialEquations.jl`. We will take the classical parameter values \\sigma 10 and \\beta 8 3 , but we will allow \\rho to vary its classical value is 28 . \"\"\" function lorenz u, p, t x, y, z u σ, ρ, β p dx σ y x dy x ρ z y dz x y β z return dx, dy, dz end md\"\"\" ρ bind ρ Slider 0.0 0.1 100.0, show value true, default 10.0 \"\"\" lorenz params σ 10.0, ρ ρ, β 8 3 begin lorenz prob ODEProblem lorenz, 0.01, 0.01, 0.01 , 0.0, 100.0 , lorenz params lorenz soln solve lorenz prob end plot lorenz soln, vars 1, 2, 3 , xlabel \"x\", ylabel \"y\", zlabel \"z\", xlims 25, 25 , ylims 25, 25 , zlims 0, 60 md\"\"\" As \\rho increases we see a sequence of bifurcations. Above a critical value, trajectories converge to a fractal strange attractor , on which the dynamics is chaotic . \"\"\" md\"\"\" Deterministic chaos occurs when nearby initial conditions separate exponentially fast in state space. This has been given the name butterfly effect the perturbation to the atmosphere's state caused by a butterfly flapping its wings could end up being magnified to modify the direction in which a tornado moves. We can see this in a simple way by perturbing the initial condition slightly and calculating the distance between the two systems as a function of t \"\"\" begin ϵ 1e 10 lorenz prob2 ODEProblem lorenz, 0.01 ϵ, 0.01 ϵ, 0.01 ϵ , 0.0, 50.0 , lorenz params lorenz soln2 solve lorenz prob2 end begin ts 0 0.01 50 distances norm lorenz soln2 t lorenz soln t for t in ts end plot ts, distances, yscale log10, label \"distance\", xlabel \"t\", leg topleft md\"\"\" We indeed see a significant window over which the distance grows exponentially straight line on the semi logarithmic graph for large enough values of \\rho , before it saturates at a value of the order of the diameter of the attractor. The rate of the exponential growth is known as the Lyapunov exponent there are better ways to measure it more accurately. \"\"\" md\"\"\" Since the Lorenz equations model the atmosphere, we could expect that the dynamics of the atmosphere is at least as complicated. \"\"\" md\"\"\" We can also plot the x coordinate of each trajectory for comparison \"\"\" begin plot lorenz soln, vars 1, label \"original\", size 500, 300 , leg topleft plot lorenz soln2, vars 1, label \"perturbed\" ylabel \"x t \" end md\"\"\" At long enough times the trajectories separate and behave very differently. \"\"\" md\"\"\" Now let's look at the \"climate\" in this model, namely the average value of each coordinate over a long time \"\"\" mean v sum v length v begin T 1000.0 lorenz prob3 ODEProblem lorenz, 1 ϵ, 1 ϵ, 1 ϵ , 0.0, T , lorenz params lorenz soln3 solve lorenz prob3 lorenz prob4 ODEProblem lorenz, 1, 1, 1 , 0.0, T , lorenz params lorenz soln4 solve lorenz prob4 end mean abs. lorenz soln3 t for t in T 2 T mean abs. lorenz soln4 t for t in T 2 T md\"\"\" We see that the average mean value of each component is approximately the same, even though the individual trajectories are wildly different. This is an example of how statistical properties can be the same, even if individual behaviour is very different, and motivates the idea that climate i.e. \"average weather\" can be stable, even if day to day variations differ a lot. \"\"\" md\"\"\" Function library \"\"\" euler step f, x, h x h f x function euler f, x0, h, t final ts 0.0 xs x0 x x0 t 0.0 while t t final x euler step f, x, h t h push xs, x push ts, t end return ts ts, xs xs a named tuple end results euler logistic, 0.5, 0.01, 20.0 begin plot results.ts, results.xs, size 400, 300 , leg false, xlabel L\"t\", ylabel L\"x t \", lw 3 scatter results.ts 1 , results.xs 1 ylims 0.4, 1.1 end let p plot xlabel L\"t\", ylabel L\"x t \", leg false, ylim 1, 2 results euler logistic, x0, 0.01, 5.0 plot results.ts, results.xs, alpha 1, lw 3 scatter 0.0 , x0 hline 0.0 , ls dash as svg p end let p plot xlabel L\"t\", ylabel L\"x t \", leg false, ylim 1, 2 for x0 in 0.5 0.05 2.0 for x0 in 0.0 0.1 2.0 for x0 in 0.5 0.1 2.0 results euler logistic, x0, 0.05, 5.0 for x0 in 0.5 0.05 2.0 results euler logistic, x0, 0.01, 10.0 plot p, results.ts, results.xs, alpha 0.8, lw 1, arrow true end md\"\"\" Trajectories for ẋ x 1 x p \"\"\" end derivative f, x, h 0.001 f x h f x h 2h \"Draw 1D vector field using centred arrows\" function horiz vector field f xlo, xhi 2, 2.5 arrow size 0.07 tol 1e 5 tolerance to check for fixed point p plot size 400, 100 , xlim xlo, xhi , ylim 0.5, 0.5 , leg false, yticks for x in xlo 0.2 xhi d f x derivative if d tol plot x arrow size, x arrow size , 0, 0 , arrow true, c blue, alpha 0.5, lw 1.5 elseif d tol plot x arrow size, x arrow size , 0, 0 , arrow true, c red, alpha 0.5, lw 1.5 end end roots find zeros f, 10, 10 for root in roots stability sign derivative f, root if stability 0 scatter root , 0 , c green, alpha 0.5, m square, ms 5 unstable else scatter root , 0 , c black, alpha 0.5, ms 6 stable end end p end \"Draw vertical vector field of 1D ODE on plot p with parameter μ\" function vector field p, μ, f xlo, xhi 2, 2 arrow size 0.07 tol 1e 5 tolerance to check for fixed point for x in xlo 0.2 xhi d f x derivative if d tol plot μ, μ , x arrow size, x arrow size , arrow 3.0, 2.0 , arrowstyle triangle, c blue, alpha 0.4, lw 1.5 elseif d tol plot μ, μ , x arrow size, x arrow size , arrow true, c red, alpha 0.4, lw 1.5 end end roots find zeros f, 10, 10 for root in roots stability sign derivative f, root if stability 0 scatter μ , root , c green, alpha 0.5, m square, ms 2 unstable else scatter μ , root , c black, alpha 0.5, ms 3 stable end end p end function vector field f p plot leg false, xticks , xrange 1, 1 , size 100, 200 vector field p, 0, f end vector field logistic begin vector field x g λ, x end function bifurcation diagram h p plot leg false, ratio 1 for μ in 2 0.15 2 vector field p, μ, x h μ, x end xlabel \"μ\" ylabel \"fixed points and dynamics with given μ\" return p end bifurcation diagram g bifurcation diagram h function fixed points f p plot leg false, ratio 1 for μ in 2 0.01 2 roots find zeros x f μ, x , 10, 10 for root in roots stability sign derivative x f μ, x , root if stability 0 scatter μ , root , c green, alpha 0.5, m square unstable else scatter μ , root , c black, alpha 0.5 stable end end end xlabel L\"\\mu\" ylabel L\"x^ \\mu \" return p end fixed points h "},{"url":"climate_science/resistors_and_stencils/","title":"Resistors, stencils and climate models","tags":["lecture","module3","PDE","differential equation","ghost cell","boundary condition","climate","modeling","track_climate","track_math","stencil"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v DdTWgBlDgr0\" image \"https user images.githubusercontent.com 6933510 136200635 33c007ff 89f6 48dc b1d3 eb56fd16003e.gif\" section 8 order 8 title \"Resistors, stencils and climate models\" layout \"layout.jlhtml\" youtube id \"DdTWgBlDgr0\" description \"\" tags \"lecture\", \"module3\", \"PDE\", \"differential equation\", \"ghost cell\", \"boundary condition\", \"climate\", \"modeling\", \"track climate\", \"track math\", \"stencil\" using Markdown using InteractiveUtils using PlutoUI, OffsetArrays TableOfContents md\"\"\" Julia `CartesianIndex` `OffsetArray` from `OffsetArrays` \"\"\" md\"\"\" Resistors, Equilibrium, and Poisson's equation. 17 48 minute video \"\"\" md\"\"\" This video is really about the interaction of the discrete and the continuous. We set up a grid of resistor problem, and we show that solving this problem is the discretized version of solving a partial differential equation known as Poisson's equation. There are some references to fall 2020 which date this lecture, including a reference to the Biden Trump election, and a reference to John Urschel's lecture which some of you might want to check out John Urschel's video https youtu.be rRCGNvMdLEY \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid UKG xk2F3Ak params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" Stencils first 2.5 minutes or so Don't miss the fun video effects \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid sbqPVPLHl5Q params \"modestbranding 1&rel 0&end 180\" lite youtube \"\"\" md\"\"\" In the remainder of this notebook, we will show how to set up ghost cells in Julia so as to be able to apply a stencil. \"\"\" md\"\"\" Cartesian Indices \"\"\" md\"\"\" Grab a 6x7 matrix that we'd like to apply a stencil to. \"\"\" data rand 1 9,6,7 md\"\"\" A \"Cartesian index\" lets you access an element with one index variable \"\"\" i CartesianIndex 2,3 data i md\"\"\" Obtain all the Cartesian Indices of your data matrix. \"\"\" I CartesianIndices data Dump I md\"\"\" Offset Arrays \"\"\" md\"\"\" An offset array lets you index in ways other than the standard 1 m, 1 n \"\"\" A OffsetArray zeros Int,8,9 , 0 7 ,0 8 for i ∈ I A i data i copy data end A A 1,1 A 0,0 A I . data I md\"\"\" Neighborhood a 3x3 window built from Cartesian Indices \"\"\" neighborhood CartesianIndices 1 1, 1 1 md\"\"\" Grab all the neighborhoods of `A`. \"\"\" A i. neighborhood for i ∈ I md\"\"\" Stencil \"\"\" stencil 0 1 0 1 4 1 0 1 0 sum A i. neighborhood . stencil for i ∈ I md\"\"\" Notice the result is the same size as the original data, and the stencil \"worked\" on the edges. \"\"\" md\"\"\" Other boundary conditions. We just saw 0 boundary conditions, what about periodic or zero derivative? \"\"\" begin B copy A B 0, B 6, periodic B 7, B 1, B ,0 B ,7 B ,8 B ,1 B 0, B 1, zero derivative B 7, B 7 B ,0 B ,1 B ,8 B ,7 B end for i∈I B i sum A i. neighborhood . stencil end B md\"\"\" Climate Models in the Real World \"\"\" md\"\"\" play from t 28 122,330 1200 \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid mOJ0jABAhq4 params \"modestbranding 1&rel 0\" lite youtube \"\"\" "},{"url":"climate_science/time_stepping/","title":"Time stepping","tags":["lecture","module3","epidemiology","track_math","track_climate","plotting","continuous","discrete","ODE","differential equation","modeling"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 video \"https www.youtube.com watch?v 3Y5gVyO8KcI\" image \"https user images.githubusercontent.com 6933510 136199718 ff811eb3 aad6 4d6b 99e0 f6bf922816b4.png\" section 1 order 1 title \"Time stepping\" layout \"layout.jlhtml\" youtube id \"3Y5gVyO8KcI\" description \"\" tags \"lecture\", \"module3\", \"epidemiology\", \"track math\", \"track climate\", \"plotting\", \"continuous\", \"discrete\", \"ODE\", \"differential equation\", \"modeling\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, StatsBase, Statistics PlutoUI.TableOfContents md\"\"\" Modeling component failure Discrete and continuous \"\"\" md\"\"\" Let's think about a simple model for failure of components such as light bulbs. We actually derived this global, or macroscopic, model from a microscopic stochastic model a few lectures ago. Components can fail at any moment but we'll start off by checking once per day to count the number that have failed during that day. Then we'll check several times per day. This is still a discrete model. Finally we'll see how we can turn this into a continuous model where we can talk about the number that have failed by any real time t . \"\"\" md\"\"\" Checking failures once per day integer time steps \"\"\" md\"\"\" Let's call N k the average number of bulbs that are still functioning on day number k , starting from an initial number N 0 . We can find an equation for the number N k 1 that are still functioning at the end of day number k 1 by working out how many fail on day k 1 . Let's call p the probability that each bulb fails each day. For example, if 10% of the bulbs fail each day then p 0.1 . If there are 100 bulbs and 10% fail on the first day, then 10 fail, so there will be 90 remaining. In general, if a proportion p of the N k fail, in total we expect p \\, N k to fail. Hence N k 1 N k p \\, N k or N k 1 N k p N k. In this very simple model we can actually solve this recurrence relation analytically to find the number still functioning at time t N k 1 1 p N k so N k 1 p N k 1 1 p ^2 N k 2 \\cdots hence N k N 0 \\, 1 p ^k. \"\"\" md\"\"\" Checking failures n times per day \"\"\" md\"\"\" Now suppose instead we ask how many light bulbs fail in half a day. If 10% fail per day, it's natural to think that 5% fail in half a day. However, this is not quite right due to the effect of compounding as in compound interest . If 5% fail and then 5% of the remainder fail, then how many are left? \"\"\" 1 0.05 1 0.05 md\"\"\" So slightly fewer than 10% in total have failed, due to the effect of compounding. Nonetheless, the result is approximately right, so let's take that. \"\"\" md\"\"\" So let's suppose that n times a day, a proportion p n fail for example, 10% and twice a day gives 5% failing each time, approximately . Then the number remaining after the first failure check on day k is \"\"\" md\"\"\" N k \\frac 1 n \\textstyle 1 \\frac p n N k Here we have used a subscript since we are in a discrete situation. We could also have written instead N k \\textstyle \\frac 1 n . The solution at the next day is N k 1 N k \\, 1 \\textstyle \\frac p n ^n. And the full solution for the number remaining after k days is N k N 0 \\, \\textstyle 1 \\frac p n ^ nk \"\"\" md\"\"\" Let's plot these to see what they look like \"\"\" md\"\"\" n n slider bind n Slider 0 8, show value true \"\"\" p 0.4 let N0 100 T 20 N N0 1 p ^t for t in 0 T plot 0 T, N, m o, alpha 0.5, ms 3, label \"once daily\", lw 2 N2 N0 1 p5 2 ^ t for t in 0 2T plot 0 0.5 T, N2, m o, alpha 0.5, ms 2, label \"twice a day\" N4 N0 1 p5 4 ^ t for t in 0 4T plot 0 0.25 T, N4, m o, alpha 0.5, ms 2, label \"four times a day\" N N0 1 p 2^n ^ t for t in 0 2^n T plot 0 2.0^ n T, N, m o, alpha 0.5, ms 2, label \" 2^n times per day\" xlabel \"days k \" ylabel \"N k\" title \" 2^n times per day\" plot t N0 exp p5 t end md\"\"\" Continuous time \"\"\" md\"\"\" Thinking back to the class on \"discrete to continuous\", we see that we are producing more and more discrete values to keep track of, but after a while the curve they trace out does not really change. It thus makes sense to define a limiting object , which is what would happen if you could take smaller and smaller time steps in the right way, i.e. take the limit as n \\to \\infty . In that case we could imagine being able to calculate the average number N t of bulbs that are functioning at time t , where t can be any positive real number. \"\"\" md\"\"\" In calculus we learn ways to see that \"\"\" md\"\"\" 1 p n ^n converges to \\exp p as n \\to \\infty . \"\"\" md\"\"\" An alternative approach is to look at the time evolution in terms of differences . In a time 1 n a proportion p n decays. Here we are thinking of \\delta t 1 n as the time step between consecutive checks. If there are N t bulbs functioning at a time t and we take a small time step of general length \\delta t , a proportion p \\, \\delta t should fail, so that \"\"\" md\"\"\" N t \\delta t N t p \\, \\delta t \\, N t . \"\"\" md\"\"\" Dividing through by \\delta t we find \"\"\" md\"\"\" \\frac N t \\delta t N t \\delta t p \\, N t . \"\"\" md\"\"\" We now recognise the left hand side of the equation if we take the limit as \\delta t \\to 0 , we have exactly the definition of the derivative \\frac dN t dt . Hence, taking that limit we obtain \"\"\" md\"\"\" \\frac dN t dt p \\, N t with N 0 N 0 , the initial number. This is an ordinary differential equation it is an equation relating the value of the function N t at time t to the derivative slope of that function at that point. This relationship must hold for all t . It is not obvious that this equation even makes sense although it should do, given the way we have derived it , but in differential equations courses we see that it does make sense under some technical conditions , and uniquely specifies a function satisfying the ODE together with the initial condition. This is called an \"initial value problem\". \"\"\" md\"\"\" In this particular case, once again we are lucky enough to be able to solve this equation analytically N t is a function whose derivative is a multiple of the same function, and hence it must be exponential \"\"\" md\"\"\" N t N 0 \\exp p \\, t \"\"\" md\"\"\" This is an alternative way to define the exponential function. We can add this to the above plot \"\"\" n slider let N0 100 T 20 N N0 1 p ^t for t in 0 T plot 0 T, N, m o, alpha 0.5, ms 3, label \"once daily\", lw 2 N2 N0 1 p5 2 ^ t for t in 0 2T plot 0 0.5 T, N2, m o, alpha 0.5, ms 2, label \"twice a day\" N4 N0 1 p5 4 ^ t for t in 0 4T plot 0 0.25 T, N4, m o, alpha 0.5, ms 2, label \"four times a day\" N N0 1 p 2^n ^ t for t in 0 2^n T plot 0 2.0^ n T, N, m o, alpha 0.5, ms 2, label \" 2^n times per day\" xlabel \"days k \" ylabel \"N k\" title \" 2^n times per day\" plot t N0 exp p t , label \"continuous\", lw 2 end md\"\"\" We see graphically that this is indeed the correct limiting curve. \"\"\" md\"\"\" In this context, p is called a rate it is a probability per unit time , i.e. a ratio of probability and time. To get the probability of decaying in a time \\delta t , we multiplied p by \\delta t . \"\"\" md\"\"\" Let's summarise what we have found \"\"\" md\"\"\" | Step type | Time stepping | Difference | Solution | | | | | | | Integer | N k 1 1 p N k | N k 1 N k p N k | N k N 0 1 p ^k | Rational | N k \\frac 1 n \\textstyle 1 \\frac p n N k | N k \\frac 1 n N k \\textstyle \\frac p n N k | N k N 0 1 \\frac p n ^ n k | Continuous | N t \\delta t 1 p \\, \\delta t N t | \\frac dN t dt p \\, N t | N t N 0 \\exp p \\, t \"\"\" md\"\"\" SIR model \"\"\" md\"\"\" Let's look at a more complicated example, the SIR model of the spread of an epidemic, or of a rumour, in a population. You are surely familiar with models of this type modelling the spread of COVID 19, and from the homework. As in the homework, we can make a fully discrete, stochastic agent based model, where we give microscopic rules specifying how individual agents interact with one another. When we run such models with large enough systems, we observe that the results are often quite smooth. An alternative approach is to try to write down macroscopic discrete equations that describe the dynamics of averages. Often it is easier to understand the behaviour of such systems by formulating a continuous version of the model. Some people make discrete models because they're not happy with continuous models large discrete models can also be computationally wasteful. On the other hand, they can also include effects that might be more difficult to model in a continuous framework, e.g. non local effects or things that \"don't become continuous very well\". \"\"\" md\"\"\" Discrete time SIR model \"\"\" md\"\"\" First let's think about the SI model agents can be susceptible S and infectious I . A susceptible person becomes infectious when they come into contact with an infectious person, with some probability. \"\"\" md\"\"\" Let's call S t and I t be the number of susceptible and infectious people at time t , respectively, and let's call N the total number of people. Let's suppose that at each time step, each infectious person has the chance to interact with one other person on average . That person will be chosen at random from the total population of size N . A new infection occurs only if that chosen person is susceptible, which happens with probability S t N , and only if the infection attempt is successful, with probability b , say. Hence the change in the number of infectious people after that step is \"\"\" md\"\"\" \\Delta I t I t 1 I t b \\, I t \\, \\left \\frac S t N \\right \"\"\" md\"\"\" The decrease in S t is also given by \\Delta I t . \"\"\" md\"\"\" There is also recovery, with a constant probability c at each step, once you are infectious. It is useful to normalize by N , so we define the proportions of the population that are susceptible, infectious and recovered as s t \\frac S t N \\quad i t \\frac I t N \\quad r t \\frac R t N . \"\"\" md\"\"\" Including recovery with probability c we obtain the discrete time SIR model \"\"\" md\"\"\" \\begin align s t 1 & s t b \\, s t \\, i t \\\\ i t 1 & i t b \\, s t \\, i t c \\, i t\\\\ r t 1 & r t c \\, i t \\end align \"\"\" md\"\"\" Continuous time SIR model \"\"\" md\"\"\" We can now go through the same process as with the failure model, where we take time steps of length \\delta t instead, and replace probabilities b and c with rates \\beta and \\gamma . Taking the limit \\delta t \\to 0 we get \"\"\" md\"\"\" \\begin align \\frac ds t dt & \\beta \\, s t \\, i t \\\\ \\frac di t dt & \\beta \\, s t \\, i t & \\gamma \\, i t \\\\ \\frac dr t dt & & \\gamma \\, i t \\end align \"\"\" md\"\"\" We can think of this as a model of a chemical reaction with species S, I and R. The term s t i t is known as the mass action https en.wikipedia.org wiki Law of mass action form of interaction. Note that no analytical solutions of these simple nonlinear ODEs are known as a function of time However, parametric solutions are known https arxiv.org abs 1403.2160 . \"\"\" md\"\"\" Below is an example simulation of the discrete time model. \"\"\" begin NN 100 SS NN 1 II 1 RR 0 end ss, ii, rr SS NN, II NN, RR NN p infection, p recovery 0.1, 0.01 TT 1000 function discrete SIR s0, i0, r0, T 1000 s, i, r s0, i0, r0 results s s, i i, r r for t in 1 T Δi p infection s i Δr p recovery i s new s Δi i new i Δi Δr r new r Δr push results, s s new, i i new, r r new s, i, r s new, i new, r new end return results end SIR discrete SIR ss, ii, rr begin ts 1 length SIR discrete time SIR plot plot ts, x.s for x in SIR , m o, label \"S\", alpha 0.2, linecolor blue, leg right, size 400, 300 plot ts, x.i for x in SIR , m o, label \"I\", alpha 0.2 plot ts, x.r for x in SIR , m o, label \"R\", alpha 0.2 xlims 0, 500 end md\"\"\" Time stepping The Euler method \"\"\" md\"\"\" Above we showed how we can think of Ordinary Differential Equations ODEs as arising in a natural way from discrete time models where we take time steps. What about if somebody gives us an ODE how should we solve it numerically? In fact, we do the reverse process We discretize the equation and reduce it to a system where we take time steps Suppose the differential equation is \\dot x f x The simplest such method is the Euler method we approximate the derivative using a small but not too small time step h what we called \\delta t above \\dot x \\simeq \\frac x t h x t h giving x t \\delta t \\simeq x t h \\, f x . The Euler method with constant time step is then the following algorithm x k 1 x k h \\, f x k If we have several variables in our ODE, we can wrap the variables up into a vector and use the same method for the ODE \\dot \\mathbf x \\mathbf f \\mathbf x the Euler method becomes \\mathbf x k 1 \\mathbf x k h \\, \\mathbf f \\mathbf x k , where \\mathbf f denotes is a vector valued function mapping the vector of variables to the vector of right hand sides of the ODEs. \"\"\" md\"\"\" However, in general the Euler method is not a good algorithm to simulate the dynamics of an ODE We can see why that might be from the graphs at the start of this notebook taking time steps like this actually does a bad job at approximating the continuous curve. Numerical analysis courses show how to design better numerical methods to approximate the true solution of an ODE more accurately. The Julia SciML DifferentialEquations.jl https diffeq.sciml.ai stable tutorials ode example ecosystem provides a large suite of methods for solving ODEs and many other types of differential equations using state of the art methods. \"\"\" "},{"url":"data_science/discrete_and_continuous/","title":"Discrete and Continuous","tags":["lecture","module2","track_math","discrete","continuous"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v H6Dcx3YeTkE\" image \"https user images.githubusercontent.com 6933510 136196552 ce16c06f bd12 427f 80e5 aedb1fbc734a.png\" section 7 order 7 title \"Discrete and Continuous\" layout \"layout.jlhtml\" youtube id \"H6Dcx3YeTkE\" description \"\" tags \"lecture\", \"module2\", \"track math\", \"discrete\", \"continuous\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, HypertextLiteral, Graphs, GraphPlot, Printf, SpecialFunctions TableOfContents md\"\"\" Julia concepts printing using css fancy stuff not really julia pedagogical concepts curiosity based learning bridging what is often two different communities \"\"\" md\"\"\" Discrete and Continuous \"\"\" md\"\"\" An exact technical definition of discrete and continuous can be difficult, Nonetheless the idea of discrete mathematics is associated with finite or countably many values that are isolated. The set 1,...,n and the integers are both discrete sets. \"\"\" md\"\"\" DISCRETE MATH OBJECTS examples 1. Finite Sets 1,2,...,100 2. Infinite Discrete Sets ``\\mathbb Z `` integers ``\\ \\ldots, 2, 1,0,1,2,\\ldots\\ `` 3. Graphs \"\"\" gplot barabasi albert 150, 2 md\"\"\" By contrast, entire intervals or the whole real line is associated with continuous mathematics. In fact, mathematicians have worked painstakingly to define these terms, inventing fields such as point set topology that among other things can recognize the discrete, and analysis to rigorously work with the continuous. \"\"\" md\"\"\" Continuous real line continuous data image png base64,iVBORw0KGgoAAAANSUhEUgAAAWUAAACNCAMAAABYO5vSAAACT1BMVEX 8AAAAAAP e3t7 AACZAMzj4 91dXXg4P AHtwcHBnZ2f9 f8AlAAAlgDz8 v7 8AmwDx8fG5ubn5 f V1f 2tv R0dG6uv9cXP Pz 9lZf AwP rq 9fVsbGzx8f8gIP c3P Tk o6P9XV 96enqVlZUAoAD 4OD4 jz6P X1 8tLf87O AIednf9ycv Bgf8jI 9DQ0MAvgD 4v96ev IyP IiP9HR 99ff9aWv 0dH xMT TEz qKj srKZmf9AQP mpv q9 pbW1v ZWX AI Li7 dnZPT 7e01NTWhoaFISEj jIwWFhb seH goL urr PDz GBj lZVMwEz AHD wP h H n91pzGn9y 6xsbH XFwArAD 2u693r3N 83 Q0NzAKf w DV5tWP2I8wwDD j7YzoTP da67Xty7Q9yoQcuo4Kj OpL s8hgAMl1xnWezJ7Et8PET uMSsX S6v Hp3 h9J41njBS8jChPn On fKf AGH XKO6hL8AGgAAegDMAAApAACZANlaAJEAAF0AAKKNADtxAD4AQQAAzwDaAABMAACZAPoAAM2pAQFRAHxjc2Mskiy2SkqjZmZmZndSUsdaWpXZNIb qP nbljrmP ZMBRsFHKgdfenP S1ZKzmNfJcdu9 71j3mOI9oi7D S HMnNd Ld8asd LOYsiPJ8mAxID Qra1T v runPZO PAN7evtzFi WNAL3bt ZstqmftLyi e7OsWcAJai 6LYhdDDUMX0Xau1AAAZuElEQVR4nO1di18TWZauVFJIpJIQqPAKRMLLEEhiEgMBCSAQeQXloQF52ARQ3gwg2LRA6yzuztqzs7Pu7G7PvqalUQdcsW1s7LbRGbf1D9t7KxUIUMmtV3q7Z LVLyQkt6pufXXqu ece sWhrGDqsuJ8Esc4XDkilmbwvEzElXkLxkOHE8QvjbpwK1cymkShe jJ4 EXGQRPe7QCN4 mY3j5aVRi2jOGCtxgHL01hIK0GVKKXQZN7oIQBaqQG6GHq9AWGCd1QoPLjMverG8ejyD5FQrNlAVuMWJR6U5C1TCWd K12cjtpVViOujnzAM2kQOimYd2GEFajtYMm5E7ExzBvKXH7WMGxpQa6sRRxycHs syuBgH ygcEsWbmyIVkSTVlFagDXgjuhbSshpxY1omu1olo24xYIjDVXTaoluf4l23OJwoM5XmqMuEUtz6qMLYl69Md Cc7hSWQEEoxXDjUgvA5iFHrGPNCde3lCoR1x5gOXy5OgldMBIG3AcVaXcVkS1dVZgx1m4Ht1o5RhRhSrw7BxhLEPxoyqdiZrILGtKNbRtJgCzKEdcU3W4PUHXaokiqTrYfOQjbTkTr8Mw3InSDKoCwXKCI78A2DNSe BFaEeZMl6QUM XZXdFPri8z2BkBl6Zp8Hxc2yFCiocha10mwdIhojo8CW4wf7TzuXBK4 dlvPO5OuduL0AsoyyZVpKnQg9BSw70Jdg0J6jw11IH1xUGwKNKJXG15bdrfSGG6CvjFeBl8XOUqoUljGCXxL0QOAaytnrW5CTby 0MMebj9exU5hhob0UUM0MJ8qWabFw4oWIgwAWgmYZCCLCX0nIt1Tq86vw CjnPqcSL3Q6cb4051vsDQ2lOowy0nTjRraWLc9qP9eQAwWrIKcAK7VaWA8qh95ABb3 HGN9hEYruzWz7ow7C0hGmgVhy6AxdkOW6xDHwIXlLIslDZSMWqZAp9Mk26PacmIlQ1MaPy9DF3wjdboGXZ3TmcN6nZNhYpVlYRe4grSKioZsHU2cu8oI6prHesITQ VLQ9lyKW3LuFEXtRSoXZoT1dLmgmuLStNXolxQoIhRhblUl5OV02AxorYTDRQHHwOQbEdeLmnOfE223pgRvRDKloFiuLFWXI am8bB3pyEwY1b9MAM2S CA4AmshJ1TkGpQsGeHISmEMmyG dAsrvS6igH hud5QzLOQTLdryqELeg9gadUIT7kJyP4 WZ gaE6w1cNHt2LnJ wM8QkcjAqHwORoEmOeEcCKLs Q2IqiRmosQtDzQWaJIBida06AXceGEWhba NKudC32JVThPWT4MjRFRXUzD4VrJyqzUJeeiy Uhq1qaxylJSKFkWaPjdIkncMwC5VsRF GPgoLc 8axBFHHHHEEUccccQRRxw Q6Rw6ZhNSflRC0laKQ6bIrkV4lT1w3sPvV9KRxeWJ53msMXTpzlU4 IlOboQdjaVw 5OnuJQ6GIRh 2lFnEgIf3CRxGOL JJSili1kiXFXHYgewEuhB2 gKHup6UcTjqdNkFdKEU2VkOhU5f4LC 0zIOZzW1oygCy lFH7HSLC quMhUQ3bxyG9mr9drPrIZLqcipSjKARl8Pp8BfjjBjeWDayeFKinJZcsvRWKZTC4oSA6tkHL6LIdTf5FhmQzVkg3ytkgsY20yFjNPkcs6ZMzOjxE4ttijGrh1mGZOLGNRzMZXO hvrC3G LNMfty7t RJb8nxQhFYJnV2qzGzgUmzcbNlhmXDtenbyytDBvbrP7UjkiLK22QdZ4 8RqYUyQA CtX1sC2PTbi0avXU6iGaxdqyp0YB0ejhzTLV ykBsXTcmtlZJt1W2JVpqQiuwM2Wg4pB9i83KW1N8 0spxRWqo1dFzBoyzLZWXn4rykfnYUkR7DlMZXK5FKrTa7FQzvgZsuRGlJDiyKIRh9flnuXiCDuHCvEzrLOEuwHZfrb djy6F2l0qZsUi6PsrIZWZflbTSjYaoRNOQDWyYP2bJ5UW3S0svUSNjXHG25LQLL3X6GZYUHK LH8h2a4j3wOnbkrCwHR8ZBFNIpfvIiZ1smf k3V21K2 Ly1bUhtkJRdZnGWcbXSJGfZUiWpcpppMuS5AcY6zG5gsvUD2Ffp8pOydEoupTK kP7Psvd8iJZhELhSJUVMZ ChnwDvO4dLZQuazu Zp4 xLI1jf7i4oV09P5Oyy7K5TdvA0uml6ZR1kq1nYy0KYZl2VlaU1JOyKLjbwNqtdYEXlr13yGK8sCvQiQrfvX3PFe9H6QZvH3Gqfyv8X38A89d aZpmaG56R95rnqAj Sw6UHgt 8ENRm8JkwxYfnBP NclWH5dwJY heeu qNLWTLy8JZhlKX1HHwbwdbmd 6gqqsDbhiwvK 8rVl4v4e0 z9G6fyvzYKt WrNuhjwEUwyx10OHr6gOcLSSfAUpQku1REf6KXzzdCuvz7z5NOnAotSR1tJ0 eOrIcw6WOC T70YKn j2kyw 42SbLOnUiaRoS1FS0SlZWxF8T r8k999xrB86vMj5U6CYzr0Dazqf 5XiGTLf8N9J13quHQyUoX30Sa7cPLzk38I2fIfTp46cXiBm7 QcSlChU FSL0UarRPX2KsmD32G1EFddl0yJMDzSu6ocYuRnKaPIMMy83F3HwMbN H ILhmNg9ViZFdollzYwQy3Y6LiEjupfhCPrLD0OS0claSN72UaT1g61fx4mDQ5MnBb9jjuOIk5aybgrq8lz4tyL9Zay2OUhyt4Gvv1z2nFGMN8cKsfvLBeVBkluDQ9r4 MtDa1AwmmxfsjpyWGpETy7oY7QdzqgFZaMoVNfDsZ95YEqtmto4RLJYfxnzdQ82K5oHaw38I yyRzMEsTPzmGV 7BF2sqPeiFv0zLjBFB62jI1 uWxr mblGvuonPTo nLS0YST HRbJFsG19jwk1vDY0d2IDaPgXlqu2o98APvbFFe2YvHfyxjKRQpW6TJyXc0hMYW8svJ5d7sX7kWKYyOnsdgS7CmFkWyZVaIz8ntgzfLkffHLfPJw5YRlWq7GOkEnO2IUN8TjJRzSh2L1eUwcMwvS8Yyp1MvMr8sL4pYXWbnJBf3Ib2Ny1EXccnin 7gwnIbhwss5WwSh0LcWr82DizLL0TsC0LvIpVL508qB 6w9FQOPVIpFzkUkqdyoIbkVCk5h8Pjtj95arrwe1fjiOMvGST PvGYwst3BZLChN7 T5UMlfwYyuD1jo2hS FBxB5LDihJLinp47kOlZWTnSFw3PzoyvTKaOx57ttaX53rM6MLcoXBd9lTLJjo3s3nz3neNEPq9LixqkEYzZPzy7bbK2xhkJQwbz1VB6b ZwRdkis87TWNLR6BK2vePyO2P2Hv74yE5DMgyDZmCqK5eK3JplQ2rcRYNszjLm3ANDEu1fYMvu4rMFFULGz1jHcEQew89 FZJ7EKZpmF0dwZzDE3rfWz54wkwnAgYHKZAuu8Wxx2GDztdEKu8bIgzdA8pzNyz77i05i5mdt5C8 wp3mS7pKy2ZTLvxyKmTWTY4uBgDagNZlWJaEZkHyFzuFfqeVljiE0MOnl7V7u6 TWBUnG8Ure1kxO011SsAdQuXZTTKsdDSnDPVoX4HnCNDEnQQNoqO5qVjygaW4RwnJJMLu8QxB3OEszqSuEFButTgHWXLamnF eb7q9Ng keV5oY4JC33pAHZgIqKa0gYER0V6zobrR7 c3N sBz4MCquyFOfxtYnvnPrF3h6tm5NYZjU6L0Vh1ToA1j95WNs0DVR6d gbY88PYGLN5XO2amNC6ehZNAfUilzxBNECSFQ uX2l 0Nys8Hfzr LrJYJYekdszyztETuvcjmtA9w4o8Va7zQ6SsuBRev5TQMwOq cX7urvDs01P l7duvhE9tFg3DE9qJp PqwMDwgEk7cUucZhg8LcCGa1pqwJ rfn8Xby8DNn1L38PuqLc7xM6317itlGPBLXbgMDs07kwjcg6SI1UGjd dyTXb Csqt NV55CY264jom8xoO15Mqd1DXhvTQRcG3xjrsPw1V5RNNecB38V3UCeB3l7Ga8AwZtvnxE7f8Q2gZ xR04rJTtwvDAj32i0J2jSHJV25FRe4Riavmpbuzxta rPxUrIyKNsxcC81QN0YvgJsGWv Ukg0DMsanOGyzX xvO bqAW1eAjb8lI vYGMTs0tA27VstmiO3vuaxEwvnhyhPzgVbogHrkZPCa0bHsS6VtsnhSqZyOnbds3gLMbsmhLZsx7 LAS3HpDJ vtv2yobjLr7he7Bl84G k2f4VAJZfYIBlYMvYm92v33NZicq24sYzmgqnEfZiU8kUr9YPNH7zN7Gb88q1zlh5cYDliZ4nXvOqSjvgxcx9w8NinQxfMYl5GqEXVwxik Zqfmt7N3e fYP13r8PhwmUvXnNSZdJtxPPzKIajHg9ajYKFoyuXQVWDJpAKBmxQt cyBjmXQ1oB2C7Z5YiLjHUXlf4232G8zW8WZa eXstAXv7bAHaMlaGccrgUFkVFTm5ZAaO1wuY73h0cvrVkGH0S5tyWlAQxR3m1QDQZam2Zmj3K2rOG7Diluaaap4hK03rtW3i2fEhL5FBFhQAkSi14KiJYNhQMnQT BVDK01K9jHM0sH7VB14KVni09cCBMNjgCMyugWZx9ttYukt77XyKnFn1LlLIwH6Fb5OIBk3hazNHd5FtXpRKlsGfoZC0U7T6xOWY3Y I37Bn XETNyImo8uMoZu25QxivtCgCy lGpjBuDHNdeKqfCjZ8S2IJbxc4JjirIvm5Qr HLafOHt0QakY7nL7 etx4fwiCDu82cZDkrkNLEWKzT988pvYivMkGXJFMMz6BeWjduHMJbhTHJ69ER8kXDz9tXliAPmJIG3R6Uel4rl81dECgZQjIVt maVXIEbW4XPFzm0dtXWH5tUEQPAslaq ijox10XJRgCWdY04Dhyds IMDy0KSMMY5YIQDEk6 UrblT4GwX2 DHY3CN2 R8vDEtEsAyD7OUhCfvxj8E7IR3L1TUKvzA3eR bBLErQL8EhiUhwCD7ZmxZlkwxoB nuCxuG5t7C7sCnCodYFlQWBIEDLInYxlkA5an5tDFuADm42rECQawZaEsO4WHJRi1smxbE1nzqICKIRHL1dCPE80yy 1RaJTW48YKEV7C5LLt9mgMwz vlGQsw3ujxPlxglmGIfY5Ec9kGl2zLcdspAAGWVZPieskCQH4cQ8Gq8Wy N2CEJYTRAV wGNesTX9MobCDHVZGpY9jX5 i0dkCPXtd4JsmQ7 RLBsWFlW3h2NSe8qBCV3aSekYVkCPw7DZoQpBgxLEA8tio5rt5XzMRopAEDKTdqJLUk2dfmK4opYWcZ2CWJGwGowLCnk1X99BENryqYYBtkjAYlaP DH a z7IU6jhnixrdC1svCRYUlmGHappz2xKr9M29pJVIMKBhdYgWjbJPYE2LLkGWLiLAEwzrnlWuxGigHh9ZqRQ7EYHD gPxgqGZJQhBtgzCEiPqCVJRMTofQ4 ZPGfS9ogbVMTg8nWFeMEo 1SYLovskwLwrczHMC0nmS1Xt9SIFgysZPM7gtOgoqNIzrcWitFl0P5Nxs6TI8cmXBuS3FXiq66VQNd2v9vmNkDuCMgEwbdJhTAUu9iPHBtfnZPmTjSDFLHT5uOvuA32PAYypp0dIpHi9Xp QndVlmhKYtubHEccccQhFj6WJZZZ Z8tqMQEViRycT1GJ6dpTIa9T8ZuVBcl75P43nfBKHtd9rr30CtaaSpDr9dnZh59ZVY5kI fx2BfibIpfLkLXlfnpRg7Tsr7jgZ9fSNPXm5sSJMIFQtydnZm9hdhr91H0YofTBJ8BJlcUnWdy7ampuWmJltwUQbf 0WFKcN98r7h4ScvF8fD7Nbc13fr6YZWqzKtcpnPkA2 6svSpbXIZzeYWYJDr9loxQscuLOySl9IL1XMkgkWRzKHkQeezpX 28vKten FbDcnr86 fDVyqgYn9K82rM sDE1BQidWj8g9NbqhsqkMgVUpnEBp7C6 3xtd1eNyPFbh D8xaM7j8JfUUPvvFZc7wYqfHRJLMA4DLQwUD5N513lw2KNT OjVpS2mz6NqKwGmW4KuEwqdUDVY3JNhTTDO94DrHhiYG54 JaAUSeeRv VZnhrsICbKY BefJnCYhQcsOW5KimlWVEPfwWhYfzVzuD9jXZFPokHHKV2tTTM7c19lIdeLrPco9JPfVky2um E9og9FjtxSKKzXNUrD8 hM tzyEkIWLZbl 2bYSTBJ03rWJ7tA2j78cA3Sa0ydU6iehq8nbo1ZvyIUPYqqura2uvjwoeqQAAHFjV0A qRs3cnpGbmRMLl9l qQOrFoEvF4oVSPAeg9uCvYuqtTrIrZsMBhgd0mz LRcGbFHHEkYlb5 hJzKwY3Xhz YuyAtA XI4aPon1cyFgx1WZr2xTwQmLh18K 3x2VaFbvN881XBLFc9vrNASUzxMJSuPP19nt49 smahtZzKDP5LSMjIZye2altTL649CPYVIyXd53kM0DarV2Y3XfkwO6DAclAgdPRFDSreA5rvbN6zfvH8 MzM7uzO5rMXxayf4hln0 M0s jWAWcftfcgWemQi85oxMq9XKPMREz8 a 5ebRNuyvG98fH19gxkUMP7U5NK61Kb9mZfHptRTA vrPRNTU7cibgOFdgW 8RiPd2c 2SMIOEv7Z8 Y794QxN7 yJkXs8BP3rux9O7ZJmI0TYIdr3IHxxjBiV8qzzlwnhMMYA X921ZoC4Pr2 AgEOt0j6F 5nXTSa1SaVVq9XrTIGxgEqtVdEvreCBA108ByPuBp8A8yLtA7F3n luhyC n5mhLZf8E x9JyO5pKQMFSDoLLTpkmk47rRnJxYk1AGfgx9T TabSFv vVrrAhy6VE9g7dN7IJfesUWXtocJS8YC4Pep1fThCVdgQGjs18XTlv 8sE18UVJSggFVYJq31zsL25vEQrCHtfcGsVBSwomrLNxZAccWafLyEpNJjMqx4Faeg PF63IP7SNvbQUfMpey1XMLfPJOaLWrwa2Zt1ymgRGvl8SGtdoBgX3avkaejlxJQpDDFwTxjlGEdwSR TWwb qfx8DSOU6oB1gOd TcRrw1jeeQPaDLIv3lrblU By kONmptm FXDtPz KOzwSPANjKrXQdFF1jULQUAFA8p8ZRSj7X0DwB4Zl8nug19w2QeXg9QemS6VV4lXZuTx7W4GXLNJfNpuP9 3dAva977yRKcEzYJ4DLAu05WK thzEe4L4EHKQP90D0nEnxDIw6q hB7Q9a86E3b1D5ViBOlcW1uXyqke UqwuHwOZvjil1v5wLNjzPnWpVwWyfP6KkAD7BZzbbHb2C0j0W4JYwrB7IZZ3F4ivsLdvXjx UYYgusBxwLIus56Zx4 fZA7S5jFo3OoBHsbqMZKpYZNJ8HDmy828U3Jld2ZCD4KBLAPjBd7X4xDLMNs5s7MD3xCpjQI7XsiwnGWln8VTCKgu56UZUvjLh0Cmu4Arx1iy Yd91808sqFWCb4r21B9nm9f2TXgDz8j7nzYWaC1AU7A9f7NLPTu7uRh5DadV17a244alWgSqITEVjoogShtxS3lWclZ tCjFblCvL98BOY54CozSQzziIm5ScrcNzKgVYmZiou3Aeh29j59nqzBgo n IqeGHEn9PgdaMtf3 vtLXkOvo58c7Ymu8GdWInbGZZJd34WaMR0lTjOL30kuS6bt15ObTHuhfmlykQnQL0 bAxo1RM iN88d5D3ejUaugF89jHwn2EUuEBzvLBzDyOfE3eukRSFlSwQC19E3EaBw rICMsukxoSRtq8I wwf3m 6ZUUumw2H7h1YxPBtNEYjAUluyebIyh4NCCqJt6B6vRuEx8 LH3YhpYNn7yqSdbQB0uBL1geesQA BdWu GQu6xJK8TDXTtO6Jz hrFgz y8JCwfgjeYIOoLqFSmHzjMQ1lc293VyH uw4j4ExRieHxkmuZjqtdwD9B8aKL7a0AxIo e0zTgRsuhoISiSc7gGZV4Om8ykWtZ70oMBtkGq Md7xkYMaOq5mkcrKHnSz0f qa2pfH4MtjONdYuA3JBfBoklQJVobBrR0I Cih1tMxnInz6apjlJlRY8cPZZm6gyP0PMbz1LyUd3VdSXRN6EOU y 3wP RhfNkJGW gHJx6Pp0Ly0Q4Y3dW8By1JlJ4DzXhfssaxqcOJ5Z pN9pguHigWfKN7c0lW7rxjtCjY0RmW57OuQBMDHjN87LILQt3h 8G WDmKQAJYHbDdz z40eO9fJt Okp8Wimsba4s9BroDioHBU3x0qQZL1M18AZQ2SC2w23dHW5ol8GvwE5n1he7bz4id6Oc wW7cd QAKHdOzEbV 0iQRrJAeLcXZILq7T1GCQixPwQ Ubu0 4waOA7iPUd4HX yavEj496dkAvAQgn1cS zJbUffEdF7pkqcTeV Fzgu8zrRneSimpv 639Oy4kQ7f7r8J8i7v8YqfGYIXmz d 7horIXyNPHujuOKvw0g5Qmg SRx8UF0j8EEwcfBAt0LYsKI4 CA0rEj4M fiQKOdZrn4 KCEw8TjOIp2umMVzpERF47YoZ0evNXl90sxgjmOCKBZrm2 3ijkAVJxcES74roH KltUcQVI3YAZuzBuq5318RZjiGqrytaamtqWuKCEVO0KxSwN1DkJNdxIEB3TMUDwFgDdrzGSY49fDGbXPAni 8DqSQuwvSdulEAAAAASUVORK5CYII \"\"\" md\"\"\" Heard in the hallways I only like discrete math. I only like continuous math. \"\"\" md\"\"\" Indexing and Function Evaluation Analogy ``v i`` ith element of v vs. f x evaluate f at x These are different, right? In the one you are extracting an element, and in the other you are applying what in some high schools might be called a \"function machine.\" However, a moment's thought tells you that a vector is really a discrete function, in that the argument can take on the values i 1,2,...,n and the evaluation is v i . That's a function. In fact, think of a range object such as 2 2 20. You could think of this as just a shorthand for the vector 2,4,...,20 but in fact when you index into this \"vector\" like thing, you are indeed explicitly evaluating a function i.e. i 2i. \"\"\" 2 2 20 this expands the \"iterator\" into an ordinary vector 2 2 20 7 Extracts an element from Memory of course there is an address calculation 2 2 20 7 Compute 2 7 more or less begin f x 2x f 7 Compute 2 7 end md\"\"\" Any which way v is a function \"machine\" whose input is \\ 1,2,3,4,5,6,7,8,9,10\\ \"\"\" gr md\"\"\" Area \"\"\" md\"\"\" Area of a circle using regular polygons \"\"\" md\"\"\" n bind sides Slider 3 100, show value true, default 6 \"\"\" area s s 2 sin 2π s begin θ 0 .01 1 2π plot cos. θ ,sin. θ , ratio 1, axis false, legend false, ticks false, lw 4, color black, fill false plot cos. θ ,sin. θ , ratio 1, axis false, legend false, ticks false, lw 4, color white, fill true, alpha .6 ϕ 0 sides 2π sides for i 1 sides plot Shape 0,cos ϕ i ,cos ϕ i 1 , 0,sin ϕ i ,sin ϕ i 1 , fill true,lw 0 end title \"Area sides 2 sin 2π sides ≈ area sides π π\" end area0 area. 2 .^ 2 10 Area of polygons with sides 4, 8, ..., 1024 π md\"\"\" A carefully chosen convolution 1 3,4 3 \"\"\" area1 4 3 area0 i 1 . 1 3 area0 i for i 1 length area0 1 function colorgoodbad should be, given indexofmistake something findfirst collect should be . collect given , length given 1, htl \"\"\" span style \"color inherit\" given 1 indexofmistake 1 span span style \"color red\" given indexofmistake end span \"\"\" end colorgoodbad string float π , string 22 7 area2 16 15 area1 i 1 . 1 15 area1 i for i 1 length area1 1 md\"\"\" Another carefully chosen convolution 1 15,16 15 , do you see the pattern? \"\"\" big π area3 64 63 area2 i 1 . 1 63 area2 i for i 1 length area2 1 area4 128 127 area3 i 1 . 1 127 area3 i for i 1 length area3 1 md\"\"\" Why does this work? \"\"\" md\"\"\" Area s `` s 2 \\sin 2\\pi s \\pi \\frac 2\\pi^3 3 s^ 2 \\frac 2\\pi^5 15 s^ 4 \\frac 4\\pi^7 315 s^ 6 \\ldots`` as `` s \\rightarrow \\infty ``. \"\"\" md\"\"\" Area s `` \\pi c 1 s^2 c 2 s^4 \\ldots`` Area 2s `` \\pi c 1 4s^2 c 2 16s^4 \\ldots`` \"\"\" md\"\"\" Think about taking 4 3 Area 2s 1 3 Area s . Now we have ``\\pi c s^ 4 `` as the leading term so doubling the s approximately reduces the area error by 16, when before it was only 4. etc. \"\"\" areab s s 2 sin big 2 big π s begin area0b areab. big. 2,4,8,16,32,62,128,256,512,1024,2048,4096,8192,16384,32768,65536 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area0b end end begin area1b 4 3 area0b i 1 . 1 3 area0b i for i 1 length area0b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area1b end end string area1b end colorgoodbad sprintf \"%.30f\" big π , sprintf \"%.30f\" big area1b end begin area2b 16 15 area1b i 1 . 1 15 area1b i for i 1 length area1b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area2b end end begin area3b 64 63 area2b i 1 . 1 63 area2b i for i 1 length area2b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area3b end end begin area4b 256 255 area3b i 1 . 1 255 area3b i for i 1 length area3b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area4b end end begin area5b 1024 1023 area4b i 1 . 1 1023 area4b i for i 1 length area4b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area5b end end begin area6b 4096 4095 area5b i 1 . 1 4095 area5b i for i 1 length area5b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area6b end end begin area7b 16384 16383 area6b i 1 . 1 16383 area6b i for i 1 length area6b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area7b end end begin area8b 65536 65535 area7b i 1 . 1 65535 area7b i for i 1 length area7b 1 colorgoodbad sprintf \"%.80f\" big π , sprintf \"%.80f\" big area8b end end big π md\"\"\" Area using inscribed squares \"\"\" bind s Slider 2 40, show value true let plot for i s s plot i s,i s , 1,1 ,color RGB 0,1,0 ,lw 1 plot 1,1 , i s,i s ,color RGB 0,1,0 ,lw 1 end P plot cos. θ ,sin. θ , ratio 1, axis false, legend false, ticks false, lw 3, color black plot P h 1 s a 0 xx floor √2 2h x xx h y x plot Shape x, x, x ,x , y, y ,y, y , color RGB 1,0,0 ,alpha .7 a a Int 2 xx ^2 for i s xx 1 , j s 1 x i h y j h if x^2 y^2≤1 & x h ^2 y h ^2 ≤1 & x^2 y h ^2 ≤1 & x h ^2 y^2 ≤1 plot Shape x, x, x h ,x h , y, y h ,y h, y , color blue plot Shape x h, x h, x , x , y, y h ,y h, y , color blue plot Shape x, x, x h ,x h , y h, y , y, y h , color blue plot Shape x h, x h, x , x , y h, y , y, y h , color blue plot Shape y, y h ,y h, y , x, x, x h ,x h , color blue plot Shape y h, y , y, y h , x, x, x h ,x h , color blue plot Shape y, y h ,y h, y , x h, x h, x , x , color blue plot Shape y h, y , y, y h , x h, x h, x , x , color blue a 8 end end xlabel \"s s\" title \" a s^2 a h^2 π π\" plot end md\"\"\" Imagine you didn't have the idea of area \"\"\" begin N 1024 h 1 N v randn N end bind j Slider 1 9, show value true, default 6 J N ÷ 2^j begin plot c 0 cumsum v . √h plot 0 N . N,c scatter 0 J N . N, c 1 J end ,legend false,m o,ms 5, color red, lw 5 plot ylims 2,2 xlabel \"time\" ylabel \"position\" end md\"\"\" A random walk is a discrete random function. It is defined at grid points. Brownian motion is a continuous random function. It is defined on an entire interval. If one has an instance of a Brownian motion, you can say its exact value at, say, .7. If one looks at the random variable that represents evaluation at .7 it is a normal distribution. \"\"\" md\"\"\" Alan's essay In what sense does the continuous even exist? The fact of the matter is that there are limits that give the same answer no matter how you get there, and these limits are important to us. For example, no matter how you cover an area, by little rectangles, the sum always converges to what we intuitively call area. The normal distribution is interesting in that no matter which starting finite distribution we might take, if add n independent copies and normalize to variance 1 we get the same limit. Again, there are so many ways to start, and yet we always end up with the same thing. Continuous mathematics is full of so many examples, where discrete objects end up behaving the same. Indeed what happens as discrete objects get larger and larger, their complexity gets out of control if one wants to keep track of every detail, but they get simpler in their aggregate behavior. \"\"\" f x,t exp x^2 t √ π t bind t Slider .01 .01 8, show value true begin x 3 .01 3 plot x, f. x,t , ylims 0,1 , legend false end begin surface 2 .05 2, .2 .01 1, f, alpha .4, c Reds, legend false for t .2 .1 1 plot 2 .05 2, fill t,length 2 .05 2 , f. 2 .05 2,t , c black end xlabel \"x\" ylabel \"t\" plot end plotly md\"\"\" Sum and a Definite Integral \"\"\" md\"\"\" Cumsum and an Indefinite Integral \"\"\" md\"\"\" Discrete Finite Differencing Filters and Derivatives Gradients \"\"\" md\"\"\" Discrete and continuous convolutions e.g. probability densities \"\"\" md\"\"\" Discrete Random Walks and Brownian Motion \"\"\" md\"\"\" Binomial Distribution and the Normal Distribution \"\"\" md\"\"\" Discrete and the Continuous Fourier Transform \"\"\" P binomial n,k for n 0 5,k 0 5 P P' begin A 1 . beta. 1 6 ', 0 5 . 1 1 5 A 1, . 1 round. Int,A end function blue s String htl \" span style 'color hsl 200deg, 60%, 50% ' s span \" end md\"\"\" It is not unusual for students and professors to gravitate towards the discrete or the continuous. We wish to point out, that the discrete and the continuous are so closely related, that it is worthwhile to be comfortable with both. Up until fairly recently, much of computer science was often associated with discrete mathematics, while computational science and engineering was associated with physical systems, hence continuous mathematics. blue \"That is blurring these days \" The popularity of machine learning has brought continuous optimization ideas such as gradient descent into the world of computer science and the impact of the physical world on us all e.g. climate change, pandemics is motivating applications in computer science. The newfound popularity of Data science and statistics is also mixing the discrete with the continuous. \"\"\" md\"\"\" blue \"Continuous math often lets you replace complicated large systems with lots of details with a simpler abstraction that is easier to work with.\" \"\"\" md\"\"\" blue \"The combination of continuous and discrete is often more useful than either one alone.\" \"\"\" md\"\"\" blue \"Machine Learning, Pandemics, climate change, etc. show how critical continuous math is these days.\" \"\"\" blue \"asdf\" function pyramid rows Vector Vector horizontal false, padding x 8, padding y 2, style padding \" padding y px padding x px\" render row xs htl \"\"\" div padder padder htl \" span style style x span \" for x in xs padder padder div \"\"\" htl \"\"\" style .pyramid flex direction column display flex font family monospace font size 0.75rem .pyramid.horizontal flex direction row .pyramid div display flex flex direction row .pyramid.horizontal div flex direction column .pyramid div span hover background rgb 255, 220, 220 font weight 900 .pyramid div padder flex 1 1 auto .pyramid div span text align center style div class \"pyramid\", horizontal ? \"horizontal\" \"vertical\" render row. rows div \"\"\" end pyramid area0,area1 , horizontal true pyramid area0,area1,area2 , horizontal true pp x colorgoodbad string float π , sprintf \"%.15f\" x pyramid pp. area0 , pp. area1 , pp. area2 , pp. area3 , pp. area4 , horizontal true "},{"url":"data_science/linearmodel_datascience/","title":"Linear Model, Data Science, & Simulations","tags":["lecture","module2","track_julia","track_data","csv","dataframe","statistics","plotting","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v O6NTKsR8TjQ\" image \"https user images.githubusercontent.com 6933510 136199721 8fd577cb d6f3 492d bbdc 37bc74664ca7.png\" section 8 order 8 title \"Linear Model, Data Science, & Simulations\" layout \"layout.jlhtml\" youtube id \"O6NTKsR8TjQ\" description \"\" tags \"lecture\", \"module2\", \"track julia\", \"track data\", \"csv\", \"dataframe\", \"statistics\", \"plotting\", \"interactive\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, DataFrames, CSV, GLM, Statistics, LinearAlgebra, Distributions TableOfContents md\"\"\" Julia concepts for data science Data Frames `DataFrames.jl` `CSVread`, `CSVwrite` `CSV.jl` `lm` linear model `GLM.jl` ` formula` formula macro to specify variables to analyze `GLM.jl` Underscore as digits separator e.g. `1 000` for 1000 The value of fast simulations. \"\"\" md\"\"\" In this lecture we will simulate a real world statistical application for the purpose of understanding what statistics is about. It is very helpful in simulations to be able to run many examples fast. \"\"\" md\"\"\" Fahrenheit and Celsius Data Set \"\"\" n 10 x sort rand 10 100, n y 5 9 . x . 17.7777777 same as y 5 9 . x . 32 begin plot x,y, m c, mc red,legend false xlabel \"°F\" annotate 4,16,text \"°C\",11 plot x, x. 30 . 2 Dave's cool approximation end x y md\"\"\" Julia Data Frames I like to think of a Data Frame as a matrix with labels. \"\"\" md\"\"\" Data Frame by Columns with labels \"\"\" data DataFrame °F x,°C y Label data md\"\"\" Data Frame with a matrix \"\"\" begin data2 DataFrame x y , auto convert Matrix to DataFrame rename data2, \"°F\",\"°C\" add column labels end Matrix data2 Convert back to a matrix lose label information md\"\"\" Julia Comment about types Notice that x y converts all the data to floats, but columns of a data frame can have different types. \"\"\" md\"\"\" Reading Writing CSV comma separated values Files \"\"\" md\"\"\" Writing Data to a CSV file readable by spreadsheet software. \"\"\" CSV.write \"testCSVwrite.csv\", data md\"\"\" Reading Data from a CSV file to a DataFrame \"\"\" data again CSV.read \"testCSVwrite.csv\", DataFrame data again ,\"°F\" or data again ,1 data again ,1 md\"\"\" Noisy Data Add some random noise to the celsius readings \"\"\" md\"\"\" The noise slider so I can find it easily \"\"\" md\"\"\" noise bind noise Slider 0 .5 1000, show value true \"\"\" begin noisy data copy data Noisy DataFrame noisy data , \"°C\" . noise randn n yy noisy data , \"°C\" noisy data end md\"\"\" Statistics Software Outputs Mysterious Tables example output from the \"linear model\" `lm` which we store in the variable `ols` for ordinary least squares by contrast weighted least squares treats the vertical displacements with unequally with differing weights. \"\"\" ols lm formula °C ~ °F , noisy data noisy data md\"\"\" This lecture is about explaining the meaning and significance to every part of this table. \"\"\" md\"\"\" Regression a few ways \"\"\" md\"\"\" The \"Coef.\" column in the table gives the slope and intercept of the best fit line \"\"\" b, m one. x x \\ yy The mysterious linear algebra solution using \"least squares\" begin scatter x, yy,m c,mc red, label \"noisy data\", ylims 40, 40 for i 1 length data ,2 plot x i ,x i , m x i b,yy i , color gray, ls dash, label false end xlabel \"°F\" annotate 15,16,text \"°C\",11 plot x, m. x . b, color blue, label \"best fit line\" plot x,y,alpha .5, color red, label \"theory\" theoretical plot legend top end function linear regression x,y a direct computation from the data n length x x0 x. mean x y0 y. mean y mᵉ sum x0 . y0 sum x0.^2 slope estimate bᵉ mean y mᵉ mean x intercept estimate s2ᵉ sum mᵉ. x . bᵉ . y .^2 n 2 noise estimate bᵉ, mᵉ, s2ᵉ end linear regression x,yy md\"\"\" So why is it called \"Regression\" anyway? http blog.minitab.com blog statistics and quality data analysis so why is it called regression anyway Dalton's original meaning not quite what it means today. \"\"\" md\"\"\" Demystifying the word \"Model\" Step I The Model is y m x b σ randn . This means that out there in the real world are b, m, and σ. You don't know them. Step II You do, however, have data points x and y which allow you to compute an bᵉ, mᵉ, and σᵉ. A statistician would call these estimates based on your data points. If you ran the experiment again, you would get different data points. The computer lets us run the experiment as many times as we want just to see what happens. In summary, there are three kinds of variables. The model variables b, m, and σ which are unknown. The predictor variable x which is considered fixed and known. The response variable y which is considered noisy. \"\"\" md\"\"\" Understanding the relationship `°C ~ 1 °F` \"\"\" ols md\"\"\" `°C ~ 1 °F` means the celsius y is Coef1 1 Coef2 °F , in general `y ~ 1 x1 x2 x3` is shorthand for ``y c 0 c 1 x 1 c 2 x 2 c 3 x 3``, etc. \"\"\" md\"\"\" Simulating the real world running many noisy models \"\"\" function simulate σ,howmany linear regression x,y . σ randn length x for i 1 howmany linear regression x,y . σ sqrt 12 .5 . rand length x for i 1 howmany linear regression x,y . σ rand 1,1 ,length x for i 1 howmany end howmany 100 000 md\"\"\" Julia underscore as a digits separator \"\"\" md\"\"\" Simulated intercepts howmany simulations \"\"\" md\"\"\" σ bind σ Slider 0 .1 3, show value true, default 1 \"\"\" s simulate σ, howmany s 1 first simulation, intercept, slope, estimation of noise σ begin histogram first. s , alpha .6, bins 100, norm true vline 17.777777 ,color white title \"intercept\" xlims 17.7777 3, 17.7777 3 ylims 0,1 plot legend false end md\"\"\" Experimental mean of the intercept \"\"\" mean first. s , 17.777777 md\"\"\" Experimental std of the intercept \"\"\" std first. s md\"\"\" Statisticians know an exact formula for the theoretical std of the intercept \"\"\" sb σ norm x norm x. mean x sqrt n md\"\"\" Simulated slopes howmany simulations \"\"\" begin histogram getindex. s, 2 , alpha .6, bins 100, norm true, legend false title \"slope\" vline 5 9 ,color white xlims 5 9 .1, 5 9 .1 ylims 0,100 end md\"\"\" Sample mean of the slope \"\"\" mean getindex. s, 2 , .555555 md\"\"\" Sample std of the slope. \"\"\" std getindex. s, 2 md\"\"\" Statisticians know a formula for the theoretical std of the slope. \"\"\" σ norm x. mean x md\"\"\" Simulated σ howmany simulations \"\"\" begin histogram last. s . σ^2 n 2 , alpha .6, bins 100, norm true,legend false vline 1 ,color white title \"residual\" vline n 2 ,color white, lw 4 xlims 0,20 ylims 0,.13 plot x pdf Chisq n 2 ,x , lw 4 , color red plot end mean last. s σ^2 std last. s σ^2 sqrt n 2 2 md\"\"\" The Linear Model Table \"\"\" ols lm formula °C ~ °F , noisy data 24.3784 19.0397 0.686156 0.330459 md\"\"\" The Coef column is just the regression formula for the best line \"\"\" mᵉ, bᵉ, σ²ᵉ linear regression x, yy md\"\"\" The Std. error column \"\"\" md\"\"\" Above we saw that statisticians had formulas for the exact std of the slope and intercept `std intercept σ norm x norm x. mean x sqrt n ` ` std slope σ norm x. mean x ` \"\"\" md\"\"\" Let's replace σ with our estimate √σ²ᵉ \"\"\" sqrt σ²ᵉ norm x norm x. mean x sqrt n sqrt σ²ᵉ norm x. mean x md\"\"\" See those are the numbers in the magic table above. I always love when I can reproduce the numbers myself. It makes me feel I understand it. \"\"\" md\"\"\" The t column \"\"\" md\"\"\" The t column, is simply the Coeff column divided by the Std. error column which we will use in a hypothesis test in the upcoming column. \"\"\" md\"\"\" The t distribution \"\"\" md\"\"\" In a statistics class you will likely see a random variable known as a t distribution. with parameter k . It is the ratio of a standard normal to a χ distribution with parameter k. Let's just use `randn` to simulate. For the data sets of most of today's experiments, the normal distribution is close enough to t, that nobody needs to even use t much anymore. In any event, with a t or a normal we are using this distribution because we are cognizant of the fact that the true σ is unknown and is merely being estimated. \"\"\" rand t k sqrt k randn norm randn k md\"\"\" k bind k Slider 3 100, show value true \"\"\" begin histogram rand t k for i 1 100000 , norm true, bins 500, label false plot x pdf TDist k ,x , lw 4 , color red, label \"t dist\" plot x pdf Normal ,x , color green, lw 2, label \"normal dist\" xlims 3, 3 ylims 0, .4 end md\"\"\" The Pr |t| column is the area of the curve outside of the interval t,t . \"\"\" md\"\"\" In statistics we ask if the coefficient ought to be considered 0 which means in this case the data has no intercept or does not depend on x or whether the coefficients are signficant with some probability. The Pr |t| column gives us the probablity that we should accept the hypothesis that the coefficients might reasonably be just 0. In a proper statistical test, you should decide at what level you might be willing to accept the hypothesis, example .99, .95, or .9 might be a reasonable level, and if the test gives a smaller probability, you will accept that the coefficients are signficant. It is not proper to produce the able and then decide whether to use .99, say. \"\"\" md\"\"\" Degrees of Freedom \"\"\" md\"\"\" It is interesting to see that the sum of squares of a demeaned Gaussian vector is the size 1. This is the reason for the n 1 in the sample mean for variance. \"\"\" md\"\"\" How many degrees of freedom are in a \"demeaned\" vector of normals? \"\"\" mean v randn 17 v. mean v sum v.^2 for i 1 1 000 000 md\"\"\" If you ever wondered why the sample variance always has you dividing by n 1 and not n, this is the crux of the reason. \"\"\" "},{"url":"data_science/optimization/","title":"Optimization","tags":["lecture","module2","track_juli","track_climate","track_data","track_math","optimization"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v 44RA9fclTdA\" image \"https user images.githubusercontent.com 6933510 136199719 a56a217b cd36 4da2 b407 7285dcec94df.png\" section 9 order 9 title \"Optimization\" layout \"layout.jlhtml\" youtube id \"44RA9fclTdA\" description \"We use Optim.jl and JuMP.jl to optimize a function automatically finding the input that maximizes output.\" tags \"lecture\", \"module2\", \"track juli\", \"track climate\", \"track data\", \"track math\", \"optimization\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using PlutoUI, Plots, Statistics, Optim, JuMP, Ipopt, ForwardDiff TableOfContents md\"\"\" Julia concepts Named tuples \"\"\" md\"\"\" Line Fitting Many Ways \"\"\" md\"\"\" Last lecture we did some line fitting \"regression\" . Let's see how we can actually solve that problem. \"\"\" md\"\"\" Exploratory Data Analysis \"\"\" md\"\"\" Let's start off by making some noisy data. \"\"\" md\"\"\" n bind n Slider 3 10 200, show value true \"\"\" x sort rand 10 100, n y 5 9 . x . 17.7777777 . 5 . randn. same as y 5 9 . x . 32 begin plot x,y, m c, mc red, legend false, ls dash xlabel \"°F\" ylabel \"°C\" plot x, x. 30 . 2 Dave's cool approximation end md\"\"\" Least Squares fitting to a straight line \"\"\" md\"\"\" Suppose we are given data x i and measurements y i . Least squares fitting a straight line means finding the best ``m`` slope and ``b`` intercept that minimize the \"error\" distance from the data in a least squares sense \\min m,b \\sum b m x i y i ^2 \"\"\" md\"\"\" Direct Formulas \"\"\" md\"\"\" The Statistician's formula \"\"\" begin m cov x,y var x same as x. mean x ⋅ y. mean y sum abs2,x. mean x b mean y m mean x b b, m m end md\"\"\" Julia Named Tuples \"\"\" nt first 1, next 2, last 3.1 kind of handy typeof nt plot x m x b, lw 3, alpha 0.7 md\"\"\" The Linear Algebraist's Formula \"\"\" md\"\"\" This is even shorter, but you need to know linear algebra. But it also generalizes. \"\"\" one. x x \\y even shorter but you need to know linear algebra, but generalizes md\"\"\" Optimization Methods Since the problem is an optimization problem, we can use optimization software to obtain an answer. This is overkill for lines, but generalizes to so many nonlinear situations, including neural networks as in machine learning. \"\"\" md\"\"\" Optim.jl A package written entirely in Julia for optimization \"\"\" md\"\"\" Optim.jl Documentation https julianlsolvers.github.io Optim.jl stable \"\"\" md\"\"\" We can ask software to just solve the problem \\min b,m \\sum i 1 ^n b m x i y i ^2 or ``\\min b,m `` `loss b,m ` \"\"\" loss b, m sum b m x i y i ^2 for i 1 n result optimize loss, 0.0,0.0 optimize f with starting guess result.minimizer md\"\"\" Functions of Functions and Computing Power Optimization such as ``\\min b,m `` `loss b, m ` is an example of a fairly heavy function of a function. By this we mean that the input is a function such as `loss b, m ` and the output is the location or value of a minimum, say. By \"heavy\" we mean that typically a large amount of computing power is needed. Not that many years ago, computers were not strong enough for realistic problems. Modern day machine learning and so much more is enabled because computers can now surround entire codes with optimization, or if the software is compatible, automatic differentiation. \"\"\" md\"\"\" JuMP.jl A popular modelling language for Optimization Problems JuMP Julia for Mathematical Programming \"\"\" let n length x model Model Ipopt.Optimizer variable model, b variable model, m objective model, Min, sum b m x i y i ^2 for i in 1 n set silent model optimize model b value b , m value m end md\"\"\" Gradients \"\"\" md\"\"\" The above optimization methods made no explicit mention of derivative or gradient information. For simple problems, gradients can be hand calculated, but for many real problems this is impractical. \"\"\" md\"\"\" Hand Computation \"\"\" md\"\"\" `` \\frac \\partial \\partial b \\sum i 1 ^n b m x i y i ^2 2\\sum i 1 ^n b m x i y i `` `` \\frac \\partial \\partial m \\sum i 1 ^n b m x i y i ^2 2\\sum i 1 ^n x i b m x i y i `` \"\"\" begin ∇loss b,m,i 2 b m x i y i . 1,x i ith summand ∇loss b,m sum ∇loss b,m,i for i 1 n end md\"\"\" Finite Difference Evaluation \"\"\" ∇loss .1,.3 begin finite difference ϵ .000000001 loss .1 ϵ ,.3 loss .1 ,.3 ϵ . loss .1 ,.3 . ϵ end md\"\"\" Automatic Differentiation AutoDiff We're all so good at calculus I suppose, but nothing like letting the computer do it. For real problems, what you learned in calculus is impractical. Note Autodiff is not finite differences. It is not as problematic as finite differences is in that with finite differences it can be hard to know which ϵ to use, etc. \"\"\" ∇loss .1,.3 hand computation ForwardDiff.gradient loss, .1,.3 md\"\"\" Gradient Descent can be difficult for complicated functions \"\"\" let b, m 0, 0 starting guess for i 1 25 db, dm ∇loss b,m Getting a good step size can be really hard I worked out the line search η by hand η sum b m x i y i db dm x i for i 1 n sum db dm x i ^2 for i 1 n b, m b, m . η . db, dm end b b, m m end md\"\"\" Hoping for ` b 18.1716, m 0.56601 ` \"\"\" md\"\"\" Stochastic Gradient Descent Pick one term of the sum or a few of them and update according to the respective gradient. This is what works in machine learning. \"\"\" let b, m 0.0, 0.0 for t 1 10 000 000 η .00002 there seems to be an art to picking these steplengths b, m b, m . η ∇loss b,m, rand 1 n end b b, m m end md\"\"\" Bells and Whistles for optim.jl \"\"\" optimize loss, 0.0,0.0 , BFGS , autodiff forward optimize loss, 0.0,0.0 , BFGS optimize loss, 0.0,0.0 , GradientDescent optimize loss, 0.0,0.0 , GradientDescent , autodiff forward "},{"url":"data_science/pca/","title":"Principal Component Analysis","tags":["lecture","module2","track_data","data","statistics","matrix","linear algebra","track_math"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v iuKrM NzxCk\" image \"https user images.githubusercontent.com 6933510 136196577 512cee99 aebf 48a9 97b8 358d5ca561ca.png\" section 1 order 1 title \"Principal Component Analysis\" layout \"layout.jlhtml\" youtube id \"iuKrM NzxCk\" description \"In this notebook we will start looking at more general kinds of data, not only images, and we'll try to extract some information from the image using statistical methods, namely principal component analysis. This method tries to answer the questions \\\"which 'directions' are the most important in the data\\\" and \\\"can we reduce the dimensionality number of useful variables of the data\\\"?\" tags \"lecture\", \"module2\", \"track data\", \"data\", \"statistics\", \"matrix\", \"linear algebra\", \"track math\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import ImageMagick using PlutoUI using Colors, ColorSchemes, Images using Plots using LaTeXStrings using Statistics, LinearAlgebra standard libraries end PlutoUI.TableOfContents aside true md\"\"\" Introduction Understanding data In this notebook we will start looking at more general kinds of data , not only images, and we'll try to extract some information from the image using statistical methods, namely principal component analysis https en.wikipedia.org wiki Principal component analysis . This method tries to answer the questions \"which 'directions' are the most important in the data\" and \"can we reduce the dimensionality https en.wikipedia.org wiki Dimensionality reduction number of useful variables of the data\"? This can be viewed as another take on the question of finding and exploiting structure in data. It also leads towards ideas of machine learning . \"\"\" md\"\"\" Rank of a matrix \"\"\" md\" Flags\" md\"Let's start off by recalling ideas about multiplication tables or outer products \" outer v, w x y for x in v, y in w outer 1 10, 1 12 md\"\"\" Each column is a multiple in general not an integer multiple of any other column and each row is a multiple of any other row. This is an example of a structured matrix , i.e. one in which we need to store less information to store the matrix than the full m \\times n table of numbers even though no element is 0 . For example, for an outer product we only need m n numbers, which is usually much less. \"\"\" md\"Some flags give simple examples of outer products, for example\" flag outer 1, 0.1, 2 , ones 6 ones 6 flag2 outer 1, 0.1, 2 , 1, 1, 1, 3, 3, 3 md\"\"\" Note that outer products are not always immediate to recognise just by looking at an image But you should be able to recognise that there is some kind of structure. \"\"\" md\" Matrix rank\" md\"\"\" If a matrix can be written exactly as a single multiplication table outer product, we say that its rank is 1, and we call it a rank 1 matrix. Similarly, if it can be written as the sum of two outer products, it has rank 2 , etc. \"\"\" md\"Let's see what a random rank 1 matrix looks like \" w 300 image outer 1 0.4 rand 50 , rand w md\"\"\" It has a characteristic checkerboard or patchwork look. \"\"\" md\"\"\" Here's a random rank 2 matrix \"\"\" md\"\"\" We see that it starts to look less regular. \"\"\" md\"\"\" Exercise Make an interactive visualisation of a random rank n matrix where you can vary n . \"\"\" md\"\"\" Effect of noise \"\"\" md\"\"\" Now what happens if we add a bit of noise , i.e. randomness, to a rank 1 matrix? \"\"\" noisy image image . 0.03 . randn. md\"\"\"The noisy image now has a rank larger than 1. But visually we can see that it is \"close to\" the original rank 1 matrix. Given this matrix, how could we discover that it is close to a structured, rank 1 matrix? We would like to be able to find this out and say that the matrix is close to a simple one.\"\"\" md\" Images as data\" md\"\"\" Now let's treat the image as a data matrix , so that each column of the image matrix is a vector representing one observation of data. In data science it is often the rows that correspond to observations. Let's try to visualize those vectors, taking just the first two rows of the image as the x and y coordinates of our data points \"\"\" image 1 2, 1 20 begin xx image 1, yy image 2, end md\" From images to data\" md\"\"\" We would like to visualise this data with the `Plots.jl` package, passing it the x and y coordinates as arguments and plotting a point at each x i, y i pair. We obtain the following plot of the original rank 1 matrix and the noisy version \"\"\" gr begin xs noisy image 1, ys noisy image 2, scatter xs, ys, label \"noisy\", m ., alpha 0.3, ms 4, ratio 1 scatter xx, yy, leg topleft, label \"rank 1\", ms 3, alpha 0.3, size 500, 400 , m square, c red, framestyle origin title \"Plotting a rank 1 matrix gives a straight line \" end md\"We see that the exact rank 1 matrix has columns that lie along a line through the origin in this representation, since they are just multiples of one another. E.g. If x 1, y 1 and x 2, y 2 are two columns with x 2 cx 1 and y 2 cy 1 , then y 2 x 2 y 1 x 1 , so they lie along the same line through the origin. The approximate rank 1 matrix has columns that lie close to the line \" md\"So, given the data, we want to look at it do see if it lies close to a line or not. How can we do so in an automatic way? \" md\"\"\" Measuring data cloud \"size\" using statistics \"\"\" md\"Looking at this cloud of data points, a natural thing to do is to try to measure it How wide is it, and how tall?\" md\"\"\"For example, let's think about calculating the width of the cloud, i.e. the range of possible x values of the data. For this purpose the y values are actually irrelevant. \"\"\" md\"\"\" A natural idea would be to just scan the data and take the maximum and minimum values. However, real data often contains anomalously large values called outliers , which would dramatically affect this calculation. Instead we need to use a statistical method where we weight the data and average over all the data points. This process will hopefully be affected less by outliers, and will give a more representative idea of the size of the bulk of the data. \"\"\" md\"\"\" A first step in analysing data is often to centre it data around 0 by subtracting the mean, sometimes called \"de meaning\" \"\"\" begin xs centered xs . mean xs ys centered ys . mean ys end scatter xs centered, ys centered, ms 5, alpha 0.5, ratio 1, leg false, framestyle origin md\"\"\" Measuring a \"width\" of a data set \"\"\" md\"\"\" A natural way to measure the width of a data set could be to measure some kind of width separately in both the x and y directions, in other words by projecting the data onto one of the axes, while ignoring the other one. Let's start with the x coordinates of the centred data and try to average them. If we literally average them we will get 0 since we have subtracted the mean . We could ask \"on average how far away from the origin is the data\". This would give the mean absolute deviation https en.wikipedia.org wiki Average absolute deviation \"\"\" begin scatter xs centered, ys centered, ms 5, alpha 0.5, ratio 1, leg false, framestyle origin scatter xs centered, zeros size xs centered , ms 5, alpha 0.1, ratio 1, leg false, framestyle origin for i in 1 length xs centered plot xs centered i , ys centered i , xs centered i , 0 , ls dash, c black, alpha 0.1 end plot end mean abs. xs centered md\"\"\" This is a perfectly good computational measure of distance. However, there is another measure which is easier to reason about theoretically analytically \"\"\" md\"\"\" Root mean square distance Standard deviation \"\"\" md\"\"\" The standard deviation is the root mean square distance of the centered data from the origin. In other words, we first square the distances or displacements from the origin, then take the mean of those, giving the variance . However, since we have squared the original distances, this gives a quantity with units \"distance ^2 \", so we need to take the square root to get back to a measurable length \"\"\" begin σ x √ mean xs centered.^2 root mean square distance from 0 σ y √ mean ys centered.^2 end md\"This gives the following approximate extents standard deviations of the cloud \" begin scatter xs centered, ys centered, ms 5, alpha 0.5, ratio 1, leg false, framestyle origin vline 2 σ x, 2 σ x , ls dash, lw 2, c green hline 2 σ y, 2 σ y , ls dash, lw 2, c blue annotate 2σ x 0.93, 0.03, text L\"2\\sigma x\", 14, green annotate 2σ x 0.88, 0.03, text L\" 2\\sigma x\", 14, green annotate 0.05, 2σ y 1.13, text L\"2\\sigma y\", 14, blue annotate 0.06, 2σ y 1.14, text L\" 2\\sigma y\", 14, blue end md\"\"\" We expect most around 95% of the data to be contained within the interval \\mu \\pm 2 \\sigma , where \\mu is the mean and \\sigma is the standard deviation. This assumes that the data is normally distributed , which is not actually the case for the data generated above. \"\"\" md\" Correlated data\" md\"\"\" However, from the figure it is clear that x and y are not the \"correct directions\" to use for this data set. It would be more natural to think about other directions the direction in which the data set is mainly pointing roughly, the direction in which it's longest , together with the approximately perpendicular direction in which it is narrowest. We need to find from the data which directions these are, and the extent width of the data cloud in those directions. However, we cannot obtain any information about those directions by looking separately at x coordinates and y coordinates, since within the same bounding boxes that we just calculated the data can be distributed in many different ways. Rather, the information that we need is encoded in the relationship between the values of x i and y i for the points in the data set . For our data set, when x is large and negative, y is also rather negative when x is 0, y is near 0 , and when x is large and positive, so is y . We say that x and y are correlated literally they are mutually \"co\" related, such that knowing some information about one of them allows us to predict something about the other. For example, if I measure a new data point from the same process and I find that x is around 0.25 then I would expect y to be within the range 0.05 to 0.2 , and it would be very surprising if y were 0.5. \"\"\" md\"\"\" Although there are standard methods to calculate this correlation, we prefer to hone our intuition using computational thinking instead \"\"\" md\"\"\" We want to think about different directions , so let's introduce an angle \\theta to describe the direction along which we are looking. We want to calculate the width of the cloud along that direction . Effectively we are changing coordinates to a new coordinate, oriented along the line. To do this requires more linear algebra than we are assuming in this course, but let's see what it looks like \"\"\" md\" Rotating the axes\" md\"\"\"By rotating the axes we can \"look in different directions\" and calculate the width of the data set \"along that direction\". What we are really doing is a perpendicular projection of the data onto that direction.\"\"\" M xs centered ys centered ' eigvals cov M' . 199 imax argmax M 1, svdvals M M gr R θ cos θ sin θ sin θ cos θ md\"In the following figure, we are rotating the axis red arrow around in the left panel. In the right panel we are viewing the data from the point of view of that new coordinate direction, in other words projecting onto that direction, effectively as if we rotated our head so the red vector was horizontal \" md\"\"\" degrees bind degrees Slider 0 360, default 28, show value true \"\"\" md\"\"\" Rotating the data \"\"\" θ π degrees 180 radians p1 begin scatter M 1, , M 2, , ratio 1, leg false, ms 2.5, alpha 0.5, framestyle origin projected cos θ sin θ M . cos θ sin θ ' scatter projected 1, , projected 2, , m 3, alpha 0.1, c green lines x reduce vcat, M 1, i , projected 1, i , NaN for i in 1 size M, 2 lines y reduce vcat, M 2, i , projected 2, i , NaN for i in 1 size M, 2 for i in 1 size M, 2 plot M 1, i , projected 1, i , M 2, i , projected 2, i , ls dash, c black, alpha 0.1 end plot lines x, lines y, ls dash, c black, alpha 0.1 plot 0.7 . cos θ , sin θ , 0.7 . cos θ , sin θ , lw 1, arrow true, c red, alpha 0.3 xlims 0.7, 0.7 ylims 0.7, 0.7 scatter M 1, imax , M 2, imax , ms 3, alpha 1, c yellow annotate 0, 1.2, text \"align arrow with cloud\", red, 10 end p1 p2 begin M2 R θ M scatter M2 1, , M2 2, ,ratio 1, leg false, ms 2.5, alpha 0.3, framestyle origin, size 500, 500 plot 0.6, 0 , 0.6, 0 , lw 3, arrow true, c red, xaxis false, yaxis false, xticks , yticks scatter M2 1, imax , M2 2, imax , ms 3, alpha 1, c yellow xlims 0.7, 0.7 ylims 0.7, 0.7 scatter M2 1, , zeros size xs centered , ms 3, alpha 0.1, ratio 1, leg false, framestyle origin, c green lines2 x reduce vcat, M2 1, i , M2 1, i , NaN for i in 1 size M2, 2 lines2 y reduce vcat, M2 2, i , 0, NaN for i in 1 size M2, 2 for i in 1 size M2, 2 plot M2 1, i , M2 2, i , M2 1, i , 0 , ls dash, c black, alpha 0.1 end plot lines2 x, lines2 y, ls dash, c black, alpha 0.1 σ std M2 1, vline 2σ, 2σ , ls dash, lw 2 plot 0.5 cos θ , cos θ , 0.5 sin θ , sin θ , c black, alpha 0.5, lw 1, arrow true plot 0.5 sin θ , sin θ , 0.5 cos θ , cos θ , c black, alpha 0.5, lw 1, arrow true plot 0.5 cos θ , cos θ , 0.5 sin θ , sin θ , c black, alpha 0.5, lw 1, arrow true plot 0.5 sin θ , sin θ , 0.5 cos θ , cos θ , c black, alpha 0.5, lw 1, arrow true title \"σ round σ, digits 4 \" annotate 2σ 0.05, 0.05, text \"2σ\", 10, green annotate 2σ 0.05, 0.05, text \" 2σ\", 10, green end plot p2 md\"\"\" We see that the extent of the data in the direction \\theta varies as \\theta changes. Let's plot the variance in direction \\theta as a function of \\theta \"\"\" begin variance θ var R θ M 1, variance θ AbstractArray variance θ 1 end p3 begin plot 0 360, variance. range 0, 2π, length 361 , leg false, size 400, 200 scatter degrees , σ^2 xlabel \"θ\" ylabel \"variance in direction θ\" end σs svdvals M variances σs.^2 . 199 1 . σs M md\"\"\"The direction in which the variance is maximised gives the most important direction It is the direction along which the data \"points\", or the direction which best distinguishes different data points. This is often called the first principal component in statistics, or the first singular vector in linear algebra. We can also now quantify how close the data is to lying along that single line, using the width in the perpendicular direction if that width is \"much smaller\" than the width in the first principal direction then the data is close to being rank 1. \"\"\" md\"The simplest way to maximise this function is to evaluate it everywhere and find one of the places where it takes the maximum value \" begin θs 0 0.01 2π fs variance. θs θmax θs argmax fs θmin θs argmin fs fmax variance θmax fmin variance θmin end begin scatter xs centered, ys centered, ms 5, alpha 0.3, ratio 1, leg false, framestyle origin plot 0, 0 , 2 sqrt fmax . cos θmax , sin θmax , arrow true, lw 3, c red plot 0, 0 , 2 sqrt fmin . cos θmin , sin θmin , arrow true, lw 3, c red end md\"\"\" Note that the directions that maximise and minimise variance are perpendicular. This is always the case, as shown using the Singular Value Decomposition SVD in linear algebra. There are different ways to think about this procedure. We can think of it as effectively \"fitting an ellipse\" to the data, where the widths of the ellipse axes show the relative importance of each direction in the data. Alternatively we can think of it as fitting a multivariate normal distribution by finding a suitable covariance matrix. \"\"\" begin circle cos. θs sin. θs ' stretch 2 sqrt fmax 0 0 2 sqrt fmin ellipse R θmax stretch circle plot ellipse 1, , ellipse 2, , series shape, alpha 0.4, fill true, c orange end md\"\"\" Note also that an ellipse is the image of the unit circle under a linear transformation. We are effectively learning what the best linear transformation is that transforms the unit circle into our data cloud. \"\"\" md\" Higher dimensions\" md\"\"\" Can we generalise this to dimensions higher than 2? Let's think about 3D. If we take columns of the first three rows of the original image, we have vectors in 3D. If we plot these vectors in 3D, a rank 1 matrix will give a straight line in 3D, while a rank 2 matrix will give a plane in 3D. Rank 2 noise gives a noisy cloud lying close to a plane. Similarly to what we did above, we need to calculate the ellipsoid that best fits the data. The widths of the axes of the ellipsoid tell us how close to being a line or a plane rank 1 or rank 2 the data is. \"\"\" md\"In more than 3D we can no longer visualise the data, but the same idea applies. The calculations are done using the SVD. If the widths of the ellipsoid in some directions are very small, we can ignore those directions and hence reduce the dimensionality of the data, by changing coordinates to the principal components.\" md\"\"\" What is the Singular Value Decomposition SVD ? \"\"\" md\"\"\" The Singular Value Decomposition SVD is a way of writing any matrix in terms of simpler matrices. Thinking in terms of linear transformations, any linear transformation T has the same effect as a sequence of three simple transformations T rotation₂ ∘ stretch ∘ rotation₁ In terms of matrices, for any matrix M it is possible to write M U \\, \\Sigma \\, V^\\text T where U and V are orthogonal matrices, i.e. they satisfy U U^\\text T I and V V^\\text T I , and \\Sigma is a diagonal matrix. Orthogonal matrices have determinant \\pm 1 , so they leave areas unchanged. They are rotations, possibly combined with reflections. There are algorithms from numerical linear algebra to calculate this decomposition. In Julia we call the `svd` function, e.g. \"\"\" let M 2 1 1 1 svd M end md\"\"\" Let's look at the action of the matrix on the unit disc. To generate points in the unit disc we generate points in 1, 1 ^2 and reject those lying outside \"\"\" begin unit disc 1.0 . 2.0 . rand 2 for i in 1 2000 unit disc reduce hcat, x for x in unit disc if x 1 ^2 x 2 ^2 1 end scatter unit disc 1, , unit disc 2, , ratio 1, leg false, alpha 0.5, ms 3 md\"\"\" t bind tt Slider 0 0.01 1, show value true \"\"\" pp1 begin scatter unit disc 1, , unit disc 2, , ratio 1, leg false, alpha 0.5, title \"stretch rotate\" result 1 tt tt tt 1 unit disc scatter result 1, , result 2, , alpha 0.2 ylims 3, 3 xlims 3, 3 end pp2 begin UU, Sigma, VV svd 1 tt tt tt 1 scatter unit disc 1, , unit disc 2, , ratio 1, leg false, alpha 0.5, title \"stretch\" result2 Diagonal Sigma unit disc scatter result2 1, , result2 2, , alpha 0.2 ylims 3, 3 xlims 3, 3 end plot pp2, pp1 md\"\"\" Rotations in 300 dimensions \"\"\" md\"\"\" We have been thinking of 300 points in 2 dimensions. Instead, we could think about 2 points in 300 dimensions . In some sense, this is what \"transpose really does\" \"\"\" md\"\"\" The V in the SVD is another rotation that we \"cannot see\" in the above pictures. In our case it is a rotation in 300 dimensions But in fact we can visualise it as follows, where we multiply our data by a 300 \\times 300 orthogonal matrix We first take a random anti symmetric matrix, i.e. one for which M M^\\text t . We then turn that into an orthogonal matrix by taking the so called matrix exponential . \"\"\" begin dim size M, 2 anti symmetric randn dim, dim anti symmetric anti symmetric' end bind t Slider 0 0.0002 1, show value true, default 0.0 begin M rotated M exp t anti symmetric scatter M rotated 1, , M rotated 2, , leg false, alpha 0.5, scatter M 1, , M 2, , alpha 0.5 ylims 0.3, 0.3 xlims 0.6, 0.6 end md\"\"\" We see that the data rotates around in 300 dimensions, but always is projected to the same ellipse. \"\"\" U, Σ, V svd M, full true M18 M V scatter M18 1, , M18 2, , alpha 0.5, leg false, ratio 1, xlim 5, 5 md\" Appendix\" begin show image M get. Ref ColorSchemes.rainbow , M . maximum M show image x AbstractVector show image x' end show image flag show image flag2 show image image begin image2 outer 1 0.4 rand 50 , rand w outer rand 52 , rand w show image image2 end show image image show image noisy image show image image 1 2, 1 20 loss M1, M2 sum M1 i M2 i ^2 for i in 1 length M1 if ismissing M2 i function split up v, m, n return v 1 m , v m 1 m n , v m n 1 2m n , v 2m n 1 2m 2n end function ff v, m, n v1, w1, v2, w2 split up v, m, n loss outer v1, w1 outer v2, w2 , M3 end ff2 v ff v, m, n "},{"url":"data_science/random_variables_as_types/","title":"Random Variables as Types","tags":["lecture","module2","track_julia","type","programming","probability","interactive","random","track_math","Symbolics"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v xKAO38UsXo0\" image \"https user images.githubusercontent.com 6933510 136196570 478bbb89 05fb 4799 99a0 0ede06354cb6.png\" section 4 order 4 title \"Random Variables as Types\" layout \"layout.jlhtml\" youtube id \"xKAO38UsXo0\" description \"\" tags \"lecture\", \"module2\", \"track julia\", \"type\", \"programming\", \"probability\", \"interactive\", \"random\", \"track math\", \"Symbolics\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Statistics, Plots, PlutoUI, LinearAlgebra, Symbolics TableOfContents aside true md\"\"\" Concepts for today We won't be introducing any new Julia functions in this lecture, but we are going to show off a key way in which Julia really shines its type system . With a few carefully chosen definitions, we can gain a great deal of power Concepts for today Types for code organisation of abstract concepts Abstract types Subtypes Building up expressions \"under the hood\" using types `sum` over an iterator generator expression \"\"\" md\"\"\" Random variables as types \"\"\" md\"\"\" This lecture might appear to be about random variables and it is . But we would be thrilled if you see it rather as a more general discussion on software engineering and abstractions, since the principles involved extend to many different contexts. \"\"\" md\"\"\" Random variables \"\"\" md\"\"\" Recall that a random variable X is an object which has different possible outcomes x , to which we assign probabilities \\mathbb P X x . The correspondence of probabilities to outcomes is called the probability distribution of the random variable. \"\"\" md\"\"\" Gaussian distributions \"\"\" md\"\"\" Let's remind ourselves of a very important type of distribution, namely the Gaussian distribution with mean \\mu and standard deviation \\sigma or variance \\sigma^2 . We can sample from a Gaussian distribution with mean 0 and variance 1 with the `randn` function short for \"random normal\" . We can then shift and scale to get the Gaussian distribution that we want \"\"\" md\"\"\" μ bind μ Slider 3 0.01 3, show value true, default 0.0 σ bind σ Slider 0.01 0.01 3, show value true, default 1.0 \"\"\" bell curve x exp x^2 2 √ 2π bell curve x, μ, σ bell curve x μ σ σ bell curve 0, 3, 2 md\"\"\" Sum of two Gaussians \"\"\" md\"\"\" Let's try to add two Gaussians. From the empirical experimental point of view the sum of two random variables is easy to compute Just sample both of them and add the resulting values to get the value of the random variable that is their sum \"\"\" md\"\"\" We see that we get back a Gaussian again In fact, the sum of two Gaussians with means \\mu 1 and \\mu 2 , and variances \\sigma 1^2 and \\sigma 2^2 , is a Gaussian again with mean \\mu 1 \\mu 2 and variance \\sigma 1^2 \\sigma 2^2 . Below we will see how to encode this fact computationally. \"\"\" md\"\"\" Theoretical random variables vs. sampling What should a theoretical random variable be able to do? What should we be able to do with sampling? Naming , e.g. `Gaussian` Parameters , e.g. \\mu and \\sigma^2 Theoretical Theoretical mean Theoretical variance Theoretical sum of two random variables Theoretical product of two random variables Probability distribution Sampling Sample a random variable Sample mean Sample variance Sample sum Histogram \"\"\" md\"\"\" Why define a type at all? \"\"\" md\"\"\" How can we represent a random variable in software? In some languages there are different names for the various functions associated to a random variable, but no name for the random variable itself For example, in R there is a standard naming convention in R, with `d` for density, etc., followed by the name like `norm` for normal. The indicators are `d` for the density `p` for the distribution function `q` for the quantile function `r` for generating random variates See e.g. Normal distribution in R https www.rdocumentation.org packages stats versions 3.6.2 topics Normal Chi squared distribution in R https www.rdocumentation.org packages stats versions 3.6.2 topics Chisquare What's wrong with this? All these functions are referring to an underlying random variable or probability distribution , which you will find in any course in probability, and yet there's no way to refer to the underlying mathematical object \"\"\" md\"\"\" Instead, we would like to be able to refer to the random variable or probability distribution itself. We should be able to provide a type with the name and parameters of a random variable, but not yet specify how to generate random instances variates ? It turns out that this is a very good example of thinking ahead by providing an abstraction . We can later provide a means for random sampling and even, if a new algorithm comes along that is more efficient, we can replace it some day \"\"\" md\"\"\" Defining abstract types for random variables \"\"\" md\"\"\" We define an abstract type using `abstract type Name end`, where ` Name ` is replaced with the name of the type. We can think of an abstract type as being a collection of types that share a particular property. In our case, we want to create a type to represent \"any random variable\", and also the sub types \"any continuous valued random variable\" and \"any discrete valued random variable\". This will allow us to specify later on whether a given concrete i.e. particular random variable is discrete or continuous. We use ` ` to denote sub type \"\"\" begin abstract type RandomVariable end abstract type DiscreteRandomVariable RandomVariable end abstract type ContinuousRandomVariable RandomVariable end end md\"\"\" Defining a type for a Gaussian random variable \"\"\" md\"\"\" Let's start off by looking at Gaussian random variables, also called normal random variables. We reminded ourselves above what they look like. \"\"\" md\"\"\" Name and parameters \"\"\" begin struct Gaussian ContinuousRandomVariable μ mean σ² variance end Gaussian Gaussian 0.0, 1.0 normalised Gaussian with mean 0 and variance 1 end G Gaussian 1, 2 md\"\"\" Note that here we have created a Gaussian random variable with given parameter values, without sampling from it. \"\"\" md\"\"\" Theoretical mean and variance \"\"\" md\"\"\" Now we can extend the `mean`, `var` variance and `std` standard deviation functions from the `Statistics` library to act on this object \"\"\" begin Statistics.mean X Gaussian X.μ Statistics.var X Gaussian X.σ² end md\"\"\" Planning ahead Standard deviation for any random variable, not just Gaussians \"\"\" md\"\"\" Once we have defined the variance, we know how to calculate the standard deviation it's just the square root of the variance. But, thinking ahead, this is true for any random variable, so we can define it to act on any random variable that we will define later \"\"\" md\"\"\" This is an example of good software design. \"\"\" md\"\"\" Sum of two Gaussian random variables \"\"\" md\"\"\" Gaussians have a special property the sum of two Gaussians is always a Gaussian. We say that Gaussians are stable distributions there are others https en.wikipedia.org wiki Stable distribution . Note that we don't need random samples for this theoretical observation, embodied in the following code \"\"\" Base. X Gaussian, Y Gaussian Gaussian X.μ Y.μ, X.σ² Y.σ² begin G1 Gaussian 0, 1 G2 Gaussian 5, 6 end md\"\"\" The theoretical product of two Gaussians is not Gaussian we will do the general case later. \"\"\" md\"\"\" Probability distribution of a Gaussian \"\"\" md\"\"\" A Gaussian random variable is a continuous random variable, i.e. it has a continuous range of possible outcomes. The possible range of outcomes is called the support of the distribution. For a Gaussian it is the whole real line, \\infty, \\infty . \"\"\" md\"\"\" One way to specify a continous random variable X is via its probability density function , or PDF , f X . The probability that X lies in the interval a, b is given by an area under the curve f X x from a to b \\mathbb P X \\in a, b \\int a ^b f X x \\, dx. \"\"\" md\"\"\" For a Gaussian distribution with mean \\mu and variance \\sigma^2 , the PDF is given by f X X \\frac 1 \\sqrt 2\\pi \\sigma^2 \\exp \\left \\frac 1 2 \\left \\frac x \\mu \\sigma \\right ^2 \\right . \"\"\" pdf X Gaussian x exp 0.5 x X.μ ^2 X.σ² √ 2π X.σ² pdf G pdf Gaussian 0.0 md\"\"\" μ bind μμ Slider 3 0.01 3, show value true, default 0.0 σ bind σσ Slider 0.01 0.01 3, show value true, default 1.0 \"\"\" begin plot pdf Gaussian μμ, σσ , leg false xlims 6, 6 ylims 0, 0.5 end md\"\"\" Sampling from a Gaussian distribution \"\"\" md\"\"\" We can also specify how to sample from a Gaussian distribution. We can re purpose `rand` for this \"\"\" md\"\"\" More general distributions \"\"\" md\"\"\" Let's recall the Bernoulli distribution from last lecture. This represents a weighted coin with probability p to come up \"heads\" 1 , and probability 1 p to come up \"tails\" 0 . Note that this is a discrete random variable the possible outcomes are the discrete values 0 and 1 . \"\"\" struct Bernoulli DiscreteRandomVariable p Float64 end B Bernoulli 0.25 md\"\"\" Again we can specify the theoretical mean and variance \"\"\" begin Statistics.mean X Bernoulli X.p Statistics.var X Bernoulli X.p 1 X.p end md\"\"\" And again `std` just works \"\"\" md\"\"\" Finally we specify how to sample \"\"\" Base.rand X Bernoulli Int rand X.p md\"\"\" Adding two random variables \"\"\" md\"\"\" What happens if we add two Bernoulli random variables? There are two routes we could go We could use the known theoretical sum, or we could write a general purpose tool. Let's do the latter. \"\"\" md\"\"\" When we add two Bernoulli random variables we do not get a Bernoulli back. To see this it's enough to observe that the sum can have the outcome 2, which is impossible for a Bernoulli. So the result is just the random variable \"the sum of these two given random variables\". In general it won't even have a common name. So we actually need to define a new type to represent the \"sum of two given random variables\", which itself will be a random variable \"\"\" struct SumOfTwoRandomVariables RandomVariable X1 RandomVariable X2 RandomVariable end begin B1 Bernoulli 0.25 B2 Bernoulli 0.6 end md\"\"\" Now we can define the sum of two random variables of any type \"\"\" Base. X1 RandomVariable, X2 RandomVariable SumOfTwoRandomVariables X1, X2 data μ . σ . randn 10^5 transform standard normal begin data1 4 . sqrt 0.3 . randn 10^5 data2 6 . sqrt 0.7 . randn 10^5 total data1 data2 end G1 G2 Base.rand X Gaussian X.μ √ X.σ² randn md\"\"\" For example, let's sum two Bernoullis \"\"\" B1 B2 md\"\"\" However, for the special case of Gaussians we still get the correct result we have not overwritten the previous definition \"\"\" G1 G2 md\"\"\" Now we need to define the various functions on this type representing a sum \"\"\" Statistics.mean S SumOfTwoRandomVariables mean S.X1 mean S.X2 Statistics.mean total mean G mean G1 G2 mean G1 mean G2 mean B1 B2 md\"\"\" To have a simple equation for the variance, we need to assume that the two random variables are independent . Perhaps the name should have been `SumOfTwoIndependentRandomVariables`, but it seems too long. \"\"\" Statistics.var S SumOfTwoRandomVariables var S.X1 var S.X2 var G Statistics.std X RandomVariable sqrt var X Statistics.std total std G mean B , var B , std B md\"\"\" How can we sample from the sum? It's actually easy \"\"\" Base.rand S SumOfTwoRandomVariables rand S.X1 rand S.X2 md\"\"\" Now it's easy to look at the sum of a Bernoulli and a Gaussian. This is an example of a mixture distribution https en.wikipedia.org wiki Mixture distribution . \"\"\" md\"\"\" Let's extend the `histogram` function to easily draw the histogram of a random variable \"\"\" md\"\"\" Now... What if we sum more random variables? \"\"\" mixture Bernoulli 0.25 Bernoulli 0.75 Gaussian 0, 0.1 md\"\"\" Generic programming `sum` \"\"\" md\"\"\" Now we have defined ` `, Julia's generic definition of `sum` can kick in to define the sum of many random variables \"\"\" S sum Bernoulli 0.25 for i in 1 30 md\"\"\" Note that we do not need the ` ... ` in the following expression. There is no need to actually create an array of random variables instead we are using an iterator or generator expression \"\"\" mean S var S md\"\"\" This is a big deal Everything just works. By the way, the sum of n Bernoulli random variables with the same probability p is called a binomial random variable with parameters n, p . \"\"\" md\"\"\" If we were worried about performance, we would probably want to define a separate `Binomial` type, rather than using nested sums of Bernoullis. You will do this in the homework. \"\"\" md\"\"\" χ₁² distribution \"\"\" md\"\"\" Another well known distribution is the chi squared distribution, which is defined as the sum of squares of Gaussians. Chi 1 squared \\chi 1^2 is the square of a single Gaussian \"\"\" struct ChiSquared1 ContinuousRandomVariable end Base.rand X ChiSquared1 rand Gaussian ^2 histogram rand Gaussian μμ, σσ for i in 1 10^4 , alpha 0.5, norm true Plots.histogram X RandomVariable kw... histogram rand X for i in 1 10^6 , norm true, leg false, alpha 0.5, size 500, 300 , kw... begin histogram data, alpha 0.2, norm true, bins 100, leg false, title \"μ μ , σ σ \", size 500, 300 xlims 6, 6 ylims 0, 0.7 xs μ σ, μ, μ σ plot 6 0.01 6, x bell curve x, μ, σ , lw 2 plot μ σ 0.01 μ σ , x bell curve x, μ, σ , fill true, alpha 0.5, c purple plot μ, μ , 0.05, bell curve μ, μ, σ , ls dash, lw 2, c white annotate μ, 0.03, text \"μ\", white annotate μ σ, 0.03, text \"μ σ\", yellow annotate μ, 0.03, text \"μ\", white end histogram total, alpha 0.5, leg false, norm true, size 500, 300 histogram Bernoulli 0.25 Bernoulli 0.75 histogram Bernoulli 0.25 Gaussian 0, 0.1 histogram mixture histogram S rand mixture rand S histogram ChiSquared1 md\"\"\" We can get the \\chi n^2 distribution by summing several \\chi 1^2 \"\"\" histogram sum ChiSquared1 for i in 1 4 md\"\"\" Using symbolics \"\"\" md\"\"\" We can even do some of this symbolically , e.g. \"\"\" variables μ₁, σ₁², μ₂, σ₂² introduce symbolic variables from Symbolics.jl Gaussian μ₁, σ₁² Gaussian μ₂, σ₂² Gaussian 17, 3 Gaussian μ₂, σ₂² "},{"url":"data_science/random_vars/","title":"Sampling and Random Variables","tags":["lecture","module2","track_data","track_math","random","statistics","track_julia","image","probability","plotting","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v 7HrpoFZzITI\" image \"https user images.githubusercontent.com 6933510 136196576 70e45c9d ef0e 4498 bf61 58d9ae854c3e.png\" section 2 order 2 title \"Sampling and Random Variables\" layout \"layout.jlhtml\" youtube id \"7HrpoFZzITI\" description \"\" tags \"lecture\", \"module2\", \"track data\", \"track math\", \"random\", \"statistics\", \"track julia\", \"image\", \"probability\", \"plotting\", \"interactive\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import ImageMagick using Plots, PlutoUI, Colors, Images, StatsBase, Distributions using Statistics end PlutoUI.TableOfContents aside true md\"\"\" Julia Useful tidbits \"\"\" md\"\"\" The package that each function comes from is shown in brackets, unless it comes from `Base`. \"\"\" md\"\"\" Julia `Base` library no `using` required `if...else...end` `Dict` Julia's dictionary type `÷` or `div` integer division type `\\div` tab `sum S ` sum of elements in the collection `S`, e.g. an array `count S ` count the number of true elements of a Boolean collection `rand S ` random sampling from a collection `S` `Statistics.jl` pre loaded standard library just needs `using` `mean S ` calculate the mean of a collection `S` `std S ` calculate the standard deviation of a collection `S` `StatsBase.jl` `countmap` `Plots.jl` `histogram x ` Plot a histogram of data vector `x` Plots `bar d ` Plot a bar graph of categorical data Plots `Colors.jl` `distinguishable colors n ` Make `n` distinguishable colours Colors \"\"\" md\"\"\" Random sampling with `rand` \"\"\" md\"\"\" The `rand` function in Julia is quite versatile it tries to generate, or sample , a random object from the argument that you pass in \"\"\" rand 1 6 rand 2, 3, 5, 7, 11 rand \"MIT\" rand 'a' 'z' typeof 'a' 'z' rand random number between 0 and 1 rand methods rand md\"\"\" We can take random objects from a collection of objects of any type, for example \"\"\" distinguishable colors 10 typeof distinguishable colors 10 rand distinguishable colors 3 from Colors.jl package md\"\"\" Several random objects \"\"\" md\"\"\" To sample several random objects from the same collection, we could write an array comprehension \"\"\" rand 1 6 for i in 1 10 md\"\"\" But in fact, just adding another argument to `rand` does the trick \"\"\" rand 1 6, 10 md\"\"\" In fact, you can also generate not only random vectors, but also random matrices \"\"\" rand 1 6, 10, 12 rand distinguishable colors 5 , 10, 10 md\"\"\" We can also use random images \"\"\" penny image load download \"https www.usacoinbook.com us coins lincoln memorial cent.jpg\" begin head penny image , 1 end÷2 tail penny image , end÷2 end end rand head, tail , 5, 5 md\"\"\" Uniform sampling \"\"\" md\"\"\" So far, each use of `rand` has done uniform sampling , i.e. each possible object as the same probability. Let's count heads and tails using the `countmap` function from the `StatsBase.jl` package \"\"\" tosses rand \"head\", \"tail\" , 10000 toss counts countmap tosses \"tail\" 3 typeof \"tail\" 3 toss counts \"tail\" md\"\"\" We see that `countmap` returns a dictionary `Dict` , which maps keys to values we will say more about dictionaries in another chapter. \"\"\" prob tail toss counts \"tail\" length tosses md\"\"\" As we increase the number of tosses, we \"expect\" the probability to get closer to 1 2 . \"\"\" md\"\"\" Tossing a weighted coin \"\"\" md\"\"\" How could we model a coin that is weighted , so that it is more likely to come up heads? We want to assign a probability p 0.7 to heads, and q 0.3 to tails. \"\"\" md\"\"\" One way would be to generate random integers between 1 and 10 and assign heads to a subset of the possible results with the desired probability, e.g. 1 7 get heads, and 8 10 get tails \"\"\" function simple weighted coin outcome if rand 1 10 ≤ 7 \"heads\" else could have elseif \"tails\" end return outcome end function simple weighted coin2 if rand 1 10 ≤ 7 \"heads\" else could have elseif \"tails\" end end result for i in 1 10 end result nothing simple weighted coin2 md\"\"\" Note that `if` statements have a return value in Julia. \"\"\" md\"\"\" How could we generalise this to an arbitrary probability p ∈ 0, 1 ? We can generate a uniform floating point number between 0 and 1, and check if it is less than p . This is called a Bernoulli trial . \"\"\" rand rand 0.314159 md\"\"\" Note that comparisons also return a value in Julia. Here we have switched from heads tails to true false as the output. \"\"\" md\"\"\" Let's make that into a function \"\"\" bernoulli p rand p bernoulli 0.7 md\"\"\" p bind p Slider 0.0 0.01 1.0, show value true, default 0.7 \"\"\" countmap bernoulli p for i in 1 1000 md\"\"\" Bar charts and histograms \"\"\" md\"\"\" Once we have generated several random objects, it is natural to want to count how many times each one occurred. \"\"\" md\"\"\" Let's roll a fair die 1000 times \"\"\" md\"\"\" An obvious way to find the counts would be to run through the data looking for 1s, then run through again looking for 2s, etc. \"\"\" md\"\"\" Note that this is not the most efficient algorithm \"\"\" md\"\"\" We can plot categorical data using a bar chart , `bar` in Plots.jl. This counts each discrete item. \"\"\" rolls rand 1 6, 100000 try modifying 100 by adding more zeros rolls . 1 count rolls . 1 counts count rolls . i for i in 1 6 begin bar counts, alpha 0.5, leg false, size 500, 300 hline length rolls 6 , ls dash title \"number of die rolls length rolls \" ylims 0, length rolls 3 end md\"\"\" Probability densities Rolling multiple dice \"\"\" roll dice n sum rand 1 12, n trials 10^6 md\"\"\" Converging shape \"\"\" md\"\"\" n bind n Slider 1 50, show value true \"\"\" experiment roll dice n experiment sum randn ^2 for i in 1 n data experiment for t in 1 trials data histogram data, alpha 0.5, legend false, bins 200, c lightsalmon1, title \"n n\" c RGB 0.1, 0.2, 0.3 md\"\"\" Here we have switched from a bar chart to a histogram , which counts the number of items falling into a given range or bin . When n is small this tends to look like a bar chart, but it looks like a \"smooth bar chart\" as n gets larger, due to the aggregation . \"\"\" md\"\"\" Does the above histogram look like a bell to you? \"\"\" begin bell load download \"https encrypted tbn0.gstatic.com images?q tbn ANd9GcRmxRAIQt L X99A 4FoP3vsC l WHlC3TtAw&usqp CAU\" bell 1 end 9÷10, end md\"\"\" Normalising the y axis \"\"\" md\"\"\" Notice that the shape of the curve seems to converge to a bell shaped curve, but the axes do not. What can we do about this? We need to normalise so that the total shaded area is 1. We can use the histogram option `norm true` to do so. \"\"\" histogram data, alpha 0.5, legend false, bins 50, norm true, c lightsalmon1, title \"n n\", ylims 0, 0.05 md\"\"\" Normalising the x axis \"\"\" md\"\"\" As we changed n above, the range of values on the x axis also changed. We want to find a way to rescale x at the same time so that both axes and shape stop changing as we increase n . We need to make sure that the data is centred in the same place we will choose 0. And we need to make sure that the width is the same we will divide by the standard deviation. \"\"\" σ std data mean data , std data normalised data data . mean data . std data histogram normalised data, bins 5 1 2σ 1 σ 5, norm true, alpha 0.5, leg false, ylim 0, 0.41 , size 500, 300 plot x exp x^2 2 √ 2π , lw 2, c red, alpha 0.5 md\"\"\" Note that in the limit, the data becomes continuous , no longer discrete. The probability of any particular value is 0 We then talk about a probability density function , f x the integral of this function over the interval a, b gives the probability of being in that interval. \"\"\" md\"\"\" Options for plotting functions \"\"\" md\"\"\" Other options for the `histogram` function options `legend false` turns off the legend key , i.e. the plot labels `bins 50` specifies the number of bins can also be a vector of bin edges `linetype stephist` use steps instead of bars `alpha` a general plot option specifying transparency 0 invisible 1 opaque `c` or `color` a general plot option for the colour `lw` line width default 1 There are several different ways to specify colours. Here http juliagraphics.github.io Colors.jl stable namedcolors is a list of named colours, but you can also specify `RGB 0.1, 0.2, 0.3 `. \"\"\" md\"\"\" Note that `linetype stephist` will give a stepped version of the histogram \"\"\" md\"\"\" Sampling from other distributions \"\"\" md\"\"\" dof bind dof Slider 1 50, show value true \"\"\" chisq data rand Chisq dof , 100000 histogram chisq data, norm true, bins 100, size 500, 300 , leg false, alpha 0.5, xlims 0, 10 √ dof histogram sum randn .^2 for 1 dof for 1 100000 , norm true, alpha 0.5, leg false "},{"url":"data_science/random_walks/","title":"Random Walks","tags":["lecture","module2","programming","track_julia","plotting","structure","type","interactive","random","statistics","track_math","track_data"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v 14hHtGJ4s g\" image \"https user images.githubusercontent.com 6933510 136196563 f4b5b44c 5252 4e67 8c82 c550de891c55.png\" section 5 order 5 title \"Random Walks\" layout \"layout.jlhtml\" youtube id \"14hHtGJ4s g\" description \"\" tags \"lecture\", \"module2\", \"programming\", \"track julia\", \"plotting\", \"structure\", \"type\", \"interactive\", \"random\", \"statistics\", \"track math\", \"track data\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, BenchmarkTools TableOfContents aside true md\"\"\" Julia concepts Benchmarking BenchmarkTools.jl Plotting in a loop Generic programming Mutable vs immutable structs Vectors of vectors Aliasing of memory `cumsum` \"\"\" md\"\"\" Motivation Dynamics of hard discs \"\"\" md\"\"\" Brown observed Brownian motion in 1827 Large particles like sand or pollen in water move around seemingly at random. Einstein explained this in 1905 as repeated impacts with water molecules. We can visualise that with a simulation of hard discs bouncing off one another. Even though the dynamics is not random each disc follows Newton's laws if we just look at a single one of them it looks random. \"\"\" md\"\"\" Visualising random walks \"\"\" md\"\"\" A random walk models random motion in time and space. At each time step an object moves in a random direction. Let's visualise the result in 2 dimensions. \"\"\" md\"\"\" N bind N Slider 1 6, show value true, default 1 \"\"\" md\"\"\" t bind t Slider 1 10^N, show value true, default 1 \"\"\" md\"\"\" We see that the dynamics closely resembles, at least qualitatively, that of the hard disc. \"\"\" md\"\"\" Why use random walks? Why should we model with random processes? Either we need to get a lot of data to precisely characterise a system, or we assume it's random It may well be that can have all the details, but by making a simplifying assumption we actually gain more understanding . Examples Stock price going up and down Pollutants getting dispersed in the air Neutral genes moving through a population \"\"\" md\"\"\" Simple random walk The simplest version is called simple random walk one object jumps on the integers at each time step it jumps left or right. Each such step is a new random variable, taking values \\pm 1 at each step, each with probability 1 2 in the simplest case . We can think of this as a scaling of a Bernoulli random variable. In order to simulate this, we need to know how to generate the jumps. \"\"\" md\"\"\" Julia Benchmarking \"\"\" md\"\"\" There are various ways we could generate random values \\pm 1 . Let's use this as an opportunity to learn about benchmarking in Julia, i.e. measuring run time to compare performance. In this case we will do \"micro benchmarks\", which try to compare tiny pieces of code which we intend to run many millions of times, so that small differences in run time can be significant. The `BenchmarkTools.jl` package provides relatively easy to use tools for measuring this, by running the code many times and calculating statistics. The ` btime` macro is a simple way to estimate actual running time. Each option should be enclosed in a function. \"\"\" md\"\"\" Here are a few different ways we could generate random steps \"\"\" begin step1 rand 1, 1 step2 2 rand 0.5 1 step3 2 rand Bool 1 step4 sign randn end with terminal do btime step1 btime step2 btime step3 btime step4 end md\"\"\" Trajectory of a random walk \"\"\" md\"\"\" We can now calculate the trajectory of a 1D random walk as it takes several steps. It starts at an initial position, for example, 0, and takes consecutive steps \"\"\" function walk1D N x 0 xs x for i in 1 N x step1 push xs, x end return xs end begin plot for i in 1 10 plot walk1D 100 , leg false, size 500, 300 , lw 2, alpha 0.5 end plot end md\"\"\" Making it more general Random walks using types \"\"\" md\"\"\" Now suppose we want to think about more general random walks, for example moving around in 2D. Then we need to generalise the above function. Based on our experience from last time, you should suspect that a good way to do this is with types . We will define \"\"\" abstract type Walker end struct Walker1D Walker pos Int end position w Walker w.pos step w Walker1D rand 1, 1 struct Walker2D Walker x Int y Int end position w Walker2D w.x, w.y update w W, step where W Walker W position w step step w Walker2D rand 1, 0 , 0, 1 , 1, 0 , 0, 1 update w Walker2D, step Vector Walker2D w.x step 1 , w.y step 2 function trajectory w W, N where W W is a type parameter ws position w for i in 1 N pos position w w update w, step w push ws, position w end return ws end trajectory Walker1D 0 , 10 traj trajectory Walker2D 0, 0 , 10^N begin plot traj 1 t , ratio 1, leg false, alpha 0.5, lw 2 scatter traj 1 , traj t , c red, green xlims minimum first. traj 1, maximum first. traj 1 ylims minimum last. traj 1, maximum last. traj 1 end md\"\"\" Random walks as sum of random variables \"\"\" md\"\"\" We can connect up with the discussion from last lecture about the sum of random variables The position S n of a random walk at time n is a random variable that is the sum of n random variables which are often independent and id S n X 1 \\cdots X n where X i is the random variable modelling the i th step of the walk. Often each X i will be independent random variables of the same type, in which case we talk about an independent and identically distributed IID collection. In principle we could use the method from last lecture to model this, but there is a difficulty since we are interested in all intermediate steps The values S n 1 and S n , for example, are not independent even though the X i are . For example, if S n 1 is large, then S n is also about as large, so they are highly correlated. \"\"\" md\"\"\" Cumulative sum \"\"\" md\"\"\" Suppose we generate steps X i , e.g. \"\"\" steps rand 1, 1 , 10 md\"\"\" The trajectory, or sample path, of the random walk, is the collection of positions over time. These are the partial sums \\begin align S 1 & X 1 \\\\ S 2 & X 1 X 2 \\\\ S 3 & X 1 X 2 X 3 \\end align etc. \"\"\" md\"\"\" We can calculate all of these values using the `cumsum` \"cumulative sum\", also called \"prefix sum\" or \"scan\" function \"\"\" cumsum steps md\"\"\" Let's plot this \"\"\" plot cumsum steps , m o, leg false, size 500, 300 md\"\"\" Trajectories vs evolving probability distributions \"\"\" md\"\"\" So far we have looked at single trajectories of random walks. We can think of this as the equivalent of sampling using `rand`. Suppose that we were to conceptually run millions or billions of trajectories of our random walk. At each time step, we can ask for the probability distribution in space , averaged over all those walks. Note that we now have two variables. We could do this by literally running many walks and computing histograms. Instead we can look at how probabilities move around. Let's call p i^t the probability of being at site i at time t . We can then ask what the probability of being at site i at time t 1 . It must have been in one of the neighbouring sites at time t , so p i^ t 1 \\textstyle \\frac 1 2 p i 1 ^ t p i 1 ^ t . This is sometimes called the master equation even though that is rather a useless, non descriptive name . It describes how the probability distribution \"\"\" md\"\"\" At time t we have a whole probability distribution, which we can think of as a vector \\mathbf p ^t . Let's write a function to evolve the vector to the next time step. \"\"\" function evolve p p′ similar p make a vector of the same length and type to store the probability vector at the next time step for i in 2 length p 1 p′ i 0.5 p i 1 p i 1 end p′ 1 0 p′ end 0 return p′ end md\"\"\" Wait... Do you recognise this? We have seen this before This is just a convolution , like the blurring kernel from image processing except that in our particular model `p i ` itself does not contribute to the value of `new p i `. \"\"\" md\"\"\" Note that just as we did with images, we have a problem at the edges of the system. We need to specify boundary conditions . For now we have put 0s at the boundary. This corresponds to probability escaping at the boundary any probability that arrives at the boundary i.e. the first or last cell at a given time step does not stay in the system. We can think of the probability as analogous to a chemical that disappears \"\"\" md\"\"\" We also need to specify an initial condition \\mathbf p 0 . This tells us where our walker is at time 0 . Suppose that all walkers start at 0 . We will place this in the middle of our vector. Then the probability at that location is 1, while the probability elsewhere is 0 \"\"\" function initial condition n p0 zeros n p0 n ÷ 2 1 1 return p0 end md\"\"\" Now let's try and visualise the time evolution. \"\"\" function time evolution p0, N ps p0 p p0 for i in 1 N p evolve p push ps, copy p end return ps end p0 initial condition 101 ps time evolution p0, 100 ps 2 md\"\"\" t bind tt Slider 1 length ps , show value true, default 1 \"\"\" plot ps tt , ylim 0, 1 , leg false, size 500, 300 ps tt M reduce hcat, ps ' heatmap M, yflip true "},{"url":"data_science/random_walks_II/","title":"Random Walks II","tags":["lecture","module2","track_julia","track_data","statistics","plotting","random","structure","type","programming","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v pIAFHyLmwbM\" image \"https user images.githubusercontent.com 6933510 191981723 657fd4b8 c23d 4fb9 a3f2 496b4574fac1.png\" section 6 order 6 title \"Random Walks II\" layout \"layout.jlhtml\" youtube id \"pIAFHyLmwbM\" description \"\" tags \"lecture\", \"module2\", \"track julia\", \"track data\", \"statistics\", \"plotting\", \"random\", \"structure\", \"type\", \"programming\", \"interactive\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using PlutoUI, Plots, LinearAlgebra, SparseArrays TableOfContents aside true md\"\"\" Julia concepts Customised display of objects Structured matrices in Julia `cumsum` Vectors of vectors Concatenation of vectors `hcat` `heatmap` Plots.jl `surface` Plots.jl \"\"\" md\"\"\" Pascal's triangle \"\"\" md\"\"\" Let's start by thinking about Pascal's triangle. Note that Pascal was not the first person to study these numbers https en.wikipedia.org wiki Pascal%27s triangle . \"\"\" pascal N binomial n, k for n 0 N, k 0 N pascal 10 md\"\"\" The non zero entries are the binomial coefficients the k th entry on the n th row is the coefficient of x^k in the expansion 1 x ^n , starting from n 0 in the first row and k 0 in the first column. \"\"\" md\"\"\" Note that there are 0s above the main diagonal in other words the matrix is lower triangular . Julia has special types to represent some classes of structured matrices in the standard library `LinearAlgebra` package \"\"\" L LowerTriangular pascal 10 md\"\"\" We see that the display is special the known \" structural \" zeros are shown as dots, instead of numbers. \"\"\" md\"\"\" As we have already seen, Julia also has a sparse matrix type that we could use for this, in the `SparseArrays` standard library \"\"\" sparse pascal 10 md\"\"\" For fun, let's look at where the entries of Pascal's triangle are odd. We'll make a slider \"\"\" bind n pascal Slider 1 63, show value true, default 1 sparse isodd. pascal n pascal md\"\"\" Note that the visual representation of a sparse matrix changes at a particular value of n . For larger values of n the sparsity pattern is displayed using dots for non zero values. \"\"\" md\"\"\" The pattern is quite striking \"\"\" md\"\"\" You may be interested to know that there is an alternative way to look at Pascal's triangle \"\"\" binomial i j, i for i 0 10, j 0 10 md\"\"\" and that in fact this can be produced from the previous version using matrix multiplication \"\"\" pascal 10 pascal 10 ' md\"\"\" Convolutions build Pascal's triangle \"\"\" pascal 6 md\"\"\" Where do those binomial coefficients come from? That's not how we learnt to build Pascal's triangle at school We learned to build each number up by summing two adjacent numbers from the previous row . In the lower triangular representation we we sum the number immediately above with the one to the left of it. \"\"\" md\"\"\" We can think of this as... a convolution with the vector 1, 1 Recall that convolutions like this are used as image filters , where we want to apply the same local operation everywhere along a vector i.e. one in which we modify a vector in a neighbourhood . \"\"\" md\"\"\" Random walks Independent and identically distributed random variables \"\"\" md\"\"\" The discussion of Pascal's triangle and convolutions will surprisingly turn up below. For now let's go back to thinking about random walks . Recall that in a random walk, at each tick of a clock we take a step in space, say left or right. Each spatial step is random, so we can think of each step as being a random variable with a certain probability distribution, for example the random variable that takes the value 1 with probability \\frac 1 2 and 1 also with probability \\frac 1 2 . Often we will think about random walks in which each step is \"the same\". What do we mean by \"the same\"? We don't mean that the outcome is the same, but rather that each step has the same probability distribution , i.e. the same probability of taking each possible value. In other words, each step is given by copies of the same random variable . Steps are also independent of each other the choice of step direction right now does not affect the choice at the next step . Hence the steps form a collection of independent and identically distributed random variables , or IID random variables. \"\"\" md\"\"\" Random walks as a cumulative sum \"\"\" md\"\"\" Let's call the random variable that describes the i th step X i . Then the overall position S n of the random walk at time n is given by S n X 1 \\cdots X n \\sum i 1 ^n X i. Here we have taken the initial position S 0 equal to 0. If the initial position is not zero, then this instead gives the displacement S n S 0 at time n . Recall that by the sum of two random variables we mean a new random variable whose outcomes are sums of the outcomes, with probabilities given by taking the possible pairs of outcomes giving each new outcome. \"\"\" md\"\"\" Cumulative sum \"\"\" md\"\"\" We previously looked at sums like this. The difference now is that we are interested in the entire trajectory , i.e. the sequence S 1 , S 2 , \\ldots , S n , \\ldots . The trajectory is given by partial sums \"\"\" md\"\"\" \\begin align S 1 & X 1 \\\\ S 2 & X 1 X 2 \\\\ S 3 & X 1 X 2 X 3 \\end align etc. \"\"\" md\"\"\" Note that S n 1 is not independent of S n or the other S m . E.g. if S 100 happens to be large, then S 101 will be about as large. Thinking about types, as we did a couple of lectures ago, we would need to define a new type to represent a random walk, since we cannot generate consecutive values as independent samples. \"\"\" md\"\"\" How could we calculate a trajectory of a walk? Suppose we generate steps X i , e.g. \"\"\" steps rand 1, 1 , 10 md\"\"\" The whole trajectory is given by the cumulative sum also called \"prefix sum\" or \"scan\". Julia has the function `cumsum` to calculate this \"\"\" cumsum 1, 2, 3, 4 md\"\"\" So the trajectory is given by \"\"\" cumsum steps plot cumsum steps , m o, leg false, size 500, 300 md\"\"\" Note that in Julia this is just a convenience function it will be no more performant than writing the `for` loop yourself unlike in some other languages, where `for` loops are slow . \"\"\" md\"\"\" Evolving probability distributions in time \"\"\" md\"\"\" So far we have looked at single trajectories of random walks. We can think of this as the equivalent of sampling using `rand`. Suppose that we run many trajectories of a random walk. At a given time t we can ask where all of the walks are by taking a histogram of the S t^ k , where the superscript ^ k denotes the k th trajectory out of the collection. Doing so gives us the probability distribution of the random variable S t . Let's call p i^t \\mathbb P S t i the probability of being at site i at time t . Then the probability distribution at time t is given by the collection of all the p i^t we can group these into a vector \\mathbf p ^t . Now we can ask what happens at the next time step t 1 . Taking histograms again gives the probabilities p j^ t 1 and the vector \\mathbf p ^t of all of them. But \\mathbf p ^ t 1 and \\mathbf p ^ t are related in some way, since we go from S t to S t 1 by just taking a single step. Let's think about the case of a simple random walk in one dimension. To arrive at site i at time t 1 , we must have been in one of the neighbouring sites at time t and jumped with probability \\frac 1 2 in the direction of site i . Hence we have p i^ t 1 \\textstyle \\frac 1 2 p i 1 ^ t p i 1 ^ t . This is sometimes called the master equation even though that is rather a useless, non descriptive name it describes how the probability distribution of the random walk evolves in time . \"\"\" md\"\"\" Implementing time evolution of probabilities Let's write a function to evolve a probability vector to the next time step for a simple random walk \"\"\" function evolve p p′ similar p make a vector of the same length and type to store the probability vector at the next time step for i in 2 length p 1 iterate over the bulk of the system p′ i 0.5 p i 1 p i 1 end boundary conditions p′ 1 0 p′ end 0 return p′ end md\"\"\" Wait... Do you recognise this? This is just a convolution again The kernel is now \\frac 1 2 , 0, \\frac 1 2 . Apart from the extra 0 and the \\frac 1 2 , this is the same as in Pascal's triangle... so probabilities in simple random walk behave like Pascal's triangle \"\"\" md\"\"\" Note that just as with images we have a problem at the edges of the system We need to specify boundary conditions on the first and last cells. For now we have put 0s at the boundary, corresponding to probability escaping at the boundary any probability that arrives at the boundary at a given time step does not stay in the system. We can think of the probability as analogous to a chemical moving through a system that leaves our system and e.g. moves into the outside world when it reaches an edge. \"\"\" md\"\"\" We also need to specify an initial condition \\mathbf p 0 . This tells us where our walker is at time 0 . Suppose that all walkers start at site i 0 . We will place this in the middle of our vector. Then the probability of being at 0 is 1 certainty , while the probability at any other site is 0 impossible \"\"\" function initial condition n p₀ zeros n p₀ n ÷ 2 1 1 return p₀ end md\"\"\" Now let's evolve the probability vector in time by applying the `evolve` function repeatedly, starting from the initial probability distribution \"\"\" function time evolution p0, N ps p0 p p0 for i in 1 N p evolve p push ps, copy p end return ps end md\"\"\" Let's visualise this \"\"\" begin grid size 101 p0 initial condition grid size end ps time evolution p0, 100 md\"\"\" Note that `ps` is a `Vector`, whose elements are each `Vector`s I.e. we have a vector of vectors. This is often a convenient way to build up a matrix. \"\"\" ps 2 md\"\"\" t bind tt Slider 1 length ps , show value true, default 1 \"\"\" bar ps tt , ylim 0, 1 , leg false, size 500, 300 , alpha 0.5 md\"\"\" Concatenating vectors into a matrix \"\"\" md\"\"\" Now we want to visualise this in a different way, for which we must join up concatenate all the probability vectors into a matrix \"\"\" M reduce hcat, ps ' md\"\"\" We can visualise the matrix with a heat map \"\"\" heatmap M, yflip true md\"\"\" We can also visualise this as a 3D surface \"\"\" plotly surface M md\"\"\" But this is not necessarily very clear, so let's draw it ourselves as stacked histograms \"\"\" gr ylabels grid size÷2 grid size÷2 begin plot leg false for which in 1 15 for i in 1 length ps which plot which, which , grid size÷2 i, grid size÷2 i , 0, ps which i , c which, alpha 0.8, lw 2 end end xlims 1, 15 plot end let color list red, RGB 0, 1, 0.1 , blue xs ys zs cs cs2 for which in 1 15 for i in 1 length ps which push xs, which, which, NaN push ys, ylabels i , ylabels i , NaN push zs, 0, ps which i , NaN push zs, 0, 1, NaN D push cs, color list mod1 which, 3 , color list mod1 which, 3 , color list mod1 which, 3 end push cs2, which end plot xs, ys, zs plot leg false plot 1 15, 0 cumsum sign. randn 14 , zeros 15 , alpha 0.6, m o, lw 2, c color list plot xs, ys, zs, c cs, xlims 1, 15 , ylims 30, 30 , zlims 0, 1 , lw 2.5, alpha 0.7, xticks 1 15 , yticks 20 10 20 xlabel \"t\" ylabel \"space\" end "},{"url":"data_science/simulating_component_failure/","title":"Modeling with Stochastic Simulation","tags":["lecture","module2","track_julia","probability","statistics","track_math","epidemiology","interactive","plotting","programming","type","discrete","continuous","ODE","differential equation","agent based model"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 video \"https www.youtube.com watch?v d8BohH76C7E\" image \"https user images.githubusercontent.com 6933510 136196572 b11974d5 7335 4678 9092 630e034bbe8f.png\" section 3 order 3 title \"Modeling with Stochastic Simulation\" layout \"layout.jlhtml\" youtube id \"d8BohH76C7E\" description \"\" tags \"lecture\", \"module2\", \"track julia\", \"probability\", \"statistics\", \"track math\", \"epidemiology\", \"interactive\", \"plotting\", \"programming\", \"type\", \"discrete\", \"continuous\", \"ODE\", \"differential equation\", \"agent based model\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, StatsBase, Statistics, HypertextLiteral PlutoUI.TableOfContents aside true md\"\"\" Julia features \"\"\" md\"\"\" Extending a function from another package not `Base` Why create a new type? Abstractions and concentrating information around objects organisation Plotting shapes String interpolation \"\"\" md\"\"\" Individual based \"microscopic\" models \"\"\" md\"\"\" In individual based models, we literally specify the behaviour, or actions, of each individual in a set of rules. But often we are interested in a global picture of how the whole system, consisting of many individuals, evolves in time how many infectious individuals there are in total at a given time, for example, or the behaviour of the whole stock market. In this notebook we will see how we can start from an individual based probabilistic probabilistic model, and how sometimes we can find deterministic equations for a macroscopic system level description. Those macroscopic equations can either be in discrete time recurrence relations or difference equations , or we can take a continuous limit and convert them into ordinary differential equations . \"\"\" md\"\"\" Modelling time to success or time to failure \"\"\" md\"\"\" Let's start with a very simple model of time to success . Suppose we are playing a game in which we have a probability p of success on each turn. How many turns do we need until we succeed? For example, how many rolls of a die do we need until we roll a 6? How many rolls of 2 dice until we get a double 6? \"\"\" md\"\"\" This can be used as a model in many different situations, for example Time to failure of a light bulb or a piece of machinery time for a radioactive nucleus to decay time to recover from an infection, etc. \"\"\" md\"\"\" The basic question is Suppose we have N light bulbs that are working correctly on day 0 . If each bulb has probability p to fail on each day, how many are still working at day number t ? How long, on average, will a given bulb last? And, do light bulbs really fail exactly at midnight each night? Can you imagine a more realistic model? As usual we will take a computational thinking point of view let's code up the simulation and plot the results. Then we can step back and \"\"\" md\"\"\" Visualizing component failure \"\"\" gr begin rectangle w, h, x, y x . 0,w,w,0 , y . 0,0,h,h circle r,x,y θ LinRange 0, 2π, 30 x. r. cos. θ , y. r. sin. θ end md\"\"\" String interpolation \"\"\" md\"\"\" As an aside, how could we display a picture of Daniel Bernoulli and resize it in Pluto? To do so we use a piece of HTML, which we represent as a string. We need to substitute the value of a Julia variable into the string, which we do with string interpolation, with the syntax ` variable ` inside the string. Then we convert the string to HTML with the `HTML ... ` constructor \"\"\" bind bernoulliwidth Slider 10 10 500, show value true url \"https upload.wikimedia.org wikipedia commons thumb b b3 ETH BIB Bernoulli%2C Daniel %281700 1782%29 Portrait Portr 10971.tif %28cropped%29.jpg 440px ETH BIB Bernoulli%2C Daniel %281700 1782%29 Portrait Portr 10971.tif %28cropped%29.jpg\" s \" img src url width bernoulliwidth \" md\"\"\" Note that we can use three sets of double quotes `\"` to represent a multi line string, or to enclose another string that itself contains quotes. \"\"\" HTML s md\"\"\" Math Bernoulli random variables \"\"\" md\"\"\" Recall that a Bernoulli random variable models a weighted coin it takes the value 1, with probability p , and 0, with probability 1 p \"\"\" md\"\"\" Note that `rand p` returns a `Bool` true or false . We are converting to `Int` to get a value 1 or 0. \"\"\" md\"\"\" Let's generate sample some Bernoulli random variates \"\"\" md\"\"\" It is natural to ask what the mean , or expected value , is \"\"\" md\"\"\" If you think about how to calculate the mean sum up and divide by the total number of flips , it just gives the proportion of 1s, which should be around p . Exercise Calculate the variance of a Bernoulli random variable. Hint What happens when you sum the squares? Remember that you also need to center the data. \"\"\" md\"\"\" Julia Make it a type \"\"\" md\"\"\" Currently we need one function for sampling from a Bernoulli random variable, a different function to calculate its mean, a different function for its standard deviation, etc. From a mathematical point of view we have the concept \"Bernoulli random variable\" and we are calculating properties of that concept. Computationally we can do the same thing by creating a new object to represent \"a Bernoulli random variable\". \"\"\" struct Bernoulli p Float64 end md\"\"\" We want to be able to sample from it, using `rand`, and take its `mean`. To do so we will extend sometimes called \"overload\" the `rand` function from Julia's `Base` library, and the `mean` function from the `Statistics` standard library. Note that we are adding methods to these functions you will do this in the homework. \"\"\" Base.rand X Bernoulli Int rand X.p B Bernoulli 0.25 md\"\"\" The object `B` really represents \"a Bernoulli random variable with probability of success p \". Since all such random variables are the same, this represents any Bernoulli random variable with that probability. We should use this type any time we need a Bernoulli random variable. If you need this in another notebook you will either need to copy and paste the definition or, better, make your own mini library. However, note that types like this are already available in the `Distributions.jl` package and the new `MeasureTheory.jl` package. \"\"\" Statistics.mean X Bernoulli X.p mean B md\"\"\" Running the stochastic simulation \"\"\" md\"\"\" Let's take the simulation and run it a few times. \"\"\" T 100 md\"\"\" N bind N Slider 1 1000, show value true, default 70 p bind ppp Slider 0 0.01 1, show value true, default 0.25 t bind t Slider 1 T, show value true \"\"\" p 0.1 md\"\"\" Time evolution of the mean Intuitive derivation \"\"\" md\"\"\" The mean seems to behave in a rather predictable way over time. Can we derive this? Let N t be the number of green light bulbs at time t . This decreases because some bulbs fail. Since bulbs fail with probability p , the number of bulbs that fail at time t is, on average, p I t . Note that one time unit corresponds to one sweep of the simulation. At time t there are N t green bulbs. How many decay? Each decays with probability p , so on average p I t fail, so are removed from the number of infectious, giving the change \\Delta I t \\color lightgreen I t 1 \\color lightgreen I t \\color red p \\, I t So \\color lightgreen I t 1 \\color lightgreen I t \\color red p \\, I t or I t 1 1 p I t . \"\"\" md\"\"\" We can now take one step backwards I t 1 1 p 1 p I t 1 1 p ^2 I t 1 and then continue to solve the recurrence I t 1 p ^t \\, I 0. \"\"\" md\"\"\" Let's compare the exact and numerical results \"\"\" md\"\"\" They agree well, as they should. The agreement is expected to be better i.e. the fluctuations smaller for a larger population. \"\"\" md\"\"\" Binomial distribution \"\"\" md\"\"\" At time 0 there are N 0 light bulbs. How many will turn `` \\color red \\text red `` fail at the first step? Let's call this \\Delta N 0 . Intuitively, the mean is \\mean \\Delta N 0 p N 0 , but in fact \\Delta N 0 is a random variable In principle, it could be that no light bulbs fail, or all of them fail, but both of those events have very small probability. For each of the N 0 bulbs, i 1, \\ldots, N 0 , we have a Bernoulli random variable that tells us if bulb i will fail. Let's call them we call B 0^i . Then \\Delta N 0 \\sum i 1 ^ N 0 B 0^i \"\"\" md\"\"\" Let's make a type to represent the sum of N Bernoullis with probability p . This is called a binomial random variable . The only information that we require is just that, N and p . \"\"\" struct Binomial N Int64 p Float64 end md\"\"\" Note that does not require or even allow methods at first, as some other languages would. You can add methods later, and other people can add methods too if they can load your package. But they do not need to modify your code. \"\"\" Base.rand X Binomial sum rand Bernoulli X.p for i in 1 X.N function simulate N, p v fill 0, N, N t 0 while any v . 0 && t 100 t 1 for i 1 N, j 1 N if rand p && v i,j 0 v i,j t end end end return v end bernoulli p rand p flips Int bernoulli 0.25 for i in 1 100 mean flips function step infectious, p for i in 1 length infectious if infectious i && bernoulli p infectious i false end end return infectious end begin infected true for i in 1 N results copy step infected, ppp for i in 1 T pushfirst results, trues N end function simulate recovery p, T infectious trues N num infectious N for t in 1 T step infectious, p push num infectious, count infectious end return num infectious end begin pp 0.05 plot simulate recovery pp, T , label \"run 1\", alpha 0.5, lw 2, m o plot simulate recovery pp, T , label \"run 2\", alpha 0.5, lw 2, m o xlabel \"time t\" ylabel \"number infectious\" end exact N 1 pp ^t for t in 0 T all data simulate recovery pp, T for i in 1 30 begin plot all data, alpha 0.1, leg false, m o, ms 1, size 500, 400 , label \"\" xlabel \"time t\" ylabel \"number still functioning\" end plot mean all data , leg true, label \"mean\", lw 3, c red, m o, alpha 0.5, size 500, 400 begin plot replace. all data, 0.0 NaN , yscale log10, alpha 0.3, leg false, m o, ms 1, size 500, 400 plot mean all data , yscale log10, lw 3, c red, m o, label \"mean\", alpha 0.5 xlabel \"time t\" ylabel \"number still functioning\" end begin plot mean all data , m o, alpha 0.5, label \"mean of stochastic simulations\", size 500, 400 plot exact, lw 3, alpha 0.8, label \"deterministic model\", leg right title \"Experiment vs. theory\" xlabel \"time\" ylabel \"\"\"number of \"greens\" \"\"\" end rand B rand Binomial 10, 0.25 md\"\"\" N bind binomial N Slider 1 100, show value true, default 1 p bind binomial p Slider 0.0 0.01 1, show value true, default 0 \"\"\" begin binomial data rand Binomial binomial N, binomial p for i in 1 10000 bar countmap binomial data , alpha 0.5, size 500, 300 , leg false, bin width 0.5 end md\"\"\" Let's call q 1 p . Then for each bulb we are choosing either p failure or q non failure . This is the same as flipping n independent, weighted coins. The number of ways of choosing such that k bulbs fail is given by the coefficient of p^k in the expansion of p q ^n , namely the binomial coefficient \\begin pmatrix n \\\\ k \\end pmatrix \\frac n k \\, n k , where n 1 \\times 2 \\times \\cdots n is the factorial of n . \"\"\" md\"\"\" Continuous time If we look at the graph of the mean as a function of time, it seems to follow a smooth curve. Indeed it makes sense to ask not only how many people have recovered each day , but to aim for finer granularity. Suppose we instead increment time in steps of \\delta t the above analysis was for \\delta t 1 . Then we will need to adjust the probability of recovery in each time step. It turns out that to make sense in the limit \\delta t \\to 0 , we need to choose the probability p \\delta t to recover in time t to be proportional to \\delta t p \\delta t \\simeq \\lambda \\, \\delta t, where \\lambda is the recovery rate . Note that a rate is a probability per unit time . We get \"\"\" md\"\"\" I t \\delta t I t \\simeq \\lambda \\,\\delta t \\, I t \"\"\" md\"\"\" Dividing by \\delta t gives \\frac I t \\delta t I t \\delta t \\simeq \\lambda \\, I t We recognise the left hand side as the definition of the derivative when \\delta t \\to 0 . Taking that limit finally gives \"\"\" md\"\"\" \\frac dI t dt \\lambda \\, I t That is, we obtain an ordinary differential equation that gives the solution implicitly. Solving this equation with initial condition I 0 I 0 gives \"\"\" md\"\"\" I t I 0 \\exp \\lambda \\, t . \"\"\" md\"\"\" Alternatively, we can derive this by recognising the exponential in the limit \\delta t \\to 0 of the following expression, which is basically the expression for compounding interest \"\"\" md\"\"\" I t 1 \\lambda \\, \\delta t ^ t \\delta t I 0 \"\"\" md\"\"\" Discrete to continuous \"\"\" function plot cumulative p, N, δ 1 kw... ps p 1 p ^ n 1 for n in 1 N cumulative cumsum ps ys 0 reduce vcat, cumulative n , cumulative n for n in 1 N pop ys pushfirst ys, 0 xs 0 reduce vcat, n δ, n δ for n in 1 N plot xs, ys scatter n δ for n in 1 N , cumulative kw... end plotly begin plot size 500, 300 , leg false plot cumulative 0.1, 30, 1.0, ms 2, c red, alpha 1 plot cumulative 0.1, 30, 0.5, ms 2, c red plot cumulative 0.05, 60, 0.5 label \"\", ms 2, c lightgreen, alpha 1 plot cumulative 0.025, 120, 0.25 label \"\", ms 1, c lightgreen, alpha 1 plot cumulative 0.0125, 240, 0.125 label \"\", ms 1, c lightgreen, alpha 1 end 1 0.95^2 almost 10% begin λ log 1 0.1 plot 0 0.01 20, t 1 exp λ t , lw 1 plot 0 0.01 20, t 1 exp 0.1 t , lw 1 end md\"\"\" What does it mean to talk about a rate a probability per unit time. \"\"\" md\"\"\" How many light bulbs turn red in 1 second, half a second. Looks like 0 0. If have billions of light bulbs. \"\"\" md\"\"\" People get sick light bulbs not on discrete time clock. Limit as \\delta t \\to 0 You measure it discretely \"\"\" λ md\"\"\" SIR model \"\"\" md\"\"\" Now let's extend the procedure to the full SIR model, S \\to I \\to R . Since we already know how to deal with recovery, consider just the SI model, where susceptible agents are infected via contact, with probability \"\"\" md\"\"\" Let's denote by S t and I t be the number of susceptible and infectious people at time t , respectively, and by N the total number of people. On average, in each sweep each infectious individual has the chance to interact with one other individual. That individual is chosen uniformly at random from the total population of size N . But a new infection occurs only if that chosen individual is susceptible, which happens with probability S t N , and then if the infection is successful, with probability b , say. Hence the change in the number of infectious people after that step is. The decrease in S t is also given by \\Delta I t . \"\"\" md\"\"\" \\Delta I t I t 1 I t b \\, I t \\, \\left \\frac S t N \\right \"\"\" md\"\"\" It is useful to normalize by N , so we define s t \\frac S t N \\quad i t \\frac I t N \\quad r t \\frac R t N \"\"\" md\"\"\" Including recovery with probability c we obtain the discrete time SIR model \"\"\" md\"\"\" \\begin align s t 1 & s t b \\, s t \\, i t \\\\ i t 1 & i t b \\, s t \\, i t c \\, i t\\\\ r t 1 & r t c \\, i t \\end align \"\"\" md\"\"\" Again we can obtain this from the stochastic process by taking expectations exercise . Hint Ignore recovery to start with and take variables Y t^i that are 0 if the person is susceptible and 1 if it is infected. \"\"\" md\"\"\" And again we can allow the processes to occur in steps of length \\delta t and take the limit \\delta t \\to 0 . With rates \\beta and \\gamma we obtain the standard continuous time SIR model \"\"\" md\"\"\" \\begin align \\textstyle \\frac ds t dt & \\beta \\, s t \\, i t \\\\ \\textstyle \\frac di t dt & \\beta \\, s t \\, i t & \\gamma \\, i t \\\\ \\textstyle \\frac dr t dt & & \\gamma \\, i t \\end align \"\"\" md\"\"\" We can think of this as a model of a chemical reaction with species S, I and R. The term s t i t is known as the mass action https en.wikipedia.org wiki Law of mass action form of interaction. Note that no analytical solutions of these simple nonlinear ODEs are known as a function of time However, parametric solutions are known https arxiv.org abs 1403.2160 . \"\"\" md\"\"\" Below is a simulation of the discrete time model. Note that the simplest numerical method to solve approximately the system of ODEs, the Euler method , basically reduces to solving the discrete time model A whole suite of more advanced ODE solvers is provided in the Julia `DiffEq` ecosystem https diffeq.sciml.ai dev . \"\"\" begin NN 100 SS NN 1 II 1 RR 0 end ss, ii, rr SS NN, II NN, RR NN p infection, p recovery 0.1, 0.01 TT 1000 function discrete SIR s0, i0, r0, T 1000 s, i, r s0, i0, r0 results s s, i i, r r for t in 1 T Δi p infection s i Δr p recovery i s new s Δi i new i Δi Δr r new r Δr push results, s s new, i i new, r r new s, i, r s new, i new, r new end return results end SIR discrete SIR ss, ii, rr begin ts 1 length SIR discrete time SIR plot plot ts, x.s for x in SIR , m o, label \"S\", alpha 0.2, linecolor blue, leg right, size 400, 300 plot ts, x.i for x in SIR , m o, label \"I\", alpha 0.2 plot ts, x.r for x in SIR , m o, label \"R\", alpha 0.2 xlims 0, 500 end macro bindname name Symbol, ex Expr quote HypertextLiteral. htl \"\"\" div style 'display flex ' code style 'font weight bold' String name code bind name esc ex div \"\"\" end end bindname M Slider 2 20, show value true, default 8 bindname prob Slider 0.01 .01 1, show value true, default .1 simulation simulate M, prob bindname tt Slider 1 100, show value true, default 1 begin w .9 h .9 c RGB 0,1,0 , RGB 1,0,0 , purple 1 . simulation . tt . simulation . tt. 1 plot ratio 1, legend false, axis false, ticks false for i 1 M, j 1 M plot rectangle w,h, i, j , c black, fill true, alpha 0.5 plot circle .3,i .45,j .45 , c c i, j , fill true end for i 1 M, j 1 M if simulation i,j tt annotate i .45, j .5, text \" simulation i,j \", font 7 , white end end plot lims 0.5, M 1.1 , title \"time tt 1 failed count sum simulation. tt \" end begin plot size 500, 300 cdf count simulation .≤ i for i 0 100 bar cdf, c purple, legend false, xlim 0,tt ,alpha 0.8 end begin newcdf count simulation . i for i 0 100 bar newcdf, c RGB 0,1,0 , legend false, xlim 0,tt ,alpha 0.8 end bar countmap simulation , c red, legend false, xlim 0, tt .5 , size 500, 300 "},{"url":"homework/hw0/","title":"hw0","tags":[],"text":" A Pluto.jl notebook v0.19.45 using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Compose using PlutoUI md\" Homework 0, version 4 \" md\"\"\" Homework 0 Getting up and running First of all, welcome to the course We are excited to teach you about real world applications of scientific computing, using the same tools that we work with ourselves. This first homework is a little mic check. We'd like all MIT students to submit this zeroth homework assignment . It will not affect your grade, but it will help us get everything running smoothly when the course starts. If you're stuck or don't have much time, just fill in your name and ID and submit 🙂 \"\"\" md\"\"\" Homework Logistics Homeworks are in the form of Pluto notebooks https plutojl.org . Your must complete them and submit them on Canvas if you are an MIT student. . If you are not an MIT student, we encourage you to join Discord https discord.gg Z5qnVf8 and find someone to cross grade. HW0 is for you to get your system set up correctly and to test our grading software. You must submit it but it will not count towards your grade. \"\"\" md\" Requirements of this HW0 Install Julia and set up Pluto Do the required Exercise 0. That’s it, but if you like you can do the OPTIONAL exercises that follow.\" md\"\"\" Installation Before being able to run this notebook successfully locally, you will need to set up Julia and Pluto. Spring21 installation One you have Julia and Pluto installed, you can click the button at the top right of this page and follow the instructions to edit this notebook locally and submit. \"\"\" md\" Required Exercise 0 Making a basic function Computing the square of a number is easy you just multiply it with itself. Algorithm Given x Output x^2 1. Multiply `x` by `x`\" function basic square x return 1 this is wrong, write your code here end let result basic square 5 if result isa Number md\"\"\" warning \"Not a number\" `basic square` did not return a number. Did you forget to write `return`? \"\"\" elseif abs result 5 5 0.01 md\"\"\" correct Well done \"\"\" else md\"\"\" warning \"Incorrect\" Keep working on it \"\"\" end end md\"That's all that's required for this week. Please submit the notebook. We just wanted to make sure that you're up and running. If you want to explore further, we have included a few optional exercises below.\" md\" Optional Exercise 1 Square root by Newton's method Computing the square of a number is easy you already did it. But how does one compute the square root of a number? Algorithm Given x Output \\sqrt x 1. Take a guess `a` 1. Divide `x` by `a` 1. Set a the average of `x a` and `a`. The square root must be between these two numbers. Why? 1. Repeat until `x a` is roughly equal to `a`. Return `a` as the square root. In general, you will never get to the point where `x a` is exactly equal to `a`. So if our algorithm keeps going until `x a a`, then it will get stuck. So instead, the algorithm takes a parameter `error margin`, which is used to decide when `x a` and `a` are close enough to halt. \" md\" Exercise 1.1 Step 3 in the algorithm sets the new guess to be the average of `x a` and the old guess `a`. This is because the square root must be between the numbers `x a` and `a`. Why? \" ex 1 1 md\"\"\" your answer here \"\"\" you might need to wait until all other cells in this notebook have completed running. scroll down the page to see what's up if isdefined ex 1 1 md\"\"\"Do not change the name of the variable write you answer as `ex 1 1 \"...\"`\"\"\" end md\" Exercise 1.2 Write a function newton sqrt x which implements the above algorithm.\" function newton sqrt x, error margin 0.01, a x 2 a x 2 is the default value of `a` return x this is wrong, write your code here end newton sqrt 2 let result newton sqrt 2, 0.01 if result isa Number md\"\"\" warning \"Not a number\" `newton sqrt` did not return a number. Did you forget to write `return`? \"\"\" elseif abs result sqrt 2 0.01 md\"\"\" correct Well done \"\"\" else md\"\"\" warning \"Incorrect\" Keep working on it \"\"\" end end md\"\"\" hint `abs r s ` is the distance between `r` and `s` \"\"\" md\"\"\" hint If you're stuck, feel free to cheat, this is homework 0 after all 🙃 Julia has a function called `sqrt` \"\"\" md\" Optional Exercise 2 Sierpinksi's triangle Sierpinski's triangle is defined recursively Sierpinski's triangle of complexity N is a figure in the form of a triangle which is made of 3 triangular figures which are themselves Sierpinski's triangles of complexity N 1. A Sierpinski's triangle of complexity 0 is a simple solid equilateral triangle \" md\"To draw Sierpinski's triangle, we are going to use an external package, Compose.jl https giovineitalia.github.io Compose.jl latest tutorial . Let's import it A package contains a coherent set of functionality that you can often use a black box according to its specification. There are lots of Julia packages https juliahub.com ui Home . \" md\"Just like the definition above, our `sierpinksi` function is recursive it calls itself.\" complexity 3 if complexity 3 md\"\"\" Try changing the value of `complexity` to `5` in the cell above. Hit `Shift Enter` to affect the change. \"\"\" else md\"\"\" Great As you can see, all the cells in this notebook are linked together by the variables they define and use. Just like a spreadsheet \"\"\" end md\" Exercise 2.1 As you can see, the total area covered by triangles is lower when the complexity is higher.\" md\"\"\" Can you write a function that computes the area of `sierpinski n ` , as a fraction of the area of `sierpinski 0 `? So ``` area sierpinski 0 1.0 area sierpinski 1 0.?? ... ``` \"\"\" function area sierpinski n return 1.0 end if area sierpinski 0 1.0 && area sierpinski 1 3 4 md\"\"\" correct Well done \"\"\" else md\"\"\" warning \"Incorrect\" Keep working on it \"\"\" end md\" Let's try it out below \" md\"Complexity bind n Slider 0 6, show value true \" md\"\"\" hint Can you write `area sierpinksi n ` as a function of `area sierpinski n 1 `? \"\"\" md\"That's it for now, see you next week \" triangle compose context , polygon 1, 1 , 0, 1 , 1 2, 0 It does not matter which order you define the building blocks functions of the program in. The best way to organize code is the one that promotes understanding. function place in 3 corners t Uses the Compose library to place 3 copies of t in the 3 corners of a triangle. treat this function as a black box, or learn how it works from the Compose documentation here https giovineitalia.github.io Compose.jl latest tutorial Compose is declarative 1 compose context , context 1 4, 0, 1 2, 1 2 , t , context 0, 1 2, 1 2, 1 2 , t , context 1 2, 1 2, 1 2, 1 2 , t end function sierpinski n if n 0 triangle else t sierpinski n 1 recursively construct a smaller sierpinski's triangle place in 3 corners t place it in the 3 corners of a triangle end end sierpinski complexity sierpinski. 0 6 md\"\"\" Sierpinski's triangle of complexity n sierpinski n has area area sierpinski n \"\"\" "},{"url":"homework/hw1/","title":"Images and Arrays","tags":["homework","module1","image","track_julia","track_math","track_climate","track_data","programming","interactive","type","matrix"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 section 2.5 order 2.5 homework number 1 title \"Images and Arrays\" layout \"layout.jlhtml\" tags \"homework\", \"module1\", \"image\", \"track julia\", \"track math\", \"track climate\", \"track data\", \"programming\", \"interactive\", \"type\", \"matrix\" description \"Practice Julia basics by working with arrays of colors. At the end of this homework, you can see all of your filters applied to your webcam image \" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import ImageMagick using Images using PlutoUI using HypertextLiteral end md\"\"\" Homework 1 images and arrays `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Exercise 1 Manipulating vectors 1D images A `Vector` is a 1D array. We can think of that as a 1D image. \"\"\" example vector 0.5, 0.4, 0.3, 0.2, 0.1, 0.0, 0.7, 0.0, 0.7, 0.9 md\"\"\" html\" br \" Exercise 1.1 👉 Make a random vector `random vect` of length 10 using the `rand` function. \"\"\" random vect missing replace `missing` with your code begin colored line x Vector hcat Gray. Float64. x ' colored line x Any nothing end colored line example vector colored line random vect md\" Exercise 1.2 👉 Make a function `my sum` using a `for` loop, which computes the total of a vector of numbers.\" function my sum xs your code here return missing end my sum 1,2,3 md\" Exercise 1.3 👉 Use your `my sum` function to write a function `mean`, which computes the mean average of a vector of numbers.\" function mean xs your code here return missing end mean 1, 2, 3 md\"👉 Define `m` to be the mean of `random vect`.\" m missing replace `missing` with your code md\"\"\" Exercise 1.4 👉 Write a function `demean`, which takes a vector `xs` and subtracts the mean from each value in `xs`. Use your `mean` function \"\"\" md\"\"\" Note about mutation There are two ways to think about this exercise, you could modify the original vector, or you can create a new vector . We often prefer the second version, so that the original data is preserved. We generally only use code of the first variant in the most performance sensitive parts of a program, as it requires more care to write and use correctly. Be careful not to get carried away in optimizing code , especially when learning a new language There is a convention among Julians that functions that modify their argument have a ` ` in their name. For example, `sort x ` returns a sorted copy of `x`, while `sort x ` modifies `x` to be sorted. Tips for writing non mutating code 1. Rewriting an existing mutating function to be non mutating can feel like a 'tedious' and 'inefficient' process. Often, instead of trying to rewrite a mutating function, it's best to take a step back and try to think of your problem as constructing something new . Instead of a `for` loop, it might make more sense to use descriptive primitives like broadcasting with the dot syntax https docs.julialang.org en v1 manual functions man vectorized also for math operators https docs.julialang.org en v1 manual mathematical operations man dot operators , and map and filter https www.youtube.com watch?v O HBDZMLrM . 2. If a mutating algorithm makes the most sense for your problem, then you can first use `copy` to create a copy of an array, and then modify that copy. We will cover this topic more in the later exercises \"\"\" function demean xs your code here return missing end test vect let feel free to change your test case here to create 1.0, 1.5, 8.5 this cell is a bit funky to deal with a common pitfall from last year it regenerates the vector if you accidentally wrote a mutating function don't worry about how it works for this exercise demean to create end md\"To verify our function, let's check that the mean of the `demean test vect ` is 0 Due to floating point round off error it may not be exactly 0. \" demeaned test vect demean test vect mean demeaned test vect md\"\"\" Exercise 1.5 👉 Generate a vector of 100 elements. Where the center 20 elements are set to `1`, and all other elements are `0`. \"\"\" function create bar your code here return missing end create bar colored line create bar url \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" philip filename download url download to a local file. The filename is returned philip load philip filename md\" Hi there Philip \" philip head philip 470 800, 140 410 md\"\"\" Recall from Section 1.1 https computationalthinking.mit.edu Spring21 week1 that in Julia, an image is just a 2D array of color objects \"\"\" typeof philip md\"\"\" Every pixel i.e. element of the 2D array is of the `RGB` type \"\"\" philip pixel philip 100,100 typeof philip pixel md\"\"\" To get the values of its individual color channels, use the `r`, `g` and `b` attributes \"\"\" philip pixel.r, philip pixel.g, philip pixel.b md\"\"\" And to create an `RGB` object yourself \"\"\" RGB 0.1, 0.4, 0.7 md\"\"\" Exercise 2.1 👉 Write a function `get red` that takes a single pixel, and returns the value of its red channel. \"\"\" function get red pixel AbstractRGB your code here return missing end get red RGB 0.8, 0.1, 0.0 md\"\"\" Exercise 2.2 👉 Write a function `get reds` note the extra `s` that accepts a 2D color array called `image`, and returns a 2D array with the red channel value of each pixel. The result should be a 2D array of numbers . Use your function `get red` from the previous exercise. \"\"\" function get reds image AbstractMatrix your code here return missing end get reds philip head md\"\"\" Great By extracting the red channel value of each pixel, we get a 2D array of numbers. We went from an image 2D array of RGB colors to a matrix 2D array of numbers . Exercise 2.3 Let's try to visualize this matrix. Right now, it is displayed in text form, but because the image is quite large, most rows and columns don't fit on the screen. Instead, a better way to visualize it is to view a number matrix as an image . This is easier than you might think We just want to map each number to an `RGB` object, and the result will be a 2D array of `RGB` objects, which Julia will display as an image. First, let's define a function that turns a number into a color . \"\"\" function value as color x return RGB x, 0, 0 end value as color 0.0 , value as color 0.6 , value as color 1.0 md\"\"\" 👉 Use the functions `get reds` and `value as color` to visualize the red channel values of `philip head`. Tip Like in previous exercises, use broadcasting 'dot syntax' to apply a function element wise . Use the ➕ button at the bottom left of this cell to add more cells. \"\"\" md\"\"\" Exercise 2.4 👉 Write four more functions, `get green`, `get greens`, `get blue` and `get blues`, to be the equivalents of `get red` and `get reds`. Use the ➕ button at the bottom left of this cell to add new cells. \"\"\" md\"\"\" Exercise 2.5 👉 Write a function `mean color` that accepts an object called `image`. It should calculate the mean amounts of red, green and blue in the image and return the average color. Be sure to use functions from previous exercises \"\"\" function mean color image your code here return missing end mean color philip md\"\"\" At the end of this homework, you can see all of your filters applied to your webcam image \"\"\" function invert color AbstractRGB your code here return missing end md\"Let's invert some colors \" color black RGB 0.0, 0.0, 0.0 invert color black color red RGB 0.8, 0.1, 0.1 invert color red md\"👉 Can you invert the picture of Philip?\" philip inverted missing replace `missing` with your code md\"\"\" At the end of this homework, you can see all of your filters applied to your webcam image \"\"\" md\"\"\" Exercise 3.2 👉 Look up the documentation on the `floor` function. Use it to write a function `quantize x Number ` that takes in a value x which you can assume is between 0 and 1 and \"quantizes\" it into bins of width 0.1. For example, check that 0.267 gets mapped to 0.2. \"\"\" function quantize x Number your code here return missing end md\"\"\" Intermezzo multiple methods In Julia, we often write multiple methods for the same function. When a function is called, the method is chosen based on the input arguments. Let's look at an example These are two methods to the same function, because they have the same name, but different input types \"\"\" function double x Number return x 2 end function double x Vector return x..., x... end md\"\"\" When we call the function `double`, Julia will decide which method to call based on the given input argument \"\"\" double 24 double 1,2,37 md\"\"\" We call this multiple dispatch , and it is one of Julia's key features. Throughout this course, you will see lots of real world application, and learn to use multiple dispatch to create flexible and readable abstractions \"\"\" md\"\"\" Exercise 3.3 👉 Write the second method of the function `quantize`, i.e. a new version of the function with the same name. This method will accept a color object called `color`, of the type `AbstractRGB`. Here, ` AbstractRGB` is a type annotation . This ensures that this version of the function will be chosen when passing in an object whose type is a subtype of the `AbstractRGB` abstract type. For example, both the `RGB` and `RGBX` types satisfy this. The method you write should return a new `RGB` object, in which each component r , g and b are quantized. Use your previous method for `quantize` \"\"\" function quantize color AbstractRGB your code here return missing end md\"\"\" Exercise 3.4 👉 Write a method `quantize image AbstractMatrix ` that quantizes an image by quantizing each pixel in the image. You may assume that the matrix is a matrix of color objects. \"\"\" function quantize image AbstractMatrix your code here return missing end quantize 0.267 , quantize 0.91 md\"Let's apply your method \" quantize philip md\"\"\" Exercise 3.5 👉 Write a function `noisify x Number, s ` to add randomness of intensity s to a value x , i.e. to add a random value between s and s to x . If the result falls outside the range 0, 1 you should \"clamp\" it to that range. Julia has a built in `clamp` function, or you can write your own function. \"\"\" function noisify x Number, s your code here return missing end md\"\"\" 👉 Write the second method `noisify c AbstractRGB, s ` to add random noise of intensity s to each of the r, g, b values in a colour. Use your previous method for `noisify`. Remember that Julia chooses which method to use based on the input arguments. So to call the method from the previous exercise, the first argument should be a number. \"\"\" function noisify color AbstractRGB, s your code here return missing end md\"\"\" Noise strength \"\"\" bind color noise Slider 0 0.01 1, show value true md\"\"\" Note about array comprehension At this point, you already know of a few ways to make a new array based on one that already exists. 1. you can use a for loop to go through a array 1. you can use function broadcasting over a array 1. you can use array comprehension The third option you are about to see demonstrated below and following the following syntax ``` function to apply args for args in some iterable of your choice ``` This creates a new iterable that matches what you iterate through in the second part of the comprehension. Below is an example with `for` loops through two iterables that creates a 2 dimensional `Array`. \"\"\" md\"\"\" 👉 Write the third method `noisify image AbstractMatrix, s ` to noisify each pixel of an image. This function should be a single line \"\"\" function noisify image AbstractMatrix, s your code here return missing end noisify 0.5, 0.1 edit this test case original color red, with noise noisify color red, color noise noisify color red, strength for strength in 0 0.05 1, row in 1 10 ' md\"\"\" Exercise 3.6 Move the slider below to set the amount of noise applied to the image of Philip. \"\"\" bind philip noise Slider 0 0.01 1, show value true noisify philip head, philip noise if philip noise 1 md\"\"\" What's this? The noise intensity is `1.0`, but we can still recognise Philip in the picture... 👉 Modify the definition of the slider to go further than `1.0`. \"\"\" end md\"\"\" 👉 For which noise intensity does it become unrecognisable? You may need noise intensities larger than 1. Why? \"\"\" answer about noise intensity md\"\"\" The image is unrecognisable with intensity ... \"\"\" md\"\"\" Results \"\"\" function custom filter pixel AbstractRGB your code here return pixel end function custom filter image AbstractMatrix return custom filter. image end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" You can find out more about any function like `rand` by clicking on the Live Docs in the bottom right of this Pluto window, and typing a function name in the top. image https user images.githubusercontent.com 6933510 107848812 c934df80 6df6 11eb 8a32 663d802f5d11.png image https user images.githubusercontent.com 6933510 107848846 0f8a3e80 6df7 11eb 818a 7271ecb9e127.png We recommend that you leave the window open while you work on Julia code. It will continually look up documentation for anything you type Help, I don't see the Live Docs Try the following 🙋 Are you viewing a static preview? The Live Docs only work if you run the notebook. If you are reading this on our course website, then click the button in the top right to run the notebook. 🙋 Is your screen too small? Try resizing your window or zooming out. \"\"\" | hint hint md\"\"\"Check out this page for a refresher on basic Julia syntax Basic Julia Syntax https computationalthinking.mit.edu Spring21 basic syntax \"\"\" hint md\"\"\" In Section 1.1 https computationalthinking.mit.edu Spring21 week1 , we drew a red square on top of the image Philip with a simple command... \"\"\" md\"\"\" This exercise can be quite difficult if you use a `for` loop or list comprehension. Instead, you should use the dot syntax https docs.julialang.org en v1 manual functions man vectorized to apply a function element wise to an array. For example, this is how you get the square root of `3` ``` sqrt 3 ``` and this is how you get the square roots of 1, 2 and 3 ``` sqrt. 1, 2, 3 ``` \"\"\" | hint hint md\"`rand ` generates a uniformly random floating point number between 0 and 1 .\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let result get red RGB 0.2, 0.3, 0.4 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 0.2 correct else keep working end end let test RGB 0.2, 0, 0 RGB 0.6, 0, 0 result get reds test if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 0.2 0.6 correct else keep working end end let result invert RGB 1.0, 0.5, 0.25 I chose these values because they can be represented exactly by Float64 shouldbe RGB 0.0, 0.5, 0.75 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractRGB keep working md\"You need to return a color , i.e. an object of type `RGB`. Use `RGB r, g, b ` to create a color with channel values `r`, `g` and `b`.\" elseif result shouldbe keep working else correct end end let result quantize RGB .297, .1, .0 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractRGB keep working md\"You need to return a color , i.e. an object of type `RGB`. Use `RGB r, g, b ` to create a color with channel values `r`, `g` and `b`.\" elseif result RGB 0.2, .1, .0 keep working else correct end end let result noisify 0.5, 0 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 0.5 results noisify 0.9, 0.1 for in 1 1000 if 0.8 ≤ minimum results 0.81 && 0.99 ≤ maximum results ≤ 1 result noisify 5, 3 if result 1 correct else keep working md\"The result should be restricted to the range `` 0,1 ``.\" end else keep working end else keep working md\"What should `noisify 0.5, 0 ` be?\" correct end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined random vect not defined random vect elseif ismissing random vect still missing elseif random vect isa Vector keep working md\"`random vect` should be a `Vector`.\" elseif eltype random vect Float64 almost md\"\"\" You generated a vector of random integers. For the remaining exercises, we want a vector of `Float64` numbers. The optional first argument to `rand` specifies the type of elements to generate. For example `rand Bool, 10 ` generates 10 values that are either `true` or `false`. Try it \"\"\" elseif length random vect 10 keep working md\"`random vect` does not have the correct size.\" elseif length Set random vect 10 keep working md\"`random vect` is not 'random enough'\" else correct md\"Well done You can run your code again to generate a new vector \" end if isdefined my sum not defined my sum else let result my sum 1,2,3 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 6 keep working else correct end end end if isdefined mean not defined mean else let result mean 1,2,3 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 2 keep working else correct end end end if isdefined m not defined m elseif ismissing m still missing elseif m isa Number keep working md\"`m` should be a number.\" elseif m mean random vect keep working else correct end if isdefined mean not defined mean else let input Float64 1,2,3 result demean input if input result almost md\"\"\" It looks like you modified `xs` inside the function. It is preferable to avoid mutation inside functions, because you might want to use the original data again. For example, applying `demean` to a dataset of sensor readings would modify the original data, and the rest of your analysis would be erroneous. \"\"\" elseif ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractVector || length result 3 keep working md\"Return a vector of the same size as `xs`.\" elseif abs sum result 3 1e 10 correct else keep working end end end if isdefined create bar not defined create bar else let result create bar if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa Vector || length result 100 keep working md\"The result should be a `Vector` with 100 elements.\" elseif result 1,50,100 0,1,0 keep working else correct end end end if isdefined mean color not defined mean color else let input reshape RGB 1.0, 1.0, 1.0 , RGB 1.0, 1.0, 0.0 , 2, 1 result mean color input shouldbe RGB 1.0, 1.0, 0.5 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractRGB keep working md\"You need to return a color , i.e. an object of type `RGB`. Use `RGB r, g, b ` to create a color with channel values `r`, `g` and `b`.\" elseif result shouldbe keep working else correct end end end if isdefined quantize not defined quantize else let result quantize .3 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result .3 if quantize 0.35 .3 almost md\"What should quantize `0.2` be?\" else keep working end else correct end end end todo text HTML \"\"\" div style \"background rgb 220, 200, 255 padding 2em border radius 1em \" h1 TODO h1 repr MIME\"text html\" , text div \"\"\" bigbreak html\" br br br br br \" bigbreak md\"\"\" bigbreak Exercise 2 Manipulating images In this exercise we will get familiar with matrices 2D arrays in Julia, by manipulating images. Recall that in Julia images are matrices of `RGB` color objects. Let's load a picture of Philip again. \"\"\" md\"\"\" bigbreak Exercise 3 More filters In the previous exercises, we learned how to use Julia's dot syntax to apply a function element wise to an array. In this exercise, we will use this to write more image filters, that you can then apply to your own webcam image Exercise 3.1 👉 Write a function `invert` that inverts a color, i.e. sends r, g, b to 1 r, 1 g, 1 b . \"\"\" md\"\"\" bigbreak Camera input \"\"\" md\"\"\" bigbreak Write your own filter \"\"\" bigbreak function camera input max size 200, default url \"https i.imgur.com SUmi94P.png\" \"\"\" span class \"pl image waiting for permission\" style .pl image.popped out position fixed top 0 right 0 z index 5 .pl image video container width 250px .pl image video border radius 1rem 1rem 0 0 .pl image.waiting for permission video container display none .pl image prompt display none .pl image.waiting for permission prompt width 250px height 200px display grid place items center font family monospace font weight bold text decoration underline cursor pointer border 5px dashed rgba 0,0,0,.5 .pl image video display block .pl image .bar width inherit display flex z index 6 .pl image .bar top position absolute flex direction column .pl image .bar bottom background black border radius 0 0 1rem 1rem .pl image .bar button flex 0 0 auto background rgba 255,255,255,.8 border none width 2rem height 2rem border radius 100% cursor pointer z index 7 .pl image .bar button shutter width 3rem height 3rem margin 1.5rem auto .2rem auto .pl image video.takepicture animation pictureflash 200ms linear keyframes pictureflash 0% filter grayscale 1.0 contrast 2.0 100% filter grayscale 0.0 contrast 1.0 style div id \"video container\" div id \"top\" class \"bar\" button id \"stop\" title \"Stop video\" ✖ button button id \"pop out\" title \"Pop out pop in\" ⏏ button div video playsinline autoplay video div id \"bottom\" class \"bar\" button id \"shutter\" title \"Click to take a picture\" 📷 button div div div id \"prompt\" span Enable webcam span div script based on https github.com fonsp printi static by the same author const span currentScript.parentElement const video span.querySelector \"video\" const popout span.querySelector \"button pop out\" const stop span.querySelector \"button stop\" const shutter span.querySelector \"button shutter\" const prompt span.querySelector \".pl image prompt\" const maxsize max size const send source source, src width, src height const scale Math.min 1.0, maxsize src width, maxsize src height const width Math.floor src width scale const height Math.floor src height scale const canvas html` canvas width \\ width height \\ height ` const ctx canvas.getContext \"2d\" ctx.drawImage source, 0, 0, width, height span.value width width, height height, data ctx.getImageData 0, 0, width, height .data, span.dispatchEvent new CustomEvent \"input\" const clear camera window.stream.getTracks .forEach s s.stop video.srcObject null span.classList.add \"waiting for permission\" prompt.onclick navigator.mediaDevices.getUserMedia audio false, video facingMode \"environment\", , .then function stream stream.onend console.log window.stream stream video.srcObject stream window.cameraConnected true video.controls false video.play video.controls false span.classList.remove \"waiting for permission\" .catch function error console.log error stop.onclick clear camera popout.onclick span.classList.toggle \"popped out\" shutter.onclick const cl video.classList cl.remove \"takepicture\" void video.offsetHeight cl.add \"takepicture\" video.play video.controls false console.log video send source video, video.videoWidth, video.videoHeight document.addEventListener \"visibilitychange\", if document.visibilityState \"visible\" clear camera Set a default image const img html` img crossOrigin \"anonymous\" ` img.onload console.log \"helloo\" send source img, img.width, img.height img.src \" default url \" console.log img script span \"\"\" | HTML end bind cam data camera input function process raw camera data raw camera data the raw image data is a long byte array, we need to transform it into something more \"Julian\" something with more structure . The encoding of the raw byte stream is every 4 bytes is a single pixel every pixel has 4 values Red, Green, Blue, Alpha we ignore alpha for this notebook So to get the red values for each pixel, we take every 4th value, starting at the 1st reds flat UInt8. raw camera data \"data\" 1 4 end greens flat UInt8. raw camera data \"data\" 2 4 end blues flat UInt8. raw camera data \"data\" 3 4 end but these are still 1 dimensional arrays, nicknamed 'flat' arrays We will 'reshape' this into 2D arrays width raw camera data \"width\" height raw camera data \"height\" shuffle and flip to get it in the right shape reds reshape reds flat, width, height ' 255.0 greens reshape greens flat, width, height ' 255.0 blues reshape blues flat, width, height ' 255.0 we have our 2D array for each color Let's create a single 2D array, where each value contains the R, G and B value of that pixel RGB. reds, greens, blues end cam image process raw camera data cam data mean color cam image invert. cam image quantize cam image noisify cam image, .5 invert. cam image quantize cam image noisify cam image, .5 custom filter cam image custom filter cam image md\" homework 1, version 9 \" "},{"url":"homework/hw10/","title":"Climate modeling I","tags":["homework","module3","track_climate","track_julia","track_data","track_math","climate","plotting","interactive","modeling","climate model","economics","bifurcation","probability"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 section 7.5 order 7.5 homework number 10 title \"Climate modeling I\" layout \"layout.jlhtml\" tags \"homework\", \"module3\", \"track climate\", \"track julia\", \"track data\", \"track math\", \"climate\", \"plotting\", \"interactive\", \"modeling\", \"climate model\", \"economics\", \"bifurcation\", \"probability\" description \"Play around with an energy balance model of the climate system, to explore the effect of doubling CO₂, and to examine the 'snowball earth' phenomenon.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using LaTeXStrings using Plots using PlutoUI using Random, Distributions end md\" homework 10, version 3 \" md\"\"\" Homework 10 Climate modeling I `18.S191`, Fall 2023 \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" html\"\"\" iframe width \"100%\" height \"300\" src \"https www.youtube.com embed Gi4ZZVS2GLA\" frameborder \"0\" allow \"accelerometer autoplay clipboard write encrypted media gyroscope picture in picture\" allowfullscreen iframe \"\"\" md\"\"\" Before working on the homework, make sure that you have watched the first lecture on climate modeling 👆. We have included the important functions from this lecture notebook in the next cell. Feel free to have a look \"\"\" module Model const S 1368 solar insolation W m^2 energy per unit time per unit area const α 0.3 albedo, or planetary reflectivity unitless const B 1.3 climate feedback parameter W m^2 °C , const T0 14. preindustrial temperature °C absorbed solar radiation α α, S S S 1 α 4 W m^2 outgoing thermal radiation T A A, B B A B T const A S 1. α 4 B T0 W m^2 . greenhouse effect CO2 a a, CO2 PI CO2 PI a log CO2 CO2 PI const a 5.0 CO2 forcing coefficient W m^2 const CO2 PI 280. preindustrial CO2 concentration parts per million ppm CO2 const t CO2 PI constant CO2 concentrations const C 51. atmosphere and upper ocean heat capacity J m^2 °C function timestep ebm append ebm.T, ebm.T end ebm.Δt tendency ebm append ebm.t, ebm.t end ebm.Δt end tendency ebm 1. ebm.C absorbed solar radiation α ebm.α, S ebm.S outgoing thermal radiation ebm.T end , A ebm.A, B ebm.B greenhouse effect ebm.CO2 ebm.t end , a ebm.a, CO2 PI ebm.CO2 PI begin mutable struct EBM T Array Float64,1 t Array Float64,1 Δt Float64 CO2 Function C Float64 a Float64 A Float64 B Float64 CO2 PI Float64 α Float64 S Float64 end Make constant parameters optional kwargs EBM T Array Float64,1 , t Array Float64,1 , Δt Real, CO2 Function C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S EBM T, t, Δt, CO2, C, a, A, B, CO2 PI, α, S Construct from float inputs for convenience EBM T0 Real, t0 Real, Δt Real, CO2 Function C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S EBM Float64 T0 , Float64 t0 , Δt, CO2 C C, a a, A A, B B, CO2 PI CO2 PI, α α, S S end begin function run ebm EBM, end year Real while ebm.t end end year timestep ebm end end run ebm run ebm, 200. run for 200 years by default end CO2 hist t CO2 PI 1 . fractional increase t fractional increase t t . 1850. 220 .^3 begin CO2 RCP26 t CO2 PI 1 . fractional increase t . min. 1., exp. t . 1850. . 170 100 RCP26 EBM T0, 1850., 1., CO2 RCP26 run RCP26, 2100. CO2 RCP85 t CO2 PI 1 . fractional increase t . max. 1., exp. t . 1850. . 170 100 RCP85 EBM T0, 1850., 1., CO2 RCP85 run RCP85, 2100. end end md\"\"\" Exercise 1 policy goals under uncertainty A recent ground breaking review paper https agupubs.onlinelibrary.wiley.com doi 10.1029 2019RG000678 produced the most comprehensive and up to date estimate of the climate feedback parameter , which they find to be B \\approx \\mathcal N 1.3, 0.4 , i.e. our knowledge of the real value is normally distributed with a mean value \\overline B 1.3 W m² K and a standard deviation \\sigma 0.4 W m² K. These values are not very intuitive, so let us convert them into more policy relevant numbers. Definition Equilibrium climate sensitivity ECS is defined as the amount of warming \\Delta T caused by a doubling of CO₂ e.g. from the pre industrial value 280 ppm to 560 ppm , at equilibrium. At equilibrium, the energy balance model equation is 0 \\frac S 1 α 4 A BT eq a \\ln\\left \\frac 2\\ \\text CO ₂ \\text PI \\text CO ₂ \\text PI \\right From this, we subtract the preindustrial energy balance, which is given by 0 \\frac S 1 α 4 A BT 0 , The result of this subtraction, after rearranging, is our definition of \\text ECS \\text ECS \\equiv T eq T 0 \\frac a\\ln 2 B \"\"\" md\"\"\"The plot below provides an example of an \"abrupt 2 × CO₂\" experiment, a classic experimental treatment method in climate modelling which is used in practice to estimate ECS for a particular model. Note in complicated climate models the values of the parameters a and B are not specified a priori , but emerge as outputs of the simulation. The simulation begins at the preindustrial equilibrium, i.e. a temperature T 0 14 °C is in balance with the pre industrial CO₂ concentration of 280 ppm until CO₂ is abruptly doubled from 280 ppm to 560 ppm. The climate responds by warming rapidly, and after a few hundred years approaches the equilibrium climate sensitivity value, by definition. \"\"\" md\"\"\" ``B `` bind B slider Slider 2.5 .001 0 show value true, default 1.3 \"\"\" md\"\"\" Exercise 1.1 Develop understanding for feedbacks and climate sensitivity \"\"\" md\"\"\" 👉 Change the value of B using the slider above. What does it mean for a climate system to have a more negative value of B ? Explain why we call B the climate feedback parameter . \"\"\" observations from changing B md\"\"\" Hello world \"\"\" md\"\"\" 👉 What happens when B is greater than or equal to zero? \"\"\" observations from nonnegative B md\"\"\" Hello world \"\"\" md\"Reveal answer bind reveal nonnegative B answer CheckBox \" if reveal nonnegative B answer md\"\"\" This is known as the \"runaway greenhouse effect\", where warming self amplifies so strongly through positive feedbacks that the warming continues forever or until the oceans boil away and there is no longer a reservoir or water to support a water vapor feedback . This is thought to explain Venus' extremely hot and hostile climate, but as you can see is extremely unlikely to occur on present day Earth. \"\"\" end md\"\"\" 👉 Create a graph to visualize ECS as a function of B. \"\"\" md\"\"\" Exercise 1.2 Doubling CO₂ To compute ECS, we doubled the CO₂ in our atmosphere. This factor 2 is not entirely arbitrary without substantial effort to reduce CO₂ emissions, we are expected to at least double the CO₂ in our atmosphere by 2100. Right now, our CO₂ concentration is 415 ppm round 415 280, digits 3 times the pre industrial value of 280 ppm from 1850. The CO₂ concentrations in the future depend on human action. There are several models for future concentrations, which are formed by assuming different policy scenarios . A baseline model is RCP8.5 a \"worst case\" high emissions scenario. In our notebook, this model is given as a function of ``t``. \"\"\" md\"\"\" 👉 In what year are we expected to have doubled the CO₂ concentration, under policy scenario RCP8.5? \"\"\" expected double CO2 year let missing end md\"\"\" Exercise 1.3 Uncertainty in B The climate feedback parameter ``B`` is not something that we can control– it is an emergent property of the global climate system. Unfortunately, ``B`` is also difficult to quantify empirically the relevant processes are difficult or impossible to observe directly , so there remains uncertainty as to its exact value. A value of ``B`` close to zero means that an increase in CO₂ concentrations will have a larger impact on global warming, and that more action is needed to stay below a maximum temperature. In answering such policy related question, we need to take the uncertainty in ``B`` into account. In this exercise, we will do so using a Monte Carlo simulation we generate a sample of values for ``B``, and use these values in our analysis. \"\"\" B̅ 1.3 σ 0.4 ECS B B̅, a Model.a a log 2. . B let double CO2 t if t 0 2 Model.CO2 PI else Model.CO2 PI end the definition of A depends on B, so we recalculate A Model.S 1. Model.α 4 B slider Model.T0 create the model ebm ECS Model.EBM 14., 100., 1., double CO2, A A, B B slider Model.run ebm ECS, 300 ecs ECS B B slider p plot size 500, 250 , legend bottomright, title \"Transient response to instant doubling of CO₂\", ylabel \"temperature change °C \", xlabel \"years after doubling\", ylim .5, isfinite ecs && ecs 4 ? 4 10 , plot p, ebm ECS.t 1 , ebm ECS.t end , ecs . 1,1 , ls dash, color darkred, label \"ECS\" plot p, ebm ECS.t, ebm ECS.T . ebm ECS.T 1 , label \"ΔT t T t T₀\" end | as svg B samples let B distribution Normal B̅, σ Nsamples 5000 samples rand B distribution, Nsamples we only sample negative values of B filter x x 0, samples end histogram B samples, size 600, 250 , label nothing, xlabel \"B W m² K \", ylabel \"samples\" md\"\"\" 👉 Generate a probability distribution for the ECS based on the probability distribution function for B above. Plot a histogram. \"\"\" ECS samples missing md\" Answer \" md\"It looks like the ECS distribution is not normally distributed , even though B is. 👉 How does \\overline \\text ECS B compare to \\text ECS \\overline B ? What is the probability that \\text ECS B lies above \\text ECS \\overline B ? \" md\"👉 Does accounting for uncertainty in feedbacks make our expectation of global warming better less implied warming or worse more implied warming ?\" observations from the order of averaging md\"\"\" Hello world \"\"\" md\"\"\" Exercise 1.5 Running the model In the lecture notebook we introduced a mutable struct `EBM` energy balance model , which contains the parameters of our climate simulation `C`, `a`, `A`, `B`, `CO2 PI`, `α`, `S`, see details below a function `CO2`, which maps a time `t` to the concentrations at that year. For example, we use the function `t 280` to simulate a model with concentrations fixed at 280 ppm. `EBM` also contains the simulation results, in two arrays `T` is the array of temperatures °C, `Float64` . `t` is the array of timestamps years, `Float64` , of the same size as `T`. \"\"\" html\"\"\" style .hello td font family sans serif font size .8em max width 300px soft opacity .5 style p Properties of an code EBM code obect p table class \"hello\" thead tr th Name th th Description th tr thead tbody tr th code A code th td Linearized outgoing thermal radiation offset soft W m² soft td tr tr th code B code th td Linearized outgoing thermal radiation slope. em or em b climate feedback parameter b soft W m² °C soft td tr tr th code α code th td Planet albedo, 0.0 1.0 soft unitless soft td tr tr th code S code th td Solar insulation soft W m² soft td tr tr th code C code th td Atmosphere and upper ocean heat capacity soft J m² °C soft td tr tr th code a code th td CO₂ forcing effect soft W m² soft td tr tr th code CO2 PI code th td Pre industrial CO₂ concentration soft ppm soft td tr tbody table \"\"\" md\"\"\" You can set up an instance of `EBM` like so \"\"\" empty ebm Model.EBM 14.0, initial temperature 1850, initial year 1, Δt t 280.0, CO2 function md\"\"\" Have a look inside this object. We see that `T` and `t` are initialized to a 1 element array. Let's run our model \"\"\" simulated model let ebm Model.EBM 14.0, 1850, 1, t 280.0 Model.run ebm, 2020 ebm end md\"\"\" Again, look inside `simulated model` and notice that `T` and `t` have accumulated the simulation results. In this simulation, we used `T0 14` and `CO2 t 280`, which is why `T` is constant during our simulation. These parameters are the default, pre industrial values, and our model is based on this equilibrium. 👉 Run a simulation with policy scenario RCP8.5, and plot the computed temperature graph. What is the global temperature in the year 2100? \"\"\" simulated rcp85 model let missing end md\"\"\" Additional parameters can be set using keyword arguments. For example ```julia Model.EBM 14, 1850, 1, t 280.0 B 2.0 ``` Creates the same model as before, but with `B 2.0`. \"\"\" md\"\"\" 👉 Write a function `temperature response` that takes a function `CO2` and an optional value `B` as parameters, and returns the temperature at 2100 according to our model. \"\"\" function temperature response CO2 Function, B Float64 1.3 return missing end temperature response t 280 temperature response Model.CO2 RCP85 temperature response Model.CO2 RCP85, 1.0 md\"\"\" Exercise 1.6 Application to policy relevant questions We talked about two emissions scenarios RCP2.6 strong mitigation controlled CO2 concentrations and RCP8.5 no mitigation high CO2 concentrations . These are given by the following functions \"\"\" t 1850 2100 plot t, Model.CO2 RCP85. t , ylim 0, 1200 , ylabel \"CO2 concentration ppm \" bind t scenario test Slider t show value true, default 1850 Model.CO2 RCP26 t scenario test , Model.CO2 RCP85 t scenario test md\"\"\" We are interested in how the uncertainty in our input B the climate feedback parameter propagates through our model to determine the uncertainty in our output T t , for a given emissions scenario. The goal of this exercise is to answer the following by using Monte Carlo Simulation for uncertainty propagation 👉 What is the probability that we see more than 2°C of warming by 2100 under the low emissions scenario RCP2.6? What about under the high emissions scenario RCP8.5? \"\"\" md\"\"\" Exercise 2 How did Snowball Earth melt? In lecture 21 see below , we discovered that increases in the brightness of the Sun are not sufficient to explain how Snowball Earth eventually melted. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid Y68tnH0FIzc params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" We talked about a second theory a large increase in CO₂ by volcanoes could have caused a strong enough greenhouse effect to melt the Snowball. If we imagine that the CO₂ then decreased e.g. by getting sequestered by the now liquid ocean , we might be able to explain how we transitioned from a hostile Snowball Earth to today's habitable \"Waterball\" Earth. In this exercise, you will estimate how much CO₂ would be needed to melt the Snowball and visualize a possible trajectory for Earth's climate over the past 700 million years by making an interactive bifurcation diagram . Exercise 2.1 In the lecture notebook https github.com hdrake simplEarth blob master 2 ebm multiple equilibria.jl video above , we had a bifurcation diagram of S solar insolation vs T temperature . We increased S , watched our point move right in the diagram until we found the tipping point. This time we will do the same, but we vary the CO₂ concentration, and keep S fixed at its default present day value. \"\"\" md\"\"\" Below we have an empty diagram, which is already set up with a CO₂ vs T diagram, with a logarithmic horizontal axis. Now it's your turn We have written some pointers below to help you, but feel free to do it your own way. \"\"\" md\"\"\" We used two helper functions \"\"\" function add cold hot areas p left, right xlims p plot p, left, right , 60, 60 , fillrange 10., 10. , fillalpha 0.3, c lightblue, label nothing annotate p, left 12, 19, text \"completely\\nfrozen\", 10, darkblue, left plot p, left, right , 10, 10 , fillrange 80., 80. , fillalpha 0.09, c red, lw 0., label nothing annotate p, left 12, 15, text \"no ice\", 10, darkred, left end function add reference points p plot p, Model.CO2 PI, Model.CO2 PI , 55, 75 , color grey, alpha 0.3, lw 8, label \"Pre industrial CO2\" plot p, Model.CO2 PI , Model.T0 , shape circle, color orange, markersize 8, label \"Our preindustrial climate\" plot p, Model.CO2 PI , 38.3 , shape circle, color aqua, markersize 8, label \"Alternate preindustrial climate\" end md\"\"\" 👉 Create a slider for `CO2` between `CO2min` and `CO2max`. Just like the horizontal axis of our plot, we want the slider to be logarithmic . \"\"\" md\"\"\" 👉 Write a function `step model ` that takes an existing `ebm` and `new CO2`, which performs a step of our interactive process Reset the model by setting the `ebm.t` and `ebm.T` arrays to a single element. Which value? Assign a new function to `ebm.CO2`. What function? Run the model. \"\"\" function step model ebm Model.EBM, CO2 Real your code here return ebm end md\"\"\" 👉 Inside the plot cell, call the function `step model `. \"\"\" md\"\"\" Parameters \"\"\" CO2min 10 CO2max 1 000 000 Tneo 48 ebm Model.EBM Tneo, 0., 5., Model.CO2 const let p plot xlims CO2min, CO2max , ylims 55, 75 , xaxis log, xlabel \"CO2 concentration ppm \", ylabel \"Global temperature T °C \", title \"Earth's CO2 concentration bifurcation diagram\", legend topleft add cold hot areas p add reference points p your code here plot p, ebm.CO2 ebm.t end , ebm.T end , label nothing, color black, shape circle, end | as svg md\"\"\" The albedo feedback is implemented by the methods below \"\"\" function α T α0 Model.α, αi 0.5, ΔT 10. if T ΔT return αi elseif ΔT T ΔT return αi α0 αi T ΔT 2ΔT elseif T ΔT return α0 end end function Model.timestep ebm ebm.α α ebm.T end Added this line append ebm.T, ebm.T end ebm.Δt Model.tendency ebm append ebm.t, ebm.t end ebm.Δt end md\"\"\" If you like, make the visualization more informative Like in the lecture notebook, you could add a trail behind the black dot, or you could plot the stable and unstable branches. It's up to you \"\"\" md\"\"\" Exercise 2.2 👉 Find the lowest CO₂ concentration necessary to melt the Snowball, programmatically i.e., using code . \"\"\" co2 to melt snowball let missing end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"The function `findfirst` might be helpful.\" hint md\" ```julia bind log CO2 Slider ❓ ``` ```julia CO2 10^log CO2 ``` \" hint md\"Use a condition on the albedo or temperature to check whether the Snowball has melted.\" hint md\"Start by writing a function `equilibrium temperature CO2 ` which creates a new `EBM` at the Snowball Earth temperature T Tneo and returns the final temperature for a given CO2 level.\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" TODO html\" span style 'display inline font size 2em color purple font weight 900 ' TODO span \" "},{"url":"homework/hw2/","title":"Convolutions","tags":["homework","module1","track_data","track_julia","track_climate","track_math","convolution","matrix","interactive","image","type","webcam","programming"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 section 4.5 order 4.5 homework number 2 title \"Convolutions\" layout \"layout.jlhtml\" tags \"homework\", \"module1\", \"track data\", \"track julia\", \"track climate\", \"track math\", \"convolution\", \"matrix\", \"interactive\", \"image\", \"type\", \"webcam\", \"programming\" description \"Create your own image filters using mathematical convolution \" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Images, ImageIO, FileIO using PlutoUI using HypertextLiteral using OffsetArrays end md\"\"\" homework 2, version 3 \"\"\" md\"\"\" Homework 2 convolutions `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Exercise 1 Convolutions in 1D As we have seen in the lectures, we can produce cool effects using the mathematical technique of convolutions . We input one image M and get a new image M' back. Conceptually we think of M as a matrix. In practice, in Julia it will be a `Matrix` of color objects, and we may need to take that into account. Ideally, however, we should write a generic function that will work for any type of data contained in the matrix. A convolution works on a small window of an image, i.e. a region centered around a given point i, j . We will suppose that the window is a square region with odd side length 2\\ell 1 , running from \\ell, \\ldots, 0, \\ldots, \\ell . The result of the convolution over a given window, centred at the point i, j is a single number this number is the value that we will use for M' i, j . Note that neighbouring windows overlap. To get started, in Exercise 1 we'll restrict ourselves to convolutions in 1D. So a window is just a 1D region from \\ell to \\ell . \"\"\" md\"\"\" Exercise 1.1 Let's create a vector `v` of random numbers of length `n 100`. \"\"\" n 60 v rand n md\" Feel free to experiment with different values Let's use the function `colored line` to view this 1D number array as a 1D image. \" begin colored line x Vector hcat Gray. Float64. x ' colored line x Any nothing end colored line v md\"👉 Try changing `n` and `v` around. Notice that you can run the cell `v rand n ` again to regenerate new random values.\" md\"\"\" Exercise 1.2 We need to decide how to handle the boundary conditions , i.e. what happens if we try to access a position in the vector `v` beyond `1 n`. The simplest solution is to assume that v i is 0 outside the original vector however, this may lead to strange boundary effects. A better solution is to use the closest value that is inside the vector. Effectively we are extending the vector and copying the extreme values into the extended positions. Indeed, this is one way we could implement this these extra positions are called ghost cells . 👉 Write a function `extend v, i ` that checks whether the position i is inside `1 n`. If so, return the HTML \" br \" ``i``th component of `v` otherwise, return the nearest end value. \"\"\" function extend v AbstractVector, i return missing end md\" Some test cases \" example vector 0.8, 0.2, 0.1, 0.7, 0.6, 0.4 colored line example vector md\" Extended with 0 \" colored line 0, 0, example vector..., 0, 0 md\" Extended with your `extend` function \" md\"\"\" Exercise 1.3 👉 Write or copy the `mean` function from Homework 1, which takes a vector and returns the mean. \"\"\" function mean v return missing end md\"\"\" 👉 Write a function `box blur v, l ` that blurs a vector `v` with a window of length `l` by averaging the elements within a window from \\ell to \\ell . This is called a box blur . Use your function `extend` to handle the boundaries correctly. Return a vector of the same size as `v`. \"\"\" function box blur v AbstractArray, l return missing end colored line box blur example vector, 1 let try test v rand n original copy test v box blur test v, 5 if test v original md\"\"\" danger \"Oopsie \" It looks like your function modifies `v`. Can you write it without doing so? Maybe you can use `copy`. \"\"\" end catch end end md\"\"\" Exercise 1.4 👉 Apply the box blur to your vector `v`. Show the original and the new vector by creating two cells that call `colored line`. Make the parameter \\ell interactive, and call it `l box` instead of `l` to avoid a naming conflict. \"\"\" md\"\"\" Exercise 1.5 The box blur is a simple example of a convolution , i.e. a linear function of a window around each point, given by v' i \\sum m \\, v i m \\, k m , where k is a vector called a kernel . Again, we need to take care about what happens if v i m falls off the end of the vector. 👉 Write a function `convolve v, k ` that performs this convolution. You need to think of the vector k as being centred on the position i . So m in the above formula runs between \\ell and \\ell , where 2\\ell 1 is the length of the vector k . You will either need to do the necessary manipulation of indices by hand, or use the `OffsetArrays.jl` package. \"\"\" function convolve v AbstractVector, k return missing end md\" Edit the cell above, or create a new cell with your own test cases \" md\"\"\" Exercise 1.6 👉 Define a function `box blur kernel l ` which returns a kernel i.e. a vector which, when used as the kernel in `convolve`, will reproduce a box blur of length `l`. \"\"\" function box blur kernel l return missing end bind box kernel l Slider 1 5 box blur kernel test box blur kernel box kernel l md\"\"\" Let's apply your kernel to our test vector `v` first cell , and compare the result to our previous box blur function second cell . The two should be identical. \"\"\" let result box blur v, box kernel l colored line result end md\"\"\" Exercise 1.7 👉 Write a function `gaussian kernel`. The definition of a Gaussian in 1D is G x \\frac 1 \\sqrt 2\\pi \\sigma^2 \\exp \\left \\frac x^2 2\\sigma^2 \\right , or as a Julia function \"\"\" md\"\"\" Write a function `gauss` that takes `σ` as a keyword argument and implements this function. \"\"\" gauss x Real σ 1 1 sqrt 2π σ^2 exp x^2 2 σ^2 md\"\"\" We need to sample i.e. evaluate this at each pixel in an interval of length 2n 1 , and then normalize so that the sum of the resulting kernel is 1. \"\"\" function gaussian kernel 1D n σ 1 return missing end colored line gaussian kernel 1D 4 σ 1 md\"\"\" You can edit the cell above to test your kernel function Let's try applying it in a convolution. \"\"\" bind gaussian kernel size 1D Slider 0 6 function create bar x zeros 100 x 41 60 . 1 x end md\"\"\" Exercise 2 Convolutions in 2D Now let's move to 2D images. The convolution is then given by a kernel matrix K M' i, j \\sum k, l \\, M i k, j l \\, K k, l , where the sum is over the possible values of k and l in the window. Again we think of the window as being centered at i, j . A common notation for this operation is \\star ```math M' M \\star K ``` \"\"\" md\"\"\" Exercise 2.1 👉 Write a new method for `extend` that takes a matrix `M` and indices `i` and `j`, and returns the closest element of the matrix. \"\"\" function extend M AbstractMatrix, i, j return missing end extend 5,6,7 , 1 extend 5,6,7 , 8 extend 5,6,7 , 10 if extend v,1 missing missing else colored line extend example vector, i for i in 1 length example vector 2 end md\" Let's test it \" small image Gray. rand 5,5 md\" Extended with `0` \" get small image, i, j , Gray 0 for i,j in Iterators.product 1 7, 1 7 md\" Extended with your `extend` function \" extend small image, i, j for i,j in Iterators.product 1 7, 1 7 md\"\"\" Extending Philip \"\"\" url \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" philip filename download url download to a local file. The filename is returned philip load philip filename philip head philip 470 800, 140 410 extend philip head, i, j for i in 50 size philip head,1 51, j in 50 size philip head,2 51 md\"\"\" Exercise 2.2 👉 Implement a new method `convolve M, K ` that applies a convolution to a 2D array `M`, using a 2D kernel `K`. Use your new method `extend` from the last exercise. \"\"\" function convolve M AbstractMatrix, K AbstractMatrix return missing end test convolution let v 1, 10, 100, 1000, 10000 k 1, 1, 0 convolve v, k end colored line test convolution let result convolve v, box blur kernel test colored line result end test gauss 1D a let k gaussian kernel 1D gaussian kernel size 1D if k missing convolve v, k end end colored line test gauss 1D a test gauss 1D b let v create bar k gaussian kernel 1D gaussian kernel size 1D if k missing convolve v, k end end colored line test gauss 1D b md\" Let's test it out 🎃 \" test image with border get small image, i, j , Gray 0 for i,j in Iterators.product 1 7, 1 7 K test 0 0 0 1 2 0 1 2 0 0 0 convolve test image with border, K test md\" Edit `K test` to create your own test case \" convolve philip head, K test md\"\"\" You can create all sorts of effects by choosing the kernel in a smart way. Today, we will implement two special kernels, to produce a Gaussian blur and a Sobel edge detection filter. Make sure that you have watched the lecture about convolutions \"\"\" md\"\"\" Exercise 2.3 The 2D Gaussian kernel will be defined using G x,y \\frac 1 2\\pi \\sigma^2 \\exp\\left \\frac x^2 y^2 2\\sigma^2 \\right How can you express this mathematically using the 1D Gaussian function that we defined before? \"\"\" gauss x, y σ 1 2π σ^2 gauss x σ σ gauss y σ σ md\"\"\" 👉 Write a function that applies a Gaussian blur to an image. Use your previous functions, and add cells to write helper functions as needed \"\"\" function with gaussian blur image σ 3, l 5 return missing end md\" Let's make it interactive. 💫 \" bind face σ Slider 0.1 0.1 10 show value true bind face l Slider 0 20 show value true md\"\"\" When you set `face σ` to a low number e.g. `2.0` , what effect does `face l` have? And vice versa? \"\"\" md\"\"\" Exercise 2.4 👉 Create a Sobel edge detection filter . Here, we will need to create two filters that separately detect edges in the horizontal and vertical directions, given by the following kernels ```math G x \\begin bmatrix 1 & 0 & 1 \\\\ 2 & 0 & 2 \\\\ 1 & 0 & 1 \\\\ \\end bmatrix \\qquad G y \\begin bmatrix 1 & 2 & 1 \\\\ 0 & 0 & 0 \\\\ 1 & 2 & 1 \\\\ \\end bmatrix ``` We can think of these filters as derivatives in the x and y directions, as we discussed in lectures. Then we combine them by finding the magnitude of the gradient in the sense of multivariate calculus by defining G \\text total \\sqrt G x^2 G y^2 , where each operation applies element wise on the matrices. Use your previous functions, and add cells to write helper functions as needed \"\"\" function with sobel edge detect image return missing end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"Have a look at the lecture notes to see examples of adding interactivity with a slider. You can read the Interactivity and the PlutoUI sample notebooks to learn more, you can find them in Pluto's main menu. Right click the Pluto logo in the top left Open in new tab .\" md\"\"\" You can use the `÷` operator you type `\\div TAB ` to get it with autocomplete to do integer division . For example ```julia 8 6 ≈ 1.3333333 a floating point number 8 6 4 3 a fraction 8 ÷ 6 1 an integer ``` \"\"\" | hint hint md\"`num rows, num columns size M `\" hint md\"`num rows, num columns size K `\" hint md\"Can we just copy the 1D code? What is different in 2D?\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next exercise.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let result gaussian kernel 1D 5 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractVector keep working md\"The returned object is not a `Vector`.\" elseif size result 11, hint md\"The returned vector has the wrong dimensions.\" elseif sum result ≈ 1.0 keep working md\"You need to normalize the result.\" elseif gaussian kernel 1D 3 σ 1 gaussian kernel 1D 3 σ 2 keep working md\"Use the keyword argument `σ` in your function.\" else correct end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined extend not defined extend else let result extend 6,7 , 10 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 6 || extend 6,7 ,10 7 keep working else correct end end end if isdefined convolve not defined convolve else let x 1, 10, 100 result convolve x, 0, 1, 1 shouldbe 11, 110, 200 shouldbe2 2, 11, 110 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractVector keep working md\"The returned object is not a `Vector`.\" elseif size result size x keep working md\"The returned vector has the wrong dimensions.\" elseif result shouldbe && result shouldbe2 keep working else correct end end end if isdefined box blur kernel not defined box blur kernel else let result box blur kernel 2 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa AbstractVector keep working md\"The returned object is not a `Vector`.\" elseif size result 5, hint md\"The returned vector has the wrong dimensions.\" else x 1, 10, 100 result1 box blur x, 2 result2 convolve x, result if result1 ≈ result2 correct else keep working end end end end if isdefined extend not defined extend else let input 42 37 1 0 result extend input, 2, 2 if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result 42 || extend input, 1, 3 37 keep working else correct end end end bigbreak html\" br br br br br \" bigbreak bigbreak bigbreak bigbreak function camera input max size 200, default url \"https i.imgur.com SUmi94P.png\" \"\"\" span class \"pl image waiting for permission\" style .pl image.popped out position fixed top 0 right 0 z index 5 .pl image video container width 250px .pl image video border radius 1rem 1rem 0 0 .pl image.waiting for permission video container display none .pl image prompt display none .pl image.waiting for permission prompt width 250px height 200px display grid place items center font family monospace font weight bold text decoration underline cursor pointer border 5px dashed rgba 0,0,0,.5 .pl image video display block .pl image .bar width inherit display flex z index 6 .pl image .bar top position absolute flex direction column .pl image .bar bottom background black border radius 0 0 1rem 1rem .pl image .bar button flex 0 0 auto background rgba 255,255,255,.8 border none width 2rem height 2rem border radius 100% cursor pointer z index 7 .pl image .bar button shutter width 3rem height 3rem margin 1.5rem auto .2rem auto .pl image video.takepicture animation pictureflash 200ms linear keyframes pictureflash 0% filter grayscale 1.0 contrast 2.0 100% filter grayscale 0.0 contrast 1.0 style div id \"video container\" div id \"top\" class \"bar\" button id \"stop\" title \"Stop video\" ✖ button button id \"pop out\" title \"Pop out pop in\" ⏏ button div video playsinline autoplay video div id \"bottom\" class \"bar\" button id \"shutter\" title \"Click to take a picture\" 📷 button div div div id \"prompt\" span Enable webcam span div script based on https github.com fonsp printi static by the same author const span currentScript.parentElement const video span.querySelector \"video\" const popout span.querySelector \"button pop out\" const stop span.querySelector \"button stop\" const shutter span.querySelector \"button shutter\" const prompt span.querySelector \".pl image prompt\" const maxsize max size const send source source, src width, src height const scale Math.min 1.0, maxsize src width, maxsize src height const width Math.floor src width scale const height Math.floor src height scale const canvas html` canvas width \\ width height \\ height ` const ctx canvas.getContext \"2d\" ctx.drawImage source, 0, 0, width, height span.value width width, height height, data ctx.getImageData 0, 0, width, height .data, span.dispatchEvent new CustomEvent \"input\" const clear camera window.stream.getTracks .forEach s s.stop video.srcObject null span.classList.add \"waiting for permission\" prompt.onclick navigator.mediaDevices.getUserMedia audio false, video facingMode \"environment\", , .then function stream stream.onend console.log window.stream stream video.srcObject stream window.cameraConnected true video.controls false video.play video.controls false span.classList.remove \"waiting for permission\" .catch function error console.log error stop.onclick clear camera popout.onclick span.classList.toggle \"popped out\" shutter.onclick const cl video.classList cl.remove \"takepicture\" void video.offsetHeight cl.add \"takepicture\" video.play video.controls false console.log video send source video, video.videoWidth, video.videoHeight document.addEventListener \"visibilitychange\", if document.visibilityState \"visible\" clear camera Set a default image const img html` img crossOrigin \"anonymous\" ` img.onload console.log \"helloo\" send source img, img.width, img.height img.src \" default url \" console.log img script span \"\"\" | HTML end bind gauss raw camera data camera input max size 100 bind sobel raw camera data camera input max size 200 function process raw camera data raw camera data the raw image data is a long byte array, we need to transform it into something more \"Julian\" something with more structure . The encoding of the raw byte stream is every 4 bytes is a single pixel every pixel has 4 values Red, Green, Blue, Alpha we ignore alpha for this notebook So to get the red values for each pixel, we take every 4th value, starting at the 1st reds flat UInt8. raw camera data \"data\" 1 4 end greens flat UInt8. raw camera data \"data\" 2 4 end blues flat UInt8. raw camera data \"data\" 3 4 end but these are still 1 dimensional arrays, nicknamed 'flat' arrays We will 'reshape' this into 2D arrays width raw camera data \"width\" height raw camera data \"height\" shuffle and flip to get it in the right shape reds reshape reds flat, width, height ' 255.0 greens reshape greens flat, width, height ' 255.0 blues reshape blues flat, width, height ' 255.0 we have our 2D array for each color Let's create a single 2D array, where each value contains the R, G and B value of that pixel RGB. reds, greens, blues end gauss camera image process raw camera data gauss raw camera data with gaussian blur gauss camera image σ face σ, l face l sobel camera image Gray. process raw camera data sobel raw camera data Gray. with sobel edge detect sobel camera image "},{"url":"homework/hw3/","title":"Structure and language","tags":["homework","module1","track_julia","track_math","track_climate","linguistics","programming","interactive","type","structure","data","artificial intelligence"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 section 6.5 order 6.5 homework number 3 title \"Structure and language\" layout \"layout.jlhtml\" tags \"homework\", \"module1\", \"track julia\", \"track math\", \"track climate\", \"linguistics\", \"programming\", \"interactive\", \"type\", \"structure\", \"data\", \"artificial intelligence\" description \"Automatically detect the language of a piece of text, and generate realistic looking random text \" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors using PlutoUI using Compose using LinearAlgebra end md\"\"\" homework 3, version 7 \"\"\" md\"\"\" Homework 3 Structure and Language `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Introduction \"\"\" md\"\"\" So far in the class the data that we have been dealing with has mainly been in the form of images. But, of course, we know that data comes in many other forms too, as we briefly discussed in the first lecture. In this homework we will look at another very important data source written text in natural language . The word \"natural\" here is to distinguish human natural languages from computer languages. We will both analyse actual text and try to generate random text that looks like natural language. Both the analysis and synthesis of natural language are key components of artificial intelligence and are the subject of much current research https en.wikipedia.org wiki GPT 3 . \"\"\" md\"\"\" Exercise 1 Language detection In this exercise we will create a very simple Artificial Intelligence . Natural language can be quite messy, but hidden in this mess is structure , which we will look for today. Let's start with some obvious structure in English text the set of characters that we write the language in. If we generate random text by sampling choosing random characters `Char` in Julia , it does not look like English \"\"\" rand Char, 5 sample 5 random characters String rand Char, 40 join into a string md\"\"\" `Char` in Julia is the type for a Unicode https en.wikipedia.org wiki Unicode character, which includes characters like `日` and `⛄`. \"\"\" md\"\"\" Instead, let's define an alphabet , and only use those letters to sample from. To keep things simple we will ignore punctuation, capitalization, etc., and use only the following 27 characters English letters plus \"space\" \"\"\" alphabet 'a' 'z' ' ' includes the space character md\"\"\" Let's sample random characters from our alphabet \"\"\" Text String rand alphabet, 40 md\"\"\" That already looks a lot better than our first attempt But it still does not look like English text we can do better. Frequency table English words are not well modelled by this random Latin characters model. Our first observation is that some letters are more common than others . To put this observation into practice, we would like to have the frequency table of the Latin alphabet. We could search for it online, but it is actually very simple to calculate ourselves The only thing we need is a representative sample of English text. The following samples are from Wikipedia, but feel free to type in your own sample You can also enter a sample of a different language, if that language can be expressed in the Latin alphabet. Remember that the html\" img src 'https cdn.jsdelivr.net gh ionic team ionicons 5.0.0 src svg eye outline.svg' style 'width 1em height 1em margin bottom .2em ' \" button on the left of a cell will show or hide the code. We also include a sample of Spanish, which we'll use later \"\"\" md\"\"\" Exercise 1.1 Data cleaning Looking at the sample, we see that it is quite messy it contains punctuation, accented letters and numbers. For our analysis, we are only interested in our 27 character alphabet i.e. `'a'` through `'z'` plus `' '` . We are going to clean the data using the Julia function `filter`. \"\"\" filter isodd, 6, 7, 8, 9, 5 md\"\"\" `filter` takes two arguments a function and a collection . The function is applied to each element of the collection, and it must return either `true` or `false` for each element. Such a function is often called a predicate . If the result is `true`, then that element is included in the final collection. Did you notice something cool? Functions are also just objects in Julia, and you can use them as arguments to other functions Fons thinks this is super cool. html\" br \" We have written a function `isinalphabet`, which takes a character and returns a boolean \"\"\" function isinalphabet character character ∈ alphabet end isinalphabet 'a' , isinalphabet ' ' md\"👉 Use `filter` to extract just the characters from our alphabet out of `messy sentence 1` \" messy sentence 1 \" wow 2020 ¥500 blingbling \" cleaned sentence 1 missing md\"\"\" html\" br \" We are not interested in the capitalization of letters i.e. `'A'` vs `'a'` , so we want to map these to lower case before we apply our filter. If we don't, all upper case letters would get deleted. Julia has a `map` function to do exactly this. Like `filter`, its first argument is the function we want to map over the vector in the second argument. \"\"\" md\"👉 Use the function `lowercase` to convert `messy sentence 2` into a lower case string, and then use `filter` to extract only the characters from our alphabet.\" messy sentence 2 \"Awesome 😍\" cleaned sentence 2 missing md\"\"\" html\" br \" Finally, we need to deal with accents simply deleting accented characters from the source text might deform it too much. We can add accented letters to our alphabet, but a simpler solution is to replace accented letters with the corresponding unaccented base character. We have written a function `unaccent` that does just that. \"\"\" french word \"Égalité \" import Unicode \"\"\" Turn `\"áéíóúüñ asdf\"` into `\"aeiouun asdf\"`. \"\"\" unaccent str Unicode.normalize str, stripmark true samples English \"\"\" Although the word forest is commonly used, there is no universally recognised precise definition, with more than 800 definitions of forest used around the world. 4 Although a forest is usually defined by the presence of trees, under many definitions an area completely lacking trees may still be considered a forest if it grew trees in the past, will grow trees in the future, 9 or was legally designated as a forest regardless of vegetation type. 10 11 The word forest derives from the Old French forest also forès , denoting \"forest, vast expanse covered by trees\" forest was first introduced into English as the word denoting wild land set aside for hunting 14 without the necessity in definition of having trees on the land. 15 Possibly a borrowing, probably via Frankish or Old High German, of the Medieval Latin foresta, denoting \"open wood\", Carolingian scribes first used foresta in the Capitularies of Charlemagne specifically to denote the royal hunting grounds of the King. The word was not endemic to Romance languages, e. g. native words for forest in the Romance languages derived from the Latin silva, which denoted \"forest\" and \"wood land \" confer the English sylva and sylvan confer the Italian, Spanish, and Portuguese selva the Romanian silvă and the Old French selve, and cognates in Romance languages, e. g. the Italian foresta, Spanish and Portuguese floresta, etc., are all ultimately derivations of the French word. \"\"\", Spanish \"\"\" Un bosque es un ecosistema donde la vegetación predominante la constituyen los árboles y matas.1​ Estas comunidades de plantas cubren grandes áreas del globo terráqueo y funcionan como hábitats para los animales, moduladores de flujos hidrológicos y conservadores del suelo, constituyendo uno de los aspectos más importantes de la biosfera de la Tierra. Aunque a menudo se han considerado como consumidores de dióxido de carbono atmosférico, los bosques maduros son prácticamente neutros en cuanto al carbono, y son solamente los alterados y los jóvenes los que actúan como dichos consumidores.2​3​ De cualquier manera, los bosques maduros juegan un importante papel en el ciclo global del carbono, como reservorios estables de carbono y su eliminación conlleva un incremento de los niveles de dióxido de carbono atmosférico. Los bosques pueden hallarse en todas las regiones capaces de mantener el crecimiento de árboles, hasta la línea de árboles, excepto donde la frecuencia de fuego natural es demasiado alta, o donde el ambiente ha sido perjudicado por procesos naturales o por actividades humanas. Los bosques a veces contienen muchas especies de árboles dentro de una pequeña área como la selva lluviosa tropical y el bosque templado caducifolio , o relativamente pocas especies en áreas grandes por ejemplo, la taiga y bosques áridos montañosos de coníferas . Los bosques son a menudo hogar de muchos animales y especies de plantas, y la biomasa por área de unidad es alta comparada a otras comunidades de vegetación. La mayor parte de esta biomasa se halla en el subsuelo en los sistemas de raíces y como detritos de plantas parcialmente descompuestos. El componente leñoso de un bosque contiene lignina, cuya descomposición es relativamente lenta comparado con otros materiales orgánicos como la celulosa y otros carbohidratos. Los bosques son áreas naturales y silvestre \"\"\" | unaccent, unaccent french word md\"\"\" html\" br \" 👉 Let's put everything together. Write a function `clean` that takes a string, and returns a cleaned version, where accented letters are replaced by their base characters upper case letters are converted to lower case it is filtered to only contain characters from `alphabet` \"\"\" function clean text return missing end clean \"Crème brûlée est mon plat préféré.\" first sample clean first samples function letter frequencies txt ismissing txt && return missing f count. string. alphabet , txt f . sum f end alphabet sample freqs letter frequencies first sample md\"\"\" The result is a 27 element array, with values between `0.0` and `1.0`. These values correspond to the frequency of each letter. `sample freqs i 0.0` means that the i th letter did not occur in your sample, and `sample freqs i 0.1` means that 10% of the letters in the sample are the i th letter. To make it easier to convert between a character from the alphabet and its index, we have the following function \"\"\" index of letter letter findfirst isequal letter , alphabet index of letter 'a' , index of letter 'b' , index of letter ' ' md\"\"\" html\" br \" 👉 Which letters from the alphabet did not occur in the sample? \"\"\" unused letters 'a', 'b', 'c' replace with your answer md\"\"\" Random letters at the correct frequencies \"\"\" md\"\"\" By considering the frequencies of letters in English, we see that our model is already a lot better Our next observation is that some letter combinations are more common than others . Our current model thinks that `potato` is just as 'English' as `ooaptt`. In the next section, we will quantify these transition frequencies , and use it to improve our model. \"\"\" function rand sample frequencies x rand findfirst z z x, cumsum frequencies . sum frequencies end function rand sample letter frequencies alphabet rand sample frequencies end function transition counts cleaned sample count string a, b , cleaned sample for a in alphabet, b in alphabet end normalize array x x . sum x transition frequencies normalize array ∘ transition counts transition frequencies first sample md\"What we get is a 27 by 27 matrix . Each entry corresponds to a character pair. The row corresponds to the first character, the column is the second character. Let's visualize this \" md\"\"\" The brightness of each letter pair indicates how frequent that pair is here space is indicated as ` `. \"\"\" md\"\"\" Answer the following questions with respect to the cleaned English sample text , which we called `first sample`. Let's also give the transition matrix a name \"\"\" sample freq matrix transition frequencies first sample if first sample missing md\"\"\" danger \"Don't worry \" 👆 These errors will disappear automatically once you have completed the earlier exercises \"\"\" end md\"\"\"👉 What is the frequency of the combination `\"th\"`?\"\"\" th frequency missing md\"\"\"👉 What about `\"ht\"`?\"\"\" ht frequency missing md\"\"\" 👉 Write code to find which le tt ers appeared doubled in our sample. \"\"\" double letters 'a', 'b', 'c' replace with your answer md\"\"\" 👉 Which letter is most likely to follow a W ? You are free to do this partially by hand, partially using code, whatever is easiest \"\"\" most likely to follow w 'x' replace with your answer md\"\"\" 👉 Which letter is most likely to precede a W ? You are free to do this partially by hand, partially using code, whatever is easiest \"\"\" most likely to precede w 'x' replace with your answer md\"\"\" 👉 What is the sum of each row? What is the sum of each column? What is the sum of the matrix? How can we interpret these values?\" \"\"\" row sums missing col sums missing row col answer md\"\"\" Blablabla \"\"\" md\"\"\" We can use the measured transition frequencies to generate text in a way that it has the same transition frequencies as our original sample. Our generated text is starting to look like real language \"\"\" bind ex23 sample Select v String k for k, v in zip fieldnames typeof samples , samples md\"\"\" Random letters from the alphabet \"\"\" md\"\"\" Random letters at the correct frequencies \"\"\" md\"\"\" Random letters at the correct transition frequencies \"\"\" function sample text A, n first index rand sample vec sum A, dims 1 indices reduce 1 n init first index do word, prev last word freq normalize array A prev, next rand sample freq word..., next end String alphabet indices end md\"\"\" It looks like we have a decent language model, in the sense that it understands transition frequencies in the language. In the demo above, try switching the language between join string. fieldnames typeof samples , \" and \" the generated text clearly looks more like one or the other, demonstrating that the model can capture differences between the two languages. What's remarkable is that our \"training data\" was just a single paragraph per language. In this exercise, we will use our model to write a classifier a program that automatically classifies a text as either join string. fieldnames typeof samples , \" or \" . This is not a difficult task you can download dictionaries for both languages, and count matches but we are doing something much more exciting we only use a single paragraph of each language, and we use a language model as classifier. \"\"\" html\" h4 id 'mystery detect' Mystery sample h4 p Enter some text here we will detect in which language it is written p \" dont delete me bind mystery sample TextField 70, 8 , default \"\"\" Small boats are typically found on inland waterways such as rivers and lakes, or in protected coastal areas. However, some boats, such as the whaleboat, were intended for use in an offshore environment. In modern naval terms, a boat is a vessel small enough to be carried aboard a ship. Anomalous definitions exist, as lake freighters 1,000 feet 300 m long on the Great Lakes are called \"boats\". \"\"\" mystery sample md\"\"\" Let's compute the transition frequencies of our mystery sample Type some text in the box above, and check whether the frequency matrix updates. \"\"\" transition frequencies mystery sample md\"\"\" Our model will compare the transition frequencies of our mystery sample to those of our two language samples. The closest match will be our detected language. The only question left is How do we compare two matrices? When two matrices are almost equal, but not exactly, we want to quantify the distance between them. 👉 Write a function called `matrix distance` which takes 2 matrices of the same size and finds the distance between them by 1. Subtracting corresponding elements 2. Finding the absolute value of the difference 3. Summing the differences \"\"\" function matrix distance A, B return missing do something with A . B end distances map samples do sample matrix distance transition frequencies mystery sample , transition frequencies sample end try assert ismissing distances.English \"\"\" h2 It looks like this text is argmin distances h2 \"\"\" | HTML catch end md\"\"\" Exercise 2 Language generation Our model from Exercise 1 has the property that it can easily be 'reversed' to generate text. While this is useful to demonstrate its structure, the produced text is mostly meaningless it fails to model words, let alone sentence structure. To take our model one step further, we are going to generalize what we have done so far. Instead of looking at letter combinations , we will model word combinations . And instead of analyzing the frequencies of bigrams combinations of two letters , we are going to analyze n grams . Dataset This also means that we are going to need a larger dataset to train our model on the number of English words and their combinations is much higher than the number of letters. We will train our model on the novel Emma 1815 , by Jane Austen https en.wikipedia.org wiki Emma novel . This work is in the public domain, which means that we can download the whole book as a text file from `archive.org`. We've done the process of downloading and cleaning already, and we have split the text into word and punctuation tokens. \"\"\" emma let raw text read download \"https ia800303.us.archive.org 24 items EmmaJaneAusten 753 emma pdf djvu.txt\" , String first words \"Emma Woodhouse\" last words \"THE END\" start index findfirst first words, raw text 1 stop index findlast last words, raw text end raw text start index stop index end function splitwords text clean up whitespace cleantext replace text, r\"\\s \" \" \" split on whitespace or other word boundaries tokens split cleantext, r\" \\s|\\b \" end emma words splitwords emma forest words splitwords samples.English md\"\"\" Exercise 2.1 bigrams revisited The goal of the upcoming exercises is to generalize what we have done in Exercise 1. To keep things simple, we split up our problem into smaller problems. The solution to any computational problem. First, here is a function that takes an array, and returns the array of all neighbour pairs from the original. For example, ```julia bigrams 1, 2, 3, 42 ``` gives ```julia 1,2 , 2,3 , 3,42 ``` We used integers as \"words\" in this example, but our function works with any type of word. \"\"\" function bigrams words starting positions 1 length words 1 map starting positions do i words i i 1 end end bigrams 1, 2, 3, 42 md\"\"\" 👉 Next, it's your turn to write a more general function `ngrams` that takes an array and a number n , and returns all subsequences of length n . For example ```julia ngrams 1, 2, 3, 42 , 3 ``` should give ```julia 1,2,3 , 2,3,42 ``` and ```julia ngrams 1, 2, 3, 42 , 2 bigrams 1, 2, 3, 42 ``` \"\"\" function ngrams words, n return missing end ngrams 1, 2, 3, 42 , 3 ngrams forest words, 4 md\"\"\" If you are stuck, you can write `ngrams words, n bigrams words ` ignoring the true value of n , and continue with the other exercises. Exercise 2.2 frequency matrix revisisted In Exercise 1 we use a 2D array to store the bigram frequencies, where each column or row corresponds to a character from the alphabet. To use trigrams we could store the frequencies in a 3D array, and so on. However, when counting words instead of letters we run into a problem A 3D array with one row, column and layer per word has too many elements to store on our computer \"\"\" md\"\"\" Emma consists of length Set emma words unique words. This means that there are Int floor length Set emma words ^3 10^9 billion possible trigrams that's too many \"\"\" md\"\"\" html\" br \" Although the frequency array would be very large, most entries are zero . For example, \"Emma\" is a common word, but the sequence of words \"Emma Emma Emma\" never occurs in the novel. We about the sparsity of the non zero entries in a matrix. When a matrix is sparse in this way, we can store the same information in a more efficient structure . Julia's `SparseArrays.jl` package https docs.julialang.org en v1 stdlib SparseArrays index.html might sound like a logical choice, but the arrays from that package support only 1D and 2D types, and we also want to directly index using strings, not just integers. So instead we will use a dictionary , or `Dict` in Julia. Take a close look at the next example. Note that you can click on the output to expand the data viewer. \"\"\" healthy Dict \"fruits\" \"🍎\", \"🍊\" , \"vegetables\" \"🌽\", \"🎃\", \"🍕\" healthy \"fruits\" md\"\"\" Did you notice something funny? The dictionary is unordered , this is why the entries were printed in reverse from the definition. You can dynamically add or change values of a `Dict` by assigning to `my dict key `. You can check whether a key already exists using `haskey my dict, key `. 👉 Use these two techniques to write a function `word counts` that takes an array of words, and returns a `Dict` with entries `word number of occurences`. For example ```julia word counts \"to\", \"be\", \"or\", \"not\", \"to\", \"be\" ``` should return ```julia Dict \"to\" 2, \"be\" 2, \"or\" 1, \"not\" 1 ``` \"\"\" function word counts words Vector counts Dict your code here return counts end word counts \"to\", \"be\", \"or\", \"not\", \"to\", \"be\" md\"\"\" 👉 Write code to find how many times `\"Emma\"` occurs in the book. \"\"\" emma count missing md\"\"\" Great Let's get back to our n grams. For the purpose of generating text, we are going to store a completion cache . This is a dictionary where each key is an n 1 gram, and the corresponding value is the vector of all those words which can complete it to an n gram. Let's look at an example ```julia let trigrams ngrams split \"to be or not to be that is the question\", \" \" , 3 cache completions cache trigrams cache Dict \"to\", \"be\" \"or\", \"that\" , \"be\", \"or\" \"not\" , \"or\", \"not\" \"to\" , ... end ``` So for trigrams the keys are the first 2 words of each trigram, and the values are arrays containing every third word of those trigrams. If the same n gram occurs multiple times e.g. \"said Emma laughing\" , then the last word \"laughing\" should also be stored multiple times. This will allow us to generate trigrams with the same frequencies as the original text. 👉 Write the function `completion cache`, which takes an array of ngrams i.e. an array of arrays of words, like the result of your `ngram` function , and returns a dictionary like described above. \"\"\" function completion cache grams cache Dict your code here cache end let trigrams ngrams split \"to be or not to be that is the question\", \" \" , 3 completion cache trigrams end md\"\"\" What is this cache telling us? In our sample text, the words \"to be\" were followed by \"or\" and by \"that\". So if we are generating text, and the last two words we wrote are \"to be\", we can look at the cache, and see that the next word can be either \"or\" or \"that\". \"\"\" md\"\"\" Exercise 2.4 write a novel We have everything we need to generate our own novel The final step is to sample random ngrams, in a way that each next ngram overlaps with the previous one. We've done this in the function `generate from ngrams` below feel free to look through the code, or to implement your own version. \"\"\" \"\"\" generate from ngrams grams, num words Given an array of ngrams i.e. an array of arrays of words , generate a sequence of `num words` words by sampling random ngrams. \"\"\" function generate from ngrams grams, num words n length first grams cache completion cache grams we need to start the sequence with at least n 1 words. a simple way to do so is to pick a random ngram sequence rand grams ... we iteratively add one more word at a time for i ∈ n 1 num words the previous n 1 words tail sequence end n 2 end possible next words completions cache tail choice rand completions push sequence, choice end sequence end \"Compute the ngrams of an array of words, but add the first n 1 at the end, to ensure that every ngram ends in the the beginning of another ngram.\" function ngrams circular words, n ngrams words..., words 1 n 1 ... , n end completion cache ngrams circular forest words, 3 \"\"\" generate source text AbstractString, num token n 3, use words true Given a source text, generate a `String` that \"looks like\" the original text by satisfying the same ngram frequency distribution as the original. \"\"\" function generate source text AbstractString, s n 3, use words true preprocess if use words splitwords else collect end words preprocess source text if length words n \"\" else grams ngrams circular words, n result generate from ngrams grams, s if use words join result, \" \" else String result end end end md\" Interactive demo Enter your own text in the box below, and use that as training data to generate anything \" bind generate demo sample TextField 50, 5 , default samples.English md\"\"\"Using bind generate sample n letters NumberField 1 5 grams for characters\"\"\" md\"\"\"Using bind generate sample n words NumberField 1 5 grams for words\"\"\" md\"\"\" Automatic Jane Austen Uncomment the cell below to generate some Jane Austen text \"\"\" generate emma, 100 n 4 | Quote md\" Function library Just some helper functions used in the notebook.\" function Quote text AbstractString text | Markdown.Paragraph | Markdown.BlockQuote | Markdown.MD end samples.English | Quote String rand alphabet, 400 | Quote if sample freqs missing String rand sample letter sample freqs for in 1 400 | Quote end String rand alphabet, 400 | Quote String rand sample letter letter frequencies ex23 sample for in 1 400 | Quote sample text transition frequencies clean ex23 sample , 400 | Quote generate generate demo sample, 400 n generate sample n letters, use words false | Quote generate generate demo sample, 100 n generate sample n words, use words true | Quote function compimg img, labels c d for c in replace alphabet, ' ' \" \" , d in replace alphabet, ' ' \" \" xmax, ymax size img xmin, ymin 0, 0 arr j 1, i 1 for i 1 ymax, j 1 xmax compose context units UnitBox xmin, ymin, xmax, ymax , fill vec img , compose context , fill \"white\" , font \"monospace\" , text first. arr . .1, last. arr . 0.6, labels , rectangle first. arr , last. arr , fill 1.0, length arr , fill 1.0, length arr end function show pair frequencies A imshow let to rgb x RGB 0.36x, 0.82x, 0.8x to rgb. A . maximum abs. A end compimg imshow end show pair frequencies transition frequencies first sample hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"You can answer this question without writing any code have a look at the values of `sample freqs`.\" hint md\"First answer this question by looking at the pair frequency image.\" hint md\"Start out with the same code as `bigrams`, and use the Julia documentation to learn how it works. How can we generalize the `bigram` function into the `ngram` function? It might help to do this on paper first.\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let output ngrams 1, 2, 3, 42 , 2 if output isa Missing still missing elseif output isa Vector Vector keep working md\"Make sure that `ngrams` returns an array of arrays.\" elseif output 1,2 , 2,3 , 3,42 if ngrams 1,2,3 , 1 1 , 2 , 3 if ngrams 1,2,3 , 3 1,2,3 if ngrams \"a\" ,1 \"a\" correct else keep working md\"`ngrams` should work with any type, not just integers \" end else keep working md\"`ngrams x, 3 ` did not give a correct result.\" end else keep working md\"`ngrams x, 1 ` did not give a correct result.\" end else keep working md\"`ngrams x, 2 ` did not give the correct bigrams. Start out with the same code as `bigrams`.\" end end let output word counts \"to\", \"be\", \"or\", \"not\", \"to\", \"be\" if output nothing keep working md\"Did you forget to write `return`?\" elseif output Dict still missing md\"Write your function `word counts` above.\" elseif output isa Dict keep working md\"Make sure that `word counts` returns a `Dict`.\" elseif output Dict \"to\" 2, \"be\" 2, \"or\" 1, \"not\" 1 correct else keep working end end if emma count isa Missing still missing elseif emma count 865 correct else keep working end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined messy sentence 1 not defined messy sentence 1 elseif isdefined cleaned sentence 1 not defined cleaned sentence 1 else if cleaned sentence 1 isa Missing still missing elseif cleaned sentence 1 isa Vector Char keep working md\"Use `String x ` to turn an array of characters `x` into a `String`.\" elseif cleaned sentence 1 filter isinalphabet, messy sentence 1 correct else keep working end end if isdefined messy sentence 2 not defined messy sentence 2 elseif isdefined cleaned sentence 2 not defined cleaned sentence 2 else if cleaned sentence 2 isa Missing still missing elseif cleaned sentence 2 isa Vector Char keep working md\"Use `String x ` to turn an array of characters `x` into a `String`.\" elseif cleaned sentence 2 filter isinalphabet, lowercase messy sentence 2 correct else keep working end end if isdefined clean not defined clean else let input \"Aè x1\" output clean input if output isa Missing still missing elseif output isa Vector Char keep working md\"Use `String x ` to turn an array of characters `x` into a `String`.\" elseif output \"ae x\" correct else keep working end end end if isdefined unused letters not defined unused letters else if sample freqs missing md\"\"\" warning \"Oopsie \" You need to complete the previous exercises first. \"\"\" elseif unused letters isa Missing still missing elseif unused letters isa String keep working md\"Use `collect` to turn a string into an array of characters.\" elseif Set index of letter. unused letters Set findall isequal 0.0 , sample freqs correct else keep working end end if isdefined th frequency not defined th frequency elseif isdefined ht frequency not defined ht frequency else if th frequency isa Missing || ht frequency isa Missing still missing elseif th frequency ht frequency keep working md\"Looks like your answers should be flipped. Which combination is more frequent in English?\" elseif th frequency sample freq matrix index of letter 't' , index of letter 'h' && ht frequency sample freq matrix index of letter 'h' , index of letter 't' correct else keep working end end if isdefined double letters not defined double letters else let result double letters if result isa Missing still missing elseif result isa String keep working md\"Use `collect` to turn a string into an array of characters.\" elseif result isa AbstractVector Char || result isa AbstractSet Char keep working md\"Make sure that `double letters` is a `Vector`.\" elseif Set result Set alphabet diag sample freq matrix . 0 correct elseif push Set result , ' ' Set alphabet diag sample freq matrix . 0 almost md\"We also consider the space `' '` as one of the letters in our `alphabet`.\" else keep working end end end if isdefined most likely to follow w not defined most likely to follow w else let result most likely to follow w if result isa Missing still missing elseif result isa Char keep working md\"Make sure that you return a `Char`. You might want to use the `alphabet` to index a character.\" elseif result alphabet map alphabet do c sample freq matrix index of letter 'w' , index of letter c end | argmax correct else keep working end end end if isdefined most likely to precede w not defined most likely to precede w else let result most likely to precede w if result isa Missing still missing elseif result isa Char keep working md\"Make sure that you return a `Char`. You might want to use the `alphabet` to index a character.\" elseif result alphabet map alphabet do c sample freq matrix index of letter c , index of letter 'w' end | argmax correct else keep working end end end if isdefined matrix distance not defined matrix distance else try let A rand Float64, 5, 4 B rand Float64, 5, 4 output matrix distance A,B if output isa Missing still missing elseif output isa Number keep working md\"Make sure that `matrix distance` returns a number.\" elseif output 0.0 keep working md\"Two different matrices should have non zero distance.\" else if matrix distance A,B 0 || matrix distance B,A 0 keep working md\"The distance between two matrices should always be positive.\" elseif matrix distance A,A 0 almost md\"The distance between two identical matrices should be zero.\" elseif matrix distance 1 1 , 0 0 0.0 almost md\"`matrix distance 1 1 , 0 0 ` should not be zero.\" else correct end end end catch keep working md\"The function errored.\" end end todo text HTML \"\"\" div style \"background rgb 220, 200, 255 padding 2em border radius 1em \" h1 TODO h1 repr MIME\"text html\" , text div \"\"\" bigbreak html\" br br br br br \" bigbreak md\"\"\" bigbreak Exercise 1.2 Letter frequencies We are going to count the frequency of each letter in this sample, after applying your `clean` function. Can you guess which character is most frequent? \"\"\" md\"\"\" bigbreak Now that we know the frequencies of letters in English, we can generate random text that already looks closer to English Random letters from the alphabet \"\"\" md\"\"\" bigbreak Exercise 1.3 Transition frequencies In the previous exercise we computed the frequency of each letter in the sample by counting their occurences, and then dividing by the total number of counts. In this exercise, we are going to count letter transitions , such as `aa`, `as`, `rt`, `yy`. Two letters might both be common, like `a` and `e`, but their combination, `ae`, is uncommon in English. To quantify this observation, we will do the same as in our last exercise we count occurences in a sample text , to create the transition frequency matrix . \"\"\" bigbreak md\"\"\" bigbreak Exercise 1.4 Language detection \"\"\" md\"\"\" We have written a cell that selects the language with the smallest distance to the mystery language. Make sure sure that `matrix distance` is working correctly, and scroll up mystery detect to the mystery text to see it in action Further reading It turns out that the SVD of the transition matrix can mysteriously group the alphabet into vowels and consonants, without any extra information. See this paper http languagelog.ldc.upenn.edu myl Moler1983.pdf if you want to try it yourself We found that removing the space from `alphabet` to match the paper gave better results. bigbreak \"\"\" bigbreak "},{"url":"homework/hw4/","title":"Dynamic programming","tags":["homework","module1","track_julia","track_math","structure","programming","dynamic programming","matrix","recursion"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 section 9.5 order 9.5 homework number 4 title \"Dynamic programming\" layout \"layout.jlhtml\" tags \"homework\", \"module1\", \"track julia\", \"track math\", \"structure\", \"programming\", \"dynamic programming\", \"matrix\", \"recursion\" description \"\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Images, ImageIO, FileIO, TestImages, ImageFiltering using Statistics using PlutoUI using BenchmarkTools end all image urls \"https wisetoast.com wp content uploads 2015 10 The Persistence of Memory salvador deli painting.jpg\" \"Salvador Dali — The Persistence of Memory replica \", \"https i.imgur.com 4SRnmkj.png\" \"Frida Kahlo — The Bride Frightened at Seeing Life Opened\", \"https upload.wikimedia.org wikipedia commons thumb 5 5b Hilma af Klint Group IX SUW%2C The Swan No. 1 %2813947%29.jpg 477px Hilma af Klint Group IX SUW%2C The Swan No. 1 %2813947%29.jpg\" \"Hilma Klint The Swan No. 1\", \"https upload.wikimedia.org wikipedia commons thumb a a4 Piet Mondriaan%2C 1930 Mondrian Composition II in Red%2C Blue%2C and Yellow.jpg 300px Piet Mondriaan%2C 1930 Mondrian Composition II in Red%2C Blue%2C and Yellow.jpg\" \"Piet Mondriaan Composition with Red, Blue and Yellow\", \"https user images.githubusercontent.com 6933510 110993432 950df980 8377 11eb 82e7 b7ce4a0d04bc.png\" \"Mario\", removal test image Gray. rand 4,4 begin brightness c RGB mean c.r, c.g, c.b brightness c RGBA mean c.r, c.g, c.b brightness c Gray gray c end convolve img, k imfilter img, reflect k uses ImageFiltering.jl package behaves the same way as the `convolve` function used in our lectures and homeworks float to color x RGB max 0, x , max 0, x , 0 energy ∇x, ∇y sqrt. ∇x.^2 . ∇y.^2 function energy img ∇y convolve brightness. img , Kernel.sobel 1 ∇x convolve brightness. img , Kernel.sobel 2 energy ∇x, ∇y end html\"\"\" iframe width \"100%\" height \"450px\" src \"https www.youtube.com embed rpB6zQNsbQU?start 777&end 833\" frameborder \"0\" allow \"accelerometer autoplay encrypted media gyroscope picture in picture\" allowfullscreen iframe \"\"\" random seam m, n, i reduce a, b a..., clamp last a rand 1 1 , 1, n , 1 m 1 init i grant example 1 8 8 3 5 4 7 8 1 0 8 4 8 0 4 7 2 9 9 0 0 5 9 4 2 4 0 2 4 5 2 4 2 5 3 0 . 10 Gray. grant example function fib n base case basis if n 0 || n 1 `||` means \"or\" return 1 end recursion induction return fib n 1 fib n 2 end grant example grant example optimal seam 4, 3, 2, 2, 3, 3 sum grant example i, grant example optimal seam i for i in 1 6 , grant example optimal seam 2 begin struct AccessTrackerArray T,N AbstractArray T,N data Array T,N accesses Ref Int end Base.IndexStyle Type AccessTrackerArray IndexLinear Base.size x AccessTrackerArray size x.data Base.getindex x AccessTrackerArray, i Int... x.accesses 1 x.data i... Base.setindex x AccessTrackerArray, v, i... x.accesses 1 x.data i... v track access x AccessTrackerArray x, Ref 0 function track access f Function, x Array tracked track access x f tracked tracked.accesses end end md\" homework 4, version 5 \" md\"\"\" Homework 4 Dynamic programming `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Choose your image bind image url Select all image urls Maximum image size bind max height str Select string. 50,100,200,500 pixels. Using a large image might lead to long runtimes in the later exercises. \"\"\" img original load download image url max height parse Int, max height str \"Decimate an image such that its new height is at most `height`.\" function decimate to height img, height factor max 1, 1 size img, 1 ÷ height img 1 factor end, 1 factor end end img decimate to height img original, max height Gray. brightness. img float to color. energy img md\"\"\" Cutting a seam Below is a function called `remove in each row img, pixels `. It takes a matrix `img` and a vector of integers, `pixels`, and shrinks the image by 1 pixel in width by removing the element `img i, pixels i ` in every row. This function is one of the building blocks of the Image Seam algorithm we saw in the lecture. Read it and convince yourself that it is correct. \"\"\" function remove in each row img Matrix, column numbers Vector m, n size img assert m length column numbers same as the number of rows local img′ similar img, m, n 1 create a similar image with one column less for i, j in enumerate column numbers img′ i, 1 j 1 . view img i, 1 j 1 img′ i, j end . view img i, j 1 end end img′ end let seam 1,2,3,4 remove in each row removal test image, seam end let seam 1,1,1,1 remove in each row removal test image, seam end let seam 1,1,1,1 result1 remove in each row removal test image, seam result2 remove in each row result1, seam result2 end md\"Let's use our function to remove the diagonal from our image. Take a close look at the images to verify that we removed the diagonal. \" md\"\"\" Removing the seam ` 1,1,1,1 ` is equivalent to removing the first column \"\"\" md\"\"\" If we remove the same seam twice, we remove the first two rows \"\"\" md\"\"\" Brightness and Energy \"\"\" md\"\"\" First, we will define a `brightness` function for a pixel a color as the mean of the red, green and blue values. You should use this function whenever the problem set asks you to deal with brightness of a pixel. \"\"\" md\"\"\"We provide you with a convolve function below. \"\"\" md\"\"\" finally we define the `energy` function which takes the Sobel gradients along x and y directions and computes the norm of the gradient for each pixel. \"\"\" md\"\"\" Exercise 1 Building up to dynamic programming In this exercise and the following ones, we will use the computational problem of Seam carving. We will think through all the \"gut reaction\" solutions, and then finally end up with the dynamic programming solution that we saw in the lecture. In the process we will understand the performance and accuracy of each iteration of our solution. How to implement the solutions For every variation of the algorithm, your job is to write a function which takes a matrix of energies, and an index for a pixel on the first row, and computes a \"seam\" starting at that pixel. The function should return a vector of as many integers as there are rows in the input matrix where each number points out a pixel to delete from the corresponding row. it acts as the input to `remove in each row` . \"\"\" md\"\"\" Exercise 1.1 The greedy approach The first approach discussed in the lecture included below is the greedy approach you start from your top pixel, and at each step you just look at the three neighbors below. The next pixel in the seam is the neighbor with the lowest energy. \"\"\" md\"\"\" 👉 Implement the greedy approach. \"\"\" function greedy seam energies, starting pixel Int m, n size energies you can delete the body of this function it's just a placeholder. random seam size energies ..., starting pixel end md\"Before we apply your function to our test image, let's try it out on a small matrix of energies displayed here in grayscale , just like in the lecture snippet above clicking on the video will take you to the right part of the video . Light pixels have high energy, dark pixels signify low energy.\" md\"Starting pixel bind greedy starting pixel Slider 1 size grant example, 2 show value true, default 5 \" greedy seam result greedy seam grant example, greedy starting pixel let s sum grant example i,j for i, j in enumerate greedy seam result md\"\"\" Total energy round s,digits 1 \"\"\" end md\" Let's try it on the bigger image \" begin reactive references to uncheck the checkbox when the functions are updated greedy seam, img, grant example md\"Compute shrunk image bind shrink greedy CheckBox \" end md\"\"\" Exercise 1.2 Recursion A common pattern in algorithm design is the idea of solving a problem as the combination of solutions to subproblems. The classic example, is a Fibonacci number https en.wikipedia.org wiki Fibonacci number generator. The recursive implementation of Fibonacci looks something like this \"\"\" md\"\"\" Notice that you can call a function from within itself which may call itself and so on until a base case is reached. Then the program will combine the result from the base case up to the final result. In the case of the Fibonacci function, we added the solutions to the subproblems `fib n 1 `, `fib n 2 ` to produce `fib n `. An analogy can be drawn to the process of mathematical induction in mathematics. And as with mathematical induction there are parts to constructing such a recursive algorithm Defining a base case Defining an recursion i.e. finding a solution to the problem as a combination of solutions to smaller problems. \"\"\" md\"\"\" 👉 Define a `least energy` function which returns 1. the lowest possible total energy for a seam starting at the pixel at i, j 2. the column to jump to on the next move in row i 1 , which is one of j 1 , j or j 1 , up to boundary conditions. Return these two values in a tuple. \"\"\" returns lowest possible sum energy at pixel i, j , and the column to jump to in row i 1. function least energy energies, i, j m, n size energies base case if i something return energies ... , ... no need for recursive computation in the base case end induction combine results from recursive calls to `least energy`. end least energy grant example, 1, 4 md\"\"\" Expected output As shown in the lecture, the optimal seam from the point 1,4 should be \"\"\" md\"\"\" So we expect the output of `least energy grant example, 1, 4 ` to be \"\"\" md\"\"\" This is elegant and correct, but inefficient Let's look at the number of accesses made to the energies array needed to compute the least energy seam of a 10x10 image \"\"\" track access rand 10,10 do tracked least energy tracked, 1, 5 end md\"Whoa We will need to optimize this later \" md\"\"\" Exercise 1.3 Exhaustive search with recursion Now use the `least energy` function you defined above to define the `recursive seam` function which takes the energies matrix and a starting pixel, and computes the seam with the lowest energy from that starting pixel. This will give you the method used in the lecture to perform exhaustive search of all possible paths https youtu.be rpB6zQNsbQU?t 839 . \"\"\" function recursive seam energies, starting pixel m, n size energies Replace the following line with your code. rand 1 starting pixel for i 1 m end md\"\"\" We won't use this function to shrink our larger image, because it is too inefficient. Your notebook might get stuck But let's try it on the small example matrix from the lecture, to verify that we have found the optimal seam. \"\"\" recursive seam test recursive seam grant example, 4 md\"\"\" Exercise 1.4 State clearly why this algorithm does an exhaustive search of all possible paths. How does the number of possible seam grow as n increases for a `m×n` image? Big O notation is fine, or an approximation is fine . \"\"\" exhaustive observation md\"\"\" your answer here \"\"\" md\"\"\" Exercise 2 Memoization Memoization is the name given to the technique of storing results to expensive function calls that will be accessed more than once. As stated in the video, the function `least energy` is called repeatedly with the same arguments. In fact, we call it on the order of 3^n times, when there are only really m \\times n unique ways to call it Lets implement memoization on this function with first a dictionary https docs.julialang.org en v1 base collections Dictionaries for storage. \"\"\" md\"\"\" Exercise 2.1 Dictionary as storage Let's make a memoized version of least energy function which takes a dictionary and first checks to see if the dictionary contains the key i,j if it does, returns the value stored in that place, if not, will compute it, and store it in the dictionary at key i, j and return the value it computed. `memoized least energy energies, starting pixel, memory ` This function must recursively call itself, and pass the same `memory` object it received as an argument. You are expected to read and understand the documentation on dictionaries https docs.julialang.org en v1 base collections Dictionaries to find out how to 1. Create a dictionary 2. Check if a key is stored in the dictionary 3. Access contents of the dictionary by a key. \"\"\" function memoized least energy energies, i, j, memory Dict m, n size energies you should start by copying the code from your not memoized least energies function. end memoized least energy test memoized least energy grant example, 1, 4, Dict md\"\"\" Let's see how many matrix access we have now \"\"\" track access rand 10,10 do tracked memoized least energy tracked, 1, 5, Dict end function memoized recursive seam energies, starting pixel we set up the the memory note the key type Tuple Int,Int and the value type Tuple Float64,Int . If you need to memoize something else, you can just use Dict without types. memory Dict Tuple Int,Int ,Tuple Float64,Int m, n size energies Replace the following line with your code. you should start by copying the code from your not memoized recursive seam function. end memoized recursive seam grant example, 4 grant example optimal seam md\"\"\" Exercise 2.2 Matrix as storage optional The dictionary based memoization we tried above works well in general as there is no restriction on what type of keys can be used. But in our particular case, we can use a matrix as a storage, since a matrix is naturally keyed by two integers. 👉 Write a variant of `matrix memoized least energy` and `matrix memoized seam` which use a matrix as storage. \"\"\" function matrix memoized least energy energies, i, j, memory Matrix m, n size energies Replace the following line with your code. end function matrix memoized seam energies, starting pixel memory Matrix Union Nothing,Tuple Float64,Int nothing, size energies use me instead of you use a different element type memory Matrix Any nothing, size energies m, n size energies Replace the following line with your code. starting pixel for i 1 m end begin matrix memoized seam, img md\"Compute shrunk image bind shrink matrix CheckBox \" end md\"\"\" Exercise 3 Dynamic programming without recursion Now it's easy to see that the above algorithm is equivalent to one that populates the memory matrix in a for loop. Exercise 3.1 👉 Write a function which takes the energies and returns the least energy matrix which has the least possible seam energy for each pixel. This was shown in the lecture, but attempt to write it on your own. \"\"\" function least energy matrix energies result copy energies m, n size energies your code here return result end img brightness brightness. img le test least energy matrix img brightness spooky A Matrix Real map sqrt. A . maximum A do x RGB .8x, x, .8x end spooky le test md\"\"\" Exercise 3.2 👉 Write a function which, when given the matrix returned by `least energy matrix` and a starting pixel on the first row , computes the least energy seam from that pixel. \"\"\" function seam from precomputed least energy energies, starting pixel Int least energies least energy matrix energies m, n size least energies Replace the following line with your code. starting pixel for i 1 m end begin img, seam from precomputed least energy md\"Compute shrunk image bind shrink bottomup CheckBox \" end md\" Function library Just some helper functions used in the notebook.\" function mark path img, path img′ RGB. img also makes a copy m size img, 2 for i, j in enumerate path if size img, 2 50 To make it easier to see, we'll color not just the pixels of the seam, but also those adjacent to it for j′ in j 1 j 1 img′ i, clamp j′, 1, m RGB 1,0,1 end else img′ i, j RGB 1,0,1 end end img′ end function shrink n min seam Function, img Matrix Colorant , n, imgs show lightning true, n 0 && return push imgs, img e energy img seam energy seam sum e i, seam i for i in 1 size img, 1 , min j findmin map j seam energy min seam e, j , 1 size e, 2 min seam vec min seam e, min j img′ remove in each row img, min seam vec if show lightning push imgs, mark path img, min seam vec else push imgs, img′ end shrink n min seam, img′, n 1, imgs show lightning show lightning end if shrink greedy local n min 200, size img, 2 greedy carved shrink n greedy seam, img, n md\"Shrink by bind greedy n Slider 1 n show value true \" end if shrink greedy greedy carved greedy n end begin reactive references to uncheck the checkbox when the functions are updated img, memoized recursive seam, shrink n md\"Compute shrunk image bind shrink dict CheckBox \" end if shrink dict local n min 20, size img, 2 dict carved shrink n memoized recursive seam, img, n md\"Shrink by bind dict n Slider 1 n, show value true \" end if shrink dict dict carved dict n end if shrink matrix local n min 20, size img, 2 matrix carved shrink n matrix memoized seam, img, n md\"Shrink by bind matrix n Slider 1 n, show value true \" end if shrink matrix matrix carved matrix n end if shrink bottomup local n min 40, size img, 2 bottomup carved shrink n seam from precomputed least energy, img, n md\"Shrink by bind bottomup n Slider 1 n, show value true \" end if shrink bottomup bottomup carved bottomup n end function pencil X f x RGB 1 x,1 x,1 x map f, X . maximum X end function decimate img, n img 1 n end, 1 n end end hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"You can call the `least energy` function recursively within itself to obtain the least energy of the adjacent cells and add the energy at the current cell to get the total energy.\" hint md\"We recommend using a matrix with element type `Union Nothing, Tuple Float64,Int `, initialized to all `nothing`s. You can check whether the value at ` i,j ` has been computed before using `memory i,j nothing`.\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text begin function visualize seam algorithm test energies, algorithm Function, starting pixel Integer seam algorithm test energies, starting pixel visualize seam algorithm test energies, seam end function visualize seam algorithm test energies, seam Vector display img RGB. test energies for i, j in enumerate seam try display img i, j RGB 0.9, 0.3, 0.6 catch ex if ex isa BoundsError return keep working \"\" end the solution might give an illegal index end end display img end end visualize seam algorithm grant example, greedy seam result yays md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text if recursive seam test grant example optimal seam correct else keep working end let result track access rand 10,10 do tracked memoized least energy tracked, 1, 5, Dict end if result 0 nothing elseif result 200 correct else keep working md\"That's still too many accesses Did you forget to add a result to the `memory`?\" end end let aresult track access rand 10,10 do tracked memoized recursive seam tracked, 5 end if aresult 200 if memoized recursive seam grant example, 4 grant example optimal seam correct else keep working md\"The returned seam is not correct. Did you implement the non memoized version correctly?\" end else keep working md\"Careful Your `memoized recursive seam` is still making too many memory accesses, you may not want to run the visualization below.\" end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined least energy not defined least energy else let result1 least energy grant example, 6, 4 if result1 isa Tuple keep working md\"Your function should return a tuple , like ` 1.2, 5 `.\" elseif result1 isa Tuple Float64,Int keep working md\"Your function should return a tuple , like ` 1.2, 5 `.\" else result least energy grant example, 1, 4 if result isa Tuple Float64,Int keep working md\"Your function should return a tuple , like ` 1.2, 5 `.\" else a, b result if a ≈ 0.3 && b 4 almost md\"Only search the at most three cells that are within reach.\" elseif a ≈ 0.6 && b 3 correct else keep working end end end end end if isdefined least energy matrix not defined least energy matrix elseif le test isa Matrix Real keep working md\"`least energy matrix` should return a 2D array of Float64 values.\" end if isdefined seam from precomputed least energy not defined seam from precomputed least energy end function hbox x, y, gap 16 sy size y , sx size x w, h max sx 1 , sy 1 , gap sx 2 sy 2 slate fill RGB 1,1,1 , w,h slate 1 size x,1 , 1 size x,2 . RGB. x slate 1 size y,1 , size x,2 gap . 1 size y,2 . RGB. y slate end hbox float to color. convolve brightness. img , Kernel.sobel 1 , float to color. convolve brightness. img , Kernel.sobel 2 , vbox x,y, gap 16 hbox x', y' ' bigbreak html\" br br br br br \" bigbreak bigbreak bigbreak bigbreak "},{"url":"homework/hw5/","title":"Structure","tags":["homework","module2","track_julia","structure","track_math","type","matrix","linear algebra","track_data"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 section 2.5 order 2.5 homework number 5 title \"Structure\" layout \"layout.jlhtml\" tags \"homework\", \"module2\", \"track julia\", \"structure\", \"track math\", \"type\", \"matrix\", \"linear algebra\", \"track data\" description \"Create your own Julia structs and add new functionality to them, to create first class mathematical objects.\" using Markdown using InteractiveUtils using PlutoUI using LinearAlgebra begin struct FirstRankOneMatrix Your code here v Vector Float64 w Vector Float64 end Add the extra constructor here FirstRankOneMatrix v FirstRankOneMatrix v, v end ten twelve missing Your code here sqrt Base.sqrt filter Base.filter methods size function Base.size M FirstRankOneMatrix return missing Your code here end function Base.getindex M FirstRankOneMatrix, i, j return missing Your code here end ten twelve ones 10, 12 An example matrix two dimensional array function print as matrix M FirstRankOneMatrix Your code here end print as matrix ten twelve begin struct RankOneMatrix T AbstractMatrix T v AbstractVector T w AbstractVector T end Add the two extra constructors Should we make these missing by default? if so remove hint below RankOneMatrix v RankOneMatrix v, v end function Base.size M RankOneMatrix return missing Your code here end function Base.getindex M RankOneMatrix, i, j return missing Your code here end R2 RankOneMatrix 1,2 , 3,4,5 M RankOneMatrix 1 10 missing Your code here collect M typeof collect M function matvec M RankOneMatrix, x return missing Your code here end begin struct RankTwoMatrix T AbstractMatrix T Your code here A RankOneMatrix T B RankOneMatrix T end Add a constructor that uses two vectors ranges RankTwoMatrix v1, v2 RankTwoMatrix RankOneMatrix v1 , RankOneMatrix v2 end function Base.getindex M RankTwoMatrix, i, j return missing Your code here end function Base.size M RankTwoMatrix return missing Your code here end struct LowRankMatrix AbstractMatrix Float64 Your code here Ms Vector RankOneMatrix rank Int end function Base.getindex M LowRankMatrix, i, j return missing Your code here end md\" homework 5, version 3 \" md\"\"\" Homework 5 Structure `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Exercise 1 When is zero not quite zero? Students used to pure math are sometimes surprised to see numbers like `1e 15`, `1e 16`, or `1e 17` appearing in computations when `0` might have been expected. At first glance, this behaviour seems 'random' or 'noisy', but in this exercise, we will try to demonstrate some structure behind floating point artihmetic. Exercise 1.1 👉 Find all integers j with 1 ≤ j ≤ 1000 for which Julia's result satisfies `j 1 j ≠ 1`. \"\"\" md\"\"\" Notice that when you re run the computation, the result does not change. Floating point arithmetic is not random Exercise 1.2 👉 Take the smallest number `j` you found above and compute the error, i.e. the distance between `j 1 j ` and `1`. Is this an integer power of 2? Which one? `log2` might help. \"\"\" md\"\"\" Exercise 1.3 Caculate all of the following 32 23 736 3.2 23 73.6 3.2 2.3 7.36 \"\"\" md\"\"\" We wanted to show you that floating point arithmetic has structure to it. It is not the fuzz or slop that you may have seen as experimental errors in maybe a chemistry or physics class. If you are interested, we may show more later in the semester. \"\"\" md\"\"\" Exercise 2 Rank one matrices In this exercise we will go into some more detail about how to define types in Julia in order to make a structured matrix type, similar to the `OneHotVector` type from class. To begin, we will make a type to represent a rank 1 matrix. A rank 1 matrix is a matrix whose columns are all multiples of each other. This already tells us a lot about the matrix in fact, we can represent any such matrix as the outer product of two vectors `v` and `w`. Only the two vectors will be stored the matrix elements will be calculated on demand, i.e. when we index into the object. \"\"\" md\"\"\" Exercise 2.1 Let's make a `FirstRankOneMatrix` type that contains two vectors of floats, `v` and `w`. Here `v` represents a column and `w` the multipliers for each column. We include in the same cell, due to requirements of Pluto a constructor that takes a single vector `v` and duplicates it. \"\"\" md\"\"\" 👉 Create an object of type `FirstRankOneMatrix` representing the multiplication table of the numbers from 1 to 10 and the numbers from 1 to 12. Call it `ten twelve`. \"\"\" md\"\"\" Exercise 2.2 extending Base methods Right now, our `FirstRankOneMatrix` is just a container for two arrays. To make it behave like a matrix, we add methods to functions in `Base`, Julia's standard set of functions. Most of the functions you have used so far come from base, for example \"\"\" md\"\"\" These are built in functions, and each function comes with a set of methods pre defined in Julia's source code which is mostly written in Julia . Uncomment the next cell to see the full list of methods for `size`. We will add a method to this list \"\"\" md\"\"\" Let's extend the `size` and `getindex` methods so that they can also work with our rank 1 matrices. This will allow us to easily access the size and entries of the matrix. \"\"\" md\"\"\" 👉 Extend `Base.size` for the type `FirstRankOneMatrix` to return the size as a tuple of values corresponding to each direction. \"\"\" md\"\"\" 👉 Extend `Base.getindex` for the type `FirstRankOneMatrix` to return the i, j th entry of the outer product. \"\"\" Base.getindex ten twelve, 5, 11 md\"\"\" Exercise 2.3 If you ask Julia to display the value of an object of type `FirstRankOneMatrix`, you will notice that it doesn't do what we are used to, which is probably to display the whole matrix that it corresponds to. Let's see \"\"\" md\"\"\" This is what matrices normally look like in Julia \"\"\" md\"\"\" One possible workaround for this is to create a new function specifically to display a matrix of our custom type. 👉 Make a function `print as matrix` that prints the entries of a matrix of type `FirstRankOneMatrix` in a grid format. We'll test that it works below with the matrix `R` we already created. \"\"\" md\"\"\" We should now be able to see our `FirstRankOneMatrix` type matrix displayed in the terminal \"\"\" md\"\"\" Exercise 2.4 AbstractMatrix In fact, Julia together with Pluto can do some of this work for us Julia provides facilities to make our life easier when we tell Julia that our type behaves like an array. We do so by making the type a subtype of `AbstractMatrix`. This will let our type inherit the methods and attributes that come with the `AbstractMatrix` type by default including some display functions that Pluto notebooks use. Let's do all this on a new rank one matrix type `RankOneMatrix`. As we do this, we will also remove the restriction to `Float64` type entries by using a parametrised type `T`. Parametrised types allow us flexibility to handle different types of entries without repeating a lot of code we won't go too in depth about these for now. \"\"\" md\"\"\" In the cell above, we added a second 'outer' constructor that takes a single vector as argument. 👉 Make sure that you can use both constructors by trying them out below. \"\"\" md\"\"\" 👉 Add `getindex` and `size` methods for this new type. These will allow us to access entries of our custom matrices with the usual index notation `M i,j `, as well as quickly retrieving their dimensions. \"\"\" Base.getindex R2, 2, 3 md\"\"\" 👉 Create an object of the new type for the 10 \\times 10 multiplication matrix, using a single range. \"\"\" md\"\"\" You should see two things Firstly, the matrix now contains integers, instead of floats this is thanks to our parametrised type . And secondly, Julia automatically displays the matrix as we wanted, once you have defined `getindex` and `size`, provided you have told Julia that your type is a subtype of `AbstractArray` \"\"\" md\"\"\" Julia also allows us to automatically convert the matrix to a normal \"dense\" matrix, using either `collect` or `Array`. Let's try these out. You may need to re run the cell below after completing the exercises. \"\"\" md\"\"\" Fun fact we did not define a method for `Base.collect`, and yet it works This is because we told Julia that our `RankOneMatrix` is a subtype of `AbstractMatrix`, for which `Base.collect` already has a method defined in Julia's source code. This fallback method uses `Base.getindex` and `Base.size`, which we did define. \"\"\" md\"\"\" Exercise 2.5 Why do we need a special type to represent special types of structured matrices? One reason is that not only do they give a more efficient representation in space requiring less memory to store , they can also be more efficient in time, i.e. faster . For example, let's look at matrix vector multiplication . This is a fundamental part of many, many algorithms in scientific computing, and because of this, we usually want it to be as fast as possible. For a rank one matrix given by M v w^T , the matrix vector product M \\cdot x is given by w \\cdot x v . Note that w \\cdot x is a number scalar which is multiplying the vector element by element. This computation is much faster than the usual matrix vector multiplication we are taking advantage of structure \"\"\" md\"\"\" 👉 Define a function `matvec` that takes a `RankOneMatrix` `M` and a `Vector` `x` and carries out matrix vector multiplication. We will be able to compare the result with doing the matrix vector product using the corresponding dense matrix. \"\"\" md\"\"\" Exercise 3 Low rank matrices \"\"\" md\"\"\" In this exercise we will combine rank 1 matrices into low rank matrices, which are sums of rank 1 matrices. Just like in the previous exercise, we will make custom types for these matrices, and we will be able to compute the entries of a matrix on demand as we request them instead of storing all of them always . \"\"\" md\"\"\" Exercise 3.1 👉 Make a rank 2 matrix type `RankTwoMatrix` that contains two rank 1 matrices `A` and `B`. For simplicity, you can use the `FirstRankOneMatrix` type. Include in the same cell a constructor for `RankTwoMatrix` that takes two vectors and makes the rank one matrices from those vectors. Note In principle we should check when constructing the type that the input matrices have the same dimensions, but we will just assume that they do. \"\"\" md\"\"\" Exercise 3.2 👉 Construct a rank two matrix out of the rank 1 matrix representing the multiplication table of 1.0 10.0 , together with the multiplication table of 0.0 0.1 0.9 . Note that both range arguments must contain floats, so that we can add up entries. \"\"\" md\"\"\" Exercise 3.3 👉 As with last time, extend the `getindex` and `size` methods for the `RankTwoMatrix` type. Keep in mind they are already defined for `RankOneMatrix`. \"\"\" md\"\"\" Exercise 3.4 Making a custom type for rank 2 matrices is a step forward from rank 1 matrices instead of storing two vectors, we store two rank 1 matrices themselves. What if we want to represent a rank 3 matrix? We would need to store three rank 1 matrices, instead of just two. What about rank 4, rank 5, and so on? We can go even further and make a general custom type `LowRankMatrix` for rank k matrices, for general ideally low k . In this case, we should store two main things the list of rank 1 matrices that our low rank matrix is made up of, and also the rank of the matrix which is how many rank 1 matrices we are storing . 👉 Complete the definition for the type `LowRankMatrix`. Remember to store both the rank 1 matrices and the rank of the matrix itself. \"\"\" md\"\"\" Exercise 3.5 👉 Extend the `getindex` and `size` methods to work with the `LowRankMatrix` type. As before, remember that these are already defined for `RankOneMatrix`. \"\"\" function Base.size M LowRankMatrix return missing Your code here end size Base.size Base.size ten twelve Base.size R2 let comp1, comp2 RankOneMatrix 1.0 1.0 10.0 , RankOneMatrix 0.0 0.1 0.9 ex3 LowRankMatrix comp1, comp2 , 2 end md\"\"\" Exercise 3.6 👉 Extend the method `matvec` to work with our custom type `LowRankMatrix` and a `Vector` . Remember that `matvec` is already defined for `RankOneMatrix`. \"\"\" function matvec M LowRankMatrix, x return missing Your code here end md\"\"\" Exercise 3.7 One of the big advantages of our rank 1 matrices is its space efficiency to \"store\" a n\\times n matrix, we only need to actually store two vectors of n entries, as opposed of n^2 total entries. Rank 2 matrices are a litte less efficient we store two rank 1 matrices, or four vectors of n vertices. As we increase the rank, we lose space efficiency, at the cost of being able to represent more kinds of matrices. 👉 For what rank will a matrix of rank k need the same amount of storage as the dense version? Explain your answer. \"\"\" answer md\"\"\" From rank ... Because .... \"\"\" md\"\"\" Exercise 4 The SVD for structured matrices In a math class, you may or may not learn about the singular value decomposition SVD . From a computational thinking point of view, whether you have seen this before or not we hope will not matter. Here we would like you to get to \"know\" the SVD , through experience, rather than through a math lecture. This is how we see \"computational thinking\" after all. Definitely do not look up some eigenvalue definition. \"\"\" md\"\"\" Exercise 4.1 The `LinearAlgebra` package defines a function `svd` that computes the decomposition for us. Have a look at the result of calling the `svd` function. Try not to get intimidated by the large output you may need to scroll the cell output , and look at the docs for `svd`. \"\"\" biggie rand 100,100 md\"\"\" 👉 What are the singular values of `biggie`? \"\"\" singular values of biggie missing md\"\"\" Exercise 4.2 If we try to run the `svd` function on a `RankOneMatrix`, you will see that it does not work. The error is telling us that no `svd` method has been defined for our type . Let's extend the `svd` function to work on objects of our type. \"\"\" A RankOneMatrix rand 3 ,rand 4 md\"\"\" 👉 Extend `svd` to work directly on a rank one matrix, by writing a new method. Keep things simple. Inside your method, call `LinearAlgebra.svd` on a type that it is already defined for . \"\"\" function LinearAlgebra.svd A RankOneMatrix return missing end svd biggie svd A LinearAlgebra.svd A md\"\"\" Exercise 4.3 👉 Look at the singular values, how many of them are approximately non zero? Does that make sense? \"\"\" md\"\"\" This number the number of singular values that are positive is something you will or have learn ed in a linear algebra class it is known as the rank of a matrix, and is usually defined through a complicated elimination procedure. 👉 Write a function `numerical rank` that calculates the singular values of a matrix `A`, and returns how many of them are approximately zero. To keep things simple, you can assume that \"approximately zero\" means less than `tol 1e5`. \"\"\" function numerical rank A AbstractMatrix tol 1e 5 return missing end numerical rank A numerical rank rand 3,3 md\"\"\" Exercise 4.4 👉 Write a function that takes an argument k and generates the sum of k random rank one matrices of size mxn, and counts the number of essentially non zero singular values. Hint you can do this with a `for` loop, but it can also be done with `sum`, e.g. `sum i for i 1 10 `. \"\"\" function k rank ones k, m, n return missing end k rank ones 1, 3, 3 md\"\"\" Exercise 4.5 👉 What is the answer when m and n are both ≥ k ? What if one of m or n is k ? \"\"\" md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" Try writing this without a `for` loop. Ideas ☝️ Use `filter`. ☝️ Comprehensions can have `if` clauses, as in ` j for j 1 1000 if 5 j 17 `. \"\"\" | hint hint md\"\"\" The extra constructor should have the same name as the type it should take in a single argument `v`, and use it for both the column and the multipliers. \"\"\" hint md\"\"\" Check the docs for functions like `print`, `println`, `lpad` and `rpad`. Also, you don't have to know exactly how the `do` keyword works for now, but feel free to read about it it can be useful with iterating \"\"\" hint md\"\"\" If you want to specify that your argument is a range, use the `AbstractRange` type. \"\"\" hint md\"\"\" How would we implement `matvec` for a rank 2 matrix making use of the two stored rank 1 matrices? Can you generalize this to the rank k case, when we have many rank 1 matrices? \"\"\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text let example FirstRankOneMatrix 1,2 , 3,4 if ismissing print as matrix example still missing end end keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next question.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text try A RankOneMatrix 1,2,3 , 5,6,7 result LinearAlgebra.svd A if result isa Missing still missing elseif result isa LinearAlgebra.SVD almost md\"Like all other `svd` methods implemented in `LinearAlgebra` , your method should return an SVD factorization of type `LinearAlgebra.SVD` .\" else reconstructed result.U ,1 1 result.S 1 1 result.Vt 1 1, if sum abs. A . reconstructed 1e 5 correct else keep working end end catch e if e isa MethodError && e.f LinearAlgebra.svd still missing md\"Define a new method for `LinearAlgebra.svd` that takes a `RankOneMatrix` as argument.\" else rethrow e end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" let if isdefined ten twelve not defined ten twelve else if ten twelve isa Missing still missing elseif ten twelve isa FirstRankOneMatrix keep working md\"`ten twelve` should be a `FirstRankOneMatrix`.\" elseif ten twelve.v 1 10 && ten twelve.w 1 12 || ten twelve.w 1 10 && ten twelve.v 1 12 correct else keep working end end end let if isdefined RankOneMatrix not defined RankOneMatrix else R2 FirstRankOneMatrix 1,2 , 3,4,5 if ismissing Base.size R2 still missing elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else correct end end end let if isdefined FirstRankOneMatrix not defined FirstRankOneMatrix else R2 FirstRankOneMatrix 1,2 , 3,4,5 if ismissing Base.getindex R2, 1, 1 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif ismissing Base.size R2 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else shouldbe 3 4 5 6 8 10 r all CartesianIndices shouldbe do I isapprox R2 Tuple I ... , shouldbe I , rtol 1 3 end if r correct else keep working md\"`Base.getindex` is not implemented correctly.\" end end end end let if isdefined RankOneMatrix not defined RankOneMatrix else R2 RankOneMatrix 1,2 , 3,4,5 if ismissing Base.size R2 still missing elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else correct end end end let if isdefined RankOneMatrix not defined RankOneMatrix else R2 RankOneMatrix 1,2 , 3,4,5 if ismissing Base.getindex R2, 1, 1 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif ismissing Base.size R2 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else shouldbe 3 4 5 6 8 10 r all CartesianIndices shouldbe do I isapprox R2 Tuple I ... , shouldbe I , rtol 1 3 end if r correct else keep working md\"`Base.getindex` is not implemented correctly.\" end end end end if isdefined matvec not defined matvec else let M RankOneMatrix 1 10 if ismissing matvec M, ones 10 still missing end incorrect false for i in 1 5 r ones 10 . i for j in 1 10 if matvec M, r j collect M r j incorrect true end end end if incorrect keep working else correct end end end let if isdefined RankTwoMatrix not defined RankTwoMatrix else R1 RankOneMatrix 1,2 , 3,4,5 R2 RankTwoMatrix R1, R1 if ismissing Base.size R2 still missing elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else correct end end end let if isdefined RankTwoMatrix not defined RankTwoMatrix else R1a RankOneMatrix 1,2 , 3,4,5 R1b RankOneMatrix 2,1 , 3,4,5 R2 RankTwoMatrix R1a, R1b if ismissing Base.getindex R2, 1, 1 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif ismissing Base.size R2 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else shouldbe 3 4 5 6 8 10 s2 shouldbe shouldbe 2,1 , r all CartesianIndices s2 do I isapprox R2 Tuple I ... , s2 I , rtol 1 3 end if r correct else keep working md\"`Base.getindex` is not implemented correctly.\" end end end end let if isdefined LowRankMatrix not defined LowRankMatrix else R1 RankOneMatrix 1,2 , 3,4,5 R2 LowRankMatrix R1, R1 ,2 if ismissing Base.size R2 still missing elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else correct end end end let if isdefined LowRankMatrix not defined LowRankMatrix else R1a RankOneMatrix 1,2 , 3,4,5 R1b RankOneMatrix 2,1 , 3,4,5 R2 LowRankMatrix R1a, R1b ,2 if ismissing Base.getindex R2, 1, 1 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif ismissing Base.size R2 still missing md\"Implement both `Base.size` and `Base.getindex`.\" elseif size R2 2, 3 keep working md\"`Base.size` is not implemented correctly.\" else shouldbe 3 4 5 6 8 10 s2 shouldbe shouldbe 2,1 , r all CartesianIndices s2 do I isapprox R2 Tuple I ... , s2 I , rtol 1 3 end if r correct else keep working md\"`Base.getindex` is not implemented correctly.\" end end end end if isdefined matvec not defined matvec else let comp1, comp2 RankOneMatrix 1.0 1.0 10.0 , RankOneMatrix 0.0 0.1 0.9 if ismissing comp1 | ismissing comp2 still missing end ex4 LowRankMatrix comp1, comp2 , 2 if ismissing ex4 still missing end if ismissing matvec ex4, ones 10 still missing end incorrect false for i in 1 10 r ones 10 . i v, c matvec ex4, r , collect ex4 r for j in 1 10 if abs v j c j 1e 10 incorrect true break end end end if incorrect keep working else correct end end end if isdefined singular values of biggie not defined singular values of biggie else if singular values of biggie isa Missing still missing elseif singular values of biggie isa AbstractVector keep working md\"Return the singular values as a vector .\" elseif isapprox singular values of biggie, svdvals biggie , rtol 1e 5 correct else keep working end end let if isdefined numerical rank not defined numerical rank else result1 numerical rank 1 2 0 0 0 1e 10 5 10 0 if result1 isa Missing still missing elseif result1 isa Integer keep working md\"You should return a number.\" elseif result1 1 keep working else result2 numerical rank 1 3 0 0 0 1e 10 5 10 0 if result2 2 correct else keep working end end end end let if isdefined k rank ones not defined k rank ones else result k rank ones 1, 3, 4 if result isa Missing still missing elseif result isa AbstractMatrix keep working md\"Make sure that you return a matrix.\" elseif size result 3, 4 keep working md\"Make sure that you return a matrix of the correct size.\" else numerical rank A count 1e 5 , svdvals A if numerical rank result 1 correct else result2 k rank ones 2, 10, 4 if result2 isa AbstractMatrix keep working md\"Make sure that you return a matrix.\" elseif size result2 10, 4 keep working md\"Make sure that you return a matrix of the correct size.\" elseif numerical rank result2 2 correct else keep working end end end end end todo text HTML \"\"\" div style \"background rgb 220, 200, 255 padding 2em border radius 1em \" h1 TODO h1 repr MIME\"text html\" , text div \"\"\" bigbreak html\" br br br br br \" bigbreak bigbreak bigbreak bigbreak bigbreak "},{"url":"homework/hw6/","title":"Probability distributions","tags":["homework","module2","track_julia","track_math","track_data","structure","probability","statistics","plotting","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 section 3.5 order 3.5 homework number 6 title \"Probability distributions\" layout \"layout.jlhtml\" tags \"homework\", \"module2\", \"track julia\", \"track math\", \"track data\", \"structure\", \"probability\", \"statistics\", \"plotting\", \"interactive\" description \"Calculate a probability distribution from a dataset, experiment with different statistical models, and learn how to plot your results.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using PlutoUI, Plots md\" homework 6, version 5 \" md\"\"\" Homework 6 Probability distributions `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Exercise 1 Calculating frequencies In this exercise we practise using dictionaries in Julia by writing our own version of the `countmap` function. Recall that that function counts the number of times that a given discrete value occurs in an input data set. A suitable data structure for this is a dictionary , since it allows us to store data that is very sparse i.e. for which many input values do not occur . \"\"\" function counts data Vector counts Dict Int,Int your code here return counts end counts 7, 8, 9, 7 md\"\"\" Test that your code is correct by applying it to obtain the counts of the data vector `test data` defined below. What should the result be? Test that you do get the correct result and call the result `test counts`. \"\"\" test data 1, 0, 1, 0, 1000, 1, 1, 1000 test counts counts test data md\"\"\" Exercise 1.2 The dictionary contains the information as a sequence of pairs mapping keys to values. This is not a particularly useful form for us. Instead, we would prefer a vector of the keys and a vector of the values, sorted in order of the key. We are going to make a new version `counts2` where you do the following below . Start off by just running the following commands each in their own cell on the dictionary `test counts` you got by running the previous `counts` function on the vector `test data` so that you see the result of running each command. Once you have understood what's happening at each step, add them to the `counts2` function in a new cell. 👉 Extract vectors `ks` of keys and `vs` of values using the `keys ` and `values ` functions and convert the results into a vector using the `collect` function. \"\"\" md\"\"\" 👉 Define a variable `perm` as the result of running the function `sortperm` on the keys. This gives a permutation that tells you in which order you need to take the keys to give a sorted version. \"\"\" md\"\"\" 👉 Use indexing `ks perm ` to find the sorted keys and values vectors. Here we are passing in a vector as the index. Julia extracts the values at the indices given in that vector \"\"\" md\"\"\" Verify that your new `counts2` function gives the correct result for the vector `v` by comparing it to the true result that you get by doing the counting by hand \"\"\" md\"\"\" 👉 Create the function `counts2` that performs these steps. \"\"\" function counts2 data Vector return missing end counts2 test data md\"\"\" Exercise 1.3 👉 Make a function `probability distribution` that normalizes the result of `counts2` to calculate the relative frequencies of each value, i.e. to give a probability distribution i.e. such that the sum of the resulting vector is 1 . The function should return the keys the unique data that was in the original data set, as calculated in `counts2`, and the probabilities relative frequencies . Test that it gives the correct result for the vector `vv`. We will use this function in the rest of the exercises. \"\"\" function probability distribution data Vector return missing end probability distribution test data md\"\"\" Intermezzo function vs. begin vs. let html\" span id function begin let span \" In our lecture materials, we sometimes use a `let` block in this cell to group multiple expressions together, but how is it different from `begin` or `function`? function Writing functions is a way to group multiple expressions i.e. lines of code together into a mini program. Note the following about functions A function always returns one object . ^1 This object can be given explicitly by writing `return x`, or implicitly Julia functions always return the result of the last expression by default. So `f x x 2` is the same as `f x return x 2`. Variables defined inside a function are not accessible outside the function . We say that function bodies have a local scope . This helps to keep your program easy to read and write if you define a local variable, then you don't need to worry about it in the rest of the notebook. There are two other ways to group expressions together that you might have seen before `begin` and `let`. begin `begin` will group expressions together, and it takes the value of its last subexpression. We use it in this notebook when we want multiple expressions to always run together. let `let` also groups multiple expressions together into one, but variables defined inside of it are local they don't affect code outside of the block. So like `begin`, it is just a block of code, but like `function`, it has a local variable scope. We use it when we want to define some local temporary variables to produce a complicated result, without interfering with other cells. Pluto allows only one definition per global variable of the same name, but you can define local variables with the same names whenever you wish ^1 Even a function like `f x return` returns one object the object `nothing` — try it out \"\"\" md\"\"\" Example of a scope problem with `begin` The following will not work, because `fruits` has multiple definitions \"\"\" md\"\"\" Solved using `let` \"\"\" let vegetables \"🥦\", \"🥔\", \"🥬\" length vegetables end let vegetables \"🌽\" length vegetables end md\"\"\" This works, because `vegetables` is only defined as a local variable inside the cell , not as a global \"\"\" vegetables md\"\"\" Exercise 2 Modelling component failure with the geometric distribution In this exercise, we will investigate the simple model of failure of mechanical components or light bulbs, or radioactive decay, or recovery from an infection, or... that we saw in lectures. Let's call \\tau the time to failure. We will use a simple model, in which each component has probability p to fail each day. If it fails on day n , then \\tau n . We see that \\tau is a random variable, so we need to study its probability distribution . \"\"\" md\"\"\" Exercise 2.1 👉 Define the function `bernoulli p ` from lectures. Recall that this generates `true` with probability p and `false` with probability 1 p . \"\"\" function bernoulli p Real return missing end md\"\"\" Exercise 2.2 👉 Write a function `geometric p `. This should run a simulation with probability p to recover and wait until the individual recovers, at which point it returns the time taken to recover. The resulting failure time is known as a geometric random variable , or a random variable whose distribution is the geometric distribution . \"\"\" function geometric p Real return missing end geometric 0.25 md\"\"\" We should always be aware of special cases sometimes called \"boundary conditions\" . Make sure not to run the code with p 0 What would happen in that case? Your code should check for this and throw an `ArgumentError` as follows ```julia throw ArgumentError \"...\" ``` with a suitable error message. \"\"\" md\"\"\" 👉 What happens for p 1 ? \"\"\" interpretation of p equals one md\"\"\" blablabla \"\"\" md\"\"\" Exercise 2.3 👉 Write a function `experiment p, N ` that runs the `geometric` function `N` times and collects the results into a vector. \"\"\" function experiment p Real, N Integer return missing end small experiment experiment 0.5, 20 md\"\"\" Exercise 2.4 Let's run an experiment with p 0.25 and N 10,000 . We will plot the resulting probability distribution, i.e. plot P \\tau n against n , where n is the recovery time. \"\"\" large experiment experiment 0.25, 10000 let xs, ps probability distribution large experiment bar xs, ps, alpha 0.5, leg false end md\"\"\" 👉 Calculate the mean recovery time. \"\"\" md\"\"\" 👉 Create the same plot as above, and add the mean recovery time to the plot using the `vline ` function and the `ls dash` argument to make a dashed line. Note that `vline ` requires a vector of values where you wish to draw vertical lines. \"\"\" let your code here end md\"\"\" html\" span id note about plotting span \" Note about plotting Plots.jl has an interesting property a plot is an object, not an action. Functions like `plot`, `bar`, `histogram` don't draw anything on your screen they just return a `Plots.Plot`. This is a struct that contains the description of a plot what data should be plotted in what way? , not the picture . So a Pluto cell with a single line, `plot 1 10 `, will show a plot, because the result of the function `plot` is a `Plot` object, and Pluto just shows the result of a cell. Modifying plots Nice plots are often formed by overlaying multiple plots. In Plots.jl, this is done using the modifying functions `plot `, `bar `, `vline `, etc. These take an extra first argument a previous plot to modify. For example, to plot the `sin`, `cos` and `tan` functions in the same view, we do ```julia function sin cos plot T 1.0 0.01 1.0 result plot T, sin. T plot result, T, cos. T plot result, T, tan. T return result end ``` 💡 This example demonstrates a useful pattern to combine plots 1. Create a new plot and store it in a variable 2. Modify that plot to add more elements 3. Return the plot Grouping expressions It is highly recommended that these 3 steps happen within a single cell . This can prevent some strange glitches when re running cells. There are three ways to group expressions together into a single cell `begin`, `let` and `function`. More on this here function begin let \"\"\" md\"\"\" Exercise 2.5 👉 What shape does the distribution seem to have? Can you verify that by using one or more log scales in a new plot? \"\"\" md\"\"\" Use the widgets from PlutoUI to write an interactive visualization that performs Exercise 2.3 for p varying between 0 and 1 and N between 0 and 100,000 . You might want to go back to Exercise 2.3 to turn your code into a function that can be called again. As you vary p , what do you observe? Does that make sense? \"\"\" bind hello Slider 2 0.5 10 hello md\"\"\" Exercise 2.6 👉 For fixed N 10,000 , write a function that calculates the mean time to recover, \\langle \\tau p \\rangle , as a function of p . \"\"\" md\"\"\" 👉 Use plots of your function to find the relationship between \\langle \\tau p \\rangle and p . \"\"\" md\"\"\" Based on my observations, it looks like we have the following relationship ```math \\langle \\tau p \\rangle my \\cdot answer \\cdot here ``` \"\"\" md\"\"\" Exercise 3 More efficient geometric distributions Let's use the notation P n \\mathbb P \\tau n for the probability to fail on the n th step. Probability theory tells us that in the limit of an infinite number of trials, we have the following exact results P 1 p P 2 p 1 p , and in general P n p 1 p ^ n 1 . \"\"\" md\"\"\" Exercise 3.1 👉 Fix p 0.25 . Make a vector of the values P n for n 1, \\dots, 50 . You must of course use a loop or similar construction do not do this by hand \"\"\" Ps let your code here end md\"\"\" 👉 Do they sum to 1? \"\"\" md\"\"\" Exercise 3.2 👉 Check analytically that the probabilities sum to 1 when you include all infinitely many of them. \"\"\" md\"\"\" ```math \\sum k 1 ^ \\infty P k \\dots your \\cdot answer \\cdot here \\dots 1 ``` \"\"\" md\"\"\" Exercise 3.3 Sum of a geometric series \"\"\" md\"\"\" 👉 Plot P n as a function of n . Compare it to the corresponding result from the previous exercise i.e. plot them both on the same graph . \"\"\" md\"\"\" 👉 How could we measure the error , i.e. the distance between the two graphs? What do you think determines it? \"\"\" md\"\"\" Exercise 3.4 If p is small , say p 0.001 , then the algorithm we used in Exercise 2 to sample from geometric distribution will be very slow, since it just sits there calculating a lot of `false`s The average amount of time taken is what you found in 1.8 . \"\"\" md\"\"\" Let's make a better algorithm. Think of each probability P n as a \"bin\", or interval, of length P n . If we lay those bins next to each other starting from P 1 on the left, then P 2 , etc., there will be an infinite number of bins that fill up the interval between 0 and 1 . In principle there is no upper limit on how many days it will take to recover, although the probability becomes very small. \"\"\" md\"\"\" Now suppose we take a uniform random number r between 0 and 1 . That will fall into one of the bins. If it falls into the bin corresponding to P n , then we return n as the recovery time \"\"\" md\"\"\" 👉 To draw this picture, we need to add up the lengths of the lines from 1 to n for each n , i.e. calculate the cumulative sum . Write a function `cumulative sum`, which returns a new vector. \"\"\" function cumulative sum xs Vector return missing end cumulative sum 1, 3, 5, 7, 9 md\"\"\" 👉 Plot the resulting values on a horizontal line. Generate a few random points and plot those. Convince yourself that the probability that a point hits a bin is equal to the length of that bin. \"\"\" cumulative cumulative sum Ps md\"\"\" Exercise 3.5 👉 Calculate the sum of P 1 up to P n analytically. \"\"\" md\"\"\" ```math C n \\sum k 1 ^n P k my \\cdot answer \\cdot here ``` \"\"\" md\"\"\" 👉 Use the previous result to find analytically which bin n a given value of r \\in 0, 1 falls into, using the inequality P n 1 \\le r \\le P n . \"\"\" md\"\"\" ```math n r,p my \\cdot answer \\cdot here ``` \"\"\" md\"\"\" Exercise 3.6 👉 Implement this as a function `geomtric bin r, p `, use the `floor` function. \"\"\" function geometric bin u Real, p Real return missing end md\"\"\" We can use this to define a fast version of the `geomtric` function \"\"\" geometric fast p geometric bin rand , p geometric fast 0.25 md\"\"\" Exercise 3.7 👉 Generate `10 000` samples from `geometric fast` with p 10^ 10 and plot a histogram of them. This would have taken a very long time with the previous method \"\"\" md\"\"\" Exercise 4 Distribution of the \"atmosphere\" In this question we will implement a very simple model of the density of the atmosphere, using a random walk , i.e. a particle that undergoes random motion . We will see more about random walks in lectures. We can think of a very light dust particle being moved around by the wind. We are only interested in its vertical position, y , and we will suppose for simplicity that y is an integer. The particle jumps up and down randomly as it is moved by the wind however, due to gravity it has a higher probability p of moving downwards to y 1 than upwards to y 1 , which happens with probability 1 p . At y 1 there is a boundary condition it hits a reflective boundary the \"surface of the Earth\" . We can model this using \"bounce back\" if the particle tries to jump downwards from y 1 then it hits a reflective boundary and bounces back to y 1 . In other words it remains in the same place. \"\"\" md\"\"\" Exercise 4.1 👉 Write a simulation of this model in a function `atmosphere` that accepts `p`, the initial height `y0`, and the number of steps N as variables. \"\"\" function atmosphere p Real, y0 Real, N Integer return missing end atmosphere 0.8, 10, 50 md\"\"\" Let's simulate it for 10^7 time steps with x 0 10 and p 0.55 . Exercise 4.2 👉 Calculate and plot the probability distribution of the walker's height. \"\"\" md\"\"\" 👉 What does the resulting figure look like? What form do you think this distribution has? Verify your hypothesis by plotting the distribution using different scales. You can increase the number of time steps to make the results look nicer. \"\"\" md\"\"\" Exercise 4.3 👉 Make an interactive visualization of how the distribution develops over time. What happens for longer and longer times? \"\"\" md\"\"\" 👉 Use wikipedia to find a formula for the barometric pressure at a given altitude. Does this result match your expectations? \"\"\" md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" Exercise 1.1 👉 Write a function `counts` that accepts a vector `data` and calculates the number of times each value in `data` occurs. The input will be an array of integers, with duplicates , and the result will be a dictionary that maps each occurred value to its count in the data. For example, ```julia counts 7, 8, 9, 7 ``` should give ```julia Dict 7 2, 8 1, 9 1, ``` To do so, use a dictionary called `counts`. We can create a local variable with the same name as the function. hint md\"Do you remember how we worked with dictionaries in Homework 3? You can create an empty dictionary using `Dict `. You may want to use either the function `haskey` or the function `get` on your dictionary check the documentation for how to use these functions. \" The function should return the dictionary. \"\"\" hint md\"Remember to always re use work you have done previously in this case you should re use the function `bernoulli`.\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next question.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined counts not defined counts else let result counts 51, 52, 52,53,51 if ismissing result still missing elseif result isa Dict keep working md\"Make sure that `counts` returns a Dictionary.\" elseif result Dict 51 2, 52 2, 53 1, correct else keep working end end end if isdefined test counts not defined test counts else if test counts Dict 0 2, 1000 2, 1 4 keep working else correct end end if isdefined counts2 not defined counts2 else let result counts2 51, 52, 52,53,51 if ismissing result still missing elseif result isa Tuple AbstractVector, AbstractVector keep working md\"Make sure that `counts2` returns a Tuple of two vectors.\" elseif result 52, 51, 53 , 2, 2, 1 , correct else keep working end end end if isdefined probability distribution not defined probability distribution else let result probability distribution 51, 52, 52,53,51 if ismissing result still missing elseif result isa Tuple AbstractVector, AbstractVector keep working md\"Make sure that `counts2` returns a Tuple of two vectors.\" elseif result 1 52, 51, 53 && isapprox result 2 , 2, 2, 1 . 5 correct else keep working end end end if isdefined bernoulli not defined bernoulli else let result bernoulli 0.5 if result isa Missing still missing elseif result isa Bool keep working md\"Make sure that you return either `true` or `false`.\" else if bernoulli 0.0 false && bernoulli 1.0 true correct else keep working end end end end if isdefined geometric not defined geometric else let result geometric 1.0 if result isa Missing still missing elseif result isa Int keep working md\"Make sure that you return an integer the recovery time.\" else if result 1 samples geometric 0.2 for in 1 256 a, b extrema samples if a 1 && b 20 correct else keep working end else keep working md\"`p 1.0` should return `1` the agent recovers after the first time step.\" end end end end if isdefined cumulative sum not defined cumulative sum else let result cumulative sum 1,2,3,4 if result isa Missing still missing elseif result isa AbstractVector keep working md\"Make sure that you return an Array the cumulative sum \" elseif length result 4 keep working md\"You should return an array of the same size a `xs`.\" else if isapprox result, 1, 3, 6, 10 correct else keep working end end end end if isdefined geometric bin not defined geometric bin else let result1 geometric bin 0.1, 0.1 result2 geometric bin 0.9, 0.1 if result1 isa Missing still missing elseif result1 isa Real keep working md\"Make sure that you return a number.\" elseif all isinteger, result1, result2 if result1 21 || result2 21 || result1 22 || result2 22 correct else keep working end else keep working md\"You should use the `floor` function to return an integer.\" end end end todo text HTML \"\"\" div style \"background rgb 220, 200, 255 padding 2em border radius 1em \" h1 TODO h1 repr MIME\"text html\" , text div \"\"\" bigbreak html\" br br br br br \" bigbreak bigbreak bigbreak bigbreak bigbreak bigbreak bigbreak begin fruits \"🍒\", \"🍐\", \"🍋\" length fruits end begin fruits \"🍉\" length fruits end "},{"url":"homework/hw7/","title":"Epidemic modeling I","tags":["homework","module2","epidemiology","track_data","monte carlo","statistics","track_math","ODE","agent based model","differential equation","type","structure","plotting"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 section 7.5 order 7.5 homework number 7 title \"Epidemic modeling I\" layout \"layout.jlhtml\" tags \"homework\", \"module2\", \"epidemiology\", \"track data\", \"monte carlo\", \"statistics\", \"track math\", \"ODE\", \"agent based model\", \"differential equation\", \"type\", \"structure\", \"plotting\" description \"Simulate the spread of an epidemic by creating your own agent based model from scratch, and find statistics using the Monte Carlo method.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI md\" homework 7, version 3 \" md\"\"\" Homework 7 Epidemic modeling `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" function bernoulli p Number rand p end md\"\"\" Exercise 1 Agent based model for an epidemic outbreak types In this and the following exercises we will develop a simple stochastic model for combined infection and recovery in a population, which may exhibit an epidemic outbreak i.e. a large spike in the number of infectious people . The population is well mixed , i.e. everyone is in contact with everyone else. An example of this would be a small school or university in which people are constantly moving around and interacting with each other. The model is an individual based or agent based model we explicitly keep track of each individual, or agent , in the population and their infection status. For the moment we will not keep track of their position in space we will just assume that there is some mechanism, not included in the model, by which they interact with other individuals. Exercise 1.1 Each agent will have its own internal state , modelling its infection status, namely \"susceptible\", \"infectious\" or \"recovered\". We would like to code these as values `S`, `I` and `R`, respectively. One way to do this is using an enumerated type https en.wikipedia.org wiki Enumerated type or enum . Variables of this type can take only a pre defined set of values the Julia syntax is as follows \"\"\" enum InfectionStatus S I R md\"\"\" We have just defined a new type `InfectionStatus`, as well as names `S`, `I` and `R` that are the only possible values that a variable of this type can take. 👉 Define a variable `test status` whose value is `S`. \"\"\" test status missing md\"\"\" 👉 Use the `typeof` function to find the type of `test status`. \"\"\" md\"\"\" 👉 Convert `x` to an integer using the `Integer` function. What value does it have? What values do `I` and `R` have? \"\"\" md\"\"\" Exercise 1.2 For each agent we want to keep track of its infection status and the number of other agents that it infects during the simulation. A good solution for this is to define a new type `Agent` to hold all of the information for one agent, as follows \"\"\" mutable struct Agent status InfectionStatus num infected Int64 end md\"\"\" When you define a new type like this, Julia automatically defines one or more constructors , which are methods of a generic function with the same name as the type. These are used to create objects of that type. 👉 Use the `methods` function to check how many constructors are pre defined for the `Agent` type. \"\"\" md\"\"\" 👉 Create an agent `test agent` with status `S` and `num infected` equal to 0. \"\"\" test agent missing md\"\"\" 👉 For convenience, define a new constructor i.e. a new method for the function that takes no arguments and creates an `Agent` with status `S` and number infected 0, by calling one of the default constructors that Julia creates. This new method lives outside not inside the definition of the `struct`. It is called an outer constructor . In Pluto, a struct definition and an outer constructor need to be combined in a single cell using a `begin end` block. Let's check that the new method works correctly. How many methods does the constructor have now? \"\"\" Agent md\"\"\" Exercise 1.3 👉 Write functions `set status a ` and `set num infected a ` which modify the respective fields of an `Agent`. Check that they work. Note the bang \"` `\" at the end of the function names to signify that these functions modify their argument. \"\"\" function set status agent Agent, new status InfectionStatus your code here end md\"\"\" 👉 We will also need functions `is susceptible` and `is infected` that check if a given agent is in those respective states. \"\"\" function is susceptible agent Agent return missing end function is infected agent Agent return missing end md\"\"\" Exercise 1.4 👉 Write a function `generate agents N ` that returns a vector of `N` freshly created `Agent`s. They should all be initially susceptible, except one, chosen at random i.e. uniformly , who is infectious. \"\"\" function generate agents N Integer return missing end generate agents 3 md\"\"\" We will also need types representing different infections. Let's define an immutable `struct` called `InfectionRecovery` with parameters `p infection` and `p recovery`. We will make it a subtype of an abstract `AbstractInfection` type, because we will define more infection types later. \"\"\" abstract type AbstractInfection end struct InfectionRecovery AbstractInfection p infection p recovery end md\"\"\" Exercise 1.5 👉 Write a function `interact ` that takes an affected `agent` of type `Agent`, an `source` of type `Agent` and an `infection` of type `InfectionRecovery`. It implements a single one sided interaction between two agents If the `agent` is susceptible and the `source` is infectious, then the `source` infects our `agent` with the given infection probability. If the `source` successfully infects the other agent, then its `num infected` record must be updated. If the `agent` is infected then it recovers with the relevant probability. Otherwise, nothing happens. html\" span id interactfunction span \" \"\"\" function interact agent Agent, source Agent, infection InfectionRecovery your code here end md\"\"\" Play around with the test case below to test your function Try changing the definitions of `agent`, `source` and `infection`. Since we are working with randomness, you might want to run the cell multiple times. \"\"\" let agent Agent S, 0 source Agent I, 0 infection InfectionRecovery 0.9, 0.5 interact agent, source, infection agent agent, source source end md\"\"\" Exercise 2 Agent based model for an epidemic outbreak Monte Carlo simulation In this exercise we will build on Exercise 2 to write a Monte Carlo simulation of how an infection propagates in a population. Make sure to re use the functions that we have already written, and introduce new ones if they are helpful Short functions make it easier to understand what the function does and build up new functionality piece by piece. You should not use any global variables inside the functions Each function must accept as arguments all the information it requires to carry out its task. You need to think carefully about what the information each function requires. Exercise 2.1 👉 Write a function `step ` that takes a vector of `Agent`s and an `infection` of type `InfectionRecovery`. It implements a single step of the infection dynamics as follows Choose two random agents an `agent` and a `source`. Apply `interact agent, source, infection `. Return `agents`. \"\"\" function step agents Vector Agent , infection InfectionRecovery end md\"\"\" 👉 Write a function `sweep `. It runs `step ` N times, where N is the number of agents. Thus each agent acts, on average, once per sweep a sweep is thus the unit of time in our Monte Carlo simulation. \"\"\" function sweep agents Vector Agent , infection AbstractInfection end md\"\"\" 👉 Write a function `simulation` that does the following 1. Generate the N agents. 2. Run `sweep ` a number T of times. Calculate and store the total number of agents with each status at each step in variables `S counts`, `I counts` and `R counts`. 3. Return the vectors `S counts`, `I counts` and `R counts` in a named tuple , with keys `S`, `I` and `R`. You've seen an example of named tuples before the `student` variable at the top of the notebook Feel free to store the counts in a different way, as long as the return type is the same. \"\"\" function simulation N Integer, T Integer, infection AbstractInfection return S missing, I missing, R missing end simulation 3, 20, InfectionRecovery 0.9, 0.2 simulation 100, 1000, InfectionRecovery 0.005, 0.2 bind run basic sir Button \"Run simulation again \" let run basic sir N 100 T 1000 sim simulation N, T, InfectionRecovery 0.02, 0.002 result plot 1 T, sim.S, ylim 0, N , label \"Susceptible\" plot result, 1 T, sim.I, ylim 0, N , label \"Infectious\" plot result, 1 T, sim.R, ylim 0, N , label \"Recovered\" end md\"\"\" Exercise 2.2 Alright Every time that we run the simulation, we get slightly different results, because it is based on randomness. By running the simulation a number of times, you start to get an idea of the mean behaviour of our model. This is the essence of a Monte Carlo method You use computer generated randomness to generate samples. Instead of pressing the button many times, let's have the computer repeat the simulation. In the next cells, we run your simulation `num simulations 20` times with N 100 , p \\text infection 0.02 , p \\text infection 0.002 and T 1000 . Every single simulation returns a named tuple with the status counts, so the result of multiple simulations will be an array of those. Have a look inside the result, `simulations`, and make sure that its structure is clear. \"\"\" function repeat simulations N, T, infection, num simulations N 100 T 1000 map 1 num simulations do simulation N, T, infection end end simulations repeat simulations 100, 1000, InfectionRecovery 0.02, 0.002 , 20 md\"\"\" In the cell below, we plot the evolution of the number of I individuals as a function of time for each of the simulations on the same plot using transparency `alpha 0.5` inside the plot command . \"\"\" let p plot for sim in simulations plot p, 1 1000, sim.I, alpha .5, label nothing end p end md\"\"\" 👉 Write a function `sir mean plot` that returns a plot of the means of S , I and R as a function of time on a single graph. \"\"\" function sir mean plot simulations Vector NamedTuple you might need T for this function, here's a trick to get it T length first simulations .S return missing end sir mean plot simulations let T length first simulations .S all S counts map result result.S, simulations all I counts map result result.I, simulations all R counts map result result.R, simulations S round. sum all S counts . length simulations . 100, digits 4 , I round. sum all I counts . length simulations . 100, digits 4 , R round. sum all R counts . length simulations . 100, digits 4 end md\"\"\" 👉 Allow p \\text infection and p \\text recovery to be changed interactively and find parameter values for which you observe an epidemic outbreak. \"\"\" md\"\"\" 👉 Write a function `sir mean error plot` that does the same as `sir mean plot`, which also computes the standard deviation \\sigma of S , I , R at each step. Add this to the plot using error bars , using the option `yerr σ` in the plot command use transparency. This should confirm that the distribution of I at each step is pretty wide \"\"\" function sir mean error plot simulations Vector NamedTuple you might need T for this function, here's a trick to get it T length first simulations .S return missing end md\"\"\" Exercise 2.3 👉 Plot the probability distribution of `num infected`. Does it have a recognisable shape? Feel free to increase the number of agents in order to get better statistics. \"\"\" md\"\"\" Exercse 2.4 👉 What are three simple ways in which you could characterise the magnitude size of the epidemic outbreak? Find approximate values of these quantities for one of the runs of your simulation. \"\"\" md\"\"\" Exercise 3 Reinfection In this exercise we will re use our simulation infrastructure to study the dynamics of a different type of infection there is no immunity, and hence no \"recovery\" rather, susceptible individuals may now be re infected Exercise 3.1 👉 Make a new infection type `Reinfection`. This has the same two fields as `InfectionRecovery` `p infection` and `p recovery` . However, \"recovery\" now means \"becomes susceptible again\", instead of \"moves to the `R` class. This new type `Reinfection` should also be a subtype of `AbstractInfection`. This allows us to reuse our previous functions, which are defined for the abstract supertype. \"\"\" md\"\"\" 👉 Make a new method for the `interact ` function that accepts the new infection type as argument, reusing as much functionality as possible from the previous version. \"\"\" md\"\"\" Exercise 3.2 👉 Run the simulation 20 times and plot I as a function of time for each one, together with the mean over the 20 simulations as you did in the previous exercises . Note that you should be able to re use the `sweep ` and `simulation` functions , since those should be sufficiently generic to work with the new `step ` function Modify them if they are not. \"\"\" md\"\"\" 👉 Run the new simulation and draw I averaged over runs as a function of time. Is the behaviour qualitatively the same or different? Describe what you see. \"\"\" md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" 👉 Calculate the mean number of infectious agents of our simulations for each time step. Add it to the plot using a heavier line `lw 3` for \"linewidth\" by modifying the cell above. Check the answer yourself does your curve follow the average trend? hint md\"This exercise requires some creative juggling with arrays, anonymous functions, `map`s, or whatever you see fit \" \"\"\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined set status not defined set status else let agent Agent I,2 set status agent, R if agent.status R correct else keep working end end end if isdefined is susceptible not defined is susceptible else let result1 is susceptible Agent I,2 result2 is infected Agent I,2 if result1 isa Missing || result2 isa Missing still missing elseif result1 isa Bool || result2 isa Bool keep working md\"Make sure that you return either `true` or `false`.\" elseif result1 false && result2 true if is susceptible Agent S,3 && is infected Agent R,9 correct else keep working end else keep working end end end if isdefined generate agents not defined generate agents else let result generate agents 4 if result isa Missing still missing elseif result isa Nothing keep working \"The function returned `nothing`. Did you forget to return something?\" elseif result isa Vector || all x x isa Agent, result keep working md\"Make sure that you return an array of objects of the type `Agent`.\" elseif length result 4 almost md\"Make sure that you return `N` agents.\" elseif length Set result 4 almost md\"You returned the same agent `N` times. You need to call the `Agent` constructor `N` times, not once.\" else if sum a a.status I, result 1 almost md\"Exactly one of the agents should be infectious.\" else correct end end end end if isdefined interact not defined interact else let agent Agent S, 9 source Agent I, 0 interact agent, source, InfectionRecovery 0.0, 1.0 if source.status I || source.num infected 0 keep working md\"The `source` should not be modified if no infection occured.\" elseif agent.status S keep working md\"The `agent` should get infected with the right probability.\" else agent Agent S, 9 source Agent S, 0 interact agent, source, InfectionRecovery 1.0, 1.0 if source.status S || source.num infected 0 || agent.status S keep working md\"The `agent` should get infected with the right probability if the source is infectious.\" else agent Agent S, 9 source Agent I, 3 interact agent, source, InfectionRecovery 1.0, 1.0 if agent.status R almost md\"The agent should not recover immediately after becoming infectious.\" elseif agent.status S keep working md\"The `agent` should recover from an infectious state with the right probability.\" elseif source.status I || source.num infected 4 almost md\"The `source` did not get updated correctly after infecting the `agent`.\" else correct md\"Your function treats the susceptible agent case correctly \" end end end end end if isdefined interact not defined interact else let agent Agent I, 9 source Agent S, 0 interact agent, source, InfectionRecovery 1.0, 1.0 if source.status S || source.num infected 0 keep working md\"The `source` should not be modified if `agent` is infectious.\" elseif agent.status R keep working md\"The `agent` should recover from an infectious state with the right probability.\" elseif agent.num infected 9 keep working md\"`agent.num infected` should not be modified if `agent` is infectious.\" else let agent Agent I, 9 source Agent R, 0 interact agent, source, InfectionRecovery 1.0, 0.0 if agent.status R keep working md\"The `agent` should recover from an infectious state with the right probability.\" else correct md\"Your function treats the infectious agent case correctly \" end end end end end if isdefined interact not defined interact else let agent Agent R, 9 source Agent I, 0 interact agent, source, InfectionRecovery 1.0, 1.0 if source.status I || source.num infected 0 keep working md\"The `source` should not be modified if no infection occured.\" elseif agent.status R || agent.num infected 9 keep working md\"The `agent` should not be momdified if it is in a recoved state.\" else correct md\"Your function treats the recovered agent case correctly \" end end end bigbreak html\" br br br br br \" bigbreak bigbreak "},{"url":"homework/hw8/","title":"Epidemic modeling II","tags":["homework","module2","track_math","track_data","optimization","statistics","gradient","differentiation","automatic differentiation","continuous","probability","epidemiology","monte carlo","modeling","plotting"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 2 section 9.5 order 9.5 homework number 8 title \"Epidemic modeling II\" layout \"layout.jlhtml\" tags \"homework\", \"module2\", \"track math\", \"track data\", \"optimization\", \"statistics\", \"gradient\", \"differentiation\", \"automatic differentiation\", \"continuous\", \"probability\", \"epidemiology\", \"monte carlo\", \"modeling\", \"plotting\" description \"Learn about optimisation and gradient descent with help from our visuals and automatic checks. We use these new skill to fit parameters of an epidemic model to match reality.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI md\" homework 8, version 3 \" md\"\"\" Homework 8 Epidemic modeling II `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" In the past couple homeworks, we've messed around with probability distributions and Monte Carlo simulations. This time, we'll build a more mathematical model, and we'll use optimization to fit it to given data. Models have parameters , such as the rate of recovery from infection. Where do the parameter values come from? Ideally we would like to extract them from data. The goal of this homework is to do this by fitting a model to data. For simplicity, we will use data that generated from a spatial model one that simulates the movement of a population in space instead of real world data, and we will fit the simplest SIR model. But the same ideas apply more generally. There are many ways to fit a function to data, but all must involve some form of optimization , usually minimization of a particular function, a loss function this is the basis of the vast field of machine learning . The loss function is a function of the model parameters it measures how far the model output is from the data, for the given values of the parameters. We emphasise that this material is pedagogical there is no suggestion that these specific techniques should be used actual calculations rather, it is the underlying ideas that are important. \"\"\" md\"\"\" Exercise 1 Calculus without calculus \"\"\" md\"\"\" Before we jump in to simulating the SIR equations, let's experiment with a simple 1D function. In calculus, we learn techniques for differentiating and integrating symbolic equations, e.g. ``\\frac d dx x^n nx^ n 1 ``. But in real applications, it is often impossible to apply these techniques, either because the problem is too complicated to solve symbolically, or because our problem has no symbolic expression, like when working with experimental results. Instead, as you will recall from lecture, we use ✨ computers ✨ to approximate derivatives and integrals. Instead of applying rules to symbolic expressions, we use much simpler strategies that only use the output values of our function . One such example is the finite difference method for approximating the derivative of a function. It is inspired by the analytical definition of the derivative f' a \\lim h \\rightarrow 0 \\frac f a h f a h . The finite difference method simply fixes a small value for h , say h 10^ 3 , and then approximates the derivative as f' a \\simeq \\frac f a h f a h . \"\"\" md\"\"\" Exercise 1.1 tangent line 👉 Write a function `finite difference slope` that takes a function `f` and numbers `a` and `h`. It returns the slope ``f' a ``, approximated using the finite difference formula above. \"\"\" function finite difference slope f Function, a, h 1e 3 return missing end finite difference slope sqrt, 4.0, 5.0 md\"\"\" 👉 Write a function `tangent line` that takes the same arguments `f`, `a` and `g`, but it returns a function . This function ``\\mathbb R \\rightarrow \\mathbb R `` is the tangent line with slope ``f' a `` computed using `finite difference slope` that passes through `` a, f a ``. \"\"\" function tangent line f, a, h return missing end this is our test function wavy x .1x^3 1.6x^2 7x 3 md\"\"\" The slider below controls ``h`` using a log scale . In the mathematical definition of the derivative, we take ``\\lim h \\rightarrow 0 ``. This corresponds to moving the slider to the left. Notice that, as you decrease ``h``, the tangent line gets more accurate, but what happens if you make ``h`` too small? \"\"\" bind log h Slider 16 0.01 .5, default .5 h finite diff 10.0^log h zeroten LinRange 0.0, 10.0, 300 bind a finite diff Slider zeroten, default 4 let p plot zeroten, wavy, label \"f x \" scatter p, a finite diff , wavy a finite diff , label \"a\", color \"red\" vline p, a finite diff , label nothing, color \"red\", linestyle dash scatter p, a finite diff h finite diff , wavy a finite diff h finite diff , label \"a h\", color \"green\" try result tangent line wavy, a finite diff, h finite diff plot p, zeroten, result, label \"tangent\", color \"purple\" catch end plot p, xlim 0, 10 , ylim 2, 8 end | as svg md\"\"\" html\" span id theslopeequation span \" Exercise 1.2 antiderivative In the finite differences method, we approximated the derivative of a function f' a \\simeq \\frac f a h f a h We can do something very similar to approximate the 'antiderivate' of a function. Finding the antiderivative means that we use the slope ``f'`` to compute ``f`` numerically This antiderivative problem is illustrated below. The only information that we have is the slope at any point ``a \\in \\mathbb R ``, and we have one initial value , ``f 1 ``. \"\"\" in this exercise, only the derivative is given wavy deriv x .3x^2 3.2x 7 bind a euler Slider zeroten, default 1 let slope wavy deriv a euler p plot LinRange 1.0 0.1, 1.0 0.1, 2 , wavy, label nothing, lw 3 scatter p, 1 , wavy, label \"f 1 \", color \"blue\", lw 3 p plot x a euler 0.2,a euler 0.2 for y in 4 10 plot p, x, slope . x . a euler . y, label nothing, color \"purple\", opacity .6 end vline p, a euler , color \"red\", label \"a\", linestyle dash plot p, xlim 0, 10 , ylim 2, 8 end | as svg md\"\"\" Using only this information, we want to reconstruct ``f``. By rearranging the equation above theslopeequation , we get the Euler method f a h \\simeq hf' a f a Using this formula, we only need to know the value ``f a `` and the slope ``f' a `` of a function at ``a`` to get the value at ``a h``. Doing this repeatedly can give us the value at ``a 2h``, at ``a 3h``, etc., all from one initial value ``f a ``. 👉 Write a function `euler integrate step` that applies this formula to a known function ``f'`` at ``a``, with step size ``h`` and the initial value ``f a ``. It returns the next value, ``f a h ``. \"\"\" function euler integrate step fprime Function, fa Number, a Number, h Number return missing end md\"\"\" 👉 Write a function `euler integrate` that takes takes a known function ``f'``, the initial value ``f a `` and a range `T` with `a first T ` and `h step T `. It applies the function `euler integrate step` repeatedly, once per entry in `T`, to produce the sequence of values ``f a h ``, ``f a 2h ``, etc. \"\"\" function euler integrate fprime Function, fa Number, T AbstractRange a0 T 1 h step T return missing end md\"\"\" Let's try it out on ``f' x 3x^2`` and `T` ranging from ``0`` to ``10``. We already know the analytical solution ``f x x^3``, so the result should be an array going from approximately `0.0` to `1000.0`. \"\"\" euler test let fprime x 3x^2 T 0 0.1 10 euler integrate fprime, 0, T end bind N euler Slider 2 40 let a 1 h .3 history euler integrate wavy deriv, wavy a , range a step h, length N euler slope wavy deriv a euler p plot zeroten, wavy, label \"exact solution\", lw 3, opacity .1, color \"gray\" p plot last a a N euler 1 h vline p, last a , color \"red\", label \"a\", linestyle dash try plot p, a . h . 1 N euler , history, color \"blue\", label nothing scatter p, a . h . 1 N euler , history, color \"blue\", label \"appromixation\", markersize 2, markerstrokewidth 0 plot p, 0,10 , 0,10 . last a h . wavy deriv last a h . history end , label \"tangent\", color \"purple\" catch end plot p, xlim 0, 10 , ylim 2, 8 end | as svg md\"\"\" You see that our numerical antiderivate is not very accurate, but we can get a smaller error by choosing a smaller step size. Try it out There are also alternative integration methods that are more accurate with the same step size. Some methods also use the second derivative, other methods use multiple steps at once, etc. This is the study of Numerical Methods. \"\"\" md\"\"\" Exercise 2 Simulating the SIR differential equations We will look at a type of mathematical model for epidemic outbreaks called the SIR model . In this model, we consider three functions of time t the fraction of the population s t that is susceptible to the disease, the fraction i t that is infected , and the fraction r t that has recovered . We've already used these three quantities in a simulation before, namely in the Monte Carlo simulation from last homework It turns out that, given some assumptions about how people from the three groups interact over time, we can model the behavior of the three quantities in terms of their derivatives with respect to time t . We use two parameters \\beta and \\gamma , and with them, we can form a system of ordinary differential equations ODEs for the SIR model. They are as follows \\begin align \\dot s & \\beta s \\, i \\\\ \\dot i & \\beta s \\, i \\gamma i \\\\ \\dot r & \\gamma i \\end align where ``\\dot s \\frac ds dt `` is the derivative of s with respect to time. Recall that s denotes the proportion fraction of the population that is susceptible, a number between 0 and 1 . We will use the simplest possible method to simulate these, namely the Euler method . The Euler method is not always a good method to solve ODEs accurately, but for our purposes it is good enough. In the previous exercise, we introduced the Euler method for a 1D function, which you can see as an ODE that only depends on time. For the SIR equations, we have an ODE that only depends on the previous value , not on time, and we have 3 equations instead of 1. The solution is quite simple, we apply the Euler method to each of the differential equations within a single time step to get new values for each of s , i and r at the end of the time step in terms of the values at the start of the time step. The Euler discretised equations are \\begin align s t h & s t h\\,\\cdot\\beta s t \\, i t \\\\ i t h & i t h\\,\\cdot \\beta s t \\, i t \\gamma i t \\\\ r t h & r t h\\,\\cdot \\gamma i t \\end align 👉 Implement a function `euler SIR step β, γ, sir 0, h ` that performs a single Euler step for these equations with the given parameter values and initial values, with a step size h . `sir 0` is a 3 element vector, and you should return a new 3 element vector with the values after the timestep. \"\"\" function euler SIR step β, γ, sir 0 Vector, h Number s, i, r sir 0 return missing, missing, missing, end euler SIR step 0.1, 0.05, 0.99, 0.01, 0.00 , 0.1 md\"\"\" 👉 Implement a function `euler SIR β, γ, sir 0, T ` that applies the previously defined function over a time range T . You should return a vector of vectors a 3 element vector for each point in time. \"\"\" function euler SIR β, γ, sir 0 Vector, T AbstractRange T is a range, you get the step size and number of steps like so h step T num steps length T return missing end sir T 0 0.1 60.0 sir results euler SIR 0.3, 0.15, 0.99, 0.01, 0.00 , sir T md\"\"\" Let's plot s , i and r as a function of time. \"\"\" function plot sir p, T, results label \"\", kwargs... s getindex. results, 1 i getindex. results, 2 r getindex. results, 3 plot p, T, s color 1, label label \" S\", lw 3, kwargs... plot p, T, i color 2, label label \" I\", lw 3, kwargs... plot p, T, r color 3, label label \" R\", lw 3, kwargs... p end plot sir plot , sir T, sir results md\"\"\" 👉 Do you see an epidemic outbreak i.e. a rapid growth in number of infected individuals, followed by a decline ? What happens after a long time? Does everybody get infected? \"\"\" default SIR parameters observation md\"\"\" your answer here \"\"\" md\"\"\" 👉 Make an interactive visualization, similar to the above plot, in which you vary \\beta and \\gamma via sliders. What relation should \\beta and \\gamma have for an epidemic outbreak to occur? \"\"\" md\"\"\" Exercise 3 Numerical gradient For fitting we need optimization, and for optimization we will use derivatives rates of change . In Exercise 1, we wrote a function `finite difference slope f, a ` to approximate ``f' a ``. In this exercise we will write a function to compute partial derivatives . \"\"\" md\"\"\" Exercise 3.1 👉 Write functions `∂x f, a, b ` and `∂y f, a, b ` that calculate the partial derivatives \\frac \\partial f \\partial x and \\frac \\partial f \\partial y at a, b of a function f \\mathbb R ^2 \\to \\mathbb R i.e. a function that takes two real numbers and returns one real . Recall that \\frac \\partial f \\partial x is the derivative of the single variable function g x f x, b obtained by fixing the value of y to b . You should use anonymous functions for this. These have the form `x x^2`, meaning \"the function that sends x to x^2 \". \"\"\" function ∂x f Function, a, b return missing end ∂x x, y 7x^2 y, 3, 7 function ∂y f Function, a, b return missing end ∂y x, y 7x^2 y, 3, 7 md\"\"\" Exercise 3.2 👉 Write a function `gradient f, a, b ` that calculates the gradient of a function f at the point a, b , given by the vector \\nabla f a, b \\frac \\partial f \\partial x a, b , \\frac \\partial f \\partial y a, b . \"\"\" function gradient f Function, a, b return missing end gradient x, y 7x^2 y, 3, 7 md\"\"\" Exercise 4 Minimisation using gradient descent In this exercise we will use gradient descent to find local minima of smooth enough functions, revisiting what we saw on the lectures. As a refresher, we'll want to think of a function as a hill. To find a minimum we should \"roll down the hill\". Exercise 4.1 We want to minimize a 1D function, i.e. a function f \\mathbb R \\to \\mathbb R . To do so we notice that the derivative tells us the direction in which the function increases . Positive slope means that the minimum is to the left, negative slope means to the right. So our gradient descent method is to take steps in the opposite direction, of a small size \\eta \\cdot f' x 0 . 👉 Write a function `gradient descent 1d step f, x0 ` that performs a single gradient descent step, from the point `x0` and using your function `finite difference slope` to approximate the derivative. The result should be the next guess for ``x``. \"\"\" function gradient descent 1d step f, x0 η 0.01 return missing end let f x x^2 the minimum is at 0, so we should take a small step to the left gradient descent 1d step f, 5 end bind N gradient 1d Slider 0 20 md\" ``x 0 `` bind x0 gradient 1d Slider 3 .01 1.5, default 1, show value true \" function gradient 1d viz N gradient 1d, x0 f x x^4 3x^3 3x 5. x LinRange 3, 1.5, 200 history accumulate 1 N gradient 1d, init x0 do old, gradient descent 1d step f, old, η .025 end all x0, history... slope wavy deriv a euler p plot x, f, label \"f x \", lw 3, opacity .6, color \"gray\" p plot plot p, all, f, color \"blue\", opacity range .5,step .2,length length all , label nothing scatter p, all, f, color \"blue\", label \"gradient descent\", markersize 3, markerstrokewidth 0 as svg p end gradient 1d viz N gradient 1d, x0 gradient 1d md\"\"\" 👉 Write a function `gradient descent 1d f, x0 ` that repeatedly applies the previous function `N steps` times , starting from the point `x0`, like in the vizualisation above. The result should be the final guess for ``x``. \"\"\" function gradient descent 1d f, x0 η 0.01, N steps 1000 return missing end let f x x 5 ^2 3 minimum should be at x 5 gradient descent 1d f, 0.0 end md\"\"\" Right now we take a fixed number of steps, even if the minimum is found quickly. What would be a better way to decide when to end the function? \"\"\" better stopping idea md\"\"\" your answer here \"\"\" md\"\"\" Exericse 4.2 Multivariable calculus tells us that the gradient \\nabla f a, b at a point a, b is the direction in which the function increases the fastest. So again we should take a small step in the opposite direction. Note that the gradient is a vector which tells us which direction to move in the plane a, b . We multiply this vector with the scalar ``\\eta`` to control the step size. 👉 Write functions `gradient descent 2d step f, x0, y0 ` and `gradient descent 2d f, x0, y0 ` that do the same for functions f x, y of two variables. \"\"\" function gradient descent 2d step f, x0, y0 η 0.01 return missing end function gradient descent 2d f, x0, y0 η 0.01 return missing end bind N gradient 2d Slider 0 20 md\" ``x 0 `` bind x0 gradient 2d Slider 4 .01 4, default 0, show value true \" md\" ``y 0 `` bind y0 gradient 2d Slider 4 .01 4, default 0, show value true \" himmelbau x, y x^2 y 11 ^2 x y^2 7 ^2 gradient descent 2d himmelbau, 0, 0 md\"\"\" We also prepared a 3D visualisation if you like It's a bit slow... \"\"\" run 3d visualisation false function gradient 2d viz 3d N gradient 2d, x0, y0 history accumulate 1 N gradient 2d, init x0, y0 do old, gradient descent 2d step himmelbau, old... end all x0, y0 , history... p surface 4 0.4 5, 4 0.4 4, himmelbau trace himmelbau s... for s in all plot p, first. all , last. all , trace, color \"blue\", opacity range .5,step .2,length length all , label nothing scatter p, first. all , last. all , trace, color \"blue\", label \"gradient descent\", markersize 3, markerstrokewidth 0 as svg p end if run 3d visualisation let we temporarily change the plotting backend to an interactive one plotly we dont use the sliders because this plot is quite slow x0 0.5 N 20 y0 3 p gradient 2d viz 3d N, x0, y0 gr p end end function gradient 2d viz 2d N gradient 2d, x0, y0 history accumulate 1 N gradient 2d, init x0, y0 do old, gradient descent 2d step himmelbau, old... end all x0, y0 , history... p heatmap 4 0.4 5, 4 0.4 4, himmelbau plot p, first. all , last. all , color \"blue\", opacity range .5,step .2,length length all , label nothing scatter p, first. all , last. all , color \"blue\", label \"gradient descent\", markersize 3, markerstrokewidth 0 as svg p end gradient 2d viz 2d N gradient 2d, x0 gradient 2d, y0 gradient 2d md\"\"\" 👉 Can you find different minima? \"\"\" md\"\"\" Exercise 5 Learning parameter values In this exercise we will apply gradient descent to fit a simple function y f \\alpha, \\beta x to some data given as pairs x i, y i . Here \\alpha and \\beta are parameters that appear in the form of the function f . We want to find the parameters that provide the best fit , i.e. the version f \\alpha, \\beta of the function that is closest to the data when we vary \\alpha and \\beta . To do so we need to define what \"best\" means. We will define a measure of the distance between the function and the data, given by a loss function , which itself depends on the values of \\alpha and \\beta . Then we will minimize the loss function over \\alpha and \\beta to find those values that minimize this distance, and hence are \"best\" in this precise sense. The iterative procedure by which we gradually adjust the parameter values to improve the loss function is often called machine learning or just learning , since the computer is \"discovering\" information in a gradual way, which is supposed to remind us of how humans learn. Hint This is not how humans learn. Exercise 5.1 🎲 frequencies We generate a small dataset by throwing 10 dice, and counting the sum. We repeat this experiment many times, giving us a frequency distribution in a familiar shape. \"\"\" import Statistics function dice frequencies N dice, N experiments experiment let sum of rolls sum rand 1 6, N dice end results experiment for in 1 N experiments x N dice N dice 6 y map x do total sum isequal total , results end . N experiments x, y end dice x, dice y dice frequencies 10, 20 000 md\"\"\" Let's try to fit a gaussian normal distribution. Its PDF with mean \\mu and standard deviation \\sigma is f \\mu, \\sigma x \\frac 1 \\sigma \\sqrt 2 \\pi \\exp \\left \\frac x \\mu ^2 2 \\sigma^2 \\right 👉 Not graded Manually fit a Gaussian distribution to our data by adjusting ``\\mu`` and ``\\sigma`` until you find a good fit. \"\"\" md\"μ bind guess μ Slider 1 0.1 last dice x default last dice x 0.4, show value true \" md\"σ bind guess σ Slider 0.1 0.1 last dice x 2 default 12, show value true \" md\"Show manual fit bind show manual fit CheckBox \" function gauss x, μ, σ 1 sqrt 2π σ exp x μ ^2 σ^2 2 end md\"\"\" What we just did was adjusting the function parameters until we found the best possible fit. Let's automate this process To do so, we need to quantify how good or bad a fit is. 👉 Define a loss function to measure the \"distance\" between the actual data and the function. It will depend on the values of \\mu and \\sigma that you choose \\mathcal L \\mu, \\sigma \\sum i f \\mu, \\sigma x i y i ^2 \"\"\" function loss dice μ, σ return missing end loss dice guess μ 3, guess σ loss dice guess μ, guess σ md\"\"\" 👉 Use your `gradient descent 2d` function to find a local minimum of \\mathcal L , starting with initial values \\mu 30 and \\sigma 1 . Call the found parameters `found μ` and `found σ`. \"\"\" found μ, found σ let your code here missing, missing end let p plot dice x, dice y, size 600, 200 , label \"data\" if show manual fit plot p, dice x, gauss. dice x, guess μ , guess σ , label \"manual fit\" end try plot p, dice x, gauss. dice x, found μ , found σ , label \"optimized fit\" catch end p end md\"\"\" Go back to the graph to see your optimized gaussian curve If your fit is close, then probability theory tells us that the found parameter ``\\mu`` should be close to the weighted mean of our data, and ``\\sigma`` should approximate the sample standard deviation . We have already computed these values, and we check how close they are \"\"\" stats μ sum dice x . dice y abs stats μ found μ stats σ sqrt sum dice x.^2 . dice y stats μ .^ 2 abs stats σ found σ md\"\"\" Exercise 6 Putting it all together — fitting an SIR model to data In this exercise we will fit the non spatial SIR ODE model from Exercise 2 to some data generated from a different, spatial epidemic model, as we mentioned at the beginning. If we are able to find a good fit, that would suggest that the spatial aspect \"does not matter\" too much for the dynamics of these models. If the fit is not so good, perhaps there is an important effect of space. As usual in statistics, and indeed in modelling in general, we should be very cautious of making claims of this nature. This fitting procedure will be different from that in Exercise 4, however we no longer have an explicit form for the function that we are fitting. Rather, it is simply the output of an ODE So what should we do? We will try to find the parameters \\beta and \\gamma for which the output of the ODEs when we simulate it with those parameters best matches the data Exercise 6.1 Below is the result from the spatial model. These are the average S, I, R fractions from running 20 simulations. Click on it \"\"\" hw4 results transposed S 0.99, 0.9895, 0.9895, 0.989, 0.9885, 0.9885, 0.9885, 0.988, 0.9865, 0.986, 0.9855, 0.9855, 0.9855, 0.9845, 0.9845, 0.9845, 0.984, 0.984, 0.984, 0.9835, 0.9835, 0.982, 0.982, 0.982, 0.982, 0.9815, 0.981, 0.9805, 0.98, 0.98, 0.98, 0.98, 0.9795, 0.9795, 0.979, 0.979, 0.979, 0.978, 0.9775, 0.9775, 0.977, 0.976, 0.9755, 0.9745, 0.9735, 0.9735, 0.973, 0.972, 0.972, 0.972, 0.971, 0.971, 0.9695, 0.968, 0.9675, 0.967, 0.9665, 0.966, 0.9655, 0.9645, 0.9645, 0.9645, 0.964, 0.9615, 0.9595, 0.959, 0.958, 0.9575, 0.9575, 0.9575, 0.9565, 0.956, 0.954, 0.9535, 0.9535, 0.951, 0.95, 0.949, 0.9485, 0.948, 0.947, 0.9465, 0.9465, 0.9455, 0.945, 0.9425, 0.9415, 0.9405, 0.9395, 0.9385, 0.938, 0.937, 0.9355, 0.9355, 0.935, 0.9325, 0.932, 0.93, 0.929, 0.927, 0.9265, 0.926, 0.9245, 0.924, 0.9235, 0.923, 0.9225, 0.9225, 0.922, 0.9215, 0.9195, 0.918, 0.915, 0.9125, 0.911, 0.907, 0.9055, 0.9045, 0.9025, 0.9005, 0.899, 0.898, 0.8965, 0.8955, 0.893, 0.892, 0.89, 0.8875, 0.885, 0.8835, 0.882, 0.8805, 0.8785, 0.8755, 0.8735, 0.869, 0.868, 0.868, 0.8645, 0.8625, 0.8605, 0.8575, 0.8545, 0.8505, 0.847, 0.8455, 0.8435, 0.8415, 0.84, 0.8385, 0.8365, 0.8345, 0.829, 0.826, 0.824, 0.822, 0.817, 0.8145, 0.814, 0.811, 0.8095, 0.8075, 0.805, 0.8005, 0.7985, 0.7965, 0.793, 0.7895, 0.7865, 0.785, 0.7815, 0.779, 0.776, 0.769, 0.7655, 0.764, 0.7625, 0.7595, 0.7575, 0.754, 0.751, 0.7485, 0.743, 0.7395, 0.736, 0.7355, 0.732, 0.728, 0.725, 0.7235, 0.7215, 0.718, 0.7165, 0.7135, 0.7095, 0.709, 0.705, 0.7015, 0.699, 0.697, 0.694, 0.69, 0.685, 0.6835, 0.6805, 0.6795, 0.6765, 0.6745, 0.6735, 0.6705, 0.6665, 0.6625, 0.66, 0.656, 0.653, 0.65, 0.6465, 0.641, 0.639, 0.6365, 0.6335, 0.632, 0.6285, 0.6265, 0.623, 0.6195, 0.617, 0.6125, 0.609, 0.607, 0.6045, 0.601, 0.596, 0.591, 0.5905, 0.5885, 0.5825, 0.579, 0.576, 0.574, 0.5705, 0.569, 0.563, 0.559, 0.5565, 0.555, 0.5515, 0.546, 0.5455, 0.5435, 0.5395, 0.538, 0.535, 0.5315, 0.528, 0.525, 0.523, 0.519, 0.516, 0.513, 0.511, 0.509, 0.5065, 0.5045, 0.5015, 0.497, 0.4925, 0.488, 0.485, 0.4795, 0.473, 0.47, 0.465, 0.4595, 0.457, 0.454, 0.45, 0.447, 0.444, 0.4405, 0.4385, 0.4345, 0.431, 0.4275, 0.4245, 0.422, 0.421, 0.418, 0.414, 0.411, 0.4075, 0.404, 0.4015, 0.398, 0.3945, 0.3915, 0.39, 0.3875, 0.385, 0.3825, 0.379, 0.3765, 0.3725, 0.3685, 0.3655, 0.364, 0.359, 0.3555, 0.3555, 0.354, 0.351, 0.3495, 0.347, 0.345, 0.343, 0.341, 0.3365, 0.3325, 0.3325, 0.3275, 0.3255, 0.3225, 0.3205, 0.3195, 0.3175, 0.316, 0.313, 0.3125, 0.31, 0.308, 0.3055, 0.302, 0.301, 0.299, 0.297, 0.294, 0.29, 0.2895, 0.2855, 0.283, 0.2825, 0.279, 0.276, 0.273, 0.2695, 0.2665, 0.2655, 0.2635, 0.261, 0.2595, 0.258, 0.257, 0.254, 0.2535, 0.251, 0.25, 0.2465, 0.245, 0.2435, 0.2415, 0.2405, 0.238, 0.2365, 0.235, 0.2335, 0.2325, 0.23, 0.2275, 0.226, 0.2245, 0.2235, 0.223, 0.22, 0.218, 0.2165, 0.2135, 0.2135, 0.2095, 0.2075, 0.206, 0.205, 0.2045, 0.2025, 0.201, 0.2, 0.1985, 0.1985, 0.196, 0.1945, 0.1915, 0.1895, 0.188, 0.185, 0.184, 0.1835, 0.1835, 0.183, 0.183, 0.182, 0.182, 0.1805, 0.179, 0.1785, 0.177, 0.176, 0.1745, 0.174, 0.1735, 0.173, 0.1715, 0.171, 0.1685, 0.167, 0.1665, 0.165, 0.1645, 0.163, 0.162, 0.162, 0.1605, 0.1605, 0.16, 0.159, 0.158, 0.1555, 0.155, 0.1545, 0.1545, 0.152, 0.1505, 0.15, 0.149, 0.1475, 0.1465, 0.1445, 0.1435, 0.142, 0.1405, 0.14, 0.14, 0.139, 0.139, 0.1375, 0.137, 0.136, 0.1355, 0.135, 0.134, 0.1335, 0.133, 0.133, 0.1325, 0.1315, 0.1305, 0.13, 0.1295, 0.1295, 0.1275, 0.1265, 0.126, 0.126, 0.125, 0.124, 0.124, 0.1235, 0.1225, 0.1225, 0.1225, 0.122, 0.121, 0.1205, 0.1205, 0.1195, 0.1185, 0.117, 0.1145, 0.1135, 0.113, 0.113, 0.113, 0.112, 0.1105, 0.11, 0.109, 0.1085, 0.1055, 0.1055, 0.105, 0.105, 0.105, 0.105, 0.1035, 0.103, 0.1025, 0.102, 0.101, 0.1005, 0.0995, 0.099, 0.0975, 0.0965, 0.096, 0.096, 0.095, 0.095, 0.095, 0.0945, 0.0935, 0.0935, 0.0925, 0.091, 0.091, 0.0905, 0.09, 0.09, 0.09, 0.09, 0.0895, 0.0895, 0.0895, 0.088, 0.087, 0.0865, 0.0865, 0.086, 0.085, 0.085, 0.0845, 0.0845, 0.084, 0.084, 0.084, 0.084, 0.0835, 0.0825, 0.0825, 0.082, 0.082, 0.0815, 0.081, 0.081, 0.0805, 0.0795, 0.0795, 0.0795, 0.0795, 0.0795, 0.079, 0.0785, 0.078, 0.0775, 0.077, 0.076, 0.076, 0.076, 0.076, 0.076, 0.0755, 0.0755, 0.0755, 0.0755, 0.0755, 0.075, 0.075, 0.075, 0.075, 0.074, 0.074, 0.074, 0.0735, 0.0735, 0.0735, 0.0735, 0.073, 0.072, 0.072, 0.072, 0.072, 0.0715, 0.0715, 0.0715, 0.0705, 0.0705, 0.0695, 0.0695, 0.0695, 0.0695, 0.0695, 0.0695, 0.069, 0.069, 0.0685, 0.0685, 0.0685, 0.0685, 0.0685, 0.0685, 0.068, 0.0675, 0.0665, 0.0665, 0.0665, 0.0665, 0.0665, 0.066, 0.066, 0.066, 0.0655, 0.065, 0.065, 0.065, 0.0645, 0.0645, 0.0645, 0.0645, 0.064, 0.064, 0.0635, 0.0635, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.062, 0.0615, 0.0615, 0.0615, 0.0615, 0.0615, 0.0615, 0.061, 0.0605, 0.0605, 0.0605, 0.06, 0.0595, 0.0595, 0.0595, 0.059, 0.059, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.0585, 0.058, 0.058, 0.058, 0.058, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.0575, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.057, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.0565, 0.056, 0.056, 0.056, 0.056, 0.056, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.0555, 0.055, 0.055, 0.0545, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.054, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.053, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.0525, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.052, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.0515, 0.051, 0.051, 0.051, 0.051, 0.051, 0.051, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.0505, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05 , I 0.01, 0.0105, 0.0105, 0.011, 0.0115, 0.0115, 0.0115, 0.0115, 0.013, 0.0135, 0.014, 0.014, 0.014, 0.015, 0.015, 0.015, 0.0155, 0.0155, 0.0155, 0.016, 0.016, 0.0175, 0.0175, 0.0175, 0.017, 0.0175, 0.018, 0.0185, 0.019, 0.019, 0.019, 0.019, 0.0195, 0.0195, 0.02, 0.02, 0.0195, 0.0205, 0.0205, 0.0205, 0.021, 0.022, 0.022, 0.023, 0.024, 0.024, 0.0245, 0.0255, 0.0255, 0.0255, 0.0265, 0.0265, 0.028, 0.0295, 0.03, 0.0305, 0.031, 0.0315, 0.032, 0.033, 0.033, 0.033, 0.0335, 0.036, 0.037, 0.0375, 0.0385, 0.039, 0.039, 0.039, 0.0395, 0.0395, 0.0415, 0.042, 0.0415, 0.0435, 0.0445, 0.0455, 0.0455, 0.046, 0.047, 0.0475, 0.0475, 0.0485, 0.0485, 0.051, 0.052, 0.053, 0.053, 0.054, 0.0545, 0.0555, 0.057, 0.057, 0.0575, 0.06, 0.0605, 0.0625, 0.0635, 0.0645, 0.065, 0.0655, 0.067, 0.0675, 0.068, 0.0685, 0.069, 0.069, 0.0695, 0.07, 0.072, 0.0735, 0.076, 0.0785, 0.0795, 0.0835, 0.0845, 0.0855, 0.0875, 0.0895, 0.091, 0.0915, 0.093, 0.0935, 0.096, 0.097, 0.0985, 0.101, 0.1035, 0.105, 0.1065, 0.108, 0.1095, 0.1125, 0.1135, 0.1175, 0.1185, 0.1185, 0.122, 0.124, 0.126, 0.1285, 0.1315, 0.1355, 0.1385, 0.14, 0.1415, 0.1435, 0.1445, 0.144, 0.146, 0.148, 0.1535, 0.1565, 0.1585, 0.1605, 0.1655, 0.1675, 0.168, 0.171, 0.172, 0.174, 0.176, 0.1795, 0.181, 0.1815, 0.1845, 0.188, 0.191, 0.192, 0.1955, 0.1975, 0.199, 0.206, 0.209, 0.21, 0.2105, 0.213, 0.215, 0.218, 0.2205, 0.2225, 0.2275, 0.2305, 0.2335, 0.2335, 0.2365, 0.24, 0.242, 0.242, 0.244, 0.2465, 0.2475, 0.2505, 0.2535, 0.2525, 0.256, 0.259, 0.261, 0.2625, 0.2645, 0.268, 0.273, 0.273, 0.2745, 0.2755, 0.278, 0.28, 0.281, 0.2835, 0.2865, 0.2905, 0.292, 0.295, 0.2975, 0.3, 0.303, 0.308, 0.3095, 0.3115, 0.313, 0.3145, 0.318, 0.319, 0.3225, 0.326, 0.328, 0.332, 0.333, 0.334, 0.3355, 0.3385, 0.3435, 0.348, 0.3475, 0.347, 0.353, 0.356, 0.3585, 0.36, 0.3635, 0.365, 0.371, 0.3745, 0.376, 0.3765, 0.379, 0.3835, 0.384, 0.386, 0.3875, 0.3875, 0.3895, 0.3915, 0.395, 0.397, 0.3975, 0.3995, 0.402, 0.4035, 0.4055, 0.4075, 0.409, 0.41, 0.4115, 0.416, 0.42, 0.424, 0.4255, 0.4305, 0.4355, 0.4375, 0.441, 0.445, 0.4465, 0.4475, 0.4505, 0.4535, 0.4565, 0.4595, 0.4605, 0.4635, 0.4665, 0.4695, 0.472, 0.474, 0.4745, 0.4775, 0.48, 0.483, 0.4835, 0.4865, 0.489, 0.491, 0.4935, 0.495, 0.4945, 0.4965, 0.4985, 0.5, 0.502, 0.5045, 0.508, 0.51, 0.5125, 0.514, 0.518, 0.52, 0.5185, 0.519, 0.5215, 0.522, 0.5225, 0.5235, 0.5245, 0.525, 0.529, 0.5315, 0.5315, 0.5345, 0.5355, 0.5375, 0.5385, 0.538, 0.54, 0.541, 0.5435, 0.5425, 0.5445, 0.545, 0.547, 0.549, 0.549, 0.55, 0.551, 0.553, 0.5565, 0.557, 0.56, 0.562, 0.5615, 0.564, 0.5655, 0.5655, 0.5665, 0.569, 0.567, 0.5665, 0.569, 0.568, 0.568, 0.5675, 0.5695, 0.569, 0.5705, 0.5695, 0.572, 0.5725, 0.574, 0.575, 0.5755, 0.5775, 0.5775, 0.5785, 0.5785, 0.579, 0.5805, 0.582, 0.583, 0.583, 0.583, 0.5815, 0.5825, 0.584, 0.585, 0.5855, 0.584, 0.5865, 0.587, 0.5875, 0.5875, 0.5865, 0.587, 0.587, 0.5875, 0.588, 0.5865, 0.588, 0.5885, 0.5905, 0.5915, 0.5925, 0.5945, 0.5935, 0.592, 0.5905, 0.5905, 0.5905, 0.5905, 0.59, 0.59, 0.5905, 0.589, 0.589, 0.5885, 0.5875, 0.5875, 0.585, 0.584, 0.584, 0.583, 0.5835, 0.583, 0.582, 0.5825, 0.582, 0.5825, 0.582, 0.581, 0.582, 0.581, 0.579, 0.5785, 0.5775, 0.5795, 0.58, 0.579, 0.576, 0.578, 0.579, 0.579, 0.578, 0.5795, 0.5795, 0.5805, 0.58, 0.5805, 0.5815, 0.5805, 0.5795, 0.5795, 0.5775, 0.5765, 0.576, 0.5765, 0.576, 0.5755, 0.5755, 0.5755, 0.575, 0.5745, 0.5745, 0.574, 0.573, 0.573, 0.5715, 0.568, 0.5695, 0.5705, 0.571, 0.5705, 0.5695, 0.5695, 0.569, 0.569, 0.569, 0.5685, 0.566, 0.565, 0.5655, 0.565, 0.564, 0.564, 0.563, 0.563, 0.565, 0.564, 0.564, 0.563, 0.563, 0.5635, 0.564, 0.563, 0.5635, 0.563, 0.5655, 0.564, 0.5635, 0.562, 0.562, 0.5605, 0.5605, 0.5595, 0.559, 0.5595, 0.5595, 0.559, 0.558, 0.557, 0.5575, 0.5575, 0.556, 0.5555, 0.5545, 0.5525, 0.5495, 0.549, 0.549, 0.5475, 0.5475, 0.548, 0.548, 0.548, 0.547, 0.5445, 0.5425, 0.5425, 0.5425, 0.5415, 0.541, 0.5415, 0.541, 0.5405, 0.5395, 0.5395, 0.54, 0.539, 0.539, 0.5375, 0.5365, 0.5355, 0.535, 0.5345, 0.534, 0.5345, 0.534, 0.533, 0.5325, 0.5315, 0.532, 0.5315, 0.5315, 0.5315, 0.531, 0.53, 0.5295, 0.528, 0.526, 0.5255, 0.5235, 0.524, 0.523, 0.5215, 0.521, 0.521, 0.5205, 0.5205, 0.5205, 0.52, 0.519, 0.5185, 0.517, 0.5165, 0.5145, 0.5135, 0.513, 0.513, 0.513, 0.513, 0.5135, 0.5125, 0.5125, 0.511, 0.5115, 0.5125, 0.5115, 0.511, 0.511, 0.51, 0.5095, 0.509, 0.509, 0.5065, 0.5065, 0.506, 0.5045, 0.504, 0.502, 0.4995, 0.4995, 0.4995, 0.4995, 0.4985, 0.4965, 0.493, 0.492, 0.492, 0.491, 0.49, 0.4905, 0.4905, 0.4905, 0.4905, 0.4895, 0.489, 0.488, 0.4875, 0.488, 0.486, 0.4855, 0.4855, 0.485, 0.484, 0.483, 0.4825, 0.483, 0.482, 0.4815, 0.481, 0.4815, 0.4805, 0.4785, 0.4775, 0.477, 0.475, 0.4735, 0.473, 0.4725, 0.471, 0.4705, 0.47, 0.469, 0.4675, 0.4675, 0.466, 0.466, 0.4645, 0.4635, 0.4625, 0.4615, 0.461, 0.46, 0.4595, 0.458, 0.458, 0.457, 0.457, 0.4565, 0.4545, 0.454, 0.453, 0.452, 0.451, 0.4495, 0.449, 0.448, 0.4465, 0.4465, 0.4455, 0.4455, 0.4455, 0.4455, 0.4455, 0.445, 0.444, 0.444, 0.4425, 0.4415, 0.44, 0.4385, 0.438, 0.437, 0.436, 0.4355, 0.435, 0.4345, 0.432, 0.4295, 0.4295, 0.428, 0.427, 0.426, 0.4255, 0.4245, 0.4215, 0.4215, 0.4205, 0.4205, 0.42, 0.4195, 0.4195, 0.4185, 0.4165, 0.4155, 0.4135, 0.412, 0.4125, 0.4105, 0.41, 0.409, 0.4075, 0.407, 0.407, 0.4065, 0.405, 0.4045, 0.404, 0.4035, 0.404, 0.4015, 0.4, 0.3995, 0.398, 0.398, 0.397, 0.397, 0.397, 0.396, 0.396, 0.395, 0.3945, 0.395, 0.394, 0.393, 0.393, 0.393, 0.392, 0.391, 0.3905, 0.39, 0.389, 0.3885, 0.388, 0.387, 0.386, 0.385, 0.384, 0.384, 0.3835, 0.3825, 0.3825, 0.3825, 0.3815, 0.3815, 0.38, 0.38, 0.3795, 0.379, 0.379, 0.378, 0.377, 0.3765, 0.376, 0.3755, 0.3735, 0.3715, 0.371, 0.3715, 0.371, 0.3705, 0.37, 0.37, 0.369, 0.369, 0.3675, 0.367, 0.367, 0.3655, 0.364, 0.3625, 0.3615, 0.361, 0.3605, 0.3605, 0.36, 0.3595, 0.359, 0.3575, 0.3575, 0.357, 0.3565, 0.355, 0.355, 0.354, 0.3535, 0.353, 0.352, 0.352, 0.3515, 0.351, 0.3495, 0.3495, 0.349, 0.348, 0.3475, 0.345, 0.344, 0.3435, 0.3425, 0.3425, 0.341, 0.3395, 0.3395, 0.339, 0.3375, 0.337, 0.336, 0.336, 0.3345, 0.3345, 0.334, 0.3335, 0.3335, 0.3325, 0.3325, 0.3315, 0.3295, 0.3285, 0.328, 0.328, 0.326, 0.326, 0.326, 0.326, 0.326, 0.3255, 0.325, 0.3235, 0.3225, 0.3215, 0.3215, 0.3215, 0.321, 0.3195, 0.318, 0.3175, 0.317, 0.3165, 0.316, 0.3155, 0.3155, 0.315, 0.3145, 0.3135, 0.3135, 0.313, 0.3105, 0.31, 0.31, 0.308, 0.3075, 0.3075, 0.3075, 0.307, 0.305, 0.3045, 0.3035, 0.3035, 0.3015, 0.3015, 0.3015, 0.3015, 0.301, 0.3, 0.2995, 0.2995, 0.299, 0.299, 0.298, 0.298, 0.297, 0.2965, 0.2945, 0.294, 0.293, 0.293, 0.2915, 0.2915, 0.291, 0.2905, 0.29, 0.289, 0.289, 0.288, 0.288, 0.288, 0.288, 0.288, 0.2865, 0.2855, 0.2855, 0.2845, 0.2835, 0.283, 0.282, 0.2815, 0.281, 0.281, 0.2805, 0.281, 0.2805, 0.2795, 0.2785, 0.278, 0.278, 0.2785, 0.2785, 0.277, 0.2765, 0.275, 0.2745, 0.2745, 0.2745, 0.274, 0.273, 0.273, 0.273, 0.2715, 0.27, 0.2695, 0.2695, 0.269, 0.2685, 0.268, 0.267, 0.266, 0.266, 0.265, 0.2645, 0.2645, 0.2635, 0.263, 0.2625, 0.2625, 0.262, 0.262, 0.2605, 0.26, 0.259, 0.2575, 0.2565, 0.2555, 0.255, 0.254, 0.253, 0.2525, 0.251, 0.2505, 0.2505, 0.2505, 0.25, 0.2495, 0.2495, 0.2495, 0.249, 0.249, 0.249, 0.2485, 0.2475, 0.246, 0.246, 0.245, 0.245, 0.2445, 0.2445, 0.244, 0.244, 0.2435, 0.2425, 0.242, 0.242, 0.2415, 0.2405, 0.239, 0.238, 0.2375, 0.2365, 0.2365, 0.2365, 0.236, 0.236, 0.2365, 0.2365, 0.2365, 0.2365, 0.236, 0.235, 0.2345, 0.234, 0.234, 0.2335, 0.2335, 0.233, 0.233, 0.2325, 0.232, 0.2315, 0.231, 0.2295, 0.2285 , R 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.0005, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.0015, 0.0015, 0.002, 0.002, 0.002, 0.002, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0025, 0.0035, 0.0035, 0.0035, 0.0035, 0.0035, 0.0035, 0.004, 0.0045, 0.0045, 0.0045, 0.005, 0.0055, 0.0055, 0.0055, 0.006, 0.006, 0.006, 0.006, 0.006, 0.006, 0.0065, 0.0065, 0.0065, 0.0065, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0075, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.0085, 0.009, 0.009, 0.0095, 0.0095, 0.01, 0.01, 0.01, 0.01, 0.01, 0.0105, 0.0105, 0.011, 0.011, 0.011, 0.0115, 0.0115, 0.0115, 0.0115, 0.0115, 0.0115, 0.012, 0.012, 0.013, 0.0135, 0.0135, 0.0135, 0.0135, 0.0135, 0.0135, 0.014, 0.014, 0.014, 0.0145, 0.0145, 0.015, 0.015, 0.0155, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.0175, 0.018, 0.018, 0.018, 0.0185, 0.0185, 0.019, 0.02, 0.0205, 0.022, 0.0225, 0.0225, 0.0225, 0.023, 0.023, 0.0235, 0.025, 0.025, 0.0255, 0.026, 0.027, 0.0275, 0.0275, 0.028, 0.0285, 0.029, 0.0295, 0.03, 0.0305, 0.031, 0.0315, 0.032, 0.033, 0.0345, 0.0345, 0.0355, 0.036, 0.036, 0.037, 0.0385, 0.039, 0.0395, 0.04, 0.0405, 0.0415, 0.042, 0.042, 0.0435, 0.045, 0.045, 0.0455, 0.0455, 0.0455, 0.046, 0.047, 0.047, 0.048, 0.049, 0.0495, 0.05, 0.0505, 0.051, 0.0515, 0.052, 0.0535, 0.0535, 0.0535, 0.0545, 0.0545, 0.0545, 0.055, 0.0555, 0.058, 0.059, 0.06, 0.0605, 0.0605, 0.061, 0.062, 0.0645, 0.0645, 0.065, 0.0655, 0.066, 0.066, 0.066, 0.066, 0.0665, 0.0675, 0.0685, 0.0695, 0.0705, 0.0705, 0.0705, 0.073, 0.0745, 0.0755, 0.077, 0.077, 0.078, 0.0795, 0.0815, 0.082, 0.0835, 0.0835, 0.0835, 0.0845, 0.0855, 0.087, 0.087, 0.0875, 0.088, 0.0895, 0.09, 0.0915, 0.0925, 0.094, 0.0955, 0.0965, 0.0985, 0.0995, 0.0995, 0.0995, 0.1, 0.101, 0.102, 0.1025, 0.103, 0.1035, 0.104, 0.1045, 0.1045, 0.106, 0.106, 0.109, 0.1095, 0.1095, 0.111, 0.112, 0.1135, 0.1155, 0.116, 0.1165, 0.1175, 0.119, 0.119, 0.1195, 0.1215, 0.122, 0.122, 0.123, 0.1245, 0.126, 0.127, 0.1275, 0.1285, 0.1305, 0.1315, 0.1325, 0.134, 0.1345, 0.136, 0.136, 0.138, 0.139, 0.14, 0.141, 0.1425, 0.1425, 0.143, 0.1435, 0.145, 0.1455, 0.147, 0.1475, 0.149, 0.15, 0.151, 0.152, 0.153, 0.1535, 0.1535, 0.1545, 0.155, 0.156, 0.157, 0.1585, 0.1615, 0.164, 0.1645, 0.1675, 0.17, 0.17, 0.1725, 0.174, 0.1755, 0.1765, 0.1775, 0.1785, 0.1805, 0.1815, 0.1825, 0.1825, 0.1835, 0.184, 0.1845, 0.186, 0.1865, 0.188, 0.1885, 0.1895, 0.1905, 0.191, 0.1925, 0.1935, 0.1955, 0.1975, 0.198, 0.1985, 0.201, 0.2025, 0.204, 0.2055, 0.2065, 0.2075, 0.209, 0.2105, 0.212, 0.2125, 0.2135, 0.215, 0.216, 0.217, 0.218, 0.219, 0.2195, 0.2205, 0.2225, 0.2245, 0.226, 0.2265, 0.2265, 0.2275, 0.228, 0.2295, 0.2305, 0.2325, 0.234, 0.2355, 0.238, 0.2385, 0.2415, 0.243, 0.2445, 0.246, 0.248, 0.25, 0.2515, 0.2525, 0.2535, 0.2545, 0.256, 0.257, 0.2575, 0.2585, 0.261, 0.2625, 0.2645, 0.265, 0.265, 0.2665, 0.2695, 0.27, 0.2705, 0.271, 0.273, 0.273, 0.274, 0.275, 0.2765, 0.2775, 0.278, 0.2795, 0.2805, 0.2815, 0.2835, 0.286, 0.287, 0.2875, 0.2885, 0.2895, 0.2905, 0.291, 0.292, 0.2925, 0.293, 0.2945, 0.2965, 0.297, 0.299, 0.3025, 0.303, 0.303, 0.303, 0.3035, 0.3055, 0.3065, 0.307, 0.3075, 0.3085, 0.309, 0.3115, 0.313, 0.3135, 0.3145, 0.3155, 0.3165, 0.3185, 0.32, 0.3205, 0.3225, 0.323, 0.324, 0.324, 0.3245, 0.3255, 0.327, 0.3275, 0.3285, 0.329, 0.3305, 0.3315, 0.333, 0.333, 0.3345, 0.336, 0.3375, 0.3385, 0.3385, 0.3395, 0.3405, 0.3425, 0.344, 0.345, 0.346, 0.348, 0.3485, 0.3505, 0.3525, 0.3555, 0.3565, 0.3575, 0.359, 0.36, 0.361, 0.361, 0.3615, 0.363, 0.3655, 0.3675, 0.3675, 0.368, 0.369, 0.3695, 0.3705, 0.372, 0.373, 0.374, 0.3745, 0.375, 0.376, 0.3765, 0.378, 0.3795, 0.3805, 0.381, 0.3815, 0.3825, 0.383, 0.3835, 0.385, 0.3855, 0.387, 0.387, 0.3875, 0.388, 0.389, 0.3895, 0.3905, 0.391, 0.3925, 0.395, 0.396, 0.3985, 0.3985, 0.4, 0.4025, 0.403, 0.403, 0.4035, 0.4035, 0.404, 0.4045, 0.4055, 0.406, 0.4075, 0.4085, 0.4105, 0.4115, 0.412, 0.413, 0.413, 0.413, 0.413, 0.414, 0.414, 0.4155, 0.4155, 0.4155, 0.4165, 0.417, 0.417, 0.4185, 0.419, 0.4195, 0.4205, 0.423, 0.424, 0.4245, 0.426, 0.4265, 0.4285, 0.431, 0.4315, 0.4315, 0.432, 0.433, 0.435, 0.4385, 0.4395, 0.4395, 0.441, 0.4425, 0.443, 0.443, 0.443, 0.443, 0.444, 0.445, 0.446, 0.4465, 0.4465, 0.449, 0.4495, 0.4495, 0.4505, 0.4515, 0.4525, 0.453, 0.453, 0.454, 0.455, 0.4555, 0.456, 0.457, 0.459, 0.46, 0.4605, 0.4625, 0.464, 0.4645, 0.465, 0.4665, 0.467, 0.4675, 0.4685, 0.47, 0.47, 0.4715, 0.472, 0.4735, 0.4745, 0.4755, 0.4765, 0.477, 0.478, 0.4785, 0.48, 0.48, 0.481, 0.4815, 0.482, 0.484, 0.4845, 0.4855, 0.4865, 0.488, 0.49, 0.4905, 0.4915, 0.4935, 0.494, 0.495, 0.495, 0.4955, 0.4955, 0.496, 0.4965, 0.4975, 0.4975, 0.499, 0.5, 0.5015, 0.503, 0.5035, 0.5045, 0.5055, 0.506, 0.5065, 0.5075, 0.51, 0.5125, 0.5125, 0.5145, 0.5155, 0.5165, 0.517, 0.518, 0.521, 0.521, 0.522, 0.522, 0.5225, 0.523, 0.523, 0.524, 0.526, 0.527, 0.529, 0.5305, 0.5305, 0.5325, 0.533, 0.534, 0.5355, 0.536, 0.536, 0.5365, 0.538, 0.5385, 0.539, 0.5395, 0.5395, 0.542, 0.5435, 0.544, 0.5455, 0.5455, 0.5465, 0.5465, 0.5465, 0.5475, 0.5475, 0.5485, 0.549, 0.549, 0.55, 0.551, 0.551, 0.551, 0.5525, 0.5535, 0.554, 0.5545, 0.5555, 0.556, 0.5565, 0.5575, 0.5585, 0.5595, 0.5605, 0.561, 0.5615, 0.563, 0.5635, 0.5635, 0.5645, 0.5645, 0.566, 0.566, 0.5665, 0.567, 0.567, 0.568, 0.569, 0.5695, 0.57, 0.5705, 0.5725, 0.5745, 0.575, 0.5755, 0.576, 0.5765, 0.577, 0.577, 0.578, 0.578, 0.5795, 0.58, 0.58, 0.5815, 0.583, 0.5845, 0.5855, 0.586, 0.5865, 0.5865, 0.587, 0.5875, 0.588, 0.5895, 0.5895, 0.59, 0.5905, 0.592, 0.592, 0.593, 0.5935, 0.594, 0.595, 0.595, 0.596, 0.5965, 0.598, 0.598, 0.5985, 0.5995, 0.6, 0.6025, 0.6035, 0.604, 0.605, 0.605, 0.6065, 0.608, 0.608, 0.6085, 0.61, 0.6105, 0.6115, 0.6115, 0.613, 0.613, 0.6135, 0.614, 0.614, 0.615, 0.615, 0.616, 0.618, 0.619, 0.6195, 0.6195, 0.6215, 0.6215, 0.6215, 0.6215, 0.6215, 0.622, 0.6225, 0.624, 0.625, 0.626, 0.6265, 0.6265, 0.627, 0.6285, 0.63, 0.6305, 0.631, 0.6315, 0.632, 0.6325, 0.6325, 0.633, 0.6335, 0.6345, 0.6345, 0.635, 0.6375, 0.638, 0.638, 0.64, 0.6405, 0.6405, 0.6405, 0.641, 0.643, 0.6435, 0.6445, 0.6445, 0.6465, 0.6465, 0.6465, 0.6465, 0.647, 0.648, 0.6485, 0.6485, 0.649, 0.649, 0.65, 0.65, 0.651, 0.6515, 0.6535, 0.654, 0.655, 0.655, 0.6565, 0.6565, 0.657, 0.6575, 0.658, 0.659, 0.659, 0.6605, 0.6605, 0.6605, 0.6605, 0.6605, 0.662, 0.663, 0.663, 0.664, 0.665, 0.6655, 0.6665, 0.667, 0.6675, 0.6675, 0.668, 0.668, 0.6685, 0.6695, 0.6705, 0.671, 0.671, 0.671, 0.671, 0.6725, 0.673, 0.6745, 0.675, 0.675, 0.675, 0.6755, 0.6765, 0.6765, 0.6765, 0.678, 0.6795, 0.68, 0.68, 0.6805, 0.681, 0.6815, 0.6825, 0.6835, 0.6835, 0.6845, 0.685, 0.685, 0.686, 0.6865, 0.687, 0.687, 0.6875, 0.6875, 0.689, 0.6895, 0.6905, 0.692, 0.693, 0.694, 0.6945, 0.6955, 0.6965, 0.697, 0.6985, 0.699, 0.699, 0.699, 0.6995, 0.7, 0.7, 0.7, 0.7005, 0.7005, 0.7005, 0.701, 0.702, 0.7035, 0.7035, 0.7045, 0.7045, 0.705, 0.705, 0.7055, 0.7055, 0.706, 0.707, 0.7075, 0.7075, 0.708, 0.709, 0.7105, 0.7115, 0.712, 0.713, 0.713, 0.713, 0.7135, 0.7135, 0.7135, 0.7135, 0.7135, 0.7135, 0.714, 0.715, 0.7155, 0.716, 0.716, 0.7165, 0.7165, 0.717, 0.717, 0.7175, 0.718, 0.7185, 0.719, 0.7205, 0.7215 spatial results collect. zip hw4 results transposed... spatial T 1 length spatial results md\"\"\" 👉 Not graded Manually fit the SIR curves to our data by adjusting ``\\beta`` and ``\\gamma`` until you find a good fit. This will use the `euler SIR` function from Exercise 2. \"\"\" md\"β bind guess β Slider 0.00 0.0001 0.1 default 0.05, show value true \" md\"γ bind guess γ Slider 0.00 0.0001 0.01 default 0.005, show value true \" md\"Show manual fit bind show manual sir fit CheckBox \" md\"\"\" 👉 To do this automatically, we will again need to define a loss function \\mathcal L \\beta, \\gamma . This will compare the solution of the SIR equations with parameters \\beta and \\gamma with your data. This time, instead of comparing two vectors of numbers, we need to compare two vectors of vectors , the S, I, R values. \"\"\" function loss sir β, γ return missing end loss sir guess β, guess γ md\"\"\" 👉 Use this loss function to find the optimal parameters ``\\beta`` and ``\\gamma``. \"\"\" found β, found γ let your code here missing, missing end let p plot plot sir p, spatial T, spatial results, label \"spatial\", opacity .7 if show manual sir fit guess results euler SIR guess β, guess γ, 0.99, 0.01, 0.00 , spatial T plot sir p, spatial T, guess results, label \"manual\", linestyle dash, lw 2 end try assert found β isa Missing && found γ isa Missing found results euler SIR found β, found γ, 0.99, 0.01, 0.00 , spatial T plot sir p, spatial T, found results, label \"optimized\", linestyle dot, lw 2 catch end as svg p end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"\"\" Remember that functions are objects https www.youtube.com watch?v O HBDZMLrM For example, here is a function that returns the square root function ```julia function the square root function f x sqrt x return f end ``` \"\"\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let result euler integrate step x x^2, 10, 11, 12 if result isa Missing still missing elseif result isa Number keep working md\"Make sure that you return a number.\" else if result ≈ 6358 correct elseif result ≈ 1462 almost md\"Use ``f' a h ``, not ``f' a ``.\" else keep working end end end if euler test isa Missing still missing elseif euler test isa Vector || abs length euler test 101 1 keep working md\"Make sure that you return a vector of numbers, of the same size as `T`.\" else if abs euler test 1 0 1 keep working elseif abs euler test 50 5^3 20 keep working elseif abs euler test end 10^3 100 keep working else correct end end let result1 gradient descent 1d step x x^2, 10 η 1 result2 gradient descent 1d step x x^2, 10 η 2 if result1 isa Missing still missing elseif result1 isa Real keep working md\"You need to return a number.\" else if result2 result1 10.0 correct else keep working end end end let result gradient descent 1d 10 do x x 5pi ^ 2 10 end if result isa Missing still missing elseif result isa Real keep working md\"You need to return a number.\" else error abs result 5pi if error 5.0 almost md\"It's not accurate enough yet. Maybe you need to increase the number of steps?\" elseif error 0.02 keep working else correct end end end let if found β isa Missing || found γ isa Missing still missing else if isnan found β || isnan found γ hint md\"The found parameters are `NaN`, which means that floating point errors led to an invalid value. Try setting ``\\eta`` much lower, try `1e 6`, `1e 7`, etc.\" else diffb abs found β 0.019 diffc abs found γ 0.0026 if diffb .1 || diffc .01 almost md\"Try using initial values that are closer to the expected result. For example, the values that you found using the sliders. \" elseif diffb .01 || diffc .001 almost md\"Try using initial values that are closer to the expected result. For example, the values that you found using the sliders. You can also experiment with a different loss function. Are you using the absolute error, instead of the square of the error? A parabolic loss function is 'easier to optimize' using gradient descent than a cone shaped one.\" else correct md\"\"\" If you made it this far, congratulations you have just taken your first step into the exciting world of scientific machine learning \"\"\" end end end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined finite difference slope not defined finite difference slope else let result finite difference slope sqrt, 4.0, 5.0 if result isa Missing still missing elseif result isa Real keep working md\"Make sure that you return a number.\" else if result ≈ 0.2 correct else keep working end end end end if isdefined tangent line not defined tangent line else let result tangent line sqrt, 4.0, 5.0 if result isa Missing still missing elseif result isa Function keep working md\"Make sure that you return a function.\" else if finite difference slope result, 14.0, 15.0 ≈ 0.2 if result 4.0 ≈ 2.0 correct else almost md\"The tangent line should pass through a, f a .\" end else keep working end end end end if isdefined ∂x not defined ∂x else let result ∂x x, y 2x^2 3y^2, 6, 7 if result isa Missing still missing elseif result isa Number keep working md\"Make sure that you return a number.\" else if abs result 24 1.0 correct else keep working end end end end if isdefined ∂y not defined ∂y else let result ∂y x, y 2x^2 3y^2, 6, 7 if result isa Missing still missing elseif result isa Number keep working md\"Make sure that you return a number.\" else if abs result 42 1.0 correct else keep working end end end end if isdefined gradient not defined gradient else let result gradient x, y 2x^2 3y^2, 6, 7 if result isa Missing still missing elseif result isa Vector keep working md\"Make sure that you return a 2 element vector.\" else if abs result 1 24 1 && abs result 2 42 1 correct else keep working end end end end if isdefined found μ not defined found μ elseif isdefined found σ not defined found σ else let if found μ isa Missing || found σ isa Missing still missing else diff μ abs stats μ found μ diff σ abs stats σ found σ if diff μ 1 || diff σ 1 keep working elseif diff μ .2 || diff σ .2 almost md\"The fit is close, but we can do better. Try increasing ``\\eta`` \" else correct end end end end TODO html\" span style 'display inline font size 2em color purple font weight 900 ' TODO span \" "},{"url":"homework/hw9/","title":"Epidemic modeling III","tags":["homework","module3","epidemiology","track_julia","plotting","agent based model","statistics","random","modeling","monte carlo","track_climate"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 3 section 1.5 order 1.5 homework number 9 title \"Epidemic modeling III\" layout \"layout.jlhtml\" tags \"homework\", \"module3\", \"epidemiology\", \"track julia\", \"plotting\", \"agent based model\", \"statistics\", \"random\", \"modeling\", \"monte carlo\", \"track climate\" description \"In this problem set, we will look at a simple spatial agent based epidemic model agents can interact only with other agents that are nearby.\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using Plots, PlutoUI, LinearAlgebra md\" homework 9, version 3 \" hello there md\"\"\" Homework 9 Epidemic modeling III `18.S191`, Fall 2023 This notebook contains built in, live answer checks In some exercises you will see a coloured box, which runs a test case on your code, and provides feedback based on the result. Simply edit the code, run it, and the check runs again. Feel free to ask questions \"\"\" md\" Let's create a package environment \" md\"\"\" In this problem set, we will look at a simple spatial agent based epidemic model agents can interact only with other agents that are nearby . In Homework 7, any agent could interact with any other, which is not realistic. A simple approach is to use discrete space each agent lives in one cell of a square grid. For simplicity we will allow no more than one agent in each cell, but this requires some care to design the rules of the model to respect this. We will adapt some functionality from Homework 7. You should copy and paste your code from that homework into this notebook. \"\"\" md\"\"\" Exercise 1 Wandering at random in 2D In this exercise we will implement a random walk on a 2D lattice grid . At each time step, a walker jumps to a neighbouring position at random i.e. chosen with uniform probability from the available adjacent positions . \"\"\" md\"\"\" Exercise 1.1 We define a struct type `Coordinate` that contains integers `x` and `y`. \"\"\" md\"\"\" 👉 Construct a `Coordinate` located at the origin. \"\"\" origin missing md\"\"\" 👉 Write a function `make tuple` that takes an object of type `Coordinate` and returns the corresponding tuple ` x, y `. Boring, but useful later \"\"\" function make tuple c missing end md\"\"\" Exercise 1.2 In Julia, operations like ` ` and ` ` are just functions, and they are treated like any other function in the language. The only special property you can use the infix notation you can write ```julia 1 2 ``` instead of ```julia 1, 2 ``` There are lots of special 'infixable' function names https github.com JuliaLang julia blob master src julia parser.scm L23 L24 that you can use for your own functions When you call it with the prefix notation, it becomes clear that it really is 'just another function', with lots of predefined methods. \"\"\" 1, 2 md\"\"\" Extending in the wild Because it is a function, we can add our own methods to it This feature is super useful in general languages like Julia and Python, because it lets you use familiar syntax `a b c` on objects that are not necessarily numbers One example we've see before is the `RGB` type in the first lectures. You are able to do ```julia 0.5 RGB 0.1, 0.7, 0.6 ``` to multiply each color channel by 0.5 . This is possible because `Images.jl` wrote a method https github.com JuliaGraphics ColorVectorSpace.jl blob 06d70b8a28f5c263f52e414745c2066ccb72b518 src ColorVectorSpace.jl L207 ```julia Real, AbstractRGB AbstractRGB ``` 👉 Implement addition on two `Coordinate` structs by adding a method to `Base. ` \"\"\" function Base. a TYPE, b TYPE return missing end Coordinate 3,4 Coordinate 10,10 uncomment to check works md\"\"\" Pluto has some trouble here, you need to manually re run the cell above \"\"\" md\"\"\" Exercise 1.3 In our model, agents will be able to walk in 4 directions up, down, left and right. We can define these directions as `Coordinate`s. \"\"\" uncomment this possible moves Coordinate 1, 0 , Coordinate 0, 1 , Coordinate 1, 0 , Coordinate 0, 1 , md\"\"\" 👉 `rand possible moves ` gives a random possible move. Add this to the coordinate `Coordinate 4,5 ` and see that it moves to a valid neighbor. \"\"\" md\"\"\" We are able to make a `Coordinate` perform one random step, by adding a move to it. Great 👉 Write a function `trajectory` that calculates a trajectory of a `Wanderer` `w` when performing `n` steps., i.e. the sequence of positions that the walker finds itself in. Possible steps Use `rand possible moves, n ` to generate a vector of `n` random moves. Each possible move will be equally likely. To compute the trajectory you can use either of the following two approaches 1. 🆒 Use the function `accumulate` see the live docs for `accumulate` . Use ` ` as the function passed to `accumulate` and the `w` as the starting value `init` keyword argument . 1. Use a `for` loop calling ` `. \"\"\" function trajectory w Coordinate, n Int return missing end test trajectory trajectory Coordinate 4,4 , 30 uncomment to test function plot trajectory p Plots.Plot, trajectory Vector kwargs... plot p, make tuple. trajectory label nothing, linewidth 2, linealpha LinRange 1.0, 0.2, length trajectory , kwargs... end try p plot ratio 1, size 650, 200 plot trajectory p, test trajectory color \"black\", showaxis false, axis nothing, linewidth 4 p catch end let long trajectory trajectory Coordinate 4,4 , 1000 p plot ratio 1 plot trajectory p, long trajectory p end ^ uncomment to visualize a trajectory md\"\"\" Exercise 1.4 👉 Plot 10 trajectories of length 1000 on a single figure, all starting at the origin. Use the function `plot trajectory ` as demonstrated above. Remember from last week that you can compose plots like this ```julia let Create a new plot with aspect ratio 1 1 p plot ratio 1 plot trajectory p, test trajectory plot one trajectory plot trajectory p, another trajectory plot the second one ... p end ``` \"\"\" md\"\"\" Exercise 1.5 Agents live in a box of side length 2L , centered at the origin. We need to decide i.e. model what happens when they reach the walls of the box boundaries , in other words what kind of boundary conditions to use. One relatively simple boundary condition is a collision boundary Each wall of the box is a wall, modelled using \"collision\" if the walker tries to jump beyond the wall, it ends up at the position inside the box that is closest to the goal. 👉 Write a function `collide boundary` which takes a `Coordinate` `c` and a size L , and returns a new coordinate that lies inside the box i.e. `` L,L \\times L,L `` , but is closest to `c`. This is similar to `extend mat` from Homework 1. \"\"\" function collide boundary c Coordinate, L Number return missing end collide boundary Coordinate 12,4 , 10 uncomment to test md\"\"\" Exercise 1.6 👉 Implement a 3 argument method of `trajectory` where the third argument is a size. The trajectory returned should be within the boundary use `collide boundary` from above . You can still use `accumulate` with an anonymous function that makes a move and then reflects the resulting coordinate, or use a for loop. \"\"\" function trajectory c Coordinate, n Int, L Number return missing end md\"\"\" Exercise 2 Wanderering Agents In this exercise we will create Agents which have a location as well as some infection state information. Let's define a type `Agent`. `Agent` contains a `position` of type `Coordinate` , as well as a `status` of type `InfectionStatus` as in Homework 4 . For simplicity we will not use a `num infected` field, but feel free to do so \"\"\" enum InfectionStatus S I R define agent struct here md\"\"\" Exercise 2.1 👉 Write a function `initialize` that takes parameters N and L , where N is the number of agents and 2L is the side length of the square box where the agents live. It returns a `Vector` of `N` randomly generated `Agent`s. Their coordinates are randomly sampled in the `` L,L \\times L,L `` box, and the agents are all susceptible, except one, chosen at random, which is infectious. \"\"\" function initialize N Number, L Number return missing end initialize 3, 10 Color based on infection status color s InfectionStatus if s S \"blue\" elseif s I \"red\" else \"green\" end position a Agent a.position uncomment this line color a Agent color a.status uncomment this line md\"\"\" Exercise 2.2 👉 Write a function `visualize` that takes in a collection of agents as argument, and the box size `L`. It should plot a point for each agent at its location, coloured according to its status. You can use the keyword argument `c color. agents ` inside your call to the plotting function make the point colors correspond to the infection statuses. Don't forget to use `ratio 1`. \"\"\" function visualize agents Vector, L return missing end let N 20 L 10 visualize initialize N, L , L uncomment this line end md\"\"\" Exercise 3 Spatial epidemic model Dynamics Last week we wrote a function `interact ` that takes two agents, `agent` and `source`, and an infection of type `InfectionRecovery`, which models the interaction between two agent, and possibly modifies `agent` with a new status. This week, we define a new infection type, `CollisionInfectionRecovery`, and a new method that is the same as last week, except it only infects `agent` if `agents.position source.position` . \"\"\" abstract type AbstractInfection end struct CollisionInfectionRecovery AbstractInfection p infection Float64 p recovery Float64 end md\"\"\" Write a function `interact ` that takes two `Agent`s and a `CollisionInfectionRecovery`, and If the agents are at the same spot, causes a susceptible agent to communicate the disease from an infectious one with the correct probability. if the first agent is infectious, it recovers with some probability \"\"\" function interact agent Agent, source Agent, infection CollisionInfectionRecovery missing end md\"\"\" Exercise 3.1 Your turn 👉 Write a function `step ` that takes a vector of `Agent`s, a box size `L` and an `infection`. This that does one step of the dynamics on a vector of agents. Choose an Agent `source` at random. Move the `source` one step, and use `collide boundary` to ensure that our agent stays within the box. For all other agents, call `interact other agent, source, infection `. return the array `agents` again. \"\"\" function step agents Vector, L Number, infection AbstractInfection return missing end md\"\"\" Exercise 3.2 If we call `step ` `N` times, then every agent will have made one step, on average. Let's call this one sweep of the simulation. 👉 Create a before and after plot of ``k sweeps 1000`` sweeps. Initialize a new vector of agents `N 50`, `L 40`, `infection` is given as `pandemic` below . Plot the state using `visualize`, and save the plot as a variable `plot before`. Run `k sweeps` sweeps. Plot the state again, and store as `plot after`. Combine the two plots into a single figure using ```julia plot plot before, plot after ``` \"\"\" pandemic CollisionInfectionRecovery 0.5, 0.00001 bind k sweeps Slider 1 10000, default 1000 let N 50 L 40 plot before plot 1 3 replace with your code plot after plot 1 3 plot plot before, plot after end md\"\"\" Exercise 3.3 Every time that you move the slider, a completely new simulation is created an run. This makes it hard to view the progress of a single simulation over time. So in this exercise, we we look at a single simulation, and plot the S, I and R curves. 👉 Plot the SIR curves of a single simulation, with the same parameters as in the previous exercise. Use `k sweep max 10000` as the total number of sweeps. \"\"\" k sweep max 10000 let N 50 L 30 agents initialize N, L compute k sweep max number of sweeps and plot the SIR end md\"\"\" Exercise 3.4 optional Let's make our plot come alive There are two options to make our visualization dynamic 👉1️⃣ Precompute one simulation run and save its intermediate states using `deepcopy`. You can then write an interactive visualization that shows both the state at time t using `visualize` and the history of S , I and R from time 0 up to time t . t is controlled by a slider. 👉2️⃣ Use ` gif` from Plots.jl to turn a sequence of plots into an animation. Be careful to skip about 50 sweeps between each animation frame, otherwise the GIF becomes too large. This an optional exercise, and our solution to 2️⃣ is given below. \"\"\" let N 50 L 30 missing end md\"\"\" Exercise 3.5 👉 Using L 20 and N 100 , experiment with the infection and recovery probabilities until you find an epidemic outbreak. Take the recovery probability quite small. Modify the two infections below to match your observations. \"\"\" causes outbreak CollisionInfectionRecovery 0.5, 0.001 does not cause outbreak CollisionInfectionRecovery 0.5, 0.001 md\"\"\" Exercise 3.6 👉 With the parameters of Exercise 3.2, run 50 simulations. Plot S , I and R as a function of time for each of them with transparency . This should look qualitatively similar to what you saw in the previous homework. You probably need different `p infection` and `p recovery` values from last week. Why? \"\"\" need different parameters because md\"\"\" i say so \"\"\" md\"\"\" Exercise 4 Optional Effect of socialization In this exercise we'll modify the simple mixing model. Instead of a constant mixing probability, i.e. a constant probability that any pair of people interact on a given day, we will have a variable probability associated with each agent, modelling the fact that some people are more or less social or contagious than others. \"\"\" md\"\"\" Exercise 4.1 We create a new agent type `SocialAgent` with fields `position`, `status`, `num infected`, and `social score`. The attribute `social score` represents an agent's probability of interacting with any other agent in the population. \"\"\" struct SocialAgent here... md\"\"\"define the `position` and `color` methods for `SocialAgent` as we did for `Agent`. This will allow the `visualize` function to work. on both kinds of Agents\"\"\" begin position a SocialAgent ... color a SocialAgent ... end md\"\"\" 👉 Create a function `initialize social` that takes `N` and `L`, and creates N agents within a 2L x 2L box, with `social score`s chosen from 10 equally spaced between 0.1 and 0.5. see LinRange \"\"\" function initialize social N, L return missing end md\"\"\" Now that we have 2 agent types 1. let's create an AbstractAgent type 2. Go back in the notebook and make the agent types a subtype of AbstractAgent. \"\"\" md\"\"\" Exercise 4.2 Not all two agents who end up in the same grid point may actually interact in an infectious way they may just be passing by and do not create enough exposure for communicating the disease. 👉 Write a new `interact ` method on `SocialAgent` which adds together the social scores for two agents and uses that as the probability that they interact in a risky way. Only if they interact in a risky way, the infection is transmitted with the usual probability. \"\"\" function interact agent SocialAgent, source SocialAgent, infection CollisionInfectionRecovery your code here end md\"\"\" Make sure `step `, `position`, `color`, work on the type `SocialAgent`. If `step ` takes an untyped first argument, it should work for both Agent and SocialAgent types without any changes. We actually only need to specialize `interact ` on SocialAgent. Exercise 4.3 👉 Plot the SIR curves of the resulting simulation. N 50 L 40 number of steps 200 In each step call `step ` 50N times. \"\"\" let N 50 L 40 global social agents initialize social N, L Ss, Is, Rs , , Tmax 200 gif for t in 1 Tmax 1. Step a lot 2. Count S, I and R, push them to Ss Is Rs 3. call visualize on the agents, 4. place the SIR plot next to visualize. plot left, right, size 600,300 final plot end end md\"\"\" Exercise 4.4 👉 Make a scatter plot showing each agent's `social score` on one axis, and the `num infected` from the simulation in the other axis. Run this simulation several times and comment on the results. \"\"\" md\"\"\" 👉 Run a simulation for 100 steps, and then apply a \"lockdown\" where every agent's social score gets multiplied by 0.25, and then run a second simulation which runs on that same population from there. What do you notice? How does changing this factor form 0.25 to other numbers affect things? \"\"\" md\"\"\" Exercise 5 Optional Effect of distancing We can use a variant of the above model to investigate the effect of the mis named \"social distancing\" we want people to be socially close, but physically distant . In this variant, we separate out the two effects \"infection\" and \"movement\" an infected agent chooses a neighbouring site, and if it finds a susceptible there then it infects it with probability p I . For simplicity we can ignore recovery. Separately, an agent chooses a neighbouring site to move to, and moves there with probability p M if the site is vacant. Otherwise it stays where it is. When p M 0 , the agents cannot move, and hence are completely quarantined in their original locations. 👉 How does the disease spread in this case? \"\"\" md\"\"\" 👉 Run the dynamics repeatedly, and plot the sites which become infected. \"\"\" md\"\"\" 👉 How does this change as you increase the density \\rho N L^2 of agents? Start with a small density. This is basically the site percolation https en.wikipedia.org wiki Percolation theory model. When we increase p M , we allow some local motion via random walks. \"\"\" md\"\"\" 👉 Investigate how this leaky quarantine affects the infection dynamics with different densities. \"\"\" if student.name \"Jazzy Doe\" md\"\"\" danger \"Before you submit\" Remember to fill in your name and Kerberos ID at the top of this notebook. \"\"\" end md\" Function library Just some helper functions used in the notebook.\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text hint md\"\"\" After every sweep, count the values S , I and R and push them to 3 arrays. \"\"\" hint md\"\"\" ```julia let N 50 L 40 x initialize N, L initialize to empty arrays Ss, Is, Rs Int , Int , Int Tmax 200 gif for t in 1 Tmax for i in 1 50N step x, L, pandemic end ... track S, I, R in Ss Is and Rs left visualize x, L right plot xlim 1,Tmax , ylim 1,N , size 600,300 plot right, 1 t, Ss, color color S , label \"S\" plot right, 1 t, Is, color color I , label \"I\" plot right, 1 t, Rs, color color R , label \"R\" plot left, right end end ``` \"\"\" almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text still missing text md\"Replace `missing` with your answer.\" Markdown.MD Markdown.Admonition \"warning\", \"Here we go \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text yays md\"Fantastic \", md\"Splendid \", md\"Great \", md\"Yay ❤\", md\"Great 🎉\", md\"Well done \", md\"Keep it up \", md\"Good job \", md\"Awesome \", md\"You got the right answer \", md\"Let's move on to the next section.\" correct text rand yays Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text let we need to call Base. instead of to make Pluto understand what's going on oops if isdefined Coordinate result Base. Coordinate 3,4 , Coordinate 10,10 if result isa Missing still missing elseif result isa Coordinate keep working md\"Make sure that your return a `Coordinate`. 🧭\" elseif result.x 13 || result.y 14 keep working else correct end end end not defined variable name Markdown.MD Markdown.Admonition \"danger\", \"Oopsie \", md\"Make sure that you define a variable called Markdown.Code string variable name \" if isdefined origin not defined origin else let if origin isa Missing still missing elseif origin isa Coordinate keep working md\"Make sure that `origin` is a `Coordinate`.\" else if origin Coordinate 0,0 correct else keep working end end end end if isdefined make tuple not defined make tuple else let result make tuple Coordinate 2,1 if result isa Missing still missing elseif result isa Tuple keep working md\"Make sure that you return a `Tuple`, like so `return 1, 2 `.\" else if result 2, 1 correct else keep working end end end end if isdefined trajectory not defined trajectory else let c Coordinate 8,8 t trajectory c, 100 if t isa Missing still missing elseif t isa Vector keep working md\"Make sure that you return a `Vector`.\" elseif all x isa x, Coordinate , t keep working md\"Make sure that you return a `Vector` of `Coordinate`s.\" else if length t 100 almost md\"Make sure that you return `n` elements.\" elseif 1 length Set t 90 correct else keep working md\"Are you sure that you chose each step randomly?\" end end end end if isdefined initialize not defined initialize else let N 200 result initialize N, 1 if result isa Missing still missing elseif result isa Vector || length result N keep working md\"Make sure that you return a `Vector` of length `N`.\" elseif any e e isa Agent , result keep working md\"Make sure that you return a `Vector` of `Agent`s.\" elseif length Set result N keep working md\"Make sure that you create `N` new `Agent`s. Do not repeat the same agent multiple times.\" elseif sum a a.status S, result N 1 && sum a a.status I, result 1 if 8 length Set a.position for a in result 9 correct else keep working md\"The coordinates are not correctly sampled within the box.\" end else keep working md\"`N 1` agents should be Susceptible, 1 should be Infectious.\" end end end bigbreak html\" br br br br \" bigbreak bigbreak bigbreak bigbreak bigbreak "},{"url":"images_abstractions/abstraction/","title":"Abstraction","tags":["lecture","module1","track_julia","track_math"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v 3zTO3LEY cM\" image \"\" section 2 order 2 title \"Abstraction\" layout \"layout.jlhtml\" youtube id \"3zTO3LEY cM\" description \"\" tags \"lecture\", \"module1\", \"track julia\", \"track math\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using PlutoUI using ImageShow, FileIO, ImageIO end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\" Introduction The goal of this section is to introduce you to the notion of abstraction. You can think of abstraction as an opposite to specialization. We will illustrate this by looking at the following example. What is one ? Before we get lost talking about the foundations of number theory, I will present you with a few examples that represent one to me. \" md\"Each of the items in this list is a specific, or specialized representation of one 1. as an integer 1. as a float 1. as a string 1. as a rational number 1. as a cute picture 1. as an 2x2 identity matrix 1. as a singular dog Of course, these are just a few examples of one . People have been representing one for ages in different languages, scripts, artistic expression, etc. The difference between these ones to me is clear. In fact, I just articulated it to you. Now, let's turn to how a computer sees one differently based on what I type. \" md\"So to a computer, all of these are different types.\" md\" What is a collection of one s? Now, I want to make a collection of ones for some reason. Below is a way for you to experiment building this collection with different one s. As you do this experiment, I want you to look at what stays in the same in the Julia output, and what doesn't.\" md\"\"\" Before we even look at the output, I am amazed that this code even ran. Are you telling me that the computer doesn't care which one I am using in my array? Yes That's exactly what abstraction is. By stepping back, we can now think and operate at a level that doesn't care about which specific one I am using. This is what we mean by abstraction is the opposite of specialization . The information that Julia gives back is quite informative. Here is an example of the first line of the output for a few different types ``` array 3x4 Array Int64, 2 array 3x4 Array Float64, 2 array 3x4 Array Rational Int64 , 2 ``` Notice that for all of these, we have the same `3x4 Array , 2 `. \"\"\" md\"\"\" First Taste of Abstraction Now, I want to do something to a collection of ones, that doesn't care about which one I'm using. So I'm going to write a function that takes in my collection, and add a corgi whereever I desire. \"\"\" function insert new, A, i, j B copy A B i,j new return B end begin A fill 0, 3, 4 md\"\"\" bind i Slider 1 size A,1 , show value true bind j Slider 1 size A,2 , show value true \"\"\" end begin one number array fill 1,3,4 insert 5, one number array, i, j end md\"This is still amazing. I wrote one function that just cares about how to insert an object into an array, without knowing anything about what's inside, and it worked for two completely different arrays, collections of ones of any kind .\" md\" Conclusion The key idea here is that a computer language should allow you to do operations that make sense. Often times, an operation can make sense for many different objects. So we can abstract away the specifics of the object in our implementation. It should let you step back from there. \" md\"\"\" Appendix \"\"\" begin oneimage load download \"https user images.githubusercontent.com 6933510 199281680 5ac1e8ea c68c 4fb5 a3bf b47ad28057d1.png\" corgi load download \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" nothing end one 1, 1.0, \"one\", 1 1, oneimage, 1 0 0 1 , corgi, computer ones typeof. one begin one keys \"1\", \"1.0\", \"one\", \"1 1\", \"Cute One\", \"2x2 Identity\", \"One Corgi\" selections one keys . one keys lookup element Dict one keys . one md\" bind element key Select selections \" end your chosen one element lookup element element key its type typeof element a 3x4 array of this one. array fill element,3,4 begin one image array fill oneimage,3,4 insert corgi, one image array, i, j end "},{"url":"images_abstractions/dynamicprograms/","title":"Dynamic Programming","tags":["lecture","module1","optimization","programming","track_julia","track_math","interactive","discrete","lazy"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v KyBXJV1zFlo\" image \"https user images.githubusercontent.com 6933510 136196599 c6ae60f0 9269 4134 bb0d 5bcab928bd2b.gif\" section 7 order 7 title \"Dynamic Programming\" layout \"layout.jlhtml\" youtube id \"KyBXJV1zFlo\" description \"\" tags \"lecture\", \"module1\", \"optimization\", \"programming\", \"track julia\", \"track math\", \"interactive\", \"discrete\", \"lazy\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin import ImageIO using Plots, PlutoUI, Colors, Images end md\"\"\" What is dynamic programming? The word \"programming\" here is a rather archaic word but still in use for an optimization problem , as used, for example, in the phrase \"linear programming.\" Probably the word \"programming\" should be abandoned in this context, but no doubt it is too late. \"\"\" md\"\"\" Summing over paths problem \"\"\" md\"\"\" Let's start by looking at the following problem. Given a matrix, we follow a path from top to bottom. The paths start at one of the square on the top, and can only go downwards, either South East , South , or South West . We will add up the numbers visited along each path. Our goal is to find the path that has the smallest sum . This is indeed an optimization problem in the set of all possible paths, we want to find a path that minimizes the sum of visited squares. \"\"\" md\"\"\" Try it yourself \"\"\" md\"\"\" Board size \"\"\" const n 8 M rand 0 9, n, n md\"\"\" One way to solve this problem is the naive algorithm where we enumerate all the paths, calculate the sum for each, and take the minimum. However, as the matrix gets larger the total number of paths grows exponentially . \"\"\" md\"\"\" Possible research problem Investigate the statistics of the sums over all possible paths. \"\"\" md\"\"\" Fixing a single point on a path \"\"\" md\"\"\" Let's fix a given point i, j and focus only on all those paths that pass through i, j . \"\"\" md\"\"\" i bind fixi Scrubbable 1 n j bind fixj Scrubbable 1 n \"\"\" md\"\"\" Suppose we fix the point on the penultimate row last but one . When we look at the paths below the fixed value, we're doing the same calculation over and over again. It doesn't seem sensible to keep re doing these calculations. The same holds as we move the fixed point further upwards. So instead of calculating by working \"forwards\", for each box we look at the minimum below it. \"\"\" md\"\"\" The idea of overlapping subproblems \"\"\" md\"\"\" The key point in this problem is that there are overlapping subproblems there are calculations that we don't need to repeat. The idea of dynamic programming is to remember the solution of those subproblems to get an exponential speed up in the calculation speed. \"\"\" begin struct Paths m Int n Int end Base.iterate p Paths fill 1,p.m , fill 1,p.m start the iteration with 1's Base.IteratorSize Type Paths Base.SizeUnknown function Base.iterate p Paths, state if state ≠ fill p.n,p.m end when each row has an n newstate next state,p.n return newstate, newstate end end function next path,n k length path start from the end and find the first element that can be updated by adding 1 while k≥2 && path k n || path k 1 path k 1 1 k 1 end path k 1 add the one then reset the following elements for j k 1 length path path j max path j 1 1,1 end return path end function allpaths m,n Vector Int copy p for p in Paths m,n end end begin paths allpaths n,n numpaths length paths md\"There are numpaths paths to check.\" end md\"\"\" Path bind whichpath Slider 1 numpaths show value true \"\"\" fixedpaths p for p∈paths if p fixi fixj begin number of fixedpaths length fixedpaths md\"Number of fixed paths number of fixedpaths\" end md\"\"\" Path bind whichfixedpath Slider 1 number of fixedpaths show value true \"\"\" md\"\"\" Summing Paths Demo \"\"\" begin winnernum argmin sum M i,p i for i 1 n for p∈paths winner paths winnernum winnertotal sum M i,winner i for i 1 n end md\"\"\" Our goal is to add the numbers on a path and find the minimal path. The winner is number winnernum. \"\"\" md\"\"\" Appendix \"\"\" function setup board M n size M,1 rectangle w, h, x, y Shape x . 0,w,w,0 , y . 0,0,h,h p plot the board for i 1 n, j 1 n if rem i j,2 0 plot p, rectangle 1,1,i,j , opacity .2, color red end end for i 1 n, j 1 n annotate p, j .5 ,n 2 i .5 , M i,j end plot p, legend false, aspectratio 1, xlims 1,n 1 , ylims 1,n 1 , axis nothing return p end rectangle w, h, x, y Shape x . 0,w,w,0 , y . 0,0,h,h function draw path p, path, indices 1 n kwargs... draw the line plot p, path indices . .5, n 1 . indices . .5 linewidth 4, kwargs... add little squares as background for the number in a square for j in n 1 . indices i path 1 n j plot p, rectangle .4,.4,i .3,j .3 , opacity 1, color RGB 0,1,0 , linewidth 0,fillcolor ifelse rem i j,2 0, RGBA 1,.85,.85,.2 , white end p end path text values \" join values, \" \" sum values \" let path fixedpaths whichfixedpath values M i,path i for i 1 n p setup board M annotate p, fixj .5 ,n 2 fixi .5 , M fixi,fixj , red draw path p, path, 1 fixi color black draw path p, path, fixi n color blue plot p title path text values end function viz whichpath path paths whichpath values M i,path i for i 1 n nv length values rectangle w, h, x, y Shape x . 0,w,w,0 , y . 0,0,h,h p setup board M The winning path draw path p, winner color RGB 1,.6,.6 , your path draw path p, path color black, text plot p, title path text values plot p, xlabel path text M i,winner i for i 1 n , xguidefontcolor RGB 1,.5,.5 p end viz whichpath "},{"url":"images_abstractions/images/","title":"Images as Data and Arrays","tags":["lecture","module1","philip","track_julia","matrix","image"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v 3zTO3LEY cM\" image \"https user images.githubusercontent.com 6933510 136196634 2294d0a7 e79a 40d0 bbb8 81da70f4d398.png\" section 1 order 1 title \"Images as Data and Arrays\" layout \"layout.jlhtml\" youtube id \"3zTO3LEY cM\" description \"\" tags \"lecture\", \"module1\", \"philip\", \"track julia\", \"matrix\", \"image\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using PlutoUI using PlutoTeachingTools using HypertextLiteral end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Images as examples of data all around us Welcome to the Computational Thinking using Julia for Real World Problems, at MIT in Spring 2021 The aim of this course is to bring together concepts from computer science and applied math with coding in the modern Julia language , and to see how to apply these techniques to study interesting applications and of course to have fun . We would be pleased if students who have been interested in computer science now become interested in computational science and those interested in scientific applications learn computer science they may not see elsewhere. ... and for all students, we wish to share the value of the Julia language as the best of both worlds. \"\"\" md\"\"\" Alan's Essay Are all programming languages the same? Superficially, many programming languages are very similar. \"Showoffs\" will compare functional programming vs imperative programming. Others will compare compiled languages vs dynamic languages. I will avoid such fancy terms in this little essay, preferring to provide this course's pedagogical viewpoint. Generally speaking beginning programmers should learn to create \"arrays\" write \"for loops\", \"conditionals\", \"comparisons\", express mathematical formulas, etc. So why Julia at a time when Python seems to be the language of teaching, and Java and C so prominent in the corporate world? As you might imagine, we believe Julia is special. Oh you will still have the nitty gritty of when to use a bracket and a comma. You might have strong opinions as to whether arrays should begin with 0 or 1 joke some say it's time to compromise and use ½. Getting past these irrelevant issues, you will begin to experience one by one what makes Julia so very special. For starters, a language that runs fast is more fun. We can have you try things that would just be so slow in other languages it would be boring. We also think you will start to notice how natural Julia is, how it feels like the mathematics, and how flexible it can be. Getting to see the true value of fancy terms like multiple dispatch, strong typing, generic programming, and composable software will take a little longer, but stick with us, and you too will see why Julia is so very special. \"\"\" md\"\"\" Computer Science and Computational Science Working Together \"\"\" md\"\"\" Applications of computer science in the real world use data , i.e. information that we can measure in some way. Data take many different forms, for example Numbers that change over time time series Stock price each second minute day Weekly number of infections Earth's global average temperature Video The view from a window of a self driving car A hurricane monitoring station Ultrasound e.g. prenatal Images Diseased versus healthy tissue in a medical scan Pictures of your favourite dog \"\"\" md\"\"\" Exercise Think of another two examples in each category. Can you think of other categories of data? \"\"\" md\"\"\" Computational science can be summed up by a simplified workflow \"\"\" md\"\"\" data ⟶ input ⟶ process ⟶ model ⟶ visualize ⟶ output \"\"\" md\"\"\" html\" br \" To use any data source, we need to input the data of interest, for example by downloading it, reading in the resulting file, and converting it into a form that we can use in the computer. Then we need to process it in some way to extract information of interest. We usually want to visualize the results, and we may want to output them, for example by saving to disc or putting them on a website. We often want to make a mathematical or computational model that can help us to understand and predict the behavior of the system of interest. In this course we aim to show how programming, computer science and applied math combine to help us with these goals. \"\"\" md\"\"\" Data Images as an example of data Let's start off by looking at images and how we can process them. Our goal is to process the data contained in an image in some way, which we will do by developing and coding certain algorithms . Here is the the Fall 2020 version of this lecture small variations by 3 Blue 1 Brown Grant Sanderson for your reference. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid DGojI9xcCfg params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" If we open an image on our computer or the web and zoom in enough, we will see that it consists of many tiny squares, or pixels \"picture elements\" . Each pixel is a block of one single colour, and the pixels are arranged in a two dimensional square grid. You probably already know that these pixels are stored in a computer numerically perhaps in some form of RGB red,green,blue format. This is the computer's representation of the data. Note that an image is already an approximation of the real world it is a two dimensional, discrete representation of a three dimensional reality. \"\"\" md\"\"\" Input and Visualize loading and viewing an Image in Julia \"\"\" md\"\"\" Let's use Julia to load actual images and play around with them. We can download images from the internet, your own file, or your own webcam. \"\"\" md\"\"\" Downloading an image from the internet or a local file We can use the `Images.jl` package to load an image file in three steps. \"\"\" md\"\"\" Step 1 from internet we specify the URL web address to download from html\" br \" note that Pluto places results before commands because some people believe output is more interesting than code. This takes some getting used to. \"\"\" url \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" md\"\"\" Step 2 Now we use the aptly named `download` function to download the image file to our own computer. Philip is Prof. Edelman's corgi. \"\"\" philip filename download url download to a local file. The filename is returned md\"\"\" Step 3 Using the `Images.jl` package loaded at the start of this notebook scroll up and take a look. we can load the file, which automatically converts it into usable data. We'll store the result in a variable. Remember the code is after the output. \"\"\" philip load philip filename md\" Hi there Philip \" md\"\"\" We see that the Pluto notebook has recognised that we created an object representing an image, and automatically displayed the resulting image of Philip, the cute Welsh Pembroke corgi and co professor of this course. Poor Philip will undergo quite a few transformations as we go along \"\"\" md\"\"\" 👉 Exercise change the url to use another image from the web. \"\"\" md\"\"\" Capturing an Image from your own camera \"\"\" md\"\"\" Even more fun is to use your own webcam. Try pressing the enable button below. Then press the camera to capture an image. Kind of fun to keep pressing the button as you move your hand etc. \"\"\" bind myface1 PlutoUI.WebcamInput help false, max size 150 myface1 myface1 , end 1 1 myface1 end 1 1, myface1 end 1 1, end 1 1 md\"\"\" Inspecting your data \"\"\" md\"\"\" Image size \"\"\" md\"\"\" The first thing we might want to know is the size of the image \"\"\" philip size size philip md\"\"\" Julia returns a pair of two numbers. Comparing these with the picture of the image, we see that the first number is the height, i.e. the vertical number of pixels, and the second is the width. \"\"\" philip height philip size 1 philip width philip size 2 md\"\"\" Locations in an image Indexing Now suppose that we want to examine a piece of the image in more detail. We need some way of specifying which piece of the image we want. Thinking of the image as a grid of pixels, we need a way to tell the computer which pixel or group of pixels we want to refer to. Since the image is a two dimensional grid, we can use two integers whole numbers to give the coordinates of a single pixel. Specifying coordinates like this is called indexing think of the index of a book, which tells you on which page an idea is discussed. In Julia we use square brackets, ` ` and ` ` for indexing \"\"\" a pixel philip 200, 100 md\"\"\" We see that Julia knows to draw our pixel object for us a block of the relevant color. When we index into an image like this, the first number indicates the row in the image, starting from the top, and the second the column , starting from the left. In Julia, the first row and column are numbered starting from 1, not from 0 as in some other programming languages. \"\"\" md\"\"\" We can also use variables as indices... \"\"\" md\"\"\" ...and these variables can be controlled by sliders \"\"\" bind row i Slider 1 size philip 1 , show value true bind col i Slider 1 size philip 2 , show value true row i,col i philip row i, col i md\"\"\" Locations in an image Range indexing We saw that we can use the row number and column number to index a single pixel of our image. Next, we will use a range of numbers to index multiple rows or columns at once, returning a subarray \"\"\" philip 550 650, 1 philip width md\"\"\" Here, we use `a b` to mean \" all numbers between `a` and `b` \". For example \"\"\" collect 1 10 md\"\"\" You can also use a ` ` without start and end to mean \" every index \" \"\"\" philip 550 650, md\"\"\" Let's get a single row of pixels \"\"\" philip 550, philip head philip 470 800, 140 410 md\"\"\" Scroll in on Philip's nose Use the widgets below slide left and right sides . \"\"\" bind range rows RangeSlider 1 size philip head 1 bind range cols RangeSlider 1 size philip head 2 nose philip head range rows, range cols md\"\"\" Process Modifying an image Now that we have access to image data, we can start to process that data to extract information and or modify it in some way. We might want to detect what type of objects are in the image, say to detect whether a patient has a certain disease. To achieve a high level goal like this, we will need to perform mid level operations, such as detecting edges that separate different objects based on their color. And, in turn, to carry that out we will need to do low level operations like comparing colors of neighboring pixels and somehow deciding if they are \"different\". \"\"\" md\"\"\" Representing colors We can use indexing to modify a pixel's color. To do so, we need a way to specify a new color. Color turns out to be a complicated concept, having to do with the interaction of the physical properties of light with the physiological mechanisms and mental processes by which we detect it We will ignore this complexity by using a standard method of representing colours in the computer as an RGB triple , i.e. a triple of three numbers r, g, b , giving the amount of red, of green and of blue in a colour, respectively. These are numbers between 0 none and 1 full . The final colour that we perceive is the result of \"adding\" the corresponding amount of light of each colour the details are fascinating, but beyond the scope of this course \"\"\" md\"\"\" We can create a new color in Julia as follows \"\"\" RGB 1.0, 0.0, 0.0 begin md\"\"\" A pixel with bind test r Scrubbable 0 0.1 1 default 0.1 red, bind test g Scrubbable 0 0.1 1 default 0.5 green and bind test b Scrubbable 0 0.1 1 default 1.0 blue looks like \"\"\" end RGB test r, test g, test b md\"\"\" Exercise 2.5 👉 Write a function `invert` that inverts a color, i.e. sends r, g, b to 1 r, 1 g, 1 b . \"\"\" function invert color AbstractRGB return missing end md\"Let's invert some colors \" color black RGB 0.0, 0.0, 0.0 invert color black color red RGB 0.8, 0.1, 0.1 invert color red md\"Can you invert the picture of Philip?\" philip inverted missing md\"\"\" Modifying a pixel Let's start by seeing how to modify an image, e.g. in order to hide sensitive information. We do this by assigning a new value to the color of a pixel \"\"\" let temp copy philip head temp 100, 200 RGB 1.0, 0.0, 0.0 temp end md\"\"\" Groups of pixels We probably want to examine and modify several pixels at once. For example, we can extract a horizontal strip 1 pixel tall \"\"\" philip head 50, 50 100 md\"\"\" Here, Julia is showing the strip as a collection of rectangles in a row. \"\"\" md\"\"\" And then modify it \"\"\" let temp copy philip head temp 50, 50 100 . RGB 1.0, 0.0, 0.0 temp end md\"\"\" Similarly we can modify a whole rectangular block of pixels \"\"\" let temp copy philip head temp 50 100, 50 100 . RGB 1.0, 0.0, 0.0 temp end md\"\"\" Exercise 1.2 👉 Generate a vector of 100 zeros. Change the center 20 elements to 1. \"\"\" function create bar return missing end if isdefined create bar not defined create bar else let result create bar if ismissing result still missing elseif isnothing result keep working md\"Did you forget to write `return`?\" elseif result isa Vector || length result 100 keep working md\"The result should be a `Vector` with 100 elements.\" elseif result 1,50,100 0,1,0 keep working else correct end end end md\"\"\" Reducing the size of an image \"\"\" md\"\"\" Maybe we would also like to reduce the size of this image, since it's rather large. For example, we could take every 10th row and every 10th column and make a new image from the result \"\"\" reduced image philip 1 10 end, 1 10 end md\"\"\" Note that the resulting image doesn't look very good, since we seem to have lost too much detail. Exercise Think about what we might do to reduce the size of an image without losing so much detail. \"\"\" md\"\"\" Model Creating synthetic images Think about your favorite Pixar movie e.g. Monsters Inc. Movie frames are images that are generated from complicated mathematical models. Ray tracing which may be covered in this class is a method for making images feel realistic. \"\"\" md\"\"\" Output Saving an image to a file Finally, we want to be able to save our new creation to a file. To do so, you can right click on a displayed image, or you can write it to a file. Fill in a path below \"\"\" save \"reduced phil.png\", reduced image md\"\"\" html\" br \" \"\"\" md\"\"\" Computer science Arrays An image is a concrete example of a fundamental concept in computer science, namely an array . Just as an image is a rectangular grid, where each grid cell contains a single color, an array is a rectangular grid for storing data. Data is stored and retrieved using indexing, just as in the image examples each cell in the grid can store a single \"piece of data\" of a given type. Dimension of an array An array can be one dimensional, like the strip of pixels above, two dimensional, three dimensional, and so on. The dimension tells us the number of indices that we need to specify a unique location in the grid. The array object also needs to know the length of the data in each dimension. Names for different types of array One dimensional arrays are often called vectors or, in some other languages, \"lists\" and two dimensional arrays are matrices . Higher dimensional arrays are tensors . Arrays as data structures An array is an example of a data structure , i.e. a way of arranging data such that we can access it. A key theme in computer science is that of designing different data structures that represent data in different ways. Conceptually, we can think of an array as a block of data that has a position or location in space. This can be a useful way to arrange data if, for example, we want to represent the fact that values in nearby locations in array are somehow near to one another. Images are a good example of this neighbouring pixels often represent different pieces of the same object, for example the rug or floor, or Philip himself, in the photo. We thus expect neighbouring pixels to be of a similar color. On the other hand, if they are not, this is also useful information, since that may correspond to the edge of an object. \"\"\" md\"\"\" Julia constructing arrays Creating vectors and matrices Julia has strong support for arrays of any dimension. Vectors, or one dimensional arrays, are written using square brackets and commas \"\"\" 1, 20, \"hello\" RGB 1, 0, 0 , RGB 0, 1, 0 , RGB 0, 0, 1 md\"\"\" Matrices, or two dimensional arrays, also use square brackets, but with spaces and new lines instead of commas \"\"\" RGB 1, 0, 0 RGB 0, 1, 0 RGB 0, 0, 1 RGB 0.5, 0.5, 0.5 md\"\"\" Array comprehensions It's clear that if we want to create an array with more than a few elements, it will be very tedious to do so by hand like this. Rather, we want to automate the process of creating an array by following some pattern, for example to create a whole palette of colors Let's start with all the possible colors interpolating between black, `RGB 0, 0, 0 `, and red, `RGB 1, 0, 0 `. Since only one of the values is changing, we can represent this as a vector, i.e. a one dimensional array. A neat method to do this is an array comprehension . Again we use square brackets to create an array, but now we use a variable that varies over a given range values \"\"\" RGB x, 0, 0 for x in 0 0.1 1 md\"\"\" Here, `0 0.1 1` is a range the first and last numbers are the start and end values, and the middle number is the size of the step. \"\"\" md\"\"\" In a similar way we can create two dimensional matrices, by separating the two variables for each dimension with a comma `,` \"\"\" RGB i, j, 0 for i in 0 0.1 1, j in 0 0.1 1 md\"\"\" Joining matrices We often want to join vectors and matrices together. We can do so using an extension of the array creation syntax \"\"\" philip head philip head philip head reverse philip head, dims 2 reverse philip head, dims 1 rot180 philip head md\"\"\" Pluto Interactivity using sliders \"\"\" md\"\"\" Suppose we want to see the effect of changing the number of colors in our vector or matrix. We could, of course, do so by manually fiddling with the range. It would be nice if we could do so using a user interface , for example with a slider . Fortunately, the Pluto notebook allows us to do so \"\"\" md\"\"\" We can define a slider using \"\"\" bind number reds Slider 1 100, show value true md\"\"\" The `Slider` type is defined in the `PlutoUI.jl` package. \"\"\" md\"\"\" This creates a new variable called `number reds`, whose value is the value shown by the slider. When we move the slider, the value of the variable gets updated. Since Pluto is a reactive notebook, other cells which use the value of this variable will automatically be updated too \"\"\" md\"\"\" Let's use this to make a slider for our one dimensional collection of reds \"\"\" RGB red value number reds, 0, 0 for red value in 0 number reds md\"\"\" When you move the slider, you should see the number of red color patches change \"\"\" md\"\"\" What is going on here is that we are creating a vector in which `red value` takes each value in turn from the range from `0` up to the current value of `number reds`. If we change `number reds`, then we create a new vector with that new number of red patches. \"\"\" md\"\"\" Exercise Make three sliders with variables `r`, `g` and `b`. Then make a single color patch with the RGB color given by those values. \"\"\" md\"\"\" We can do the same to create different size matrices, by creating two sliders, one for reds and one for greens. Try it out \"\"\" md\"\"\" Summary \"\"\" md\"\"\" Let's summarize the main ideas from this notebook Images are arrays of colors We can inspect and modify arrays using indexing We can create arrays directly or using array comprehensions \"\"\" begin colored line x Vector Real Gray. Float64. hcat x ' colored line x Any nothing end colored line create bar bigbreak html\" br br br br br \" bigbreak "},{"url":"images_abstractions/linear_transformation/","title":"linear_transformation","tags":[],"text":" A Pluto.jl notebook v0.19.45 using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using PlutoUI using HypertextLiteral using LinearAlgebra end md\"Show grid lines bind show grid CheckBox default true \" let range 2 .1 2 md\"\"\" a bind a Slider range default 1.0, show value true b bind b Slider range default 0.0, show value true c bind c Slider range default 0.0, show value true d bind d Slider range default 1.0, show value true Re run this cell to reset to identity transformation \"\"\" end A a b c d md\" Det A a d c b det A \" md\"\"\" Appendix \"\"\" det A det A if det A 0 invA inv A end img sources \"https user images.githubusercontent.com 6933510 108605549 fb28e180 73b4 11eb 8520 7e29db0cc965.png\" \"Corgis\", \"https user images.githubusercontent.com 6933510 108883855 39690f80 7606 11eb 8eb1 e595c6c8d829.png\" \"Arrows\", \"https images.squarespace cdn.com content v1 5cb62a904d546e33119fa495 1589302981165 HHQ2A4JI07C43294HVPD ke17ZwdGBToddI8pDm48kA7bHnZXCqgRu4g0 U7hbNpZw zPPgdn4jUwVcJE1ZvWQUxwkmyExglNqGp0IvTJZamWLI2zvYWH8K3 s 4yszcp2ryTI0HqTOaaUohrI8PISCdr 3EAHMyS8K84wLA7X0UZoBreocI4zSJRMe1GOxcKMshLAGzx4R3EDFOm1kBS fluffy corgi?format 2500w\" \"Alan\" md\"\"\" Choose an image bind img source Select img sources \"\"\" img original load download img source typeof img original function trygetpixel img AbstractMatrix, x Float64, y Float64 rows, cols size img \"The linear map 1,1 ↦ 0,1 \" f t t 1.0 1.0 1.0 i floor Int, rows f y j floor Int, cols f x rows cols if 1 i ≤ rows && 1 j ≤ cols img i,j else zero eltype img end end function with gridlines img Array Any,2 n 8 sep i size img, 1 ÷ n sep j size img, 2 ÷ n result copy img stroke zero eltype img RGBA RGB 1,1,1 , 0.75 stroke RGBA 1, 1, 1, 0.75 result 1 sep i end, . stroke result , 1 sep j end . stroke a second time, to create a line 2 pixels wide result 2 sep i end, . stroke result , 2 sep j end . stroke return result end img if show grid with gridlines img original else img original end if det A 0 RGB 1.0, 1.0, 1.0 else in x, in y invA out x, out y trygetpixel img, in x, in y end for out y in LinRange 2, 2, 300 , out x in LinRange 2, 2, 300 "},{"url":"images_abstractions/newton_method/","title":"The Newton Method","tags":["lecture","module1","track_math","continuous","differentiation","automatic differentiation","ForwardDiff","interactive","Symbolics","transformation"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v Wjcx9sNSLP8\" image \"https user images.githubusercontent.com 6933510 136196605 b6119b9d 223c 44bc 97d5 ef7cfce66483.gif\" section 6 order 6 title \"The Newton Method\" layout \"layout.jlhtml\" youtube id \"Wjcx9sNSLP8\" description \"This lecture explains a method for finding the root of a function, but using code an illustrations instead of a chalkboard We will illustrate the Newton method using the ForwardDiff.jl package to carry out automatic differentiation, but we will also try to understand what's going on \\\"under the hood\\\".\" tags \"lecture\", \"module1\", \"track math\", \"continuous\", \"differentiation\", \"automatic differentiation\", \"ForwardDiff\", \"interactive\", \"Symbolics\", \"transformation\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Symbolics, ForwardDiff, Plots, PlutoUI, LaTeXStrings using ForwardDiff jacobian end md\"\"\" Solving equations and finding inverse transformations using the Newton method \"\"\" md\"\"\" In science and engineering we often need to solve systems of equations . If the equations are linear then linear algebra tells us a general method to solve them these are now routinely applied to solve systems of millions of linear equations. If the equations are non linear then things are less obvious. The main solution methods we know work by... reducing the nonlinear equations to a sequence of linear equations They do this by approximating the function by a linear function and solving that to get a better solution, then repeating this operation as many times as necessary to get a sequence of increasingly better solutions. This is an example of an iterative algorithm . A well known and elegant method, which can be used in many different contexts, is the Newton method . It does, however, have the disadvantage that it requires derivatives of the function. This can be overcome using automatic differentiation techniques. We will illustrate the Newton method using the `ForwardDiff.jl` package to carry out automatic differentiation, but we will also try to understand what's going on \"under the hood\". \"\"\" md\"\"\" The Newton method in 1D We would like to solve equations like f x g x . We rewrite that by moving all the terms to one side of the equation so that we can write h x 0 , with h x f x g x . A point x^ such that h x^ 0 is called a root or zero of h . The Newton method finds zeros, and hence solves the original equation. \"\"\" md\"\"\" The idea of the Newton method is to follow the direction in which the function is pointing We do this by building a tangent line at the current position and following that instead, until it hits the x axis. Let's look at that visually first \"\"\" md\"\"\" n bind n2 Slider 0 10, show value true, default 0 \"\"\" md\"\"\" x₀ bind x02 Slider 10 10, show value true, default 6 \"\"\" md\"\"\" n bind n Slider 0 10, show value true, default 0 \"\"\" md\"\"\" x₀ bind x0 Slider 10 10, show value true, default 6 \"\"\" md\"\"\" Using symbolic calculations to understand derivatives and nonlinear maps \"\"\" md\"\"\" We can use Julia's new symbolic capabilities to understand what's going on with a nonlinear polynomial function. Let's see what happens if we perturb a function f around a point z by a small amount \\eta . \"\"\" variables z, η md\"\"\" m bind m Slider 1 6, show value true \"\"\" f x x^m 2 f′ x ForwardDiff.derivative f, x f z f z η md\"\"\" When \\eta is small, \\eta^2 is very small, so we can ignore it. We are left with terms that either don't contain \\eta constants , or multiply \\eta linear . The part that multiplies \\eta is the derivative \"\"\" f′ z f z η f′ z md\"\"\" The derivative gives the \" linear part\" of the function. `ForwardDiff.jl`, and forward mode automatic differentiation in general, effectively uses this although not symbolically in this sense to just propagate the linear part of each function through a calculation. \"\"\" md\"\"\" Mathematics of the Newton method \"\"\" md\"\"\" We can convert the idea of \"following the tangent line\" into equations as follows. You can also do so by just looking at the geometry in 1D, but that does not help in 2D. \"\"\" md\"\"\" Suppose we have a guess x 0 for the root and we want to find a hopefully better guess x 1 . Let's set x 1 x 0 \\delta , where x 1 and \\delta are still unknown. We want x 1 to be a root, so \"\"\" md\"\"\" f x 1 f x 0 \\delta \\simeq 0 \"\"\" md\"\"\" If we are already \"quite close\" to the root then \\delta should be small, so we can approximate f using the tangent line f x 0 \\delta \\, f' x 0 \\simeq 0 and hence \\delta \\simeq \\frac f x 0 f' x 0 so that x 1 x 0 \\frac f x 0 f' x 0 Now we can repeat so that x 2 x 1 \\frac f x 1 f' x 1 and in general x n 1 x n \\frac f x n f' x n . This is the Newton method in 1D. \"\"\" md\"\"\" Implementation in 1D \"\"\" function newton1D f, x0 f′ x ForwardDiff.derivative f, x \\prime TAB x0 37.0 starting point sequence x0 x x0 for i in 1 10 x f x f′ x end return x end newton1D x x^2 2, 37.0 sqrt 2 md\"\"\" Symbolic derivative in 2D \"\"\" md\"\"\" Let's see what happens when we perturb by small amounts \\delta in the x direction and \\epsilon in the y direction around the point a, b \"\"\" md\"\"\" p bind p Slider 0 0.01 1, show value true \"\"\" variables a, b, δ, ϵ md\"\"\" Newton for transformations in 2 dimensions T \\mathbb R ^2 \\to \\mathbb R ^2 \"\"\" md\"\"\" We want to find the inverse T^ 1 y , i.e. to solve the equation T x y for x . We use the same idea as in 1D, but now in 2D \"\"\" md\"\"\" T x 0 \\delta \\simeq 0 T x 0 J \\cdot \\delta \\simeq 0, where J DT x 0 is the Jacobian matrix of T at x 0 , i.e. the best linear approximation of T near to x 0 . \"\"\" md\"\"\" Hence \\delta is the solution of the system of linear equations \"\"\" md\"\"\" J \\cdot \\delta T x 0 Then we again construct the new approximation x 1 as x 1 x 0 \\delta . \"\"\" md\"\"\" In 2D we have an explicit formula for the inverse of the matrix. \"\"\" md\"\"\" Implementation in 2D \"\"\" function newton2D step T, x J ForwardDiff.jacobian T, x should use StaticVectors δ J \\ T x J^ 1 T x return x δ end \"Looks for x such that T x 0\" function newton2D T, x0, n 10 x x0 for i in 1 n x newton2D step T, x end return x end md\"\"\" Remember that Newton is designed to look for roots , i.e. places where T x 0 . We want T x y , so we need another layer \"\"\" \"Looks for x such that f x y, i.e. f x y 0\" function inverse f, y, x0 0, 0 return newton2D x f x y, x0 end inverse f y inverse f, y straight x0, y0, x, m y0 m x x0 function standard Newton f, n, x range, x0, ymin 10, ymax 10 f′ x ForwardDiff.derivative f, x p plot f, x range, lw 3, ylim ymin, ymax , legend false, size 400, 300 hline 0.0 , c \"magenta\", lw 3, ls dash scatter x0 , 0 , c \"green\", ann x0, 5, L\"x 0\", 10 for i in 1 n plot x0, x0 , 0, f x0 , c gray, alpha 0.5 scatter x0 , f x0 , c red m f′ x0 plot x range, straight x0, f x0 , x, m for x in x range , c blue, alpha 0.5, ls dash, lw 2 x1 x0 f x0 m scatter x1 , 0 , c \"green\", ann x1, 5, L\"x % i\", 10 x0 x1 end p | as svg end let f x x^2 2 standard Newton f, n2, 1 0.01 10, x02, 10, 70 end let f x 0.2x^3 4x 1 standard Newton f, n, 10 0.01 10, x0, 10, 70 end T α x, y , x α y^2, y α x^2 jacobian T p , a, b .| Text jacobian T p , a, b δ, ϵ md\"\"\" α bind α Slider 0.0 0.01 1.0, show value true \"\"\" T α 0.3, 0.4 inverse T α 0.3, 0.4 T α ∘ inverse T α 0.3, 0.4 md\"\"\" Appendix \"\"\" expand ex simplify ex, polynorm true expand f z η expand f z η f z η f′ z image expand. T p a δ , b ϵ image T p a, b simplify. expand. image T p a, b jacobian T p , a, b δ, ϵ "},{"url":"images_abstractions/seamcarving/","title":"Seam Carving","tags":["lecture","module1","image","matrix","track_julia","track_climate","track_data","optimization","interactive"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v KyBXJV1zFlo\" image \"https user images.githubusercontent.com 6933510 136196584 b3c806a8 aa61 48d9 9e73 30583fcc38bf.gif\" section 8 order 8 title \"Seam Carving\" layout \"layout.jlhtml\" youtube id \"KyBXJV1zFlo\" description \"\" tags \"lecture\", \"module1\", \"image\", \"matrix\", \"track julia\", \"track climate\", \"track data\", \"optimization\", \"interactive\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using ImageMagick using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using ImageFiltering using Plots, PlutoUI Standard libraries using Statistics, LinearAlgebra end md\"\"\" Scroll through the images in this notebook. The idea of seam carving is to shrink an image by removing the \"least interesting\" parts of the image, but without resizing the objects within the image. We want to remove the \"dead space\" within the image. We try to find a \"seam\", i.e. a connected path of pixels from top to bottom of the image, which consists of the \"least important\" pixels, by some measure. We then remove the pixels in that seam to give an image that is one pixel narrower. In order to do this, we need to decide how to measure which pixels are \"important\". \"\"\" md\"\"\" Fall 2020 MIT Class Video from Grant Sanderson Here is Grant Sanderson 3Blue1Brown explaining seam carving using this notebook from the Fall 2020 edition of this class. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid rpB6zQNsbQU params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" The seam carving algorithm We need to specify a notion of importance of pixels. The seam will then sum up the importance of pixels over the seam and pick the seam which minimizes this total importance. We will assign importance as \"the extent to which a pixel sits inside an edge\". So we need to calculate the \"edgeness\" of each pixel. \"\"\" md\"\"\" 1. We will use convolution with Sobel filters for edge detection. 2. Then we will use that to write an algorithm that removes \"uninteresting\" bits of an image in order to shrink it. \"\"\" image urls \"https wisetoast.com wp content uploads 2015 10 The Persistence of Memory salvador deli painting.jpg\", \"https upload.wikimedia.org wikipedia commons thumb 1 17 Gustave Caillebotte Paris Street%3B Rainy Day Google Art Project.jpg 1014px Gustave Caillebotte Paris Street%3B Rainy Day Google Art Project.jpg\", \"https upload.wikimedia.org wikipedia commons thumb 1 17 Gustave Caillebotte Paris Street%3B Rainy Day Google Art Project.jpg 1014px Gustave Caillebotte Paris Street%3B Rainy Day Google Art Project.jpg\", \"https upload.wikimedia.org wikipedia commons thumb c cc Grant Wood American Gothic Google Art Project.jpg 480px Grant Wood American Gothic Google Art Project.jpg\", \"https wisetoast.com wp content uploads 2015 10 The Persistence of Memory salvador deli painting.jpg\", \"https upload.wikimedia.org wikipedia commons thumb 7 7d A Sunday on La Grande Jatte%2C Georges Seurat%2C 1884.jpg 640px A Sunday on La Grande Jatte%2C Georges Seurat%2C 1884.jpg\", \"https upload.wikimedia.org wikipedia commons thumb e ea Van Gogh Starry Night Google Art Project.jpg 758px Van Gogh Starry Night Google Art Project.jpg\", \"https web.mit.edu facilities photos construction Projects stata 1 large.jpg\", image url image urls 1 img load download image url arbitrarily choose the brightness of a pixel as mean of rgb brightness c AbstractRGB mean c.r, c.g, c.b Use a weighted sum of rgb giving more weight to colors we perceive as 'brighter' Based on https www.tutorialspoint.com dip grayscale to rgb conversion.htm brightness c AbstractRGB 0.3 c.r 0.59 c.g 0.11 c.b Gray. brightness. img md\"\"\" Edge detection filter Spoiler alert We use the Sobel edge detection filter we created in our Homework. ```math \\begin align G x & \\begin bmatrix 1 & 0 & 1 \\\\ 2 & 0 & 2 \\\\ 1 & 0 & 1 \\\\ \\end bmatrix \\star A\\\\ 10pt G y & \\begin bmatrix 1 & 2 & 1 \\\\ 0 & 0 & 0 \\\\ 1 & 2 & 1 \\\\ \\end bmatrix \\star A \\end align ``` Here, \\star denotes convolution. Here A is the array corresponding to your image. We can think of G x and G y as calculating discretized derivatives in the x and y directions. Then we combine them by finding the magnitude of the discretized gradient , in the sense of multivariate calculus, by defining G \\text total \\sqrt G x^2 G y^2 . \"\"\" md\"\"\" Here are the Sobel kernels for the derivatives in each direction \"\"\" Sy, Sx Kernel.sobel collect Int. 8 . Sx , collect Int. 8 . Sy plotly surface brightness. img md\"\"\" blue shows positive values red shows negative values \"\"\" function shrink image image, ratio 5 height, width size image new height height ÷ ratio 1 new width width ÷ ratio 1 list mean image ratio i ratio i 1 , ratio j ratio j 1 , for j in 1 new width for i in 1 new height reshape list, new height, new width end function convolve M, kernel height, width size kernel half height height ÷ 2 half width width ÷ 2 new image similar M i, j loop over the original image m, n size M inbounds for i in 1 m for j in 1 n k, l loop over the neighbouring pixels accumulator 0 M 1, 1 for k in half height half height height 1 for l in half width half width width 1 Mi i k Mj j l First index into M if Mi 1 Mi 1 elseif Mi m Mi m end Second index into M if Mj 1 Mj 1 elseif Mj n Mj n end accumulator kernel k, l M Mi, Mj end end new image i, j accumulator end end return new image end function edgeness img Sy, Sx Kernel.sobel b brightness. img ∇y convolve b, Sy ∇x convolve b, Sx sqrt. ∇x.^2 ∇y.^2 end function show colored array array pos color RGB 0.36, 0.82, 0.8 neg color RGB 0.99, 0.18, 0.13 to rgb x max x, 0 pos color max x, 0 neg color to rgb. array maximum abs. array end show colored array Sx , show colored array Sy let ∇y convolve brightness. img , Sy ∇x convolve brightness. img , Sx data md\"``G x``\", md\"``G y``\", zoom in on the clock img 300 end, 1 300 , img 300 end, 1 300 , show colored array. ∇x 300 end, 1 300 , ∇y 300 end, 1 300 ... avoid collating the images into one big matrix todisplay permutedims reshape data, 2,3 , 2,1 assert length todisplay 10 PlutoUI.ExperimentalLayout.grid todisplay end md\"\"\" Seam carving idea The idea of seam carving is to find a path from the top of the image to the bottom of the image where the path minimizes the edgeness. In other words, this path minimizes the number of edges in the image that it crosses . We will call the edgeness the energy . \"\"\" md\"\"\" At every step in going down, the path is allowed to go south west, south or south east. We want to find a connected path, or seam , with the minimum possible sum of \"energies\" along the path. We start by writing a `least edgy` function which takes a matrix of energies and returns a new matrix. The new matrix has entries M i, j which gives the minimum possible energy when starting from the pixel i, j and going from there down to a pixel in the bottom row. \"\"\" e x,y ↙ ↓ ↘ pick the next path which gives the least overall energy e x 1,y 1 e x,y 1 e x 1,y 1 Basic calculation e x,y min e x 1,y 1 , e x,y , e x 1,y `dirs` records which direction we take from 1 SW, 0 S, 1 SE function least edgy E least E zeros size E dirs zeros Int, size E least E end, . E end, the minimum energy on the last row is the energy itself m, n size E Go from the last row up, finding the minimum energy for i in m 1 1 1 for j in 1 n j1, j2 max 1, j 1 , min j 1, n e, dir findmin least E i 1, j1 j2 least E i,j e least E i,j E i,j dirs i, j 1, 0, 1 dir j 1 end end return least E, dirs end The bright areas are screaming \"AVOID ME \" least e, dirs least edgy edgeness img show colored array least e md\"\"\" Here are the directions that we should take at each step \"\"\" direction the path should take at every pixel. reduce x, y x y \"\\n\", reduce , getindex. \" \", \"↙\", \"↓\", \"↘\" , , dirs 1 25, 1 60 . 3 , dims 2, init \"\" , init \"\" | Text md\" Remove seams\" md\"\"\" We now compress an image horizontally by successively removing a number of seams of lowest energy. \"\"\" function get seam at dirs, j m size dirs, 1 js fill 0, m js 1 j for i 2 m js i js i 1 dirs i 1, js i 1 end return tuple. 1 m, js end get seam at dirs, 2 function mark path img, path img′ copy img m size img, 2 for i, j in path To make it easier to see, we'll color not just the pixels of the seam, but also those adjacent to it for j′ in j 1 j 1 img′ i, clamp j′, 1, m RGB 1,0,1 end end return img′ end md\"\"\" In the visualization below, the slider specifies which column we start with at the top. The pink seam is the best least total energy that will be snipped out. \"\"\" bind start column Slider 1 size img, 2 , show value true path get seam at dirs, start column function pencil X f x RGB 1 x,1 x,1 x map f, X . maximum X end e edgeness img md\"\"\" Lowest energy path We can use `findmin` to find the path with the least energy \"\"\" function rm path img, path img′ img , 1 end 1 one less column for i, j in path img′ i, 1 j 1 . img i, 1 j 1 img′ i, j end . img i, j 1 end end img′ end function shrink n img, n imgs marked imgs e edgeness img for i 1 n least E, dirs least edgy e , min j findmin view least E 1, seam get seam at dirs, min j img rm path img, seam Recompute the energy for the new image Note, this currently involves rerunning the convolution on the whole image, but in principle the only values that need recomputation are those adjacent to the seam, so there is room for a meanintful speedup here. e edgeness img e rm path e, seam push imgs, img push marked imgs, mark path img, seam end imgs, marked imgs end n examples min 200, size img, 2 returns two vectors of n successively smaller images The second images have markings where the seam is cut out carved, marked carved shrink n img, n examples md\"\"\" Seam carving in action \"\"\" md\"\"\" Here is the algorithm in action. Now the slider tells us on which step of the algorithm we are, having removed each least energy seam at each step \"\"\" md\"\"\" Shrink by bind n Slider 1 length carved show value true \"\"\" md\"\"\" Appendix \"\"\" function downsample img maxheight 100 h,w size img if h maxheight img else img floor. Int,LinRange 1,h,maxheight , floor. Int,LinRange 1,w,floor Int, maxheight w h end end downsample img maxheight 40 function hbox x, y, gap 16 sy size y , sx size x w, h max sx 1 , sy 1 , gap sx 2 sy 2 slate fill RGB 1,1,1 , w,h slate 1 size x,1 , 1 size x,2 . RGB. x slate 1 size y,1 , size x,2 gap . 1 size y,2 . RGB. y slate end function hbox imgs... maxheight 200 g x PlutoUI.ExperimentalLayout.Div downsample x maxheight style Dict \"display\" \"flex\", \"flex\" \"1 0 auto\" PlutoUI.ExperimentalLayout.Div collect map g, imgs style Dict \"display\" \"flex\", \"flex direction\" \"row\", \"aspect ratio\" length imgs size imgs 1 ,2 size imgs 2 ,1 end begin img brightness brightness. img ∇x convolve img brightness, Sx ∇y convolve img brightness, Sy hbox show colored array ∇x , show colored array ∇y end begin edged edgeness img hbox img, pencil edged hbox img, Gray. edgeness img maximum abs. edged end hbox mark path img, path , mark path show colored array least e , path hbox mark path img, path , mark path pencil e , path let least energy path of them all , k findmin least e 1, path get seam at dirs, k hbox mark path img, path , mark path show colored array least e , path end hbox img, marked carved n vbox x... PlutoUI.ExperimentalLayout.vbox collect x size img size carved n "},{"url":"images_abstractions/structure/","title":"Taking Advantage of Structure","tags":["lecture","module1","track_julia","type","programming","matrix","track_data","structure"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v wZrVxbmX218\" image \"https user images.githubusercontent.com 6933510 136196581 ffdf4a3b f35c 4eb8 85a7 f07560bd421b.png\" section 9 order 9 title \"Taking Advantage of Structure\" layout \"layout.jlhtml\" youtube id \"wZrVxbmX218\" description \"\" tags \"lecture\", \"module1\", \"track julia\", \"type\", \"programming\", \"matrix\", \"track data\", \"structure\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using ImageShow.ImageCore using ColorSchemes using InteractiveUtils, PlutoUI using LinearAlgebra, SparseArrays, Statistics end PlutoUI.TableOfContents aside true myonehatvector 0, 1, 0, 0, 0, 0 also one \"cold\" 1 . myonehatvector struct OneHot AbstractVector Int n Int k Int end Base.size x OneHot x.n, Base.getindex x OneHot, i Int Int x.k i md\"\"\" One hot vectors This example comes from machine learning. \"\"\" md\"\"\" A one hot vector has a single \"hot\" element, i.e. a single 1 in a sea of zeros. For example \"\"\" md\"\"\"How much \"information\" numbers do you need to represent a one hot vector? Is it n or is it two? \"\"\" md\"\"\" There are also \"1 cold\" vectors \"\"\" md\"\"\" Julia `structs` creating a new type in Julia \"\"\" md\"\"\" We can create our own new types in Julia. Let's create a new type to represent one hot vectors. It will be a subtype of `AbstractVector`, meaning that it behaves like a vector. \"\"\" md\"\"\" We need to specify how long the vector is \"\"\" md\"\"\" and how to extract the i th component \"\"\" md\"\"\" Note that `x.k i` returns a Boolean value, `true` or `false`, which we are then converting to an `Int`. \"\"\" myonehotvector OneHot 6,2 myonehotvector 3 myonehotvector 2 md\"\"\" This behaves as if it were the original vector, but we are storing only 2 integers. This is an example of taking advantage of structure. \"\"\" md\"\"\" Julia `dump` \"\"\" md\"\"\" `dump` shows the internal data stored inside a given object \"\"\" dump myonehotvector md\"\"\" When used inside our code, however, it behaves and looks exactly like a vector. Pluto also displays it as a vector, with its individual elements. \"\"\" myonehotvector md\"\"\" Visualizing a one hot vector \"\"\" md\"\"\" n bind nn Slider 1 20, show value true \"\"\" md\"\"\" k bind kk Slider 1 nn, default 1, show value true \"\"\" x OneHot nn, kk md\" Diagonal matrices\" md\"Another example is diagonal matrices. Here's how you might see them in high school \" denseD 5 0 0 0 6 0 0 0 10 md\"Julia has a better way of representing them \" D Diagonal denseD md\"\"\" It even displays nicely, with dots instead of zeros. We can also create a diagonal matrix from the values on the diagonal \"\"\" Diagonal 5, 6, 10 md\"How much information is stored for each representation? We can use Julia's `dump` function to find out \" dump denseD dump D md\"\"\" We see that `Diagonal` stores only the diagonal entries, not the zeros \"\"\" md\"\"\"We should always look for structure where it exists \"\"\" md\" Sparse matrices\" md\"A sparse matrix is a matrix that has many zeros, and is hence worth storing in a sparse representation \" denseM 0 0 9 0 0 0 12 0 4 md\"\"\" The above displays a sparse matrix in so called ` i, j, value ` form. We could store sparse matrices in this way \"\"\" M sparse denseM md\"\"\" Although it looks like it's stored like this, in fact the actual storage format is different. In the Julia `SparseArrays.jl` package, the storage format is compressed sparse column CSC format, which is generally considered favorable for arithmetic, matrix vector products and column slicing. Of course, for specific matrices, other formats might be better. `nzval` contains the nonzero matrix entries `rowval` is the \"i\" or row entry for the corresponding value in nzval length rowval length nzval `colptr j ` points into nzval and tells you the first nonzero in or after column j The last entry of colptr points beyond the end of nzval to indicate no more columns. length colptr number of columns 1 \"\"\" dump sparse M md\"\"\" An example where CSC may not be a great choice is the following. The reason is that `colptr` must have an entry in each column \"\"\" M2 sparse 1, 2, 10^6 , 4, 9, 10^6 , 7, 8, 9 dump M2 M3 1 0 2 0 10 0 3 4 0 9 0 0 0 5 8 0 0 0 0 7 M4 M3 . 0 dump sparse M4 sparse M4 md\"\"\" Random vectors \"\"\" md\"How much structure is there in a random vector?\" v rand 1 9, 1 000 000 md\"\"\"You might guess that there is \"no structure\". But you can actually think of randomness itself as a structure. For example, take the mean and standard deviation some would say that's the structure. \"\"\" mean v , std v , 5, sqrt 10 2 3 md\"\"\" If you repeat the calculation, to 3 or 4 digits the mean and standard deviation don't change, and are approximately equal to the theoretical values on the right. \"\"\" md\"\"\" We can also count how many times each digit occurs in the data set \"\"\" sum v . i for i in 1 9 md\"\"\" We see that each number occurs roughly the same number of times. \"\"\" md\"Statisticians and professors who've just graded exams might say that under certain circumstances the mean and the variance give you the necessary structure, and the rest can be thrown away.\" m sum v length v mean σ² sum v . m .^ 2 length v 1 σ sqrt σ² std v md\"Sometimes the summary statistics are all you want. But sometimes not. \" md\" Multiplication tables\" outer v, w x y for x ∈ v, y ∈ w just a multiplication table outer 1 10, 1 10 md\"Did you memorize this in third grade?\" bind k Slider 1 14, show value true outer 1 k, 1 k outer 2,4,6 , 10,100,1000 md\"\"\" A multiplication table is clearly a structure, but it's not sparse there are no zeros. Nonetheless you need much less information to reconstruct the matrix. \"\"\" md\"In the context of 1 k times 1 k, just one number k is needed.\" md\"If you look at the following matrix? Does it have any structure? It's certainly more hidden.\" outer rand 3 , rand 4 but it's just a multiplication table md\"\"\" You might guess by visualizing the matrix that it is a multiplication table \"\"\" md\"We can factor out a multiplication table, if it's there \" md\"\"\" Julia Exceptions are thrown generated using `error` An exception is anything that can interrupt a program, e.g. invalid input data. \"\"\" function factor mult table v mult table , 1 w mult table 1, if v 1 ≠ 0 w v 1 end Good code has a check if outer v, w ≈ mult table return v, w else error \"Input is not a multiplication table\" end end factor outer 1, 2, 3 , 2, 2, 2 md\"\"\" A random 2x2 matrix is not a multiplication table. Most matrices are not given by multiplication tables. \"\"\" factor rand 2,2 md\" Let's add two or more multiplication tables \" A sum outer rand 3 ,rand 3 for i 1 2 md\"Is it possible, given the matrix, to find the structure? E.g. to show that a matrix is a sum of outer products multiplication table .\" md\"The answer is yes The Singular Value Decomposition SVD from algebra can find the structure \" md\"\"\" Let's take the SVD and calculate the sum of two outer products \"\"\" begin U, Σ, V svd A outer U , 1 , V , 1 Σ 1 outer U , 2 , V , 2 Σ 2 end md\"\"\" We see that we reconstruct the original matrix \" \"\"\" B rand 3,3 begin UU, ΣΣ, VV svd B outer UU ,1 , VV ,1 ΣΣ 1 outer UU ,2 , VV ,2 ΣΣ 2 end md\"and it can approximate too \" flag outer 1,1,1,2,2,2,1,1,1 , 1,1,1,1,1,1,1,1,1 cs distinguishable colors 100 cs flag cs flag flag' outer 1,1,1,2,2,2,1,1,1 , 1,1,1,1,1,1,1,1,1 outer 1,1,1,1,1,1,1,1,1 , 1,1,1,2,2,2,1,1,1 cs outer 1,1,1,2,2,2,1,1,1 , 1,1,1,1,1,1,1,1,1 outer 1,1,1,1,1,1,1,1,1 , 1,1,1,2,2,2,1,1,1 md\"\"\" Singular Value Decomposition SVD A tool to find structure \"\"\" tree url \"https user images.githubusercontent.com 6933510 110924885 d7f1b200 8322 11eb 9df7 7abf29c8db7d.png\" image load download tree url picture Float64. channelview image size picture pr, pg, pb eachslice picture, dims 1 RGB. pr, 0, 0 RGB. 0, pg, 0 RGB. 0, 0, pb begin Ur, Σr, Vr svd pr Ug, Σg, Vg svd pg Ub, Σb, Vb svd pb end bind n Slider 1 200, show value true RGB. sum outer Ur ,i , Vr ,i . Σr i for i in 1 n , sum outer Ug ,i , Vg ,i . Σg i for i in 1 n , sum outer Ub ,i , Vb ,i . Σb i for i in 1 n md\" Appendix\" md\" Syntax Learned\" md\"\"\" Syntax to be learned A `struct` is a great way to embody structure. `dump` to see what's inside a data structure. `Diagonal`, `sparse` `error` throws an exception `svd` Singular Value Decomposition \"\"\" dump, Diagonal, error, svd begin show image M get. ColorSchemes.rainbow , M . maximum M show image x AbstractVector show image x' end show image x show image outer rand 10 , rand 10 "},{"url":"images_abstractions/transformations2/","title":"Transformations II: Composability, Linearity and Nonlinearity","tags":["lecture","module1","transformation","track_math","track_julia","inverse","composition","matrix","linear algebra","nonlinear","linear"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v VDPf3RjoCpY\" image \"https user images.githubusercontent.com 6933510 136196619 0750544f cd6d 4ae3 ace7 60c24443d721.png\" section 5 order 5 title \"Transformations II Composability, Linearity and Nonlinearity\" layout \"layout.jlhtml\" youtube id \"VDPf3RjoCpY\" description \"Let's see what mathematical transformations, inverses, composition and non linearity look like in practice. i.e. applied to philip.jpg \" tags \"lecture\", \"module1\", \"transformation\", \"track math\", \"track julia\", \"inverse\", \"composition\", \"matrix\", \"linear algebra\", \"nonlinear\", \"linear\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using PlutoUI using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using PlutoUI using LinearAlgebra using ForwardDiff using NonlinearSolve using StaticArrays end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" urls corgis \"https user images.githubusercontent.com 6933510 108605549 fb28e180 73b4 11eb 8520 7e29db0cc965.png\", longcorgi \"https user images.githubusercontent.com 6933510 110868198 713faa80 82c8 11eb 8264 d69df4509f49.png\", theteam \"https news.mit.edu sites default files styles news article image gallery public images 202004 edelman%2520philip%2520sanders.png?itok ZcYu9NFeg\", img original load download urls.longcorgi md\"\"\" The fun stuff playing with transforms \"\"\" let range 1.5 .1 1.5 md\"\"\" This is a \"scrubbable\" matrix click on the number and drag to change A `` `` bind a Scrubbable range default 1.0 bind b Scrubbable range default 0.0 `` `` `` `` bind c Scrubbable range default 0.0 bind d Scrubbable range default 1.0 `` `` \"\"\" end md\"\"\" Grab a linear a0afe3ae 76b9 11eb 2301 cde7260ddd7f or nonlinear a290d5e2 7a02 11eb 37db 41bf86b1f3b3 transform, or make up your own \"\"\" md\"\"\" zoom bind z Scrubbable .1 .1 3, default 1 \"\"\" md\"\"\" pan bind panx Scrubbable 1 .1 1, default 0 , bind pany Scrubbable 1 .1 1, default 0 \"\"\" md\"\"\" α bind α Slider 30 .1 30, show value true, default 0 β bind β Slider 10 .1 10, show value true, default 5 h bind h Slider .1 .1 10, show value true, default 5 \"\"\" 1 1 md\"\"\" pixels bind pixels Slider 1 1000, default 800, show value true \"\"\" md\"\"\" Show grid lines bind show grid CheckBox default true ngrid bind ngrid Slider 5 5 20, show value true, default 10 \"\"\" md\"\"\" Circular Frame bind circular CheckBox default true radius bind r Slider .1 .1 1, show value true, default 1 \"\"\" md\"\"\" Above The original image is placed in a 1,1 x 1 1 box and transformed. \"\"\" A a b c d md\"\"\" Pedagogical note Why the Module 1 application image processing Image processing is a great way to learn Julia, some linear algebra, and some nonlinear mathematics. We don't presume the audience will become professional image processors, but we do believe that the principles learned transcend so many applications... and everybody loves playing with their own images \"\"\" md\"\"\" Last Lecture Leftovers \"\"\" md\"\"\" Interesting question about linear transformations If a transformation takes lines into lines and preserves the origin, does it have to be linear? Answer no The example of a perspective map takes all lines into lines, but parallelograms generally do not become parallelograms. \"\"\" md\"\"\" A nice interactive demo of perspective maps https www.khanacademy.org humanities renaissance reformation early renaissance1 beginners renaissance florence a linear perspective interactive from Khan academy. \"\"\" Resource \"https cdn.kastatic.org ka perseus images 1b351a3653c1a12f713ec24f443a95516f916136.jpg\" md\"\"\" Challenge exercise Rewrite this using Julia and Pluto \"\"\" md\"\"\" Julia style a little advanced Reminder about defining vector valued functions \"\"\" md\"\"\" Many people find it hard to read ```julia f v v 1 v 2 , v 1 v 2 or f v v 1 v 2 , v 1 v 2 ``` and instead prefer ```julia f x,y x y , x y or f x,y , x y , x y ``` All four of these will take a 2 vector to a 2 vector in the same way for the purposes of this lecture, i.e. `f 1,2 ` can be defined by any of the four forms. The forms with the ` ` are anonymous functions. They are still considered anonymous, even though we then name them `f`. \"\"\" md\"\"\" Functions with parameters The anonymous form comes in handy when one wants a function to depend on a parameter . For example `f α x,y , x αy, x αy ` allows you to apply the `f 7 ` function to the input vector ` 1, 2 ` by running `f 7 1, 2 ` . \"\"\" md\"\"\" Linear transformations a collection \"\"\" md\"\"\" Here are a few useful linear transformations \"\"\" begin id x, y SA x, y scalex α x, y , SA α x, y scaley α x, y , SA x, α y scale α x, y , SA α x, α y swap x, y SA y, x flipy x, y SA x, y rotate θ x, y , SA cos θ x sin θ y, sin θ x cos θ y shear α x, y , SA x α y, y end T⁻¹ id T⁻¹ rotate α T⁻¹ shear α T⁻¹ lin A uses the scrubbable T⁻¹ shear α ∘ shear α T⁻¹ nonlin shear α T⁻¹ inverse nonlin shear α T⁻¹ nonlin shear α T⁻¹ xy T⁻¹ warp α T⁻¹ x,y , x α y^2,y α x^2 may be non invertible T⁻¹ x,y , x,y^2 T⁻¹ flipy ∘ x,y , β x α y β y , h y β y md\"\"\" In fact we can write down the most general linear transformation in one of two ways \"\"\" begin lin a, b, c, d x, y , a x b y, c x d y lin A v A v... linear algebra version using matrix multiplication end md\"\"\" The second version uses the matrix multiplication notation from linear algebra, which means exactly the same as the first version when A \\begin bmatrix a & b \\\\ c & d \\end bmatrix \"\"\" md\"\"\" Nonlinear transformations a collection \"\"\" translate α,β x, y , SA x α, y β affine, but not linear nonlin shear α x, y , SA x, y α x^2 warp α x, y , rotate α √ x^2 y^2 SA x, y xy r, θ SA r cos θ , r sin θ rθ x SA norm x , atan x 2 ,x 1 exponentialish x,y , log x 1.2 , log y 1.2 merc x,y , log x^2 y^2 2 , atan y,x reim log complex y,x md\"\"\" Composition \"\"\" let x rand these two are the same a sin ∘ cos x b sin cos x a b end md\"\"\" Composing functions in mathematics Wikipedia math https en.wikipedia.org wiki Function composition In math we talk about composing two functions to create a new function the function that takes x to \\sin \\cos x is the composition of the sine function and the cosine function. We humans tend to blur the distinction between the sine function and the value of \\sin x at some point x . The sine function is a mathematical object by itself. It's a thing that can be evaluated at as many x 's as you like. If you look at the two sides of ` sin ∘ cos x ≈ sin cos x ` and see that they are exactly the same, it's time to ask yourself what's a key difference? On the left a function is built ` sin ∘ cos ` which is then evaluated at `x`. On the right, that function is never built. \"\"\" md\"\"\" Composing functions in computer science wikipedia cs https en.wikipedia.org wiki Function composition computer science A key issue is a programming language is whether it's easy to name the composition in that language. In Julia one can create the function `sin ∘ cos` and one can readily check that ` sin ∘ cos x ` always yields the same value as `sin cos x `. \"\"\" md\"\"\" Composing functions in Julia Julia's `∘` operator https docs.julialang.org en v1 manual functions Function composition and piping follows the mathematical typography https en.wikipedia.org wiki Function composition Typography convention, as was shown in the `sin ∘ cos` example above. We can type this symbol as `\\circ TAB `. \"\"\" md\"\"\" Composition of software at a higher level The trend these days is to have higher order composition of functionalities. A good example would be that an optimization can wrap a highly complicated program which might include all kinds of solvers, and still run successfully. This might require the ability of the outer software to have some awareness of the inner software. It can be quite magical when two very different pieces of software \"compose\", i.e. work together. Julia's language construction encourages composability. We will discuss this more in a future lecture. \"\"\" md\"\"\" Find your own examples Take some of the Linear and Nonlinear Transformations see the Table of Contents and find some inverses by placing them in the `T ` section of \"The fun stuff\" at the top of this notebook. \"\"\" md\"\"\" Linear transformations can be written in math using matrix multiplication notation as \\begin pmatrix a & b \\\\ c & d \\end pmatrix \\begin pmatrix x \\\\ y \\end pmatrix . \"\"\" md\"\"\" By contrast, here are a few fun functions that cannot be written as matrix times vector. What characterizes the matrix ones from the non matrix ones? \"\"\" md\"\"\" This may be a little fancy, but we see that warp is a rotation, but the rotation depends on the point where it is applied. \"\"\" begin warp₂ α,x,y rotate α √ x^2 y^2 warp₂ α x, y , warp₂ α,x,y x,y end warp3 α x, y , rotate α √ x^2 y^2 x,y warp3 1 1,2 warp 1 5,6 warp₂ 1.0 5.0,6.0 md\"\"\" Linear transformations See a matrix, think beyond number arrays \"\"\" md\"\"\" Software writers and beginning linear algebra students see a matrix and see a lowly table of numbers. We want you to see a linear transformation that's what professional mathematicians see. \"\"\" md\"\"\" What defines a linear transformation? There are a few equivalent ways of giving a definition. \"\"\" md\"\"\" Linear transformation definitions \"\"\" md\"\"\" The intuitive definition The rectangles gridlines in the transformed image above e0b657ce 7a03 11eb 1f9d f32168cb5394 always become a lattice of congruent parallelograms. The easy operational but devoid of intuition definition A transformation is linear if it is defined by v \\mapsto A v matrix times vector for some fixed matrix A . The scaling and adding definition 1. If you scale and then transform or if you transform and then scale, the result is always the same T cv c \\, T v v is any vector, and c any number. 2. If you add and then transform or vice versa the result is the same T v 1 v 2 T v 1 T v 2 . v 1,v 2 are any vectors. The mathematician's definition A consolidation of the above definition. T is linear if T c 1 v 1 c 2 v 2 c 1 T v 1 c 2 T v 2 for all numbers c 1,c 2 and vectors v 1,v 2 . This can be extended to beyond 2 terms. \"\"\" md\"\"\" The Matrix \"\"\" Resource \"https upload.wikimedia.org wikipedia en c c1 The Matrix Poster.jpg\" md\"\"\" No, not that matrix \"\"\" md\"\"\" The matrix for a linear transformation T is easy to write down The first column is T 1, 0 and the second is T 0,1 . That's it \"\"\" md\"\"\" Once we have those, the linearity relation T x,y x \\, T 1,0 y \\, T 0,1 x \\, \\mathrm column\\ 1 y \\, \\mathrm column\\ 2 is exactly the definition of matrix times vector. Try it. \"\"\" md\"\"\" Matrix multiply You know how to do it, but why? \"\"\" md\"\"\" Did you ever ask yourself why matrix multiply has that somewhat complicated multiplying and adding going on? \"\"\" let A randn 2,2 B randn 2,2 v rand 2 lin A ∘ lin B v , lin A B v end md\"\"\" Important The composition of the linear transformation is the linear transformation of the multiplied matrices There is only one definition of matmul matrix multiply that realizes this fact. To see what it is exactly, remember the first column of `lin A ∘ lin B ` should be the result of computing the two matrix times vectors y A 1,0 then z A y , and the second column is the same for 0,1 . This is worth writing out if you have never done this. \"\"\" md\"\"\" Let's try doing that with random matrices \"\"\" begin P randn 2, 2 Q randn 2, 2 T₁ lin P ∘ lin Q T₂ lin P Q lin P Q 1, 0 , lin P ∘lin Q 1, 0 end test img load download urls.corgis test pixels 300 md\"\"\" `lin P Q ` \"\"\" md\"\"\" `lin P ∘lin Q ` \"\"\" md\"\"\" Coordinate transformations vs object transformations \"\"\" md\"\"\" If you want to move an object to the right, the first thing you might think of is adding 1 to the x coordinate of every point. The other thing you could do is to subtract one from the first coordinate of the coordinate system. The latter is an example of a coordinate transform. \"\"\" md\"\"\" Coordinate transform of an array i, j vs points x, y \"\"\" md\"\"\" The original image has 1,1 in the upper left corner as an array but is thought of as existing in the entire plane. \"\"\" translate 400,400 1,1 md\"\"\" Inverses \"\"\" md\"\"\" If f is a function from 2 vectors to 2 vectors say , we define the inverse of f , denoted f^ 1 , to have the property that it \" undoes \" the effect of f , i.e. f f^ 1 v v and f^ 1 f v v . This equation might be true for all v or for some v in a region. \"\"\" md\"\"\" Example Scaling up and down \"\"\" let v rand 2 T rotate 30 ∘rotate 30 T v , v end let T scale 0.5 ∘ scale 2 v rand 2 T v .≈ v end md\"\"\" We observe numerically that `scale 2 ` and `scale .5 ` are mutually inverse transformations, i.e. each is the inverse of the other. \"\"\" md\"\"\" Inverses Solving equations \"\"\" md\"\"\" What does an inverse really do? Let's think about scaling again. Suppose we scale an input vector \\mathbf x by 2 to get an output vector \\mathbf x \\mathbf y 2 \\mathbf x Now suppose that you want to go backwards. If you are given \\mathbf y , how do you find \\mathbf x ? In this particular case we see that \\mathbf x \\frac 1 2 \\mathbf y . If we have a linear transformation, we can write \\mathbf y A \\, \\mathbf x with a matrix A . If we are given \\mathbf y and want to go backwards to find the \\mathbf x from that, we need to solve a system of linear equations . Usually , but not always , we can solve these equations to find a new matrix B such that \\mathbf x B \\, \\mathbf y , i.e. B undoes the effect of A . Then we have \\mathbf x B \\, A \\mathbf x , so that B A must be the identity matrix. We call B the matrix inverse of A , and write B A^ 1 . For 2 \\times 2 matrices we can write down an explicit formula for the matrix inverse, but in general we will need a computer to run an algorithm to find the inverse. \"\"\" md\"\"\" Inverting Linear Transformations \"\"\" let v rand 2 A randn 2,2 lin inv A ∘ lin A v , v end let A randn 2,2 B randn 2,2 inv A B ≈ inv B inv A end md\"\"\" ``A^ 1 \\begin pmatrix d & b \\\\ c & a \\end pmatrix ad bc \\quad `` if ``\\ A \\ \\begin pmatrix a & b \\\\ c & d \\end pmatrix . `` \"\"\" md\"\"\" Inverting nonlinear transformations \"\"\" md\"\"\" What about if we have a nonlinear transformation T can we invert it? In other words, if \\mathbf y T \\mathbf x , can we solve this to find \\mathbf x in terms of \\mathbf y ? In general this is a difficult question Sometimes we can do so analytically, but usually we cannot. Nonetheless, there are numerical methods that can sometimes solve these equations, for example the Newton method https en.wikipedia.org wiki Newton%27s method . There are several implementations of such methods in Julia, e.g. in the NonlinearSolve.jl package https github.com JuliaComputing NonlinearSolve.jl . We have used that to write a function `inverse` that tries to invert nonlinear transformations of our images. \"\"\" md\"\"\" The Big Diagram of Transforming Images \"\"\" Resource \"https raw.githubusercontent.com mitmath 18S191 Spring21 notebooks week3 comm2.png\" md\"\"\" Note that we are defining the map with the inverse of T so we can go pixel by pixel in the result. \"\"\" md\"\"\" Collisions \"\"\" begin function inverse f, y, u0 SVector 0.0, 0.0 prob NonlinearProblem false u, p f u, p . y, u0 solver solve prob, NewtonRaphson , tol 1e 4 return solver.u end inverse f y inverse u, p f SVector u... , y end md\"\"\" Check out Linear Map Wikipedia https en.wikipedia.org wiki Linear map Transformation Matrix Wikipedia https en.wikipedia.org wiki Transformation matrix \"\"\" md\"\"\" Why are we doing this backwards? If one moves the colors forward rather than backwards you have trouble dealing with the discrete pixels. You may have gaps. You may have multiple colors going to the same pixel. An interpolation scheme or a newton scheme could work for going forwards, but very likely care would be neeeded for a satisfying general result. \"\"\" md\"\"\" Appendix \"\"\" det A det A begin white c RGB RGB 1,1,1 white c RGBA RGBA 1,1,1,0.75 black c RGB RGB 0,0,0 black c RGBA RGBA 0,0,0,0.75 end begin function transform xy to ij img AbstractMatrix, x Float64, y Float64 convert coordinate system xy to ij center image, and use \"white\" when out of the boundary rows, cols size img m max cols, rows function to take xy to ij xy to ij translate rows 2, cols 2 ∘ swap ∘ flipy ∘ scale m 2 apply the function and \"snap to grid\" i, j floor. Int, xy to ij x, y end function getpixel img,i Int,j Int circular Bool false, r Real 200 grab image color or place default rows, cols size img m max cols,rows if circular c i rows 2 ^2 j cols 2 ^2 ≤ r m^2 4 else c true end if 1 i ≤ rows && 1 j ≤ cols && c img i, j else white img 1, 1 black img 1,1 end end function getpixel img,x Float64,y Float64 i,j transform xy to ij img,x,y getpixel img,i,j end function transform ij to xy i Int,j Int,pixels ij to xy scale 2 pixels ∘ flipy ∘ swap ∘ translate pixels 2, pixels 2 ij to xy i,j end end begin begin x, y transform ij to xy i,j, test pixels X, Y T₁ x,y i, j transform xy to ij test img,X,Y getpixel test img,i,j end for i 1 test pixels, j 1 test pixels end begin begin x, y transform ij to xy i,j, test pixels X, Y T₂ x,y i, j transform xy to ij test img,X,Y getpixel test img,i,j end for i 1 test pixels, j 1 test pixels end transform ij to xy 1,1,400 function with gridlines img Array Any,2 n 10 n 2n 1 rows, cols size img result copy img stroke zero eltype img RGBA RGB 1,1,1 , 0.75 stroke RGBA 1, 1, 1, 0.75 result floor. Int, LinRange 1, rows, n , . stroke result ceil. Int,LinRange 1, rows, n , . stroke result , floor. Int, LinRange 1, cols, n . stroke result , ceil. Int,LinRange 1, cols, n . stroke result rows ÷2 , . RGBA 0,1,0,1 result 1 rows ÷2 , . RGBA 0,1,0,1 result , cols ÷2 , . RGBA 1,0,0,1 result , 1 cols ÷2 , . RGBA 1,0,0,1 return result end img if show grid with gridlines img original n ngrid else img original end let x, y transform ij to xy i,j, pixels X, Y translate panx, pany x,y X, Y T⁻¹∘scale 1 z ∘translate panx, pany x,y i, j transform xy to ij img,X,Y getpixel img,i,j circular circular, r r end for i 1 pixels, j 1 pixels transform xy to ij img,0.0,0.0 img size img img 50,56 white background x PlutoUI.ExperimentalLayout.Div x style \"background white\" Resource \"https raw.githubusercontent.com mitmath 18S191 Spring21 notebooks week3 coord transform.png\" | white background Resource \"https raw.githubusercontent.com mitmath 18S191 Spring21 notebooks week3 collide.png\" | white background "},{"url":"images_abstractions/transformations_and_autodiff/","title":"Automatic Differentiation","tags":["lecture","module1","track_julia","track_math","programming","function","transformation","automatic differentiation","continuous","derivative"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v AAREeuaKCic\" image \"https user images.githubusercontent.com 6933510 136196632 ad67cb84 a4c9 410e ab72 f4fcfc26f69a.png\" section 3 order 3 title \"Automatic Differentiation\" layout \"layout.jlhtml\" youtube id \"AAREeuaKCic\" description \"We use the package ForwardDiff.jl to automatically differentiate functions, on scalar and vector domains.\" tags \"lecture\", \"module1\", \"track julia\", \"track math\", \"programming\", \"function\", \"transformation\", \"automatic differentiation\", \"continuous\", \"derivative\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using PlutoUI using HypertextLiteral using LinearAlgebra using ForwardDiff end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" md\"\"\" Last time, recall we defined linear combinations of images. Remember we scaled an image by multiplying by a constant combined images by adding the colors in each pixel possibly saturating In general if we perform both of these operations, we get a linear combination. \"\"\" md\"\"\" Functions in Math and Julia \"\"\" md\"\"\" Univariate Functions \"\"\" md\"\"\" In high school you learned about univariate functions e.g. f₁ x x^2 f₂ x \\sin x f₃ x x^\\alpha In Julia, functions can be written in short form, anonymous form, or long form. \"\"\" short form f₁ x x^2 subscript unicode \\ 1 tab f₁ 5 anonymous form x sin x x sin x π 2 long form function f₃ x,α 3 default parameter return x^α the \"return\" is optional end f₃ 5 f₃ 5,2 md\"\"\" Keyword arguments After the regular positional arguments, a function definition can also accept keyword arguments. In the definition, these come after a semicolon ` ` \"\"\" f₄ x α, β x β ^α md\"\"\" When calling a function with keyword arguments, you use the key \"\"\" f₄ 3, α 5, β 1 the order of keyword arguments does not matter f₄ 3, β 1, α 5 md\"\"\" See Julia's function documentation https docs.julialang.org en v1 manual functions for more. \"\"\" md\"\"\" Automatic Differentiation of Univariates \"\"\" md\"\"\" Automatic differentiation is a key enabling technology for machine learning and so much of scientific computing. It derives the answer in a manner that is interestingly different from the symbolic differentiation of elementary calculus classes and the numerical differentiation of simple finite differences. See the video at the end of this lecture. \"\"\" ForwardDiff.derivative f₁, 5 ForwardDiff.derivative x f₃ x,3 , 5 md\"\"\" Notice the use of anonymous functions to fix the parameter α 3 \"\"\" md\"\"\" In case you have forgotten what a derivative is, we remind you with a simple finite difference approximation \"\"\" begin md\"\"\" bind e Slider 6 1, default 1, show value true \"\"\" end ϵ 10.0^e sin 1 ϵ sin 1 ϵ , cos 1 , ForwardDiff.derivative sin,1 md\"\"\" Scalar Valued Multivariate Functions \"\"\" md\"\"\" Sometimes we are interested in scalar valued functions of more than 1 variable. This can be written in Julia as a function of many variables or a function of a vector. e.g. f 5 x 5\\sin x 1 x 2 2x 2 4x 3 \"\"\" begin f₅ v 5sin v 1 v 2 2 v 2 4v 3 f₅ x,y,z 5sin x y 2 y 4z end f₅ 1,2,3 , f₅ 1,2,3 md\"\"\" Better yet if you must write it the two ways you probably won't need to, but if you must , don't copy code , reuse code so if it changes in one place it changes everywhere. \"\"\" begin f₆ x,y,z 5sin x y 2 y 4z f₆ v f₆ v 1 ,v 2 ,v 3 end f₆ 1,2,3 , f₆ 1,2,3 md\"\"\" There's one other julia idea that is a trick to make vector code more readable. If you give a tuple argument, the function works directly on vectors but is defined with readable letters. \"\"\" f₇ x,y,z 5sin x y 2 y 4z more readable then 5sin v 1 v 2 2 v 2 4v 3 f₇ 1,2,3 this works with vector arguments, but not scalars f₇ 1,2,3 errros md\"\"\" You can see that the functions f 5 and f 6 has two julia methods, one with one variable, and one with three variables. \"\"\" methods f₅ md\"\"\" Automatic Differentiation Scalar valued multivariate functions \"\"\" md\"\"\" In many applications, including machine learning, one needs to take derivatives of the function in every argument direction. This is known as the gradient . Automatic differentiation works again \"\"\" ForwardDiff.gradient f₅, 1,2,3 md\"\"\" Remember f 5 x 5\\sin x 1 x 2 2x 2 4x 3 \"\"\" md\"\"\" One can check numerically by adding a small change to each of the arguments.m \"\"\" begin ∂f₅∂x f₅ 1 ϵ, 2, 3 f₅ 1, 2, 3 ϵ ∂f₅∂y f₅ 1, 2 ϵ, 3 f₅ 1, 2, 3 ϵ ∂f₅∂z f₅ 1, 2, 3 ϵ f₅ 1, 2, 3 ϵ ∇f ∂f₅∂x , ∂f₅∂y, ∂f₅∂z end md\"\"\" Whether you are an expert at multivariable calculus, or you have never seen this before, I hope seeing it numerically makes the idea intuitive. \"\"\" md\"\"\" Important Remark In machine learning, and other optimization contexts, we want to minimize a scalar function of many parameters known as a \"loss function.\" Following the negative gradient is a standard technique for minimizing functions especially when there are many variables. When there are only a few variables, there are better techniques. \"\"\" md\"\"\" Transformations Vector Valued Multivariate Functions \"\"\" md\"\"\" While scalar functions might technically be called a transformation, it is more common to use the term when both the input and output are multidimensional. \"\"\" md\"\"\" Let us consider a few functions that take in a vector of size 2 and returns a vector of size 2. \"\"\" begin idy x,y x,y lin1 x,y 2x 3y, 5x 4x scalex α x,y , α x, y scaley α x,y , x, α y rot θ x,y , cos θ x sin θ y, sin θ x cos θ y shear α x,y , x α y,y genlin a,b,c,d x,y , a x b y c x d y end rot π 2 4,5 md\"\"\" We bet you have noticed that these functions could all have been defined with matrices. Indeed the general case can be written \"\"\" a b c d x,y md\"\"\" or in math `` \\begin pmatrix a & b \\\\ c & d \\end pmatrix \\begin pmatrix x \\\\ y \\end pmatrix `` . \"\"\" md\"\"\" By contrast here are a few fun functions that can not be written as matrix times vector. What characterizes the matrix ones from the non matrix ones? \"\"\" begin function warp α x,y , begin r √ x^2 y^2 θ α r rot θ x,y end end rθ x norm x , atan x 2 ,x 1 maybe vectors are more readable here? xy r,θ r cos θ , r sin θ end md\"\"\" This may be a little fancy, but we see that warp is a rotation, but the rotation depends on the point where it is applied. \"\"\" begin warp₂ α,x,y rot α √ x^2 y^2 warp₂ α x,y , warp₂ α,x,y x,y end warp 1 5,6 warp₂ 1.0 5.0,6.0 md\"\"\" Automatic Differentiation of Transformations \"\"\" ForwardDiff.jacobian warp 3.0 , 4,5 md\"\"\" What is this thing? \"\"\" begin ∂w∂x warp 3.0 4 ϵ, 5 warp 3.0 4,5 ϵ This is a vector, right? ∂w∂y warp 3.0 4, 5 ϵ warp 3.0 4,5 ϵ This too ∂w∂x ∂w∂y end md\"\"\" But what is a transformation, really? You have very likely learned how to multiply matrices times vectors. I'll bet you think of a matrix as a table of numbers, and a vector as a column of numbers, and if you are well practiced, you know just when to multiply and just when to add. Congratulations, you now can do what computers excel at. \"\"\" md\"\"\" Matrices are often thought of as containers of numbers in a rectangular array, and hence one thinks of manipulating these tables like a spreadsheet, but actually the deeper meaning is that it is a transformation. \"\"\" md\"\"\" α bind α Slider .1 .1 3, show value true \"\"\" let range 1.5 .1 1.5 md\"\"\" This is a \"scrubbable matrix\" click on the number and drag to change. `` `` bind a Scrubbable range default 1.0 bind b Scrubbable range default 0.0 `` `` `` `` bind c Scrubbable range default 0.0 bind d Scrubbable range default 1.0 `` `` Re run this cell to reset to identity transformation \"\"\" end T shear 1 Pick a transformation T genlin a,b,c,d md\"\"\" center zoom bind z Slider .1 .1 3, show value true, default 1 \"\"\" md\"\"\" top left zoom bind f Slider .1 1 3, show value true, default 1 \"\"\" md\"Show grid lines bind show grid CheckBox default true \" A a b c d md\" Det A a d c b det A \" md\"\"\" We never seem to see this in linear algebra classes do we? Check out Linear Map Wikipedia https en.wikipedia.org wiki Linear map Transformation Matrix Wikipedia https en.wikipedia.org wiki Transformation matrix \"\"\" md\"\"\" Geometry of determinant, how areas scale. \"\"\" md\"\"\" Why are we doing this backwards? \"\"\" md\"\"\" Computer Science Solving 2 equations in 2 unknowns, and higher dimensional analogs. The top 500 supercomputers, and how many equations in how many unknowns are being solved today. \"\"\" md\"\"\" Automatic Differentiation in 10 minutes okay, 11 \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid vAp6nUMrKYg params \"modestbranding 1&rel 0\" lite youtube \"\"\" md\"\"\" What about more than 3 dimensions? Oh I've heard it all, there is no such thing as more than 3 dimensions. The fourth dimension is time, isn't it? I have trouble visualizing 3 dimensions, let alone contemplating 4. ... but mathematicians have no trouble with n spatial dimensions, and they do not usually worry about silly questions like whether these exist. \"\"\" md\"\"\" Appendix \"\"\" det A det A img sources \"https user images.githubusercontent.com 6933510 108605549 fb28e180 73b4 11eb 8520 7e29db0cc965.png\" \"Corgis\", \"https user images.githubusercontent.com 6933510 108883855 39690f80 7606 11eb 8eb1 e595c6c8d829.png\" \"Arrows\", \"https images.squarespace cdn.com content v1 5cb62a904d546e33119fa495 1589302981165 HHQ2A4JI07C43294HVPD ke17ZwdGBToddI8pDm48kA7bHnZXCqgRu4g0 U7hbNpZw zPPgdn4jUwVcJE1ZvWQUxwkmyExglNqGp0IvTJZamWLI2zvYWH8K3 s 4yszcp2ryTI0HqTOaaUohrI8PISCdr 3EAHMyS8K84wLA7X0UZoBreocI4zSJRMe1GOxcKMshLAGzx4R3EDFOm1kBS fluffy corgi?format 2500w\" \"Long Corgi\" md\"\"\" Choose an image bind img source Select img sources \"\"\" img original load download img source typeof img original begin white c RGB RGB 1,1,1 white c RGBA RGBA 1,1,1,0.75 end function trygetpixel img AbstractMatrix, x Float64, y Float64 rows, cols size img \"The linear map 1,1 ↦ 0,1 \" f t t 1.0 1.0 1.0 i floor Int, rows f y z j floor Int, cols f x rows cols z if 1 i ≤ rows && 1 j ≤ cols img i,j else white img 1,1 end end function with gridlines img Array Any,2 n 16 sep i size img, 1 ÷ n sep j size img, 2 ÷ n result copy img stroke zero eltype img RGBA RGB 1,1,1 , 0.75 stroke RGBA 1, 1, 1, 0.75 result 1 sep i end, . stroke result , 1 sep j end . stroke a second time, to create a line 2 pixels wide result 2 sep i end, . stroke result , 2 sep j end . stroke result sep i n ÷2 . 1,2 , . RGBA 0,1,0,1 result , sep j n ÷2 . 1,2 , . RGBA 1,0,0,1 return result end img if show grid with gridlines img original else img original end if det A 0 RGB 1.0, 1.0, 1.0 else in x, in y A \\ out x, out y in x, in y xy out x, out y in x, in y T out x, out y trygetpixel img, in x, in y end for out y in LinRange f, f, 500 , out x in LinRange f, f, 500 "},{"url":"images_abstractions/transforming_images/","title":"Transformations with Images","tags":["lecture","module1","image","convolution","track_math","matrix","philip"],"text":" A Pluto.jl notebook v0.19.45 frontmatter chapter 1 video \"https www.youtube.com watch?v uZYVjDDZW9A\" image \"https user images.githubusercontent.com 6933510 136196626 194e81c9 00f7 49f6 90c3 09945723b6a3.png\" section 4 order 4 title \"Transformations with Images\" layout \"layout.jlhtml\" youtube id \"uZYVjDDZW9A\" description \"\" tags \"lecture\", \"module1\", \"image\", \"convolution\", \"track math\", \"matrix\", \"philip\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end begin using PlutoUI using Colors, ColorVectorSpace, ImageShow, FileIO, ImageIO using Unitful using ImageFiltering using OffsetArrays using Plots end PlutoUI.TableOfContents aside true md\"\"\" Initializing packages When running this notebook for the first time, this could take up to 15 minutes. Hang in there \"\"\" rotabook PlutoUI.Resource \"https i.gr assets.com images S compressed.photo.goodreads.com books 1348902666l 1646354.jpg\" md\"\"\" Never run overtime a microcentury with UnitFul Running overtime is the one unforgivable error a lecturer can make. After fifty minutes one microcentury as von Neumann used to say everybody's attention will turn elsewhere even if we are trying to prove the Riemann hypothesis. One minute overtime can destroy the best of lectures. from \"Indiscrete Thoughts\" by Rota, Chpt 18, 10 Lessons I Wish I Had Been Taught \"\"\" century 100u\"yr\" a u\"yr\" is a special kind of string denoting a unit of a year century 2 century 200 century^2 g 9.8u\"m\" u\"s\"^2 uconvert u\"minute\", century 1e 6 convert into minutes the value of a microcentury PotentialEnergy 10u\"kg\" g 50u\"m\" uconvert u\"J\",PotentialEnergy md\"\"\" Adding units to numbers just works in Julia, and furthermore, does not slow down execution. We are sneaking in an example of the power of generic programming and Julia's type system, some of the underlying technology that makes us love working with Julia. More on this later in the book. Meanwhile if this helps you do your problem sets in some other class, go for it. \"\"\" md\"\"\" info \"Reminder\" Try your own pictures everywhere \"\"\" md\"\"\" Transforming Images \"\"\" md\"\"\" 2.1. Downsampling Upsampling \"\"\" md\"\"\" How can we pixelate a corgi? Found this cute picture online, but we'll pixelate a real corgi. \"\"\" pixelated corgi load download \"https i.redd.it 99lhfbnwpgd31.png\" philip load download \"https user images.githubusercontent.com 6933510 107239146 dcc3fd00 6a28 11eb 8c7b 41aaf6618935.png\" bind r Slider 1 40, show value true, default 40 downsample philip philip 1 r end, 1 r end upsample philip kron downsample philip, fill 1,r,r md\"\"\" Note the use of kron and fill. See Wikipedia Kron https en.wikipedia.org wiki Kronecker product \"\"\" md\"\"\" Exercise Use the nose selection tool from Section 1.1 to pixelate a rectangle of an image. Warning you'll have to worry about sizes if not exact multiples. \"\"\" md\"\"\" 2.2 Linear Combinations Combining Images \"\"\" md\"\"\" One big idea in mathematics is the linear combination https en.wikipedia.org wiki Linear combination . The idea combines scaling an object combining two or more objects by combining scaled versions of multiple objects. \"\"\" md\"\"\" Let's scale some corgis. \"\"\" corgis load download \"https user images.githubusercontent.com 6933510 108605549 fb28e180 73b4 11eb 8520 7e29db0cc965.png\" bind c Slider 0 .1 3, show value true, default 1 c . corgis scaling the corgis changes intensity md\"\"\" You might wonder about the dot times or pointwise times . You can delete the dot, but it is recommended for clarity and performance. The dot emphasizes that the multiplication by c is happening pixel by pixel or that the scalar is being \"broadcast\" to every pixel. \"\"\" md\"\"\" Scaling too far saturates the image. Any r,g,b ≥ 1, saturates at 1. \"\"\" md\"\"\" We need another image. We could grab one from somewhere or we can just transform the one we have. Let's do the latter and turn the corgis upsidedown. \"\"\" upsidedown corgis corgis end 1 1 , md\"\"\" Now let's scaled version of the two images to see what that does. \"\"\" .5 upsidedown corgis . .5 corgis md\"\"\" Convex Combinations \"\"\" md\"\"\" If all the coefficients are positive and add to 1, we say we have a convex combination . Let's take α and 1 α as the two coefficients adding to 1, and scale the two corgi pictures with different α's, thereby giving different weights to the rightside up and upside down corgis. \"\"\" bind α Slider 0 .01 1 , show value true, default 1.0 α . corgis . 1 α . upsidedown corgis md\"\"\" The moment I did this with α .5, I noticed my brain's tendency to see the rightsisde up corgis even though both have equal weight. For me maybe around α .39 which gives weight .61 to the upside down corgis \"feels\" balanced to me. I think this is what the field of psychology called psychometrics tries to measure perhaps someone can tell me if there are studies of the brain's tendency to use world experience to prefer rightside up corgis, and in particular to put a numerical value to this tendency. \"\"\" md\"\"\" 10 seconds with google and I found there is a thing about faces The Face Inversion effect https en.wikipedia.org wiki Face inversion effect ~ text The%20face%20inversion%20effect%20is,same%20for%20non%2Dfacial%20objects.&text The%20most%20supported%20explanation%20for,is%20the%20configural%20information%20hypothesis and also the Thatcher Effect https en.wikipedia.org wiki Thatcher effect ~ text The%20Thatcher%20effect%20or%20Thatcher,obvious%20in%20an%20upright%20face seems related. ... the article suggests objects don't suffer in the same way as faces, so I put forth that the phenomenon applies to corgi faces as much as human faces, suggesting maybe that corgi faces are processed in the face processing part of the brain,not the object processing part of the brain. Corgis are human, after all, right? Note, this is 5 minutes of armchair science, not a professional opinion. \"\"\" md\"\"\" 2.3 Fun with Photoshop What does \"filter\" mean in this context? \"\"\" md\"\"\" Photshop Filter Reference https helpx.adobe.com photoshop using filter effects reference.html \"\"\" md\"\"\" Let's play with photoshop if for no other reason, let's see what image transformations are available considered useful by the pros. Some worth emphasizing are 1. Blur 2. Sharpen 3. Stylize Find Edges 3. Pixelate 4. Distort Some of these transformations e.g. Blur, Sharpen, Find Edges are examples of convolutions which are very efficient, and show up these days in machine learning particularly in image recognition. \"\"\" md\"\"\" 2.4 Image Filtering convolutions \"\"\" md\"\"\" Last semester Grant Sanderson 3Blue1Brown lectured in this course. This lecture on convolutions in image processing was popular. Let's watch an excerpt from 1 04 to 2 48 . We pick a few exercepts, but we wouldn't blame you if you just wanted to watch the whole video. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid 8rrHTtUzyZA params \"modestbranding 1&rel 0&start 64&end 168\" lite youtube \"\"\" md\"\"\" Definition of convolutions and kernels \"\"\" md\"\"\" Wikipedia Page on Kernels https en.wikipedia.org wiki Kernel image processing Details \"\"\" md\"\"\" Computer Science Complexity The number of multiplications Number of Pixels in the Image Number of Cells in the kernel \"\"\" md\"\"\" Thought Problem Why are small kernels better than large kernels from a complexity viewpoint? \"\"\" md\"\"\" Computer Science Architectures GPUs or Graphical Processing Units Some important computations can be greatly accelerated through the use of specialized hardware such as the GPU processors that were originally designed as image renderers but it has turned out that these processors can be quite fast at other very regular computations. Convolutions is a very GPU friendly operation due to its regular structure. \"\"\" kernelize M OffsetArray M, 1 1, 1 1 begin identity 0 0 0 0 1 0 0 0 0 edge detect 0 1 0 1 4 1 0 1 0 sharpen identity . edge detect Superposition box blur 1 1 1 1 1 1 1 1 1 9 ∇x 1 0 1 1 0 1 1 0 1 2 centered deriv in x ∇y ∇x' kernels identity, edge detect, sharpen, box blur, ∇x, ∇y kernel keys \"identity\", \"edge detect\", \"sharpen\", \"box blur\", \"∇x\", \"∇y\" selections kernel keys . kernel keys kernel matrix Dict kernel keys . kernels md\" bind kernel name Select selections \" end kernel matrix kernel name imfilter corgis, kernelize kernel matrix kernel name Gray. 1.5 . abs. imfilter corgis, kernelize kernel matrix kernel name md\"\"\" Gaussian Filter \"\"\" md\"\"\" In our next Grant Sanderson segment from Fall 2020 4 35 to 7 00 , we hear about convolving images with a Gaussian kernel. \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid 8rrHTtUzyZA params \"modestbranding 1&rel 0&start 275&end 420\" lite youtube \"\"\" round. Kernel.gaussian 1 , digits 3 md\"\"\" We could have defined this ourselves with calls to the exponential function. \"\"\" begin G exp i^2 j^2 2 for i 2 2, j 2 2 round. G . sum G , digits 3 end md\"\"\" This is often known as Gaussian blur to emphasize the result of this operation. Adobe on Gaussian blur https www.adobe.com creativecloud photography discover gaussian blur.html . \"\"\" md\"\"\" Focus around 5 23 \"\"\" bind gparam Slider 0 9, show value true, default 1 kernel Kernel.gaussian gparam plotly surface kernel md\"\"\" Note black lines are contours \"\"\" md\"\"\" Computer Science Data Structure Offset Arrays \"\"\" kernel 0,0 kernel 2,2 M 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Z OffsetArray M, 1 1, 2 2 the indices c.I for c ∈ CartesianIndices Z Z 1, 2 md\"\"\" 2.5. Discrete vs Continuous \"\"\" md\"\"\" Some folks only like discrete objects, others continuous. The computer makes clear what many mathematicians already know, that while different language has evolved to describe discrete objects vs continuous objects, often the underlying conceptual idea is similar or the same. Here is one analogy \"\"\" md\"\"\" Blurring Kernels Integrals ≡ Sharpening Kernels Derivatives \"\"\" md\"\"\" Think about integrals vs derivatives in one dimension. If you replace f x with g x ∫ f t dt for x r ≤ t ≤ x r, that will blur or smooth out the features of f. However if you take the derivative,you will emphasize the changes, i.e., you will sharpen or \"edge detect.\" \"\"\" md\"\"\" 2.6 Respect my Boundaries \"\"\" md\"\"\" Applying the convolution on a boundary requires special thought because it is literally an edge case . Once again Grant said this so very well 2 53 4 19 \"\"\" html\"\"\" script src \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.js\" integrity \"sha256 wwYlfEzWnCf2nFlIQptfFKdUmBeH5d3G7C2352FdpWE \" crossorigin \"anonymous\" defer script link rel \"stylesheet\" href \"https cdn.jsdelivr.net npm lite youtube embed 0.2.0 src lite yt embed.css\" integrity \"sha256 99PgDZnzzjO63EyMRZfwIIA i OS2wDx6k 9Eo7JDKo \" crossorigin \"anonymous\" lite youtube videoid 8rrHTtUzyZA params \"modestbranding 1&rel 0&start 173&end 259\" lite youtube \"\"\" "},{"url":"week0/basic_syntax/","title":"basic_syntax","tags":[],"text":" A Pluto.jl notebook v0.19.45 using Markdown using InteractiveUtils md\" Get started with Julia live Before being able to run this notebook successfully locally, you will need to set up Julia and Pluto. Spring21 installation \" md\" Variables We can define a variable using ` ` assignment . Then we can use its value in other expressions \" x 3 y 2x md\"By default Julia displays the output of the last operation. You can suppress the output by adding ` ` a semicolon at the end. \" md\"We can ask what type a variable has using `typeof` \" typeof y md\" Functions\" md\"We can use a short form, one line function definition for simple functions \" f x 2 x md\"Typing the function's name gives information about the function. To call it we must use parentheses \" f f 10 md\"For longer functions we use the following syntax with the `function` keyword and `end` \" function g x, y z x y return z^2 end g 1, 2 md\" For loops\" md\"Use `for` to loop through a pre determined set of values \" let s 0 for i in 1 10 s i Equivalent to s s i end s end md\"Here, `1 10` is a range representing the numbers from 1 to 10 \" typeof 1 10 md\"Above we used a `let` block to define a new local variable `s`. But blocks of code like this are usually better inside functions, so that they can be reused. For example, we could rewrite the above as follows \" function mysum n s 0 for i in 1 n s i end return s end mysum 100 md\" Conditionals `if`\" md\"We can evaluate whether a condition is true or not by simply writing the condition \" a 3 a 5 md\"We see that conditions have a Boolean `true` or `false` value. We can then use `if` to control what we do based on that value \" if a 5 \"small\" else \"big\" end md\"\"\"Note that the `if` also returns the last value that was evaluated, in this case the string `\"small\"` or `\"big\"`, Since Pluto is reactive, changing the definition of `a` above will automatically cause this to be reevaluated \"\"\" md\" Arrays\" md\" 1D arrays `Vector`s \" md\"We can make a `Vector` 1 dimensional, or 1D array using square brackets \" v 1, 2, 3 typeof v md\"The `1` in the type shows that this is a 1D array. We access elements also using square brackets \" v 2 v 2 10 md\"Note that Pluto does not automatically update cells when you modify elements of an array, but the value does change.\" md\"A nice way to create `Vector`s following a certain pattern is to use an array comprehension \" v2 i^2 for i in 1 10 md\" 2D arrays matrices \" md\"We can make small matrices 2D arrays with square brackets too \" M 1 2 3 4 typeof M md\"The `2` in the type confirms that this is a 2D array.\" md\"This won't work for larger matrices, though. For that we can use e.g.\" zeros 5, 5 md\"Note that `zeros` gives `Float64`s by default. We can also specify a type for the elements \" zeros Int, 4, 5 md\"We can then fill in the values we want by manipulating the elements, e.g. with a `for` loop.\" md\"A nice alternative syntax to create matrices following a certain pattern is an array comprehension with a double `for` loop \" i j for i in 1 5, j in 1 6 "},{"url":"week0/plots_tutorial/","title":"plots_tutorial","tags":[],"text":" A Pluto.jl notebook v0.19.45 using Markdown using InteractiveUtils using Plots using SpecialFunctions using VegaLite, VegaDatasets using DataFrames using PlutoUI md\"\"\" Resources Plots Docs Useful Tips https docs.juliaplots.org latest basics Useful Tips Plots Docs Examples https docs.juliaplots.org latest generated gr Plot Attributes http docs.juliaplots.org latest generated attributes plot Axis Attributes http docs.juliaplots.org latest generated attributes axis Axis Color Names http juliagraphics.github.io Colors.jl stable namedcolors \"\"\" md\"\"\" Loading Time Plots loading time sucks big time. This is a known problem. We know, we know, Julia is fast, just grab some coffee .... ☕☕☕☕☕☕☕☕☕ \"\"\" md\"\"\" Hello World \"\"\" plot rand 4 md\"\"\" Adding Stuff \"\"\" begin plot rand 4 scatter rand 4 end md\"\"\" Removing Stuff \"\"\" plot legend false, axis false, grid false, ticks false md\"\"\" Lines and points \"\"\" md\"\"\" Square Aspect Ratio \"\"\" begin v rand 4 plot v, ratio 1, legend false scatter v end md\"\"\" Matrices with color heatmap yflip true places the 2,1 entry where you want it, since you want to interchange xy with ij and i runs down and y runs up. \"\"\" A 1 1000 1 1 1 1 1 1 1 heatmap A, ratio 1, yflip true, legend false, axis false, grid false, ticks false heatmap rand 10,10 , clim 0,1 , ratio 1, legend false, axis false, ticks false begin MM 0 1 0 0 0 0 1 0 0 whiteblack RGBA 1,1,1,0 , RGB 0,0,0 heatmap c whiteblack, MM, aspect ratio 1, ticks .5 3.5, lims .5,3.5 , gridalpha 1, legend false, axis false, ylabel \"i\", xlabel \"j\" end begin p plot 1 4,guideposition top end begin p.attr foreground color RGB 1,0,0 p.attr foreground color end p begin q p.series list 1 .plotattributes q.explicit linecolor green end p md\"\"\" Colors \"\"\" Colors.color names mycolors colorant\"lightslateblue\",colorant\"limegreen\",colorant\"red\" begin AA i for i 50 300, j 1 100 heatmap AA, c mycolors, clim 1,300 end md\"\"\" Area under curves \"\"\" begin y rand 10 plot y, fillrange y. 0 . .5, label \"above below 1 2\", legend topleft end begin x LinRange 0,2,100 y1 exp. x y2 exp. 1.3 . x plot x, y1, fillrange y2, fillalpha 0.35, c 1, label \"Confidence band\", legend topleft end let x 3 .01 3 areaplot x, exp. x.^2 2 √ 2π ,alpha .25,legend false end begin M 1 2 3 7 8 9 4 5 6 0 .5 1.5 areaplot 1 3, M, seriescolor red green blue , fillalpha 0.2 0.3 0.4 end let f x exp x^2 2 √ 2π δ .01 plot x √2 . erfinv. 2 . δ 2 δ 1 . 1 areaplot x, f. x , seriescolor red, blue , legend false plot f, x,c black end md\"\"\" Shapes \"\"\" begin rectangle w, h, x, y Shape x . 0,w,w,0 , y . 0,0,h,h circle r,x,y θ LinRange 0,2π,500 x. r. cos. θ , y. r. sin. θ end begin plot circle 5,0,0 , ratio 1, c red, fill true plot rectangle 5 √2,5 √2, 2.5 √2, 2.5 √2 ,c white,fill true,legend false end md\"\"\" Editing Plots manually \"\"\" pl plot 1 4, 1, 4, 9, 16 pl.attr pl.series list 1 pl size 300,200 pl md\"\"\" VegaLite \"\"\" vlplot point, rand 10 , rand 10 begin dataset \"zipcodes\" | vlplot circle, width 500, height 300, transform calculate \"substring datum.zip code, 0, 1 \", as digit , projection type albersUsa , longitude longitude, latitude latitude, size value 1 , color \"digit n\" end d DataFrame dataset \"zipcodes\" scatter d , longitude , d , latitude , m ., ms 1, xlim 150, 50 , ylim 20,55 begin struct RankOneMatrix T v AbstractVector T w AbstractVector T end end RankOneMatrix rand 3 , rand 3 methods RankOneMatrix begin xx .1 .1 1 plot xx.^2, xaxis log, yaxis log end begin plot exp. x , yaxis log end TableOfContents "}] \ No newline at end of file diff --git a/Fall24/reviews/index.html b/Fall24/reviews/index.html index 0fca4171..a230b707 100644 --- a/Fall24/reviews/index.html +++ b/Fall24/reviews/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • diff --git a/Fall24/search/index.html b/Fall24/search/index.html index 9f5e6042..d9b10006 100644 --- a/Fall24/search/index.html +++ b/Fall24/search/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • diff --git a/Fall24/semesters/index.html b/Fall24/semesters/index.html index d46cf09b..e4557165 100644 --- a/Fall24/semesters/index.html +++ b/Fall24/semesters/index.html @@ -95,7 +95,7 @@

    Module 1: Images, Transformations, Abstractions

  • Module 2: Social Science & Data Science

  • diff --git a/Fall24/week0/basic_syntax/index.html b/Fall24/week0/basic_syntax/index.html index 3a519fca..9bf78a92 100644 --- a/Fall24/week0/basic_syntax/index.html +++ b/Fall24/week0/basic_syntax/index.html @@ -1 +1 @@ - + diff --git a/Fall24/week0/plots_tutorial/index.html b/Fall24/week0/plots_tutorial/index.html index 8eaa6b50..4f557fc6 100644 --- a/Fall24/week0/plots_tutorial/index.html +++ b/Fall24/week0/plots_tutorial/index.html @@ -1 +1 @@ - +