private void a_valid_stochArray(double[][] data) { var stochArray = new StochArray(data); bool result = _stochArrayCache.TryAddItem(stochArray, out _stochArrayHash, StochArrayIdentifier); result.ShouldBeTrue(); }
private static string ReadStochArray(string fullPath, Cache <StochArray> cache, int maxHorizon, int maxSim) { List <double>[] unstructuredData; using (var reader = new StreamReader(fullPath)) { // Ignore first line var line = reader.ReadLine(); var values = line.Split(','); // First col is sim numbers int colCount = values.Count(val => !string.IsNullOrWhiteSpace(val)) - 1; int colsToRead = colCount > maxHorizon ? maxHorizon : colCount; // exclude the sim numbers col unstructuredData = new List <double> [colsToRead]; for (int i = 0; i < colsToRead; i++) { unstructuredData[i] = new List <double>(); } int currentSim = 1; while (!reader.EndOfStream) { if (currentSim > maxSim) { break; } line = reader.ReadLine(); values = line.Split(','); // in case you have blank rows if (values.All(val => string.IsNullOrWhiteSpace(val))) { continue; } for (int i = 0; i < colsToRead; i++) { var value = values[i + 1].ConvertParameterToDouble("Stoch Array value"); unstructuredData[i].Add(value); } currentSim++; } } var structuredData = unstructuredData.InvertEnumerableOrder(); var stochArray = new StochArray(structuredData); _ = cache.TryAddItem(stochArray, out var hash, fullPath, maxHorizon, maxSim); return(hash); }
private void a_valid_stochArray_cache_item() { var values = new double[][] { new double[] { 0.02, 0.03 }, new double[] { 0.01, 0.005 }, new double[] { 0.0, -0.034 }, }; _stochArray = new StochArray(values); var cacheOperation = _stochArrayCache.TryAddItem(_stochArray, out _, ValidStochArrayInput); cacheOperation.ShouldBeTrue(); }
private void combine_is_calculated_correctly(double [] proportions, Rebalancing rebalancingPolicy, double[][] expectation) { var combined = StochArray.Combine(_stochArrays, proportions, rebalancingPolicy); int maxSim = combined.MaxSim; int maxHor = combined.MaxHorizon; for (int i = 0; i < maxSim; i++) { for (int j = 0; j < maxHor; j++) { combined.Values[i][j].ShouldBe(expectation[i][j], 1E-06, $"Error at sim {i} and horizon {j}"); } } }
public static string Combine(object[,] hashes, object[,] proportions, object rebalancing) { // Check first if the result is already in the cache var stochArrayCache = Cache <StochArray> .GetCache; if (stochArrayCache.TryGetItem(out _, out string key, nameof(Combine), hashes, proportions, rebalancing)) { return(key); } //tranform inputs var hashes1d = hashes.Convert2DParameterTo1DArray("Stoch Array Hashes") .Select(val => val.ToString()) .ToArray(); var proportions1d = proportions.Convert2DParameterTo1DArray("Stoch Array Proportions") .Select(val => val.ConvertParameterToDouble("Proportion")) .ToArray(); Rebalancing parsedRebalEnum = rebalancing.ToString().ParseAsEnum <Rebalancing>(); parsedRebalEnum = parsedRebalEnum == Rebalancing.NotSpecified ? Rebalancing.Rebalance : parsedRebalEnum; int stochArrayNo = hashes1d.Length; int proportionsNo = proportions1d.Length; // validate if (!proportions1d.Sum().EqualsWithPrecision(1d)) { throw new ArgumentException("Sum of porportions does not equal to 1"); } if (stochArrayNo != proportionsNo) { throw new ArgumentException($"Number of proportions ({proportionsNo}) does not equal number of stoch array hashes ({stochArrayNo})"); } var stochArrays = new StochArray[stochArrayNo]; for (int i = 0; i < stochArrayNo; i++) { if (!stochArrayCache.TryGetItem(hashes1d[i], out stochArrays[i])) { throw new MissingStochArrayException(hashes1d[i]); } } int maxSim = stochArrays.First().MaxSim; int maxHor = stochArrays.First().MaxHorizon; foreach (var stochArray in stochArrays) { if (stochArray.MaxHorizon != maxHor) { throw new StochArrayDimensionsMismatchException("Horizon", maxHor, stochArray.MaxHorizon); } if (stochArray.MaxSim != maxSim) { throw new StochArrayDimensionsMismatchException("Simulations", maxSim, stochArray.MaxSim); } } // calculate var combinedArray = StochArray.Combine(stochArrays, proportions1d, parsedRebalEnum); stochArrayCache.TryAddItem(combinedArray, out string newKey, nameof(Combine), hashes, proportions, rebalancing); return(newKey); }
private void a_valid_stochArray(double[][] rates) { _stochArray = new StochArray(rates); }
public void TestCompletion() { _fxArray = null; _stochArray = null; }
public void TestInitialiser() { _fxArray = null; _stochArray = null; }
//+ TestMethods private void a_valid_stochArray(double[][] values) { _stochArray = new StochArray(values); }