fork download
  1. using System;
  2. using System.Linq;
  3.  
  4. struct ShortRateTreeNode
  5. {
  6. public double DF;
  7. public double PU;
  8. public double PM;
  9. public double PD;
  10. public int IU;
  11. public int IM;
  12. public int ID;
  13.  
  14. public ShortRateTreeNode(double df, double pu, double pm, double pd, int iu, int im, int id)
  15. {
  16. this.DF = df;
  17. this.PU = pu;
  18. this.PM = pm;
  19. this.PD = pd;
  20. this.IU = iu;
  21. this.IM = im;
  22. this.ID = id;
  23. }
  24. }
  25.  
  26. class Program
  27. {
  28. static double[] InduceForward(double[] values, ShortRateTreeNode[] nodes)
  29. {
  30. Func<int, double> next = k => values[values.Length / 2 + k];
  31. return nodes
  32. .Select(node => (node.PU * next(node.IU) + node.PM * next(node.IM) + node.PD * next(node.ID)) * node.DF)
  33. .ToArray();
  34. }
  35.  
  36. static void Main()
  37. {
  38. var ITERATION = 10000;
  39. Func<double, double[]> maturityValues = x => Enumerable.Repeat(x, 201).ToArray();
  40. var reversedTree = Enumerable.Range(0, 100)
  41. .Reverse()
  42. .Select(i => Enumerable.Range(-i, i * 2 + 1)
  43. .Select(j => new ShortRateTreeNode(1.0, 1.0 / 6.0, 2.0 / 3.0, 1.0 / 6.0, j + 1, j, j - 1))
  44. .ToArray())
  45. .ToArray();
  46. var result = Enumerable.Range(0, ITERATION)
  47. .Select(i => reversedTree.Aggregate(maturityValues(i), InduceForward)[0])
  48. .Max();
  49. Console.WriteLine(result);
  50. }
  51. }
  52.  
Runtime error #stdin #stdout 15.99s 41360KB
stdin
Standard input is empty
stdout
Standard output is empty