Beispiel #1
0
        public void CanMerge()
        {
            var x = new ResultCube();

            x.Initialize(new Dictionary <string, Type> {
                { "gwah", typeof(string) }, { "wwahh", typeof(int) }
            });
            var y = new ResultCube();

            y.Initialize(new Dictionary <string, Type> {
                { "gwah", typeof(string) }, { "wwahh", typeof(int) }
            });

            x.AddRow(new object[] { "woooh", 6 }, 77.6);
            y.AddRow(new object[] { "woooh", 6 }, 77.4);

            var d = x.MergeQuick(y);

            Assert.Equal(2, d.GetAllRows().Length);
            d = x.Merge(y);
            Assert.Equal(2, d.GetAllRows().Length);

            var z = new ResultCube();

            z.Initialize(new Dictionary <string, Type> {
                { "gwah", typeof(bool) }, { "wwahh", typeof(int) }
            });
            Assert.Throws <Exception>(() => x.MergeQuick(z));
            Assert.Throws <Exception>(() => x.Merge(z));
        }
Beispiel #2
0
        public ICube Generate(IPvModel model, Portfolio portfolio = null)
        {
            var o = new ResultCube();

            o.Initialize(new Dictionary <string, Type>
            {
                { "AxisA", typeof(string) },
                { "AxisB", typeof(string) }
            });

            var scenarios = GenerateScenarios(model);

            ICube baseRiskCube = null;

            if (ReturnDifferential)
            {
                var baseModel = model;
                if (portfolio != null)
                {
                    baseModel = baseModel.Rebuild(baseModel.VanillaModel, portfolio);
                }
                baseRiskCube = GetRisk(baseModel);
            }

            var threadLock = new object();
            var results    = new ICube[scenarios.Count];
            var scList     = scenarios.ToList();

            ParallelUtils.Instance.For(0, scList.Count, 1, i =>
            {
                var scenario = scList[i];
                var pvModel  = scenario.Value;
                if (portfolio != null)
                {
                    pvModel = pvModel.Rebuild(pvModel.VanillaModel, portfolio);
                }
                var result = GetRisk(pvModel);

                if (ReturnDifferential)
                {
                    result = result.Difference(baseRiskCube);
                }

                results[i] = result;
            }).Wait();

            for (var i = 0; i < results.Length; i++)
            {
                o = (ResultCube)o.Merge(results[i],
                                        new Dictionary <string, object>
                {
                    { "AxisA", scList[i].Key.Item1 },
                    { "AxisB", scList[i].Key.Item2 }
                }, null, true);
            }

            return(o);
        }