Skip to content

Commit

Permalink
Merge pull request #287 from fslaborg/update-interval
Browse files Browse the repository at this point in the history
  • Loading branch information
bvenn authored Aug 2, 2023
2 parents 8ab0975 + 480a9b9 commit 0460cb9
Show file tree
Hide file tree
Showing 25 changed files with 709 additions and 513 deletions.
2 changes: 1 addition & 1 deletion docs/Interpolation.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ To reduce this overfitting you can use x axis nodes that are spaced according to
// new x values are determined in the x axis range of the data. These should reduce overshooting behaviour.
// since the original data consisted of 16 points, 16 nodes are initialized
let xs_cheby =
Interpolation.Approximation.chebyshevNodes (Intervals.Interval<float>.Create(0.,3.)) 16
Interpolation.Approximation.chebyshevNodes (Interval.CreateClosed(0.,3.)) 16

// to get the corresponding y values to the xs_cheby a linear spline is generated that approximates the new y values
let ys_cheby =
Expand Down
52 changes: 31 additions & 21 deletions docs/Intervals.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ The interval module enables working with closed intervals. A closed interval inc
- $[1,2], \left\{ x | 1 \le x \le 2 \right\}$ - closed interval; 1 and 2 are _included_
- $(1,2), \left\{ x | 1 < x < 2 \right\}$ - open interval; 1 and 2 are _excluded_
- $[1,2), \left\{ x | 1 \le x < 2 \right\}$ - half open interval; 1 is _included_ but 2 is _excluded_
- $[1,2), \left\{ x | 1 \le x < 2 \right\}$ - right open interval; 1 is _included_ but 2 is _excluded_
- $(1,2], \left\{ x | 1 < x \le 2 \right\}$ - left open interval; 1 is _excluded_ but 2 is _included_
**Interval creation**
Expand All @@ -47,8 +48,17 @@ The interval module enables working with closed intervals. A closed interval inc
open FSharp.Stats
open Plotly.NET

let myInterval = Interval.CreateLeftOpen (-3.,2.)

let loi = sprintf "myInterval is: %s" (myInterval.ToString())

(*** include-value:loi***)

(**
When intervals are created from sequences, always closed intervals are generated!
*)
let collection = [3.0; -2.0; 5.0; 1.0; -6.0; 100.0]
let interval = Intervals.ofSeq collection
let interval = Interval.ofSeq collection

(*** include-value:interval***)

Expand All @@ -64,7 +74,7 @@ open Plotly.NET.StyleParam

let interval01 =
Chart.Point([])
|> Chart.withShape (Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart interval,X1=Intervals.getEnd interval,Y0=1,Y1=2,FillColor=Color.fromHex "#1f77b4"))
|> Chart.withShape (Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart interval,X1=Interval.getEnd interval,Y0=1,Y1=2,FillColor=Color.fromHex "#1f77b4"))
|> Chart.withTemplate ChartTemplates.lightMirrored
|> Chart.withXAxisStyle ("",MinMax=(-10.,120.))
|> Chart.withYAxisStyle ("",MinMax=(0.,5.))
Expand All @@ -83,8 +93,8 @@ interval01 |> GenericChart.toChartHTML
*)

let collectionBy = [("a",3.0); ("b",-2.0); ("c",5.0); ("d",1.0); ("e",-6.0); ("f",100.0)]
let intervalByFst = Intervals.ofSeqBy fst collectionBy
let intervalBySnd = Intervals.ofSeqBy snd collectionBy
let intervalByFst = Interval.ofSeqBy fst collectionBy
let intervalBySnd = Interval.ofSeqBy snd collectionBy

(*** include-value:intervalByFst***)
(*** include-value:intervalBySnd***)
Expand All @@ -104,15 +114,15 @@ i + j = [a+b,c+d]
*)

let i02 = Intervals.create 6. 8.
let i03 = Intervals.create 5. 10.
let addedInterval = Intervals.add i02 i03
let i02 = Interval.CreateClosed<float> (6.,8.)
let i03 = Interval.CreateClosed<float> (5.,10.)
let addedInterval = Interval.add i02 i03

(*** hide ***)
let interval02 =
let i1 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart i02,X1=Intervals.getEnd i02,Y0=1,Y1=2,FillColor=Color.fromHex "#1f77b4")
let i2 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart i03,X1=Intervals.getEnd i03,Y0=3,Y1=4,FillColor=Color.fromHex "#ff7f0e")
let re = Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart addedInterval,X1=Intervals.getEnd addedInterval,Y0=5,Y1=6,FillColor=Color.fromHex "#2ca02c")
let i1 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart i02,X1=Interval.getEnd i02,Y0=1,Y1=2,FillColor=Color.fromHex "#1f77b4")
let i2 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart i03,X1=Interval.getEnd i03,Y0=3,Y1=4,FillColor=Color.fromHex "#ff7f0e")
let re = Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart addedInterval,X1=Interval.getEnd addedInterval,Y0=5,Y1=6,FillColor=Color.fromHex "#2ca02c")
Chart.Point([])
|> Chart.withShapes [i1;i2;re]
|> Chart.withTemplate ChartTemplates.lightMirrored
Expand All @@ -137,13 +147,13 @@ i - j = [a-d,b-c]
*)

let subInterval = Intervals.subtract i02 i03
let subInterval = Interval.subtract i02 i03

(*** hide ***)
let interval03 =
let i1 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart i02,X1=Intervals.getEnd i02,Y0=1,Y1=2,FillColor=Color.fromHex "#1f77b4")
let i2 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart i03,X1=Intervals.getEnd i03,Y0=3,Y1=4,FillColor=Color.fromHex "#ff7f0e")
let re = Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart subInterval,X1=Intervals.getEnd subInterval,Y0=5,Y1=6,FillColor=Color.fromHex "#2ca02c")
let i1 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart i02,X1=Interval.getEnd i02,Y0=1,Y1=2,FillColor=Color.fromHex "#1f77b4")
let i2 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart i03,X1=Interval.getEnd i03,Y0=3,Y1=4,FillColor=Color.fromHex "#ff7f0e")
let re = Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart subInterval,X1=Interval.getEnd subInterval,Y0=5,Y1=6,FillColor=Color.fromHex "#2ca02c")
Chart.Point([])
|> Chart.withShapes [i1;i2;re]
|> Chart.withTemplate ChartTemplates.lightMirrored
Expand All @@ -162,15 +172,15 @@ Closed intervals include their margins. If a margin is shared between two interv
*)

let i04 = Intervals.create 2. 8.
let i05 = Intervals.create 5. 10.
let intInterval = Intervals.intersect i04 i05
let i04 = Interval.CreateClosed<float> (2.,8.)
let i05 = Interval.CreateClosed<float> (5.,10.)
let intInterval = Interval.intersect i04 i05

(*** hide ***)
let interval04 =
let i1 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart i04,X1=Intervals.getEnd i04,Y0=1,Y1=2,FillColor=Color.fromHex "#1f77b4")
let i2 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart i05,X1=Intervals.getEnd i05,Y0=3,Y1=4,FillColor=Color.fromHex "#ff7f0e")
let re = Shape.init(ShapeType=ShapeType.Rectangle,X0=Intervals.getStart intInterval.Value,X1=Intervals.getEnd intInterval.Value,Y0=5,Y1=6,FillColor=Color.fromHex "#2ca02c")
let i1 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart i04,X1=Interval.getEnd i04,Y0=1,Y1=2,FillColor=Color.fromHex "#1f77b4")
let i2 = Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart i05,X1=Interval.getEnd i05,Y0=3,Y1=4,FillColor=Color.fromHex "#ff7f0e")
let re = Shape.init(ShapeType=ShapeType.Rectangle,X0=Interval.getStart intInterval,X1=Interval.getEnd intInterval,Y0=5,Y1=6,FillColor=Color.fromHex "#2ca02c")
Chart.Point([])
|> Chart.withShapes [i1;i2;re]
|> Chart.withTemplate ChartTemplates.lightMirrored
Expand Down
6 changes: 3 additions & 3 deletions docs/Signal.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ let sampleO1 = [|45.;42.;45.5;43.;47.;51.;34.;45.;44.;46.;48.;37.;46.;|]

let outlierBordersO1 = Signal.Outliers.tukey 1.5 sampleO1

let lowerBorderO1 = Intervals.getStart outlierBordersO1
let lowerBorderO1 = Interval.getStart outlierBordersO1
// result: 37.16667

let upperBorderO1 = Intervals.getEnd outlierBordersO1
let upperBorderO1 = Interval.getEnd outlierBordersO1
// result: 51.83333

let (inside,outside) =
sampleO1
|> Array.partition (fun x -> Intervals.liesInInterval x outlierBordersO1)
|> Array.partition (fun x -> outlierBordersO1.liesInInterval x)

let tukeyOutlierChart =
[
Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Stats/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ module Array =
let current = a.[index]
loop (index+1) (min current minimum) (max current maximum)
else
Intervals.create minimum maximum
Interval.CreateClosed<'a> (minimum,maximum)
//Init by fist value
if a.Length > 1 then
loop 1 a.[0] a.[0]
else
Intervals.Interval.Empty
Interval.Empty


// Swaps items of left and right index
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/ConfidenceInterval.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ module ConfidenceInterval =
let ci ciLevel (sample : seq<float>)=
let mean = Seq.mean sample
let delta = ciDeviation ciLevel sample
Intervals.create (mean - delta) (mean + delta)
Interval.CreateClosed<float> ((mean - delta),(mean + delta))
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Bandwidth.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module Bandwidth =
|> Seq.filter (fun v -> not (isInf v))

let interval = Seq.range data'
let dmin,dmax = Intervals.values interval
let dmin,dmax = Interval.values interval

fromBinNumber dmin dmax (float(Seq.length(data)))

Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Continuous/ChiSquared.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type ChiSquared =
/// Returns the support of the exponential distribution: [0, Positive Infinity).
static member Support dof =
ChiSquared.CheckParam dof
Intervals.create 0. System.Double.PositiveInfinity
Interval.CreateClosed<float> (0.,System.Double.PositiveInfinity)


/// A string representation of the distribution.
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Continuous/Exponential.fs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type Exponential =
/// Returns the support of the exponential distribution: [0, Positive Infinity).
static member Support lambda =
Exponential.CheckParam lambda
Intervals.create 0.0 System.Double.PositiveInfinity
Interval.CreateClosed<float> (0.0,System.Double.PositiveInfinity)


/// A string representation of the distribution.
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Continuous/Uniform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type Uniform =
/// Returns the support of the exponential distribution: [0, Positive Infinity).
static member Support min max =
Uniform.CheckParam min max
Intervals.create min max
Interval.CreateClosed<float> (min,max)

/// A string representation of the distribution.
static member ToString min max =
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Discrete/Bernoulli.fs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type Bernoulli =
/// Returns the support of the bernoulli distribution: {0, 1}.
static member Support p =
Bernoulli.CheckParam p
Intervals.create 0 1
Interval.CreateClosed<int> (0,1)

/// A string representation of the distribution.
static member ToString p =
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Discrete/Binomial.fs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type Binomial =
/// Returns the support of the Binomial distribution: (0., n).
static member Support p n =
Binomial.CheckParam p n
Intervals.create 0 n
Interval.CreateClosed<int> (0,n)


/// A string representation of the distribution.
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Discrete/Hypergeometric.fs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ type Hypergeometric =
/// Returns the support of the hypergeometric distribution: (0., Positive Infinity).
static member Support N K n =
Hypergeometric.CheckParam N K n
Intervals.create (max 0 (n + K - N) ) (min K n)
Interval.CreateClosed<int> ((max 0 (n + K - N) ),(min K n))



Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Distributions/Discrete/Poisson.fs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ type Poisson =
/// Returns the support interval for this distribution.
static member Support lambda =
Poisson.CheckParam lambda
Intervals.create 0 System.Int32.MaxValue
Interval.CreateClosed<int> (0,System.Int32.MaxValue)

/// A string representation of the distribution.
static member ToString lambda =
Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Stats/Distributions/KernelDensity.fs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module KernelDensity =


let estimateWithWeight weights kernel bandwidth data =
let _from,_to = Array.range data |> Intervals.values
let _from,_to = Array.range data |> Interval.values
estimateWith 3 _from _to 512 weights kernel bandwidth data


Expand All @@ -143,7 +143,7 @@ module KernelDensity =


let estimate kernel bandwidth data =
let _from,_to = Array.range data |> Intervals.values
let _from,_to = Array.range data |> Interval.values
let weights = Array.create data.Length (1./float data.Length)
estimateWith 3 _from _to 512 weights kernel bandwidth data

Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/FSharp.Stats.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<Compile Include="Ops.fs" />
<Compile Include="Random.fs" />
<Compile Include="ServiceLocator.fs" />
<Compile Include="Intervals.fs" />
<Compile Include="Interval.fs" />
<Compile Include="Permutation.fs" />
<Compile Include="BigRational.fs" />
<Compile Include="INumeric.fs" />
Expand Down
12 changes: 6 additions & 6 deletions src/FSharp.Stats/Interpolation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ module Interpolation =
/// <param name="n">number of points that should be placed within the interval (incl. start and end)</param>
/// <returns>Collection of chebyshev spaced x values.</returns>
/// <remarks>www.mscroggs.co.uk/blog/57</remarks>
let chebyshevNodes (interval: Intervals.Interval<float>) n =
let chebyshevNodes (interval: Interval<float>) n =
let center = 0.5 * (interval.TryStart.Value + interval.TryEnd.Value)
let halfrange = 0.5 * (interval.TryEnd.Value - interval.TryStart.Value)
Array.init n (fun i ->
Expand All @@ -1497,8 +1497,8 @@ module Interpolation =
/// <param name="interval">start and end value of interpolation range</param>
/// <param name="n">number of points that should be placed within the interval (incl. start and end)</param>
/// <returns>Collection of equally spaced x values.</returns>
let equalNodes (interval: Intervals.Interval<float>) n =
let range = Intervals.getSize interval
let equalNodes (interval: Interval<float>) n =
let range = Interval.getSize interval
Vector.init n (fun k -> interval.TryStart.Value + float k * range / (float n - 1.))


Expand All @@ -1513,7 +1513,7 @@ module Interpolation =
/// <param name="n">number of points that should be placed within the interval (incl. start and end)</param>
/// <param name="spacing">X values can be spaced equally or according to chebyshev.</param>
/// <returns>Coefficients for polynomial interpolation. Use Polynomial.fit to predict function values.</returns>
static member approxWithPolynomial (f: float -> float,i: Intervals.Interval<float>,n: int,spacing: Approximation.Spacing) =
static member approxWithPolynomial (f: float -> float,i: Interval<float>,n: int,spacing: Approximation.Spacing) =
match spacing with
| Approximation.Equally ->
let xVal = Approximation.equalNodes i n
Expand All @@ -1536,14 +1536,14 @@ module Interpolation =
static member approxWithPolynomialFromValues (xData: seq<float>,yData: seq<float>,n: int,spacing: Approximation.Spacing) =
match spacing with
| Approximation.Equally ->
let i = Intervals.ofSeq xData
let i = Interval.ofSeq xData
let linearSplineCoeff = LinearSpline.interpolate (Array.ofSeq xData) (Array.ofSeq yData)
let f = LinearSpline.predict linearSplineCoeff
let xVal = Approximation.equalNodes i n
let yVal = Vector.map f xVal
Polynomial.interpolate xVal yVal
| Approximation.Chebyshev ->
let i = Intervals.ofSeq xData
let i = Interval.ofSeq xData
let linearSplineCoeff = LinearSpline.interpolate (Array.ofSeq xData) (Array.ofSeq yData)
let f = LinearSpline.predict linearSplineCoeff
let xVal = Approximation.chebyshevNodes i n
Expand Down
Loading

0 comments on commit 0460cb9

Please sign in to comment.