Example #1
0
        public static T Create <T>(string identifier, SimulationGraph graph) where T : Model, new()
        {
            var model = new T();

            model.Identifier = identifier;
            return(model);
        }
        public static void CalculateTasks(SimulationGraph graph, List <IPricer> pricers, List <NArray> derivatives = null)
        {
            var simulationCount = graph.Context.Settings.SimulationCount;
            var timePointCount  = graph.Context.Settings.SimulationTimePoints.Length;

            if (derivatives == null)
            {
                derivatives = new List <NArray>();
            }

            var tasks = new List <Task>();

            foreach (var partition in Partitioner(pricers.Count))
            {
                var resultsStorage = new CalculationResult(simulationCount, timePointCount, derivatives.Count);
                tasks.Add(Task.Run(() =>
                {
                    for (int i = 0; i < timePointCount; ++i)
                    {
                        foreach (var index in partition)
                        {
                            NArray.Evaluate(() =>
                            {
                                NArray pv;
                                pricers[index].Price(i, out pv);
                                return(pv);
                            },
                                            derivatives, Aggregator.ElementwiseAdd, resultsStorage.GetStorageForTimePoint(i));
                        }
                    }
                }));
            }

            Task.WaitAll(tasks.ToArray());
        }
Example #3
0
        public Model CreateModel(Type modelType, string identifier, SimulationGraph graph)
        {
            MethodInfo genericMethod    = typeof(Model).GetMethod("Create").MakeGenericMethod(modelType);
            var        creationDelegate = Delegate.CreateDelegate(typeof(CreateNewModel), genericMethod) as CreateNewModel;
            var        model            = creationDelegate(identifier, graph);

            return(model as Model);
        }
Example #4
0
        public object CreateFactor(Type factorType, string identifier, SimulationGraph graph)
        {
            var        modelType        = _factorModels[factorType].First();
            MethodInfo genericMethod    = typeof(Factor).GetMethod("Create").MakeGenericMethod(factorType, modelType);
            var        creationDelegate = Delegate.CreateDelegate(typeof(CreateNewFactor), genericMethod) as CreateNewFactor;
            var        factor           = creationDelegate(identifier, graph);

            return(factor);
        }
        public NArray AddFactor(string identifier, SimulationGraph graph)
        {
            var context = graph.Context;
            var factor  = context.Factory.CreateNArray(context.Settings.SimulationCount, 1);

            _factors.Add(factor);
            _factorIdentifiers.Add(identifier);
            return(factor);
        }
        public static U Create <U, V>(string identifier, SimulationGraph graph)
            where U : Factor, new()
            where V : Model, new()
        {
            var model  = RiskEngine.Framework.Model.Create <V>(identifier, graph);
            var factor = new U();

            factor._model = model;
            //factor.Identifier = identifier;
            return(factor);
        }
        public static void Calculate(SimulationGraph graph, List <IPricer> pricers, bool multiThread = true, List <NArray> derivatives = null)
        {
            var simulationCount = graph.Context.Settings.SimulationCount;
            var timePointCount  = graph.Context.Settings.SimulationTimePoints.Length;

            if (derivatives == null)
            {
                derivatives = new List <NArray>();
            }

            var threadCount = multiThread ? System.Environment.ProcessorCount : 1;
            var threads     = new Thread[threadCount];

            for (int i = 0; i < threads.Length; ++i)
            {
                var thread = new Thread(new ThreadStart(() =>
                {
                    var resultsStorage = new CalculationResult(simulationCount, timePointCount, derivatives.Count);
                    for (int j = 0; j < 1; ++j)
                    {
                        for (int index = i; index < pricers.Count; index += threadCount)
                        {
                            NArray.Evaluate(() =>
                            {
                                NArray pv;
                                pricers[index].Price(j, out pv);
                                return(pv);
                            },
                                            derivatives, Aggregator.ElementwiseAdd, resultsStorage.GetStorageForTimePoint(j));
                        }
                    }
                }));
                threads[i] = thread;
            }
            //threads[0].Start(); threads[0].Join();
            Array.ForEach(threads, t => { t.Start(); t.Join(); });
        }
Example #8
0
 public virtual void Initialise(SimulationGraph graph)
 {
     _timePoints    = graph.Context.Settings.SimulationTimePoints;
     _timeIntervals = graph.Context.Settings.SimulationIntervals;
 }
 public override void Initialise(SimulationGraph graph)
 {
     base.Initialise(graph);
     _singleFactorProcess.Initialise(graph);
 }
Example #10
0
        public virtual void Register(SimulationGraph graph)
        {
            var timePoints = graph.Context.Settings.SimulationTimePoints;

            _timePoints = timePoints.Select(t => t.DateTime).ToArray();
        }