add chapter 14

This commit is contained in:
Bogumił Kamiński 2022-03-14 00:12:33 +01:00
parent 34e80b2cba
commit b5e90546e6
5 changed files with 211 additions and 88 deletions

View File

@ -26,9 +26,9 @@ version = "0.2.0"
[[deps.Arrow]]
deps = ["ArrowTypes", "BitIntegers", "CodecLz4", "CodecZstd", "DataAPI", "Dates", "Mmap", "PooledArrays", "SentinelArrays", "Tables", "TimeZones", "UUIDs"]
git-tree-sha1 = "d4a35c773dd7b07ddeeba36f3520aefe517a70f2"
git-tree-sha1 = "85013d248b128cf13ae62c827c4bf05872e97f78"
uuid = "69666777-d1a9-59fb-9406-91d4454c9d45"
version = "2.2.0"
version = "2.2.1"
[[deps.ArrowTypes]]
deps = ["UUIDs"]
@ -78,9 +78,9 @@ version = "0.4.1"
[[deps.CSV]]
deps = ["CodecZlib", "Dates", "FilePathsBase", "InlineStrings", "Mmap", "Parsers", "PooledArrays", "SentinelArrays", "Tables", "Unicode", "WeakRefStrings"]
git-tree-sha1 = "9519274b50500b8029973d241d32cfbf0b127d97"
git-tree-sha1 = "9310d9495c1eb2e4fa1955dd478660e2ecab1fbb"
uuid = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
version = "0.10.2"
version = "0.10.3"
[[deps.Cairo_jll]]
deps = ["Artifacts", "Bzip2_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"]
@ -96,9 +96,9 @@ version = "0.5.1"
[[deps.CategoricalArrays]]
deps = ["DataAPI", "Future", "Missings", "Printf", "Requires", "Statistics", "Unicode"]
git-tree-sha1 = "c308f209870fdbd84cb20332b6dfaf14bf3387f8"
git-tree-sha1 = "5196120341b6dfe3ee5f33cf97392a05d6fe80d0"
uuid = "324d7699-5711-5eae-9e2f-1d82baa6b597"
version = "0.10.2"
version = "0.10.4"
[[deps.Chain]]
git-tree-sha1 = "339237319ef4712e6e5df7758d0bccddf5c237d9"
@ -107,9 +107,9 @@ version = "0.4.10"
[[deps.ChainRulesCore]]
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "7dd38532a1115a215de51775f9891f0f3e1bac6a"
git-tree-sha1 = "c9a6160317d1abe9c44b3beb367fd448117679ca"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.12.1"
version = "1.13.0"
[[deps.ChangesOfVariables]]
deps = ["ChainRulesCore", "LinearAlgebra", "Test"]
@ -166,9 +166,9 @@ version = "1.0.2"
[[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 = "44c37b4636bc54afac5c574d2d02b625349d6582"
git-tree-sha1 = "96b0bc6c52df76506efc8a441c6cf1adcb1babc4"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.41.0"
version = "3.42.0"
[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
@ -278,9 +278,9 @@ uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
[[deps.DualNumbers]]
deps = ["Calculus", "NaNMath", "SpecialFunctions"]
git-tree-sha1 = "84f04fe68a3176a583b864e492578b9466d87f1e"
git-tree-sha1 = "90b158083179a6ccbce2c7eb1446d5bf9d7ae571"
uuid = "fa6b7ba4-c1ee-5f82-b5fc-ecf0adba8f74"
version = "0.6.6"
version = "0.6.7"
[[deps.EarCut_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
@ -319,9 +319,9 @@ version = "0.9.17"
[[deps.FillArrays]]
deps = ["LinearAlgebra", "Random", "SparseArrays", "Statistics"]
git-tree-sha1 = "4c7d3757f3ecbcb9055870351078552b7d1dbd2d"
git-tree-sha1 = "0dbc5b9683245f905993b51d2814202d75b34f1a"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.13.0"
version = "0.13.1"
[[deps.FixedPointNumbers]]
deps = ["Statistics"]
@ -389,9 +389,9 @@ version = "0.64.0+0"
[[deps.GeometryBasics]]
deps = ["EarCut_jll", "IterTools", "LinearAlgebra", "StaticArrays", "StructArrays", "Tables"]
git-tree-sha1 = "58bcdf5ebc057b085e58d95c138725628dd7453c"
git-tree-sha1 = "83ea630384a13fc4f002b77690bc0afeb4255ac9"
uuid = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
version = "0.4.1"
version = "0.4.2"
[[deps.Gettext_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Libiconv_jll", "Pkg", "XML2_jll"]
@ -452,10 +452,9 @@ uuid = "d25df0c9-e2be-5dd7-82c8-3ad0b3e990b9"
version = "0.1.2"
[[deps.IniFile]]
deps = ["Test"]
git-tree-sha1 = "098e4d2c533924c921f9f9847274f2ad89e018b8"
git-tree-sha1 = "f550e6e32074c939295eb5ea6de31849ac2c9625"
uuid = "83e8ac13-25f8-5344-8a64-a9f2b223428f"
version = "0.5.0"
version = "0.5.1"
[[deps.InlineStrings]]
deps = ["Parsers"]
@ -469,9 +468,9 @@ uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
[[deps.InverseFunctions]]
deps = ["Test"]
git-tree-sha1 = "a7254c0acd8e62f1ac75ad24d5db43f5f19f3c65"
git-tree-sha1 = "91b5dcf362c5add98049e6c29ee756910b03051d"
uuid = "3587e190-3f89-42d0-90ee-14403ec27112"
version = "0.1.2"
version = "0.1.3"
[[deps.InvertedIndices]]
git-tree-sha1 = "bee5f1ef5bf65df56bdd2e40447590b272a5471f"
@ -507,9 +506,9 @@ version = "0.21.3"
[[deps.JSON3]]
deps = ["Dates", "Mmap", "Parsers", "StructTypes", "UUIDs"]
git-tree-sha1 = "7d58534ffb62cd947950b3aa9b993e63307a6125"
git-tree-sha1 = "8c1f668b24d999fb47baf80436194fdccec65ad2"
uuid = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
version = "1.9.2"
version = "1.9.4"
[[deps.JpegTurbo_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
@ -523,6 +522,12 @@ git-tree-sha1 = "f6250b16881adf048549549fba48b1161acdac8c"
uuid = "c1c5ebd0-6772-5130-a774-d5fcae4a789d"
version = "3.100.1+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.LZO_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "e5b909bcf985c5e2605737d2ce278ed791b89be6"
@ -536,9 +541,9 @@ version = "1.3.0"
[[deps.Latexify]]
deps = ["Formatting", "InteractiveUtils", "LaTeXStrings", "MacroTools", "Markdown", "Printf", "Requires"]
git-tree-sha1 = "a6552bfeab40de157a297d84e03ade4b8177677f"
git-tree-sha1 = "4f00cc36fede3c04b8acf9b2e2763decfdcecfa6"
uuid = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
version = "0.15.12"
version = "0.15.13"
[[deps.LazyArtifacts]]
deps = ["Artifacts", "Pkg"]
@ -600,10 +605,10 @@ uuid = "4b2f31a3-9ecc-558c-b454-b3730dcb73e9"
version = "2.35.0+0"
[[deps.Libtiff_jll]]
deps = ["Artifacts", "JLLWrappers", "JpegTurbo_jll", "Libdl", "Pkg", "Zlib_jll", "Zstd_jll"]
git-tree-sha1 = "340e257aada13f95f98ee352d316c3bed37c8ab9"
deps = ["Artifacts", "JLLWrappers", "JpegTurbo_jll", "LERC_jll", "Libdl", "Pkg", "Zlib_jll", "Zstd_jll"]
git-tree-sha1 = "c9551dd26e31ab17b86cbd00c2ede019c08758eb"
uuid = "89763e89-9b03-5906-acba-b20f662cd828"
version = "4.3.0+0"
version = "4.3.0+1"
[[deps.Libuuid_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
@ -623,9 +628,9 @@ version = "0.5.4"
[[deps.LogExpFunctions]]
deps = ["ChainRulesCore", "ChangesOfVariables", "DocStringExtensions", "InverseFunctions", "IrrationalConstants", "LinearAlgebra"]
git-tree-sha1 = "e5718a00af0ab9756305a0392832c8952c7426c1"
git-tree-sha1 = "db0eee9b3bb2b38ab2d94349a3b0272d0a68e21f"
uuid = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
version = "0.3.6"
version = "0.3.8"
[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
@ -680,9 +685,9 @@ version = "0.7.3"
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
[[deps.NaNMath]]
git-tree-sha1 = "b086b7ea07f8e38cf122f5016af580881ac914fe"
git-tree-sha1 = "737a5957f387b17e74d4ad2f440eb330b39a62c5"
uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
version = "0.3.7"
version = "1.0.0"
[[deps.NamedArrays]]
deps = ["Combinatorics", "DataStructures", "DelimitedFiles", "InvertedIndices", "LinearAlgebra", "Random", "Requires", "SparseArrays", "Statistics"]
@ -750,15 +755,15 @@ version = "8.44.0+0"
[[deps.PDMats]]
deps = ["LinearAlgebra", "SparseArrays", "SuiteSparse"]
git-tree-sha1 = "ee26b350276c51697c9c2d88a072b339f9f03d73"
git-tree-sha1 = "e8185b83b9fc56eb6456200e873ce598ebc7f262"
uuid = "90014a1f-27ba-587c-ab20-58faa44d9150"
version = "0.11.5"
version = "0.11.7"
[[deps.Parsers]]
deps = ["Dates"]
git-tree-sha1 = "13468f237353112a01b2d6b32f3d0f80219944aa"
git-tree-sha1 = "85b5da0fa43588c75bb1ff986493443f821c70b7"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "2.2.2"
version = "2.2.3"
[[deps.Pixman_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
@ -783,10 +788,10 @@ uuid = "995b91a9-d308-5afd-9ec6-746e21dbc043"
version = "1.1.3"
[[deps.Plots]]
deps = ["Base64", "Contour", "Dates", "Downloads", "FFMPEG", "FixedPointNumbers", "GR", "GeometryBasics", "JSON", "Latexify", "LinearAlgebra", "Measures", "NaNMath", "PlotThemes", "PlotUtils", "Printf", "REPL", "Random", "RecipesBase", "RecipesPipeline", "Reexport", "Requires", "Scratch", "Showoff", "SparseArrays", "Statistics", "StatsBase", "UUIDs", "UnicodeFun", "Unzip"]
git-tree-sha1 = "5c907bdee5966a9adb8a106807b7c387e51e4d6c"
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 = "9213b4c18b57b7020ee20f33a4ba49eb7bef85e0"
uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
version = "1.25.11"
version = "1.27.0"
[[deps.PooledArrays]]
deps = ["DataAPI", "Future"]
@ -796,9 +801,9 @@ version = "1.4.0"
[[deps.Preferences]]
deps = ["TOML"]
git-tree-sha1 = "2cf929d64681236a2e074ffafb8d568733d2e6af"
git-tree-sha1 = "d3538e7f8a790dc8903519090857ef8e1283eecd"
uuid = "21216c6a-2e73-6563-6e65-726566657250"
version = "1.2.3"
version = "1.2.5"
[[deps.PrettyTables]]
deps = ["Crayons", "Formatting", "Markdown", "Reexport", "Tables"]
@ -816,9 +821,9 @@ uuid = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79"
[[deps.PyCall]]
deps = ["Conda", "Dates", "Libdl", "LinearAlgebra", "MacroTools", "Serialization", "VersionParsing"]
git-tree-sha1 = "71fd4022ecd0c6d20180e23ff1b3e05a143959c2"
git-tree-sha1 = "1fc929f47d7c151c839c5fc1375929766fb8edcc"
uuid = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
version = "1.93.0"
version = "1.93.1"
[[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"]
@ -843,10 +848,10 @@ deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
[[deps.ROCAnalysis]]
deps = ["DataFrames", "LinearAlgebra", "Printf", "Random", "RecipesBase", "SpecialFunctions"]
git-tree-sha1 = "e04ce44600445a6dac9c9a9bf48ea8aa5c80e24a"
deps = ["DataFrames", "Distributions", "LinearAlgebra", "Printf", "Random", "RecipesBase", "SpecialFunctions", "StatsBase"]
git-tree-sha1 = "8ff37eccc3ff7605f292606153347f3cefe5520a"
uuid = "f535d66d-59bb-5153-8d2b-ef0a426c6aff"
version = "0.3.3"
version = "0.3.4"
[[deps.Random]]
deps = ["SHA", "Serialization"]
@ -859,9 +864,9 @@ version = "1.2.1"
[[deps.RecipesPipeline]]
deps = ["Dates", "NaNMath", "PlotUtils", "RecipesBase"]
git-tree-sha1 = "37c1631cb3cc36a535105e6d5557864c82cd8c2b"
git-tree-sha1 = "995a812c6f7edea7527bb570f0ac39d0fb15663c"
uuid = "01d81517-befc-4cb6-b9ec-a95719d0359c"
version = "0.5.0"
version = "0.5.1"
[[deps.Reexport]]
git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b"
@ -958,15 +963,15 @@ uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
[[deps.SpecialFunctions]]
deps = ["ChainRulesCore", "IrrationalConstants", "LogExpFunctions", "OpenLibm_jll", "OpenSpecFun_jll"]
git-tree-sha1 = "2735e252e72ee0367ebdb10b6148343fd15c2481"
git-tree-sha1 = "cbf21db885f478e4bd73b286af6e67d1beeebe4c"
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
version = "1.8.3"
version = "1.8.4"
[[deps.StaticArrays]]
deps = ["LinearAlgebra", "Random", "Statistics"]
git-tree-sha1 = "6354dfaf95d398a1a70e0b28238321d5d17b2530"
git-tree-sha1 = "74fb527333e72ada2dd9ef77d98e4991fb185f04"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.4.0"
version = "1.4.1"
[[deps.Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
@ -992,9 +997,9 @@ version = "0.9.16"
[[deps.StatsModels]]
deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Printf", "REPL", "ShiftedArrays", "SparseArrays", "StatsBase", "StatsFuns", "Tables"]
git-tree-sha1 = "677488c295051568b0b79a77a8c44aa86e78b359"
git-tree-sha1 = "03c99c7ef267c8526953cafe3c4239656693b8ab"
uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
version = "0.6.28"
version = "0.6.29"
[[deps.StructArrays]]
deps = ["Adapt", "DataAPI", "StaticArrays", "Tables"]
@ -1029,10 +1034,10 @@ uuid = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
version = "1.0.1"
[[deps.Tables]]
deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "TableTraits", "Test"]
git-tree-sha1 = "bb1064c9a84c52e277f1096cf41434b675cd368b"
deps = ["DataAPI", "DataValueInterfaces", "IteratorInterfaceExtensions", "LinearAlgebra", "OrderedCollections", "TableTraits", "Test"]
git-tree-sha1 = "5ce79ce186cc678bbb5c5681ca3379d1ddae11a1"
uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
version = "1.6.1"
version = "1.7.0"
[[deps.Tar]]
deps = ["ArgTools", "SHA"]
@ -1044,9 +1049,9 @@ uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[[deps.TimeZones]]
deps = ["Dates", "Downloads", "InlineStrings", "LazyArtifacts", "Mocking", "Printf", "RecipesBase", "Serialization", "Unicode"]
git-tree-sha1 = "0f1017f68dc25f1a0cb99f4988f78fe4f2e7955f"
git-tree-sha1 = "2d4b6de8676b34525ac518de36006dc2e89c7e2e"
uuid = "f269a46b-ccf7-5d73-abea-4c690281aa53"
version = "1.7.1"
version = "1.7.2"
[[deps.TranscodingStreams]]
deps = ["Random", "Test"]
@ -1090,15 +1095,15 @@ version = "1.19.0+0"
[[deps.Wayland_protocols_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "66d72dc6fcc86352f01676e8f0f698562e60510f"
git-tree-sha1 = "4528479aa01ee1b3b4cd0e6faef0e04cf16466da"
uuid = "2381bf8a-dfd0-557d-9999-79630e7b1b91"
version = "1.23.0+0"
version = "1.25.0+0"
[[deps.WeakRefStrings]]
deps = ["DataAPI", "InlineStrings", "Parsers"]
git-tree-sha1 = "c69f9da3ff2f4f02e811c3323c22e5dfcb584cfa"
git-tree-sha1 = "b1be2855ed9ed8eac54e5caff2afcdb442d52c23"
uuid = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
version = "1.4.1"
version = "1.4.2"
[[deps.WinReg]]
deps = ["Test"]

14
appB.jl
View File

@ -256,3 +256,17 @@ end
train2 = owensboro2[owensboro2.train, :]
test2 = owensboro2[.!owensboro2.train, :]
test3, train3 = groupby(owensboro2, :train, sort=true)
# Code for exercise 14.1
@time mean(x -> x < 0, -10^6:10^6)
@time mean(x -> x < 0, -10^6:10^6)
@time mean(x -> x < 0, -10^6:10^6)
@time mean(<(0), -10^6:10^6)
@time mean(<(0), -10^6:10^6)
@time mean(<(0), -10^6:10^6)
lt0(x) = x < 0
@time mean(lt0, -10^6:10^6)
@time mean(lt0, -10^6:10^6)
@time mean(lt0, -10^6:10^6)

121
ch14.jl
View File

@ -1 +1,120 @@
# consider showing Arrow
# Bogumił Kamiński, 2022
# Codes for chapter 14
# Codes for section 14.2
# start Julia with and additional -t4 command line switch
Threads.nthreads()
# Code for listing 14.1
function payoff_asian_sample(T, X0, K, r, s, m)::Float64
X = X0
sumX = X
d = T / m
for i in 1:m
X *= exp((r - s^2 / 2) * d + s * sqrt(d) * randn())
sumX += X
end
Y = sumX / (m + 1)
return exp(-r * T) * max(Y - K, 0)
end
# Code for checking the results of the payoff simulation
payoff_asian_sample(1.0, 50.0, 55.0, 0.05, 0.3, 200)
payoff_asian_sample(1.0, 50.0, 55.0, 0.05, 0.3, 200)
payoff_asian_sample(1.0, 50.0, 55.0, 0.05, 0.3, 200)
# Benchmarking map
using BenchmarkTools
@btime map(i -> payoff_asian_sample(1.0, 50.0, 55.0, 0.05, 0.3, 200), 1:10_000);
using ThreadsX
@btime ThreadsX.map(i -> payoff_asian_sample(1.0, 50.0, 55.0, 0.05, 0.3, 200), 1:10_000);
# Codes for section 14.3
# Code for listing 14.2
using Statistics
function asian_value(T, X0, K, r, s, m, max_time)
result = Float64[]
start_time = time()
while time() - start_time < max_time
append!(result, ThreadsX.map(_ -> payoff_asian_sample(T, X0, K, r, s, m), 1:10_000))
end
n = length(result)
mv = mean(result)
sdv = std(result)
lo95 = mv - 1.96 * sdv / sqrt(n)
hi95 = mv + 1.96 * sdv / sqrt(n)
zero = mean(==(0), result)
return (; n, mv, lo95, hi95, zero)
end
# Code for example of the mean function
mean(x -> x ^ 2, [1, 2, 3])
eq0 = ==(0)
eq0(1)
eq0(0)
# Code for shorthand NamedTuple notation
val1 = 10
val2 = "x"
(; val1, val2)
# Testing asian_value function
@time asian_value(1.0, 50.0, 55.0, 0.05, 0.3, 200, 0.25)
@time asian_value(1.0, 50.0, 55.0, 0.05, 0.3, 200, 0.25)
@time asian_value(1.0, 50.0, 55.0, 0.05, 0.3, 200, 0.25)
# Converting NamedTuple to JSON response
using Genie
Genie.Renderer.Json.json((firstname="Bogumił", lastname="Kamiński"))
# Codes for section 14.4
# Start a new Julia session
using HTTP
using JSON3
req = HTTP.request("POST", "http://127.0.0.1:8000",
["Content-Type" => "application/json"],
JSON3.write((K=55.0, max_time=0.25)))
JSON3.read(req.body)
HTTP.request("POST", "http://127.0.0.1:8000",
["Content-Type" => "application/json"],
JSON3.write((K="", max_time=0.25)))
using DataFrames
df = DataFrame(K=30:2:80, max_time=0.25)
df.data = map(df.K, df.max_time) do K, max_time
@show K
@time req = HTTP.request("POST", "http://127.0.0.1:8000",
["Content-Type" => "application/json"],
JSON3.write((;K, max_time)))
return JSON3.read(req.body)
end;
df
all(==("OK"), getproperty.(df.data, :status))
df2 = select(df, :K, :data => ByRow(x -> x.value) => AsTable)
using Plots
plot(plot(df2.K, df2.mv; legend=false,
xlabel="K", ylabel="expected value"),
plot(df2.K, df2.zero; legend=false,
xlabel="K", ylabel="probability of zero"))

View File

@ -1,18 +0,0 @@
using HTTP
using JSON3
using DataFrames
using Plots
df = DataFrame(K=30:2:80, max_time=0.25)
df.data = map(df.K, df.max_time) do K, max_time
@show K
@time req = HTTP.request("POST", "http://127.0.0.1:8000",
["Content-Type" => "application/json"],
JSON3.write((;K, max_time)))
return JSON3.read(req.body)
end
@assert all(==("OK"), getproperty.(df.data, :status))
df2 = select(df, :K, :data => ByRow(x -> x.value) => AsTable)
plot(plot(df2.K, df2.mv; legend=false, xlabel="K", ylabel="expected value"),
plot(df2.K, df2.zero; legend=false, xlabel="K", ylabel="probability of zero"))

View File

@ -2,22 +2,25 @@ using Genie
using Statistics
using ThreadsX
function v_asian_sample(T, X0, K, r, s, m)::Float64
function payoff_asian_sample(T, X0, K, r, s, m)::Float64
X = X0
sumX = X
d = T / m
for i in 1:m
X *= exp((r-s^2/2)*d + s*sqrt(d)*randn())
X *= exp((r - s^2 / 2) * d + s * sqrt(d) * randn())
sumX += X
end
return exp(-r*T) * max(sumX / (m + 1) - K, 0)
Y = sumX / (m + 1)
return exp(-r * T) * max(Y - K, 0)
end
function v_asian_value(T, X0, K, r, s, m, max_time)
function asian_value(T, X0, K, r, s, m, max_time)
result = Float64[]
start_time = time()
while time() - start_time < max_time
append!(result, ThreadsX.map(_ -> v_asian_sample(T, X0, K, r, s, m), 1:10_000))
append!(result,
ThreadsX.map(_ -> payoff_asian_sample(T, X0, K, r, s, m),
1:10_000))
end
n = length(result)
mv = mean(result)
@ -35,7 +38,7 @@ Genie.Router.route("/", method=POST) do
return try
K = float(message["K"])
max_time = float(message["max_time"])
value = v_asian_value(1.0, 50.0, K, 0.05, 0.3, 200, max_time)
value = asian_value(1.0, 50.0, K, 0.05, 0.3, 200, max_time)
Genie.Renderer.Json.json((status="OK", value=value))
catch
Genie.Renderer.Json.json((status="ERROR", value=""))