Created
August 27, 2022 21:31
-
-
Save alecloudenback/1263f8d3fbcc0d3763fd2884b72c2732 to your computer and use it in GitHub Desktop.
Recreating the prefix sum example from: https://github.com/actuarialopensource/jaxtuary/blob/main/learn/jax_notes_1.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### A Pluto.jl notebook ### | |
# v0.19.11 | |
using Markdown | |
using InteractiveUtils | |
# ╔═╡ 7121f532-2243-11ed-1a91-55b6e85bb022 | |
using MortalityTables | |
# ╔═╡ 4dc4b2ec-c12e-4dfe-83b2-f8fda9a63838 | |
tbls = [MortalityTables.table(i) for i in 3299:3308]; | |
# ╔═╡ 77c4c765-97c8-4e0f-8c5c-bea07efdc61b | |
issue_ages = 18:51 | |
# ╔═╡ f115099a-0d45-460e-808e-5372baba356a | |
durations = 1:25 | |
# ╔═╡ d5dbceb5-5b85-4687-990b-0d217bf2b80d | |
qs = [ | |
tbl.select[issue_age][issue_age+duration-1] | |
for duration in durations, issue_age in issue_ages, tbl in tbls | |
] | |
# ╔═╡ 7de71c17-398f-41b7-85a3-5ffa51b19146 | |
npx = mapslices(q->[1.;accumulate(*,1 .- q[begin:end-1],dims=1)],qs,dims=1) | |
# ╔═╡ 1093dbf7-cba5-4b6c-90c6-4632512308a9 | |
v = 1/1.02 | |
# ╔═╡ 93d81774-5bf5-4204-9333-de0ec7e28b05 | |
v_eoy = repeat(v .^ durations,1,length(issue_ages),length(tbls)) | |
# ╔═╡ a08becff-d0b1-45f5-b652-1512e46ab154 | |
# ╔═╡ e449267a-667b-430c-89d3-ac0708a7b638 | |
v_boy = v_eoy / v | |
# ╔═╡ 47ac0b6c-df0f-4f21-b9fa-dc5c2c7dc42f | |
face = one.(qs) * 1_000_000 | |
# ╔═╡ b0153366-83b0-4e1e-9065-7afe30a7d83c | |
repeat(v_eoy,1,1,10) | |
# ╔═╡ 3cefbe5f-bb66-45c0-aff5-2f1646c35665 | |
claims = npx .* qs .* face | |
# ╔═╡ ed2e3bc0-8ff3-48ff-a9bc-f5e114218b9f | |
prem_annual = sum(claims .* v_eoy;dims=1) ./ sum(npx .* v_boy,dims=1) | |
# ╔═╡ bdf9dff2-c782-42e7-a9ea-c96bfbf7f587 | |
prem = prem_annual .* npx | |
# ╔═╡ 87f22619-d751-4008-a190-5e0433c1df2a | |
cfs_net = claims .* v_eoy.- prem .* v_boy | |
# ╔═╡ 21003908-9034-434d-ad0b-f9324af67de2 | |
# reverse in an inner step b/c accumulate doesn't have a resverse option | |
reserves = vec(reverse(reduce(+,accumulate(+,reverse(cfs_net,dims=1);dims=1);dims=[2,3]))) | |
# ╔═╡ 00000000-0000-0000-0000-000000000001 | |
PLUTO_PROJECT_TOML_CONTENTS = """ | |
[deps] | |
MortalityTables = "4780e19d-04b9-53dc-86c2-9e9aa59b5a12" | |
[compat] | |
MortalityTables = "~2.3.0" | |
""" | |
# ╔═╡ 00000000-0000-0000-0000-000000000002 | |
PLUTO_MANIFEST_TOML_CONTENTS = """ | |
# This file is machine-generated - editing it directly is not advised | |
julia_version = "1.8.0" | |
manifest_format = "2.0" | |
project_hash = "eed80cb25ba938baea706d3a4e851050381dab04" | |
[[deps.Adapt]] | |
deps = ["LinearAlgebra"] | |
git-tree-sha1 = "195c5505521008abea5aee4f96930717958eac6f" | |
uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" | |
version = "3.4.0" | |
[[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.Compat]] | |
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] | |
git-tree-sha1 = "78bee250c6826e1cf805a88b7f1e86025275d208" | |
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" | |
version = "3.46.0" | |
[[deps.CompilerSupportLibraries_jll]] | |
deps = ["Artifacts", "Libdl"] | |
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae" | |
version = "0.5.2+0" | |
[[deps.DataStructures]] | |
deps = ["Compat", "InteractiveUtils", "OrderedCollections"] | |
git-tree-sha1 = "d1fff3a548102f48987a52a2e0d114fa97d730f0" | |
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" | |
version = "0.18.13" | |
[[deps.Dates]] | |
deps = ["Printf"] | |
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" | |
[[deps.DelimitedFiles]] | |
deps = ["Mmap"] | |
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" | |
[[deps.Distances]] | |
deps = ["LinearAlgebra", "SparseArrays", "Statistics", "StatsAPI"] | |
git-tree-sha1 = "3258d0659f812acde79e8a74b11f17ac06d0ca04" | |
uuid = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7" | |
version = "0.10.7" | |
[[deps.Distributed]] | |
deps = ["Random", "Serialization", "Sockets"] | |
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" | |
[[deps.Downloads]] | |
deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"] | |
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" | |
version = "1.6.0" | |
[[deps.EzXML]] | |
deps = ["Printf", "XML2_jll"] | |
git-tree-sha1 = "0fa3b52a04a4e210aeb1626def9c90df3ae65268" | |
uuid = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615" | |
version = "1.1.0" | |
[[deps.FileWatching]] | |
uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee" | |
[[deps.InteractiveUtils]] | |
deps = ["Markdown"] | |
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" | |
[[deps.IterTools]] | |
git-tree-sha1 = "fa6287a4469f5e048d763df38279ee729fbd44e5" | |
uuid = "c8e1da08-722c-5040-9ed9-7db0dc04731e" | |
version = "1.4.0" | |
[[deps.JLLWrappers]] | |
deps = ["Preferences"] | |
git-tree-sha1 = "abc9885a7ca2052a736a600f7fa66209f96506e1" | |
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210" | |
version = "1.4.1" | |
[[deps.LibCURL]] | |
deps = ["LibCURL_jll", "MozillaCACerts_jll"] | |
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21" | |
version = "0.6.3" | |
[[deps.LibCURL_jll]] | |
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"] | |
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0" | |
version = "7.84.0+0" | |
[[deps.LibGit2]] | |
deps = ["Base64", "NetworkOptions", "Printf", "SHA"] | |
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" | |
[[deps.LibSSH2_jll]] | |
deps = ["Artifacts", "Libdl", "MbedTLS_jll"] | |
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8" | |
version = "1.10.2+0" | |
[[deps.Libdl]] | |
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" | |
[[deps.Libiconv_jll]] | |
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"] | |
git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778" | |
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531" | |
version = "1.16.1+1" | |
[[deps.LinearAlgebra]] | |
deps = ["Libdl", "libblastrampoline_jll"] | |
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | |
[[deps.Logging]] | |
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" | |
[[deps.MacroTools]] | |
deps = ["Markdown", "Random"] | |
git-tree-sha1 = "3d3e902b31198a27340d0bf00d6ac452866021cf" | |
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" | |
version = "0.5.9" | |
[[deps.Markdown]] | |
deps = ["Base64"] | |
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" | |
[[deps.MbedTLS_jll]] | |
deps = ["Artifacts", "Libdl"] | |
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1" | |
version = "2.28.0+0" | |
[[deps.Memoize]] | |
deps = ["MacroTools"] | |
git-tree-sha1 = "2b1dfcba103de714d31c033b5dacc2e4a12c7caa" | |
uuid = "c03570c3-d221-55d1-a50c-7939bbd78826" | |
version = "0.4.4" | |
[[deps.Mmap]] | |
uuid = "a63ad114-7e13-5084-954f-fe012c677804" | |
[[deps.MortalityTables]] | |
deps = ["Memoize", "OffsetArrays", "Parsers", "Pkg", "QuadGK", "Requires", "StringDistances", "UnPack", "XMLDict"] | |
git-tree-sha1 = "8ae1af8a53147c592a95fbce9fd37dda2a50355f" | |
uuid = "4780e19d-04b9-53dc-86c2-9e9aa59b5a12" | |
version = "2.3.0" | |
[[deps.MozillaCACerts_jll]] | |
uuid = "14a3606d-f60d-562e-9121-12d972cd8159" | |
version = "2022.2.1" | |
[[deps.NetworkOptions]] | |
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" | |
version = "1.2.0" | |
[[deps.OffsetArrays]] | |
deps = ["Adapt"] | |
git-tree-sha1 = "1ea784113a6aa054c5ebd95945fa5e52c2f378e7" | |
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" | |
version = "1.12.7" | |
[[deps.OpenBLAS_jll]] | |
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"] | |
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363" | |
version = "0.3.20+0" | |
[[deps.OrderedCollections]] | |
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c" | |
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" | |
version = "1.4.1" | |
[[deps.Parsers]] | |
deps = ["Dates"] | |
git-tree-sha1 = "3d5bf43e3e8b412656404ed9466f1dcbf7c50269" | |
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" | |
version = "2.4.0" | |
[[deps.Pkg]] | |
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"] | |
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" | |
version = "1.8.0" | |
[[deps.Preferences]] | |
deps = ["TOML"] | |
git-tree-sha1 = "47e5f437cc0e7ef2ce8406ce1e7e24d44915f88d" | |
uuid = "21216c6a-2e73-6563-6e65-726566657250" | |
version = "1.3.0" | |
[[deps.Printf]] | |
deps = ["Unicode"] | |
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" | |
[[deps.QuadGK]] | |
deps = ["DataStructures", "LinearAlgebra"] | |
git-tree-sha1 = "78aadffb3efd2155af139781b8a8df1ef279ea39" | |
uuid = "1fd47b50-473d-5c70-9696-f719f8f3bcdc" | |
version = "2.4.2" | |
[[deps.REPL]] | |
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] | |
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" | |
[[deps.Random]] | |
deps = ["SHA", "Serialization"] | |
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | |
[[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.Serialization]] | |
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" | |
[[deps.SharedArrays]] | |
deps = ["Distributed", "Mmap", "Random", "Serialization"] | |
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" | |
[[deps.Sockets]] | |
uuid = "6462fe0b-24de-5631-8697-dd941f90decc" | |
[[deps.SparseArrays]] | |
deps = ["LinearAlgebra", "Random"] | |
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | |
[[deps.Statistics]] | |
deps = ["LinearAlgebra", "SparseArrays"] | |
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | |
[[deps.StatsAPI]] | |
deps = ["LinearAlgebra"] | |
git-tree-sha1 = "f9af7f195fb13589dd2e2d57fdb401717d2eb1f6" | |
uuid = "82ae8749-77ed-4fe6-ae5f-f523153014b0" | |
version = "1.5.0" | |
[[deps.StringDistances]] | |
deps = ["Distances", "StatsAPI"] | |
git-tree-sha1 = "ceeef74797d961aee825aabf71446d6aba898acb" | |
uuid = "88034a9c-02f8-509d-84a9-84ec65e18404" | |
version = "0.11.2" | |
[[deps.TOML]] | |
deps = ["Dates"] | |
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" | |
version = "1.0.0" | |
[[deps.Tar]] | |
deps = ["ArgTools", "SHA"] | |
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e" | |
version = "1.10.0" | |
[[deps.Test]] | |
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] | |
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | |
[[deps.UUIDs]] | |
deps = ["Random", "SHA"] | |
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" | |
[[deps.UnPack]] | |
git-tree-sha1 = "387c1f73762231e86e0c9c5443ce3b4a0a9a0c2b" | |
uuid = "3a884ed6-31ef-47d7-9d2a-63182c4928ed" | |
version = "1.0.2" | |
[[deps.Unicode]] | |
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" | |
[[deps.XML2_jll]] | |
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "Zlib_jll"] | |
git-tree-sha1 = "58443b63fb7e465a8a7210828c91c08b92132dff" | |
uuid = "02c8fc9c-b97f-50b9-bbe4-9be30ff0a78a" | |
version = "2.9.14+0" | |
[[deps.XMLDict]] | |
deps = ["EzXML", "IterTools", "OrderedCollections"] | |
git-tree-sha1 = "d9a3faf078210e477b291c79117676fca54da9dd" | |
uuid = "228000da-037f-5747-90a9-8195ccbf91a5" | |
version = "0.4.1" | |
[[deps.Zlib_jll]] | |
deps = ["Libdl"] | |
uuid = "83775a58-1f1d-513f-b197-d71354ab007a" | |
version = "1.2.12+3" | |
[[deps.libblastrampoline_jll]] | |
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"] | |
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93" | |
version = "5.1.1+0" | |
[[deps.nghttp2_jll]] | |
deps = ["Artifacts", "Libdl"] | |
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d" | |
version = "1.48.0+0" | |
[[deps.p7zip_jll]] | |
deps = ["Artifacts", "Libdl"] | |
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0" | |
version = "17.4.0+0" | |
""" | |
# ╔═╡ Cell order: | |
# ╠═7121f532-2243-11ed-1a91-55b6e85bb022 | |
# ╠═4dc4b2ec-c12e-4dfe-83b2-f8fda9a63838 | |
# ╠═77c4c765-97c8-4e0f-8c5c-bea07efdc61b | |
# ╠═f115099a-0d45-460e-808e-5372baba356a | |
# ╠═d5dbceb5-5b85-4687-990b-0d217bf2b80d | |
# ╠═7de71c17-398f-41b7-85a3-5ffa51b19146 | |
# ╠═1093dbf7-cba5-4b6c-90c6-4632512308a9 | |
# ╠═93d81774-5bf5-4204-9333-de0ec7e28b05 | |
# ╠═a08becff-d0b1-45f5-b652-1512e46ab154 | |
# ╠═e449267a-667b-430c-89d3-ac0708a7b638 | |
# ╠═47ac0b6c-df0f-4f21-b9fa-dc5c2c7dc42f | |
# ╠═b0153366-83b0-4e1e-9065-7afe30a7d83c | |
# ╠═3cefbe5f-bb66-45c0-aff5-2f1646c35665 | |
# ╠═ed2e3bc0-8ff3-48ff-a9bc-f5e114218b9f | |
# ╠═bdf9dff2-c782-42e7-a9ea-c96bfbf7f587 | |
# ╠═87f22619-d751-4008-a190-5e0433c1df2a | |
# ╠═21003908-9034-434d-ad0b-f9324af67de2 | |
# ╟─00000000-0000-0000-0000-000000000001 | |
# ╟─00000000-0000-0000-0000-000000000002 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment