PageRenderTime 67ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://scaly.googlecode.com/
Scala | 131 lines | 53 code | 28 blank | 50 comment | 0 complexity | eee4471d313ec74385c4f6155dcab8f0 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 scaly.parabond.util.Helper
  38. /**
  39. * This class runs a map-reduce unit test for n portfolios in the
  40. * parabond database. It uses one portfolio per actor for the first
  41. * portfolios.
  42. * @author Ron Coleman, Ph.D.
  43. */
  44. class Mr00 {
  45. /** Gets a connection to the parabond database */
  46. val mongo = MongoConnection(MongoHelper.getHost)("parabond")
  47. @Test
  48. def test {
  49. // Create the input of a list of Tuple2(portf id, curve coefficients).
  50. val input = (1 to 4).foldLeft(List[(Int,List[Double])]()) { (list, p) =>
  51. list ::: List((p,Helper.curveCoeffs))
  52. }
  53. // Run the map-reduce
  54. val t0 = System.nanoTime
  55. val result = MapReduce.mapreduceBasic(input, mapping, reducing)
  56. val t1 = System.nanoTime
  57. println("%6s %10.10s".format("PortId","Value"))
  58. // Generate the report by portfolio with the run-time
  59. result.foreach {
  60. case(portfId, values) =>
  61. println("%6d %10.2f".format(portfId,values(0)))
  62. }
  63. val dt = (t1 - t0) / 1000000000.0
  64. println("dt = %f".format(dt))
  65. }
  66. /**
  67. * Mapping function
  68. * @param portId Portfolio id
  69. * @param fitter Curve fitting coefficients
  70. * @returns List of (portf id, bond value))
  71. */
  72. def mapping(portfId: Int, fitter: List[Double]): List[(Int,Double)] = {
  73. val portfsCollecton = mongo("Portfolios")
  74. val portfsQuery = MongoDBObject("id" -> portfId)
  75. val portfsCursor : MongoCursor = portfsCollecton.find(portfsQuery)
  76. val bondIds = MongoHelper.asList(portfsCursor,"instruments")
  77. val bondsCollection = mongo("Bonds")
  78. val t0 = System.nanoTime
  79. val value = bondIds.foldLeft(0.0) { (sum, id) =>
  80. val bondsQuery = MongoDBObject("id" -> id)
  81. val bondsCursor: MongoCursor = bondsCollection.find(bondsQuery)
  82. val bond = MongoHelper.asBond(bondsCursor)
  83. // print("bond(" + bond + ") = ")
  84. val valuator = new SimpleBondValuator(bond, fitter)
  85. val price = valuator.price
  86. // println("%8.2f".format(price))
  87. sum + price
  88. }
  89. val t1 = System.nanoTime
  90. val dt = (t1 - t0) / 1000000000.0
  91. println("value = %10.2f bonds = %d dt = %f".format(value,bondIds.size,dt))
  92. List((portfId, value))
  93. }
  94. /**
  95. * Trivial reduce function since the portfolio is already reduced.
  96. * @param portfId Portfolio id
  97. * @param vals Bond valuations
  98. * @returns List of portfolio valuation, one per portfolio
  99. */
  100. def reducing(portfId: Int,vals: List[Double]): List[Double] = {
  101. List(vals(0))
  102. }
  103. }