1 people like it.

Hurst exponent

This is a simple port in F# of the R/S Hurst exponent

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
// simple port of the python code
// from this excellent article 
// https://www.quantstart.com/articles/Basics-of-Statistical-Mean-Reversion-Testing
open System

let gbmts ()  =
    let random =  MathNet.Numerics.Random.SystemRandomSource.FastDoubles(10000)
    random |> Array.fold (fun st x -> st@[((List.last st)+(x-0.5))] ) [1000.0]  |> Seq.toArray

let mrevts ()  =
    MathNet.Numerics.Random.SystemRandomSource.FastDoubles(10000)
       
let trendts ()  =
    let random =  MathNet.Numerics.Random.SystemRandomSource.FastDoubles(10000)
    random |> Array.fold (fun st x -> st@[((List.last st)+(x+1.0))] ) [1000.0]  |> Seq.toArray

let difvec (vecin:float []) lag =
    let vsub = vecin |> Array.skip lag 
    let dif = vsub |> Array.mapi(fun i x -> x - vecin.[i]  )
    MathNet.Numerics.Statistics.ArrayStatistics.StandardDeviation(dif) |> sqrt

let hurst (vecpriceraw:double array) =
    let vecprice = vecpriceraw |> Array.map(log)
    let range = [|2..100|]
    let lagvec = range |> Array.map (difvec vecprice)
    let logvec = lagvec |> Array.map (log)
    let loglag = range |>  Array.map(float >> log)
    let (_,slope) = MathNet.Numerics.LinearRegression.SimpleRegression.Fit (loglag,logvec)
    slope*2.0
/// test the hurst exponent
/// > gbmts() |> hurst;;
/// val it : float = 0.5092820778
/// > mrevts() |> hurst;;
/// val it : float = -0.0003832790107
/// > trendts() |> hurst;;
/// val it : float = 0.9824236056
namespace System
val gbmts : unit -> float []

Full name: Script.gbmts
val random : float []
namespace MathNet
namespace MathNet.Numerics
namespace MathNet.Numerics.Random
Multiple items
type SystemRandomSource =
  inherit RandomSource
  new : unit -> SystemRandomSource + 3 overloads
  static member Default : SystemRandomSource
  static member DoubleSequence : unit -> IEnumerable<float> + 1 overload
  static member Doubles : values:float[] * seed:int -> unit + 1 overload
  static member FastDoubles : values:float[] -> unit + 1 overload

Full name: MathNet.Numerics.Random.SystemRandomSource

--------------------
MathNet.Numerics.Random.SystemRandomSource() : unit
MathNet.Numerics.Random.SystemRandomSource(threadSafe: bool) : unit
MathNet.Numerics.Random.SystemRandomSource(seed: int) : unit
MathNet.Numerics.Random.SystemRandomSource(seed: int, threadSafe: bool) : unit
MathNet.Numerics.Random.SystemRandomSource.FastDoubles(length: int) : float []
MathNet.Numerics.Random.SystemRandomSource.FastDoubles(values: float []) : unit
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val fold : folder:('State -> 'T -> 'State) -> state:'State -> array:'T [] -> 'State

Full name: Microsoft.FSharp.Collections.Array.fold
val st : float list
val x : float
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val last : list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.last
module Seq

from Microsoft.FSharp.Collections
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
val mrevts : unit -> float []

Full name: Script.mrevts
val trendts : unit -> float []

Full name: Script.trendts
val difvec : vecin:float [] -> lag:int -> float

Full name: Script.difvec
val vecin : float []
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val lag : int
val vsub : float []
val skip : count:int -> array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.skip
val dif : float []
val mapi : mapping:(int -> 'T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.mapi
val i : int
namespace MathNet.Numerics.Statistics
type ArrayStatistics =
  static member Covariance : samples1:float[] * samples2:float[] -> float + 2 overloads
  static member FiveNumberSummaryInplace : data:float[] -> float[] + 1 overload
  static member GeometricMean : data:float[] -> float + 2 overloads
  static member HarmonicMean : data:float[] -> float + 2 overloads
  static member InterquartileRangeInplace : data:float[] -> float + 1 overload
  static member LowerQuartileInplace : data:float[] -> float + 1 overload
  static member Maximum : data:float[] -> float + 1 overload
  static member MaximumAbsolute : data:float[] -> float + 1 overload
  static member MaximumMagnitudePhase : data:Complex[] -> Complex + 1 overload
  static member Mean : data:float[] -> float + 2 overloads
  ...

Full name: MathNet.Numerics.Statistics.ArrayStatistics
MathNet.Numerics.Statistics.ArrayStatistics.StandardDeviation(samples: float32 []) : float
MathNet.Numerics.Statistics.ArrayStatistics.StandardDeviation(samples: int []) : float
MathNet.Numerics.Statistics.ArrayStatistics.StandardDeviation(samples: float []) : float
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
val hurst : vecpriceraw:double array -> float

Full name: Script.hurst
val vecpriceraw : double array
Multiple items
val double : value:'T -> double (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.double

--------------------
type double = Double

Full name: Microsoft.FSharp.Core.double
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
val vecprice : double []
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val log : value:'T -> 'T (requires member Log)

Full name: Microsoft.FSharp.Core.Operators.log
val range : int []
val lagvec : float []
val logvec : float []
val loglag : float []
val slope : float
namespace MathNet.Numerics.LinearRegression
type SimpleRegression =
  static member Fit : samples:IEnumerable<Tuple<float, float>> -> Tuple<float, float> + 1 overload

Full name: MathNet.Numerics.LinearRegression.SimpleRegression
MathNet.Numerics.LinearRegression.SimpleRegression.Fit(samples: Collections.Generic.IEnumerable<float * float>) : float * float
MathNet.Numerics.LinearRegression.SimpleRegression.Fit(x: float [], y: float []) : float * float
Raw view Test code New version

More information

Link:http://fssnip.net/7RE
Posted:7 years ago
Author:antiluv
Tags: #quant , #finance , #hurst