0 people like it.
Like the snippet!
Fuzzy calculus
Simple implementation of interval and fuzzy calculus.
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:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
|
open System
let min (xs : double list) = Seq.fold (fun (acc : double) x -> Math.Min(acc,x)) Double.MaxValue xs
let max (xs : double list) = Seq.fold (fun (acc : double) x -> Math.Max(acc,x)) Double.MinValue xs
type Interval =
{ a:double; b:double }
static member (*) (x : Interval, y : Interval) =
let list = [x.a * y.a; x.a * y.b; x.b * y.a; x.b * y.b]
{ a = min list; b = max list}
static member (/) (x : Interval, y : Interval) = { a = x.a / y.b; b = x.b / y.a}
static member (+) (x : Interval, y : Interval) = { a = x.a + y.a; b = x.b + y.b}
static member (-) (x : Interval, y : Interval) = { a = x.a - y.b; b = x.b - y.a}
static member (*) (x : Interval, y : double) = { a = x.a * y; b = x.b * y}
static member (/) (x : Interval, y : double) = { a = x.a / y; b = x.b / y}
static member (+) (x : Interval, y : double) = { a = x.a + y; b = x.b + y}
static member (-) (x : Interval, y : double) = { a = x.a - y; b = x.b - y}
static member (*) (x : double, y : Interval) = { a = x * y.a; b = x * y.b}
static member (/) (x : double, y : Interval) = { a = x / y.b; b = x / y.a}
static member (+) (x : double, y : Interval) = { a = x + y.a; b = x + y.b}
static member (-) (x : double, y : Interval) = { a = x - y.b; b = x - y.a}
static member pow (x : Interval, p : double) = { a = Math.Pow(x.a, p); b = Math.Pow(x.b, p)}
let zeroLengthInterval(x : double) = {a=x; b=x}
type Fuzzy =
{ Top : Interval; Bottom : Interval }
static member (*) (x : Fuzzy, y : Fuzzy) = { Top = x.Top * y.Top; Bottom = x.Bottom * y.Bottom }
static member (/) (x : Fuzzy, y : Fuzzy) = { Top = x.Top / y.Top; Bottom = x.Bottom / y.Bottom }
static member (+) (x : Fuzzy, y : Fuzzy) = { Top = x.Top + y.Top; Bottom = x.Bottom + y.Bottom }
static member (-) (x : Fuzzy, y : Fuzzy) = { Top = x.Top - y.Top; Bottom = x.Bottom - y.Bottom }
static member (*) (x : Fuzzy, y : double) = { Top = x.Top * y; Bottom = x.Bottom * y }
static member (/) (x : Fuzzy, y : double) = { Top = x.Top / y; Bottom = x.Bottom / y }
static member (+) (x : Fuzzy, y : double) = { Top = x.Top + y; Bottom = x.Bottom + y }
static member (-) (x : Fuzzy, y : double) = { Top = x.Top - y; Bottom = x.Bottom - y }
static member (*) (x : double, y : Fuzzy) = { Top = x * y.Top; Bottom = x * y.Bottom }
static member (/) (x : double, y : Fuzzy) = { Top = x / y.Top; Bottom = x / y.Bottom }
static member (+) (x : double, y : Fuzzy) = { Top = x + y.Top; Bottom = x + y.Bottom }
static member (-) (x : double, y : Fuzzy) = { Top = x - y.Top; Bottom = x - y.Bottom }
static member pow (x : Fuzzy, p : double) =
{
Top = Interval.pow(x.Top, p)
Bottom = Interval.pow(x.Bottom, p)
}
let number(a,b,c) = { Top = zeroLengthInterval(b); Bottom = { a=a; b=c } }
//Example - fuzzy bond valuation
let i1 = number(0.11,0.12,0.14)
let i2 = number(0.08,0.11,0.16)
let M = 1000.
let couponRate = 0.1
let coupon = M * couponRate
let presentValue = coupon/(1.+i1)+(coupon + M)/Fuzzy.pow(1.+i2, 2.)
|
namespace System
val min : xs:double list -> double
Full name: Script.min
val xs : double list
Multiple items
val double : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.double
--------------------
type double = Double
Full name: Microsoft.FSharp.Core.double
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
module Seq
from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State
Full name: Microsoft.FSharp.Collections.Seq.fold
val acc : double
val x : float
type Math =
static val PI : float
static val E : float
static member Abs : value:sbyte -> sbyte + 6 overloads
static member Acos : d:float -> float
static member Asin : d:float -> float
static member Atan : d:float -> float
static member Atan2 : y:float * x:float -> float
static member BigMul : a:int * b:int -> int64
static member Ceiling : d:decimal -> decimal + 1 overload
static member Cos : d:float -> float
...
Full name: System.Math
Math.Min(val1: decimal, val2: decimal) : decimal
(+0 other overloads)
Math.Min(val1: float, val2: float) : float
(+0 other overloads)
Math.Min(val1: float32, val2: float32) : float32
(+0 other overloads)
Math.Min(val1: uint64, val2: uint64) : uint64
(+0 other overloads)
Math.Min(val1: int64, val2: int64) : int64
(+0 other overloads)
Math.Min(val1: uint32, val2: uint32) : uint32
(+0 other overloads)
Math.Min(val1: int, val2: int) : int
(+0 other overloads)
Math.Min(val1: uint16, val2: uint16) : uint16
(+0 other overloads)
Math.Min(val1: int16, val2: int16) : int16
(+0 other overloads)
Math.Min(val1: byte, val2: byte) : byte
(+0 other overloads)
type Double =
struct
member CompareTo : value:obj -> int + 1 overload
member Equals : obj:obj -> bool + 1 overload
member GetHashCode : unit -> int
member GetTypeCode : unit -> TypeCode
member ToString : unit -> string + 3 overloads
static val MinValue : float
static val MaxValue : float
static val Epsilon : float
static val NegativeInfinity : float
static val PositiveInfinity : float
...
end
Full name: System.Double
field float.MaxValue = 1.79769313486e+308
val max : xs:double list -> double
Full name: Script.max
Math.Max(val1: decimal, val2: decimal) : decimal
(+0 other overloads)
Math.Max(val1: float, val2: float) : float
(+0 other overloads)
Math.Max(val1: float32, val2: float32) : float32
(+0 other overloads)
Math.Max(val1: uint64, val2: uint64) : uint64
(+0 other overloads)
Math.Max(val1: int64, val2: int64) : int64
(+0 other overloads)
Math.Max(val1: uint32, val2: uint32) : uint32
(+0 other overloads)
Math.Max(val1: int, val2: int) : int
(+0 other overloads)
Math.Max(val1: uint16, val2: uint16) : uint16
(+0 other overloads)
Math.Max(val1: int16, val2: int16) : int16
(+0 other overloads)
Math.Max(val1: byte, val2: byte) : byte
(+0 other overloads)
field float.MinValue = -1.79769313486e+308
type Interval =
{a: double;
b: double;}
static member ( + ) : x:Interval * y:Interval -> Interval
static member ( + ) : x:Interval * y:double -> Interval
static member ( + ) : x:double * y:Interval -> Interval
static member ( / ) : x:Interval * y:Interval -> Interval
static member ( / ) : x:Interval * y:double -> Interval
static member ( / ) : x:double * y:Interval -> Interval
static member ( * ) : x:Interval * y:Interval -> Interval
static member ( * ) : x:Interval * y:double -> Interval
static member ( * ) : x:double * y:Interval -> Interval
static member ( - ) : x:Interval * y:Interval -> Interval
static member ( - ) : x:Interval * y:double -> Interval
static member ( - ) : x:double * y:Interval -> Interval
static member pow : x:Interval * p:double -> Interval
Full name: Script.Interval
Interval.a: double
Interval.b: double
val x : Interval
val y : Interval
Multiple items
val list : double list
--------------------
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
val y : double
val x : double
static member Interval.pow : x:Interval * p:double -> Interval
Full name: Script.Interval.pow
val p : double
Math.Pow(x: float, y: float) : float
val zeroLengthInterval : x:double -> Interval
Full name: Script.zeroLengthInterval
type Fuzzy =
{Top: Interval;
Bottom: Interval;}
static member ( + ) : x:Fuzzy * y:Fuzzy -> Fuzzy
static member ( + ) : x:Fuzzy * y:double -> Fuzzy
static member ( + ) : x:double * y:Fuzzy -> Fuzzy
static member ( / ) : x:Fuzzy * y:Fuzzy -> Fuzzy
static member ( / ) : x:Fuzzy * y:double -> Fuzzy
static member ( / ) : x:double * y:Fuzzy -> Fuzzy
static member ( * ) : x:Fuzzy * y:Fuzzy -> Fuzzy
static member ( * ) : x:Fuzzy * y:double -> Fuzzy
static member ( * ) : x:double * y:Fuzzy -> Fuzzy
static member ( - ) : x:Fuzzy * y:Fuzzy -> Fuzzy
static member ( - ) : x:Fuzzy * y:double -> Fuzzy
static member ( - ) : x:double * y:Fuzzy -> Fuzzy
static member pow : x:Fuzzy * p:double -> Fuzzy
Full name: Script.Fuzzy
Fuzzy.Top: Interval
Fuzzy.Bottom: Interval
val x : Fuzzy
val y : Fuzzy
static member Fuzzy.pow : x:Fuzzy * p:double -> Fuzzy
Full name: Script.Fuzzy.pow
static member Interval.pow : x:Interval * p:double -> Interval
val number : a:double * b:double * c:double -> Fuzzy
Full name: Script.number
val a : double
val b : double
val c : double
val i1 : Fuzzy
Full name: Script.i1
val i2 : Fuzzy
Full name: Script.i2
val M : float
Full name: Script.M
val couponRate : float
Full name: Script.couponRate
val coupon : float
Full name: Script.coupon
val presentValue : Fuzzy
Full name: Script.presentValue
static member Fuzzy.pow : x:Fuzzy * p:double -> Fuzzy
More information