PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/quant/test/math/matrix/test_tqr_eigen_decomposition.clj

http://github.com/ovidiu-beldie/quant-clj
Clojure | 118 lines | 83 code | 24 blank | 11 comment | 12 complexity | b46d0408c3fae8d7512a5edb5230a34f MD5 | raw file
  1. ; Copyright (c) 2012 Ovidiu Beldie. All rights reserved.
  2. ; The use and distribution terms for this software are covered by the
  3. ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
  4. ; which can be found in the file epl-v10.html at the root of this distribution.
  5. ; By using this software in any fashion, you are agreeing to be bound by
  6. ; the terms of this license.
  7. ; You must not remove this notice, or any other, from this software.
  8. ; The initial QuantLib library, Clojure and any other open source software
  9. ; referenced in this library are the propriety of their respective owners
  10. (ns quant.test.math.matrix.test-tqr-eigen-decomposition
  11. (:use
  12. [quant.test.common :only (with-private-fns)]
  13. [quant.math.matrix.tqr-eigen-decomposition]
  14. [clojure.algo.generic.math-functions :only (approx=)]
  15. [clojure.test :only (deftest, is, testing)]))
  16. (def tol 1.0E-10)
  17. (def d1 [0 1 2 3 4 5 6 7 8])
  18. (def e1 [0 0 0 0 0 1 1 1 1])
  19. (def e2 [0 10 20 30 40 50 60 70 80])
  20. (def d [7 2 0 6 1 9 3 8 5])
  21. (def ev [[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16]])
  22. (with-private-fns [quant.math.matrix.tqr-eigen-decomposition [off-diag-zero?]]
  23. (deftest test-off-diag-zero?
  24. (is (true? (off-diag-zero? 1 d1 e1)))
  25. (is (false? (off-diag-zero? 6 d1 e1)))))
  26. (with-private-fns [quant.math.matrix.tqr-eigen-decomposition [comp-q]]
  27. (deftest test-comp-q
  28. (is (= 3 (comp-q d1 e2 6 3 7 :no-shift)))
  29. (is (approx= -62.50208 (comp-q d1 e2 6 3 7 :close-eigen-value) 0.0001))
  30. (is (approx= -78.8776 (comp-q d1 e2 6 3 7 :over-relaxation) 0.0001))))
  31. (def ev-result [[1 3.5 2.0 4] [5 9.5 4.0 8] [9 15.5 6.0 12] [13 21.5 8.0 16]])
  32. (with-private-fns [quant.math.matrix.tqr-eigen-decomposition [update-ev]]
  33. (deftest test-update-ev
  34. (is (= ev-result (update-ev ev 2 0.5 1)))))
  35. (with-private-fns [quant.math.matrix.tqr-eigen-decomposition [qr-transf-iter]]
  36. (def qti1 (qr-transf-iter 2 {:e e2, :ev ev, :sine 0.05, :cosine 0.025,
  37. :d d, :u 0, :q 3, :l 1})))
  38. (def e2-qr-1 [0 3.1622776601683795 20 30 40 50 60 70 80])
  39. (def d-qr-1 [7 2.1 0 6 1 9 3 8 5])
  40. (deftest test-qti1
  41. (is (= false (:recov-underflow qti1)))
  42. (is (approx= 0.3162277 (:sine qti1) 0.0001))
  43. (is (approx= 0.9486832 (:cosine qti1) 0.0001))
  44. (is (= d-qr-1 (:d qti1)))
  45. (is (approx= 0.1 (:u qti1) 0.0001))
  46. (is (= e2-qr-1 (:e qti1)))
  47. (is (approx= -0.2 (:q qti1) 0.0001)))
  48. (with-private-fns [quant.math.matrix.tqr-eigen-decomposition [qr-transf-iter]]
  49. (def qti2 (qr-transf-iter 2 {:e e1, :ev ev, :sine 0.05, :cosine 0.025,
  50. :d d, :u 9, :q 0, :l 1})))
  51. (def d-qr-2 [7 -9 0 6 1 9 3 8 5])
  52. (deftest test-qti2
  53. (is (= true (:recov-underflow qti2)))
  54. (is (= d-qr-2 (:d qti2)))
  55. (is (= e1 (:e qti2))))
  56. (def d-sort [2 4 1 3])
  57. (def d-sort-expect '(1 2 3 4))
  58. (def ev-sort [[-1 2 3 -4] [5 -6 7 8] [9 10 -11 -12] [-13 -14 15 16]])
  59. (def ev-sort-expect [[9 10 -11 -12] [1 -2 -3 4] [13 14 -15 -16] [5 -6 7 8]])
  60. (with-private-fns [quant.math.matrix.tqr-eigen-decomposition [sort-eigens]]
  61. (def call-sort-eigens (sort-eigens {:ev ev-sort, :d d-sort})))
  62. (deftest test-sort-eigens
  63. (is (= d-sort-expect (:d call-sort-eigens)))
  64. (is (= ev-sort-expect (:ev call-sort-eigens))))
  65. (def d-val [11 7 6 2 0])
  66. (def sub-val [1 1 1 1])
  67. ; I've adjusted these values as my computations
  68. ; have better precision
  69. (def ev-val-expect [11.246783221713914
  70. 7.485496736290855
  71. 5.5251516080277518
  72. 2.1811760273123308
  73. -0.4386075933448487])
  74. (def tqr-val (tqr-eigen-decomp d-val sub-val :without-eigen-vector))
  75. (def ev-val-decomp (eigen-values tqr-val))
  76. (deftest test-eigen-value
  77. (doseq [i (range (count ev-val-expect))]
  78. (is (approx= (ev-val-expect i) (ev-val-decomp i) tol))))
  79. (def diag-vect [1 1])
  80. (def sub-vect [1])
  81. (def tqr-vect (tqr-eigen-decomp diag-vect sub-vect))
  82. (def eigen-vect (eigen-vectors tqr-vect))
  83. (deftest test-eigen-vector
  84. (is (< (+ 0.25 (reduce * (map #(apply * %) eigen-vect))) tol)))
  85. (def diag-zero [12 9 6 3 0])
  86. (def sub-zero-1 [0 1 0 1])
  87. (def sub-zero-2 [1e-14 1 1e-14 1])
  88. (def tqr-zero-1 (tqr-eigen-decomp diag-zero sub-zero-1))
  89. (def tqr-zero-2 (tqr-eigen-decomp diag-zero sub-zero-2))
  90. (def eigen-zero-val-comp (eigen-values tqr-zero-1))
  91. (def eigen-zero-val-expect (eigen-values tqr-zero-2))
  92. (deftest test-zero-off-diag
  93. (doseq [i (range (count eigen-zero-val-expect))]
  94. (is (approx= (eigen-zero-val-expect i) (eigen-zero-val-comp i) tol))))