/* * private double inner_product(IEnumerable a, IEnumerable b, double val) * { * IEnumerator ita = a.GetEnumerator(); * IEnumerator itb = b.GetEnumerator(); * while( ita.MoveNext() && itb.MoveNext() ) * val += (double)ita.Current * (double)itb.Current; * return val; * } */ #region NumericalMethod interface /// <summary> /// Initialize a DiscretizedAsset object. /// </summary> /// <param name="asset"></param> /// <param name="time"></param> public override void Initialize(IDiscretizedAsset asset, double time) { int i = TimeGrid.FindIndex(time); asset.Time = time; asset.Reset(Count(i)); }
/// <summary> /// Roll-back a DiscretizedAsset object until a certain time. /// </summary> /// <param name="asset"></param> /// <param name="toTime"></param> public override void Rollback(IDiscretizedAsset asset, double toTime) { double from = asset.Time; // Can't Rollback to the future: if (from < toTime) { throw new ArgumentException("LatBackFut"); } int iFrom = TimeGrid.FindIndex(from); int iTo = TimeGrid.FindIndex(toTime); for (int i = iFrom - 1; i >= iTo; i--) { var newValues = new double[Count(i)]; Stepback(i, asset.Values, newValues); asset.Time = TimeGrid[i]; asset.Values = newValues; asset.AdjustValues(); } }
/// <summary> /// Computes the present value of an asset using Arrow-Debrew prices. /// </summary> /// <param name="asset"></param> /// <returns></returns> public double PresentValue(IDiscretizedAsset asset) { int i = TimeGrid.FindIndex(asset.Time); if (i > _statePricesLimit) { ComputeStatePrices(i); } // return DotProduct(asset.Values, statePrices[i]); var prices = (double[])_statePrices[i]; Debug.Assert( asset.Values.Length == prices.Length, "arrays with different sizes cannot be multiplied."); double val = 0.0; for (int j = 0; j < prices.Length; j++) { val += asset.Values[j] * prices[j]; } return(val); // return inner_product(asset.Values, statePriceValues(i), 0.0); }
/// <summary> /// Roll-back a DiscretizedAsset object until a certain time. /// </summary> /// <param name="asset"></param> /// <param name="toTime"></param> public abstract void Rollback(IDiscretizedAsset asset, double toTime);
/// <summary> /// Initialize a DiscretizedAsset object. /// </summary> /// <param name="asset"></param> /// <param name="time"></param> public abstract void Initialize(IDiscretizedAsset asset, double time);