PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ParaBond/src/scaly/parabond/test/Mr05.scala

http://scaly.googlecode.com/
Scala | 212 lines | 88 code | 54 blank | 70 comment | 9 complexity | f307db1407ea6a191876ba3ff3232067 MD5 | raw file
  1. /*
  2. * Copyright (c) Scaly Contributors
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Scaly Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package scaly.parabond.test
  28. import org.scalatest.junit.JUnitSuite
  29. import org.junit.Assert._
  30. import org.junit.Test
  31. import scaly.parabond.mr.MapReduce
  32. import com.mongodb.casbah.MongoConnection
  33. import com.mongodb.casbah.commons.MongoDBObject
  34. import com.mongodb.casbah.MongoCursor
  35. import scaly.parabond.util.MongoHelper
  36. import scaly.parabond.value.SimpleBondValuator
  37. import scala.util.Random
  38. import scaly.parabond.util.Helper
  39. /**
  40. * This class runs a map-reduce unit test for n portfolios in the
  41. * parabond database. It alternates between parallel and serial methods.
  42. * @author Ron Coleman, Ph.D.
  43. */
  44. class Mr05 {
  45. /** Number of bond portfolios to analyze */
  46. val PORTF_NUM = 100
  47. /** Connects to the parabond DB */
  48. val mongo = MongoConnection("127.0.0.1")("parabond")
  49. /** Record captured with each result */
  50. case class Result(id : Int, price: Double, bondCount: Int, t0: Long, t1: Long)
  51. /** Initialize the random number generator */
  52. val ran = new Random(0)
  53. /** Write a detailed report */
  54. val details = true
  55. /** Unit test entry point */
  56. @Test
  57. def test {
  58. // Set the number of portfolios to analyze
  59. val arg = System.getProperty("n")
  60. val n = if(arg == null) PORTF_NUM else arg.toInt
  61. print("\n"+this.getClass()+" "+ "N: "+n +" ")
  62. val details = if(System.getProperty("details") != null) true else false
  63. (1 to n).foreach { p =>
  64. // Choose a random portfolio
  65. val lottery = ran.nextInt(100000)+1
  66. val results = new Array[Result](2)
  67. // Evaluate the portfolios in random order
  68. if(lottery %2 == 0) {
  69. results(0) = priceParallel(lottery)
  70. results(1) = priceSerially(lottery)
  71. }
  72. else {
  73. results(1) = priceSerially(lottery)
  74. results(0) = priceParallel(lottery)
  75. }
  76. val dt0 = (results(0).t1 - results(0).t0) / 1000000000.0
  77. val dt1 = (results(1).t1 - results(1).t0) / 1000000000.0
  78. println("%6d %10.2f %10.2f %6.4f %6.4f".format(results(0).id, results(0).price, results(1).price, dt0, dt1))
  79. }
  80. }
  81. def priceParallel(portfId : Int) : Result = {
  82. val t0 = System.nanoTime
  83. val list = List((portfId,Helper.curveCoeffs))
  84. val result = MapReduce.mapreduceBasic(list, mapping, reducing)
  85. val t1 = System.nanoTime
  86. val rsult = result(portfId)(0)
  87. Result(portfId,rsult.price,rsult.bondCount,t0,t1)
  88. }
  89. def priceSerially(portfId : Int) : Result = {
  90. // Connect to the portfolio collection
  91. val t0 = System.nanoTime
  92. val portfsCollecton = mongo("Portfolios")
  93. val portfsQuery = MongoDBObject("id" -> portfId)
  94. val portfsCursor: MongoCursor = portfsCollecton.find(portfsQuery)
  95. // Get the bonds in the portfolio
  96. val bondIds = MongoHelper.asList(portfsCursor, "instruments")
  97. // Connect to the bonds collection
  98. val bondsCollection = mongo("Bonds")
  99. val value = bondIds.foldLeft(0.0) { (sum, id) =>
  100. // Get the bond from the bond collection
  101. val bondQuery = MongoDBObject("id" -> id)
  102. val bondCursor: MongoCursor = bondsCollection.find(bondQuery)
  103. val bond = MongoHelper.asBond(bondCursor)
  104. // Price the bond
  105. val valuator = new SimpleBondValuator(bond, Helper.curveCoeffs)
  106. val price = valuator.price
  107. // Add the price into the aggregate sum
  108. sum + price
  109. }
  110. val t1 = System.nanoTime
  111. Result(portfId,value,bondIds.size,t0,t1)
  112. }
  113. /**
  114. * Maps a portfolio to a single price
  115. * @param portId Portfolio id
  116. * @param fitter Curve fitting coefficients
  117. * @returns List of (portf id, bond value))
  118. */
  119. def mapping(portfId: Int, fitter: List[Double]): List[(Int,Result)] = {
  120. // Value each bond in the portfolio
  121. val t0 = System.nanoTime
  122. // Connect to the portfolio collection
  123. val portfsCollecton = mongo("Portfolios")
  124. // Retrieve the portfolio
  125. val portfsQuery = MongoDBObject("id" -> portfId)
  126. val portfsCursor : MongoCursor = portfsCollecton.find(portfsQuery)
  127. // Get the bonds in the portfolio
  128. val bondIds = MongoHelper.asList(portfsCursor,"instruments")
  129. // Connect to the bonds collection
  130. val bondsCollection = mongo("Bonds")
  131. val value = bondIds.foldLeft(0.0) { (sum, id) =>
  132. // Get the bond from the bond collection
  133. val bondQuery = MongoDBObject("id" -> id)
  134. val bondCursor: MongoCursor = bondsCollection.find(bondQuery)
  135. val bond = MongoHelper.asBond(bondCursor)
  136. // print("bond(" + bond + ") = ")
  137. // Price the bond
  138. val valuator = new SimpleBondValuator(bond, fitter)
  139. val price = valuator.price
  140. // println("%8.2f".format(price))
  141. // The price into the aggregate sum
  142. sum + price
  143. }
  144. val t1 = System.nanoTime
  145. List((portfId, Result(portfId,value,bondIds.size,t0,t1)))
  146. }
  147. /**
  148. * Reduces trivially portfolio prices.
  149. * Since there's only one price per porfolio, there's nothing
  150. * really to reduce!
  151. * @param portfId Portfolio id
  152. * @param vals Bond valuations
  153. * @returns List of portfolio valuation, one per portfolio
  154. */
  155. def reducing(portfId: Int,vals: List[Result]): List[Result] = {
  156. List(vals(0))
  157. }
  158. }