@@ -48,12 +48,32 @@ def _(mo):
4848
4949
5050@app .cell
51- def _ ():
51+ def _ (mo ):
52+ regenerate_btn = mo .ui .run_button (label = "Regenerate Path" , kind = "info" )
53+ regenerate_btn
54+ return (regenerate_btn ,)
55+
56+
57+ @app .cell
58+ def _ (regenerate_btn ):
5259 from quantflow .sp .weiner import WeinerProcess
60+ from quantflow .utils import plot
61+ from quantflow .utils .dates import start_of_day
62+
63+ regenerate_btn
64+
5365 weiner = WeinerProcess (sigma = 2.0 )
5466 weiner_paths = weiner .sample (n = 1 , time_horizon = 1 , time_steps = 24 * 60 * 60 )
55- weiner_paths .plot (title = "A path of Weiner process with sigma=2.0" )
56- return (weiner_paths ,)
67+ weiner_df = weiner_paths .as_datetime_df (start = start_of_day (), unit = "d" ).reset_index ()
68+
69+ plot .plot_lines (
70+ weiner_df ,
71+ x = weiner_df .columns [0 ],
72+ y = weiner_df .columns [1 ],
73+ title = "Weiner Process Path" ,
74+ labels = {"value" : "Value" , "variable" : "Path" , weiner_df .columns [0 ]: "Date" },
75+ )
76+ return plot , start_of_day , weiner_df , weiner_paths
5777
5878
5979@app .cell
@@ -64,23 +84,6 @@ def _(mo):
6484 return
6585
6686
67- @app .cell
68- def _ (weiner_paths ):
69-
70- from quantflow .utils .dates import start_of_day
71- df = weiner_paths .as_datetime_df (start = start_of_day (), unit = "d" ).reset_index ()
72- from quantflow .utils import plot
73-
74- plot .plot_lines (
75- df ,
76- x = df .columns [0 ],
77- y = df .columns [1 ],
78- title = "Weiner Process Path" ,
79- labels = {"value" : "Value" , "variable" : "Path" , df .columns [0 ]: "Date" },
80- )
81- return df , plot , start_of_day
82-
83-
8487@app .cell
8588def _ (mo ):
8689 mo .md (r"""
@@ -140,7 +143,7 @@ def _(mo):
140143
141144
142145@app .cell
143- def _ (df ):
146+ def _ (weiner_df ):
144147 import pandas as pd
145148 import polars as pl
146149 import math
@@ -164,7 +167,7 @@ def rstd(pdf: pl.Series, range_seconds: float) -> float:
164167 results = []
165168 for period in ("10s" , "20s" , "30s" , "1m" , "2m" , "3m" , "5m" , "10m" , "30m" ):
166169 ohlc = template .model_copy (update = dict (period = period ))
167- rf = ohlc (df )
170+ rf = ohlc (weiner_df )
168171 ts = pd .to_timedelta (period ).to_pytimedelta ().total_seconds ()
169172 data = dict (period = period )
170173 for name in ("pk" , "gk" , "rs" ):
@@ -214,7 +217,7 @@ def _(mo):
214217
215218
216219@app .cell
217- def _ (OHLC , pd ):
220+ def _ (OHLC , pd , weiner_df ):
218221 from typing import Sequence
219222 import numpy as np
220223 from collections import defaultdict
@@ -241,7 +244,7 @@ def ohlc_hurst_exponent(
241244 estimators = defaultdict (list )
242245 for period in periods :
243246 ohlc = template .model_copy (update = dict (period = period ))
244- rf = ohlc (df )
247+ rf = ohlc (weiner_df )
245248 ts = pd .to_timedelta (period ).to_pytimedelta ().total_seconds ()
246249 time_range .append (ts )
247250 for name in estimator_names :
@@ -252,8 +255,8 @@ def ohlc_hurst_exponent(
252255
253256
254257@app .cell
255- def _ (df , ohlc_hurst_exponent ):
256- ohlc_hurst_exponent (df , series = ["0" ])
258+ def _ (ohlc_hurst_exponent , weiner_df ):
259+ ohlc_hurst_exponent (weiner_df , series = ["0" ])
257260 return
258261
259262
@@ -270,13 +273,54 @@ def _(mo):
270273
271274
272275@app .cell
273- def _ (pd , start_of_day ):
276+ def _ (pd , regenerate_vasicek , start_of_day ):
274277 from quantflow .sp .ou import Vasicek
275-
278+ regenerate_vasicek
276279 p = Vasicek (kappa = 2 )
277280 paths = {f"kappa={ k } " : Vasicek (kappa = k ).sample (n = 1 , time_horizon = 1 , time_steps = 24 * 60 * 6 ) for k in (1.0 , 10.0 , 50.0 , 100.0 , 500.0 )}
278281 pdf = pd .DataFrame ({k : p .path (0 ) for k , p in paths .items ()}, index = paths ["kappa=1.0" ].dates (start = start_of_day ()))
279282 pdf .plot ()
283+ return paths , pdf
284+
285+
286+ @app .cell
287+ def _ (mo ):
288+ regenerate_vasicek = mo .ui .run_button (label = "Regenerate Paths" , kind = "info" )
289+ regenerate_vasicek
290+ return (regenerate_vasicek ,)
291+
292+
293+ @app .cell
294+ def _ (mo ):
295+ mo .md (r"""
296+ We can now estimate the Hurst exponent from the realized variance. As we can see the Hurst exponent decreases as we increase the mean reversion parameter.
297+ """ )
298+ return
299+
300+
301+ @app .cell
302+ def _ (paths , pd ):
303+ pd .DataFrame ({k : [p .hurst_exponent ()] for k , p in paths .items ()})
304+ return
305+
306+
307+ @app .cell
308+ def _ (mo ):
309+ mo .md (r"""
310+ And we can also estimate the Hurst exponent from the range-based estimators. As we can see the Hurst exponent decreases as we increase the mean reversion parameter along the same lines as the realized variance.
311+ """ )
312+ return
313+
314+
315+ @app .cell
316+ def _ (ohlc_hurst_exponent , paths , pdf ):
317+ ohlc_hurst_exponent (pdf .reset_index (), list (paths ), periods = ("10m" , "20m" , "30m" , "1h" ))
318+ return
319+
320+
321+ @app .cell
322+ def _ (pdf ):
323+ pdf .columns
280324 return
281325
282326
0 commit comments