Advertisement
mjaniec

Somers'D and ASE1 - dirty implementation in F#

Dec 15th, 2013
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.94 KB | None | 0 0
  1. // uses F# PowerPack
  2.  
  3. #r "FSharp.PowerPack.dll"
  4.  
  5. let PD             = [ 0.05; 0.10; 0.50; 1.00; 2.00; 5.00; 25.00 ]
  6. let counterparties = [ 5.; 10.; 20.; 25.; 20.; 15.; 5. ]
  7.  
  8. let groups = PD.Length // risk groups no.
  9.  
  10. let div100 x = x / 100.0
  11.  
  12. let PDprct = [ for x in PD do yield div100 x ]
  13. let CPprct = [ for x in counterparties do yield div100 x ]
  14.  
  15. let n = CPprct |> Seq.sum
  16.  
  17. let defaulted    = [ for i in 1..groups do yield CPprct.[i-1]*PDprct.[i-1]/n ]
  18. let nondefaulted = [ for i in 1..groups do yield CPprct.[i-1]*(1.0-PDprct.[i-1])/n ]
  19.  
  20. let x = matrix [ defaulted; nondefaulted ]
  21.  
  22. let xr = x.NumRows
  23.  
  24. let rowSum (x : matrix) (i : int) = Array.sum (RowVector.toArray (x.Row(i-1)))
  25.  
  26. // WR / DR
  27.  
  28. let wr =
  29.  
  30.     let mutable mat_sum = 0.0
  31.  
  32.     for i in 1..xr do
  33.         let row2  = rowSum x i ** 2.0
  34.         mat_sum   <- mat_sum + row2
  35.  
  36.     n ** 2.0 - mat_sum
  37.  
  38. // A / D
  39.  
  40. let A (x : matrix) i j =
  41.  
  42.     let xr = x.NumRows
  43.     let xc = x.NumCols
  44.  
  45.     let rowIdx1 = List.filter (fun x -> x>i) [ 1..xr ]
  46.     let colIdx1 = List.filter (fun x -> x>j) [ 1..xc ]
  47.  
  48.     let rowIdx2 = List.filter (fun x -> x<i) [ 1..xr ]
  49.     let colIdx2 = List.filter (fun x -> x<j) [ 1..xc ]
  50.  
  51.     let mutable Asum = 0.0
  52.  
  53.     for r_i in rowIdx1 do
  54.         for r_j in colIdx1 do
  55.             Asum <- Asum + x.[r_i-1,r_j-1]
  56.  
  57.     for r_i in rowIdx2 do
  58.         for r_j in colIdx2 do
  59.             Asum <- Asum + x.[r_i-1,r_j-1]
  60.  
  61.     Asum
  62.  
  63. let D (x : matrix) i j =
  64.  
  65.     let xr = x.NumRows
  66.     let xc = x.NumCols
  67.  
  68.     let rowIdx1 = List.filter (fun x -> x>i) [ 1..xr ]
  69.     let colIdx1 = List.filter (fun x -> x<j) [ 1..xc ]
  70.  
  71.     let rowIdx2 = List.filter (fun x -> x<i) [ 1..xr ]
  72.     let colIdx2 = List.filter (fun x -> x>j) [ 1..xc ]
  73.  
  74.     let mutable Dsum = 0.0
  75.  
  76.     for r_i in rowIdx1 do
  77.         for r_j in colIdx1 do
  78.             Dsum <- Dsum + x.[r_i-1,r_j-1]
  79.  
  80.     for r_i in rowIdx2 do
  81.         for r_j in colIdx2 do
  82.             Dsum <- Dsum + x.[r_i-1,r_j-1]
  83.  
  84.     Dsum
  85.            
  86. let Pf (x : matrix) =
  87.  
  88.     let xr = x.NumRows
  89.     let xc = x.NumCols
  90.  
  91.     let mutable Pfsum = 0.0
  92.  
  93.     for i in 1..xr do
  94.         for j in 1..xc do
  95.             Pfsum <- Pfsum + x.[i-1,j-1] * A x i j
  96.  
  97.     Pfsum
  98.  
  99. let Qf (x : matrix) =
  100.  
  101.     let xr = x.NumRows
  102.     let xc = x.NumCols
  103.  
  104.     let mutable Qfsum = 0.0
  105.  
  106.     for i in 1..xr do
  107.         for j in 1..xc do
  108.             Qfsum <- Qfsum + x.[i-1,j-1] * D x i j
  109.  
  110.     Qfsum
  111.  
  112. let ASE1 (x : matrix) =
  113.  
  114.    let xr = x.NumRows
  115.    let xc = x.NumCols
  116.    
  117.    let mutable sumX = 0.0
  118.  
  119.    for i in 1..xr do
  120.         for j in 1..xc do
  121.             sumX <- sumX + x.[i-1,j-1]
  122.  
  123.    let mutable tmp = 0.0
  124.  
  125.    for i in 1..xr do
  126.         for j in 1..xc do            
  127.             let sub_sum = sumX - rowSum x i
  128.             tmp <- tmp + x.[i-1,j-1] * ( wr * (A x i j - D x i j) - (Pf x - Qf x) * sub_sum ) ** 2.0
  129.  
  130.    2.0 / wr ** 2.0 * sqrt tmp
  131.  
  132. //
  133.  
  134. let SomersD = (Pf x - Qf x)/wr
  135.  
  136. let SomersD_ASE1 = ASE1 x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement