Exemple #1
0
        public void Process(IPathBlock block)
        {
            var index = 0;

            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                for (var factor = 0; factor < block.Factors; factor++)
                {
                    for (var step = 0; step < block.NumberOfSteps; step++)
                    {
                        var dim = step * block.Factors + factor;

                        for (var i = 0; i < Vector <double> .Count; i++)
                        {
                            var pathid = path + i + block.GlobalPathIndex;
                            var point  = CreatePoint(pathid, dim) / Pow(2.0, 32.0);
                            //point = ShiftNumber(point,dim);
                            if (_useNormalInv)
                            {
                                point = Math.Statistics.NormInv(point);
                            }
                            block.RawData[index] = point;
                            index++;
                        }
                    }
                }
            }
            Debug.Assert(block.RawData.Length == index);
        }
Exemple #2
0
        public void Process(IPathBlock block)
        {
            if (CompactMode)
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps = block.GetStepsForFactor(path, _factorIndex);
                    SetupAverages(steps);

                    if (CompactMode)
                    {
                        if (!PathSumsByBlock.ContainsKey(block.GlobalPathIndex))
                        {
                            PathSumsByBlock.Add(block.GlobalPathIndex, new double[steps.Length]);
                            PathCountsByBlock.Add(block.GlobalPathIndex, steps.Length);
                        }

                        for (var i = 0; i < steps.Length; i++)
                        {
                            for (var j = 0; j < Vector <double> .Count; j++)
                            {
                                PathSumsByBlock[block.GlobalPathIndex][i] += steps[i][j];
                            }
                        }
                    }
                    else
                    {
                        for (var i = 0; i < steps.Length; i++)
                        {
                            for (var j = 0; j < Vector <double> .Count; j++)
                            {
                                lock (_threadLock)
                                {
                                    PathSum[i] += steps[i][j];
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps = block.GetStepsForFactor(path, _factorIndex);
                    SetupAverages(steps);

                    for (var i = 0; i < steps.Length; i++)
                    {
                        for (var j = 0; j < Vector <double> .Count; j++)
                        {
                            lock (_threadLock)
                            {
                                PathSum[i] += steps[i][j];
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        public void Process(IPathBlock block)
        {
            ParallelUtils.Instance.For(0, block.NumberOfPaths, Vector <double> .Count, path =>
                                       //for (var path = 0; path < block.NumberOfPaths; path += Vector<double>.Count)
            {
                var stepsMain = block.GetStepsForFactor(path, _mainFactorIndex);
                for (var f = 0; f < _factorIndices.Length; f++)
                //ParallelUtils.Instance.For(0,_factorIndices.Length,1,f=>
                {
                    var previousStep = new Vector <double>(_forwardCurve(_futuresExpiries[f]));
                    var steps        = block.GetStepsForFactor(path, _factorIndices[f]);
                    var c            = 0;
                    foreach (var kv in _pastFixings.Where(x => x.Key < _startDate))
                    {
                        steps[c] = new Vector <double>(kv.Value);
                        c++;
                    }
                    steps[c] = previousStep;
                    for (var step = c + 1; step < block.NumberOfSteps; step++)
                    {
                        var W         = steps[step];
                        var dt        = new Vector <double>(_timesteps.TimeSteps[step]);
                        var bm        = (_vols[step][f] * _vols[step][f] / 2.0) * dt + (_vols[step][f] * _timesteps.TimeStepsSqrt[step] * W);
                        previousStep *= bm.Exp();
                        steps[step]   = previousStep;
                    }
                }//).Wait();

                for (var step = 0; step < block.NumberOfSteps; step++)
                {
                    var frontMonth  = block.GetStepsForFactor(path, _frontMonthFactors[step]);
                    stepsMain[step] = frontMonth[step];
                }
            }).Wait();
        }
        public void Process(IPathBlock block)
        {
            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                for (var d = 0; d < _nDims; d++)
                {
                    var steps = block.GetStepsForFactor(path, _assetIxs[d]);

                    for (var fd = 0; fd < _dateIxsFwd.Length; fd++)
                    {
                        for (var v = 0; v < Vector <double> .Count; v++)
                        {
                            if (d == 0)
                            {
                                _pathwiseValuesFwd[block.GlobalPathIndex + path + v][fd] = steps[_dateIxsFwd[fd]][v];
                                _pathwiseValuesReg[block.GlobalPathIndex + path + v]     = steps[_dateIxRegression][v];
                            }
                            else
                            {
                                _pathwiseValuesFwd[block.GlobalPathIndex + path + v][fd] *= steps[_dateIxsFwd[fd]][v];
                                _pathwiseValuesReg[block.GlobalPathIndex + path + v]     *= steps[_dateIxRegression][v];
                            }
                        }
                    }
                }
            }
        }
Exemple #5
0
 public unsafe void Process(IPathBlock block)
 {
     if (!UseNormalInverse)
     {
         for (var i = 0; i < block.TotalBlockSize; i += (UseAnthithetic ? 2 : 1))
         {
             block[i] = GenerateDouble();
             if (UseAnthithetic)
             {
                 block[i + 1] = 1.0 - block[i];
             }
         }
     }
     else
     {
         for (var i = 0; i < block.TotalBlockSize; i += (UseAnthithetic ? 2 : 1))
         {
             block[i] = Math.Statistics.NormInv(GenerateDouble());
             if (UseAnthithetic)
             {
                 block[i + 1] = -block[i];
             }
         }
     }
 }
Exemple #6
0
 public void Process(IPathBlock block)
 {
     for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
     {
         var previousStep = new Vector <double>(_forwardCurve(0));
         var steps        = block.GetStepsForFactor(path, _factorIndex);
         var c            = 0;
         foreach (var kv in _pastFixings.Where(x => x.Key < _startDate))
         {
             steps[c] = new Vector <double>(kv.Value);
             c++;
         }
         steps[c] = previousStep;
         for (var step = c + 1; step < block.NumberOfSteps; step++)
         {
             var W    = steps[step];
             var dt   = new Vector <double>(_timesteps.TimeSteps[step]);
             var vols = new double[Vector <double> .Count];
             for (var s = 0; s < vols.Length; s++)
             {
                 vols[s] = Sqrt(Max(0, _lvInterps[step - 1].Interpolate(previousStep[s])));
             }
             var volVec = new Vector <double>(vols);
             var bm     = (_drifts[step] - volVec * volVec / _two) * dt + (volVec * _timesteps.TimeStepsSqrt[step] * W);
             previousStep *= bm.Exp();
             steps[step]   = previousStep;
         }
     }
 }
Exemple #7
0
        public void Process(IPathBlock block)
        {
            var temp = new double[Vector <double> .Count];

            for (var s = 0; s < block.NumberOfSteps; s++)
            {
                for (var f = 0; f < block.Factors; f++)
                {
                    var dim = f * block.NumberOfSteps + s;
                    for (var p = 0; p < block.NumberOfPaths; p += Vector <double> .Count)
                    {
                        var path = p + block.GlobalPathIndex + 1;
                        var x    = GetValueViaGreyCode(path, dim);
                        temp[0] = ConvertRandToDouble(x, dim);
                        for (var i = 1; i < Vector <double> .Count; i++)
                        {
                            path    = i + p + block.GlobalPathIndex + 1;
                            x      ^= _v[dim + _seed][BitShifting.FindFirstSet(~(uint)path) - 1];
                            temp[i] = ConvertRandToDouble(x, dim);
                        }
                        var pathSpan = block.GetStepsForFactor(p, f);
                        pathSpan[s] = new Vector <double>(temp);
                    }
                }
            }
        }
Exemple #8
0
        public void Process(IPathBlock block)
        {
            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                var p1    = block.GetStepsForFactor(path, _assetIndex1);
                var p2    = block.GetStepsForFactor(path, _assetIndex2);
                var p1Arr = new double[Vector <double> .Count][];
                var p2Arr = new double[Vector <double> .Count][];
                for (var i = 0; i < Vector <double> .Count; i++)
                {
                    p1Arr[i] = new double[p1.Length];
                    p2Arr[i] = new double[p2.Length];
                }
                for (var step = 0; step < p1.Length; step++)
                {
                    for (var i = 0; i < Vector <double> .Count; i++)
                    {
                        p1Arr[i][step] = p1[step][i];
                        p2Arr[i][step] = p2[step][i];
                    }
                }

                var correls = new double[Vector <double> .Count];
                for (var i = 0; i < correls.Length; i++)
                {
                    var returns1 = p1Arr[i].Returns(true);
                    var returns2 = p2Arr[i].Returns(true);
                    correls[i] = returns1.Correlation(returns2).Correlation;
                }

                _results.Add(new Vector <double>(correls));
            }
        }
Exemple #9
0
        public void Process(IPathBlock block)
        {
            var nFactors = block.Factors;

            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                var randsForSteps = new Vector <double> [nFactors][];
                for (var r = 0; r < randsForSteps.Length; r++)
                {
                    randsForSteps[r] = block.GetStepsForFactor(path, r).ToArray();
                }

                for (var step = 0; step < block.NumberOfSteps; step++)
                {
                    var randsForThisStep = new Vector <double> [nFactors];

                    for (var r = 0; r < randsForThisStep.Length; r++)
                    {
                        randsForThisStep[r] = randsForSteps[r][step];
                    }

                    var correlatedRands = Correlate(randsForThisStep);

                    for (var r = 0; r < randsForSteps.Length; r++)
                    {
                        var x = block.GetStepsForFactor(path, r);
                        x[step] = correlatedRands[r];
                    }
                }
            }
        }
        public void Process(IPathBlock block)
        {
            SetupFactors();
            if (PathCalc.CompactMode)
            {
                var averagesForThisBlock = PathCalc.PathSumsByBlock[block.GlobalPathIndex].Select(a => a / PathCalc.PathCountsByBlock[block.GlobalPathIndex]).ToArray();
                var factorsForThisBlock  = _fwds.Select((f, ix) => new Vector <double>(f / averagesForThisBlock[ix])).ToArray();
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps = block.GetStepsForFactor(path, _factorIndex);

                    for (var step = 0; step < block.NumberOfSteps; step++)
                    {
                        steps[step] *= factorsForThisBlock[step];
                    }
                }
            }
            else
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps = block.GetStepsForFactor(path, _factorIndex);

                    for (var step = 0; step < block.NumberOfSteps; step++)
                    {
                        steps[step] *= _correctionFactors[step];
                    }
                }
            }
        }
        public void Process(IPathBlock block)
        {
            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                for (var d = 0; d < _nDims; d++)
                {
                    var steps = block.GetStepsForFactor(path, d);

                    var targetIx   = 0;
                    var nextTarget = _dateIndexes[targetIx];

                    for (var s = 0; s < steps.Length; s++)
                    {
                        if (s == nextTarget)
                        {
                            for (var v = 0; v < Vector <double> .Count; v++)
                            {
                                _pathwiseValues[targetIx][block.GlobalPathIndex + path + v][d] = steps[s][v];
                            }
                            targetIx++;
                            if (targetIx == _dateIndexes.Length)
                            {
                                break;
                            }
                            nextTarget = _dateIndexes[targetIx];
                        }
                    }
                }
            }
        }
Exemple #12
0
 public void Process(IPathBlock block)
 {
     for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
     {
         var steps       = block.GetStepsForFactor(path, _assetIndex);
         var finalValues = steps[_expiryIndex] - (new Vector <double>(_strike));
         finalValues = Vector.Max(new Vector <double>(0), finalValues);
         _results.Add(finalValues);
     }
 }
Exemple #13
0
        public override void Process(IPathBlock block)
        {
            var blockBaseIx = block.GlobalPathIndex;

            if (_callPut == OptionType.C)
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps = block.GetStepsForFactor(path, _assetIndex);
                    Span <Vector <double> > stepsFx = null;
                    if (_fxName != null)
                    {
                        stepsFx = block.GetStepsForFactor(path, _fxIndex);
                    }

                    var minValue = new Vector <double>(double.MaxValue);
                    for (var i = 0; i < _dateIndexes.Length; i++)
                    {
                        minValue = Vector.Min(steps[_dateIndexes[i]] * (_fxName != null ? stepsFx[_dateIndexes[i]] : _one), minValue);
                    }
                    var lastValue = steps[_dateIndexes.Last()] * (_fxName != null ? stepsFx[_dateIndexes.Last()] : _one);
                    var payoff    = (lastValue - minValue) * _notional;

                    ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());

                    var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                    _results[resultIx] = payoff;
                }
            }
            else
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps = block.GetStepsForFactor(path, _assetIndex);
                    Span <Vector <double> > stepsFx = null;
                    if (_fxName != null)
                    {
                        stepsFx = block.GetStepsForFactor(path, _fxIndex);
                    }

                    var maxValue = new Vector <double>(double.MinValue);
                    for (var i = 0; i < _dateIndexes.Length; i++)
                    {
                        maxValue = Vector.Max(steps[_dateIndexes[i]] * (_fxName != null ? stepsFx[_dateIndexes[i]] : _one), maxValue);
                    }
                    var lastValue = steps[_dateIndexes.Last()] * (_fxName != null ? stepsFx[_dateIndexes.Last()] : _one);
                    var payoff    = (maxValue - lastValue) * _notional;

                    ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());

                    var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                    _results[resultIx] = payoff;
                }
            }
        }
Exemple #14
0
 public void Process(IPathBlock block)
 {
     for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
     {
         var steps    = block.GetStepsForFactor(path, _fxPairIx);
         var stepsInv = block.GetStepsForFactor(path, _invertedFxPairIx);
         for (var s = 0; s < steps.Length; s++)
         {
             stepsInv[s] = _one / steps[s];
         }
     }
 }
Exemple #15
0
        public unsafe void Process(IPathBlock block)
        {
            for (var f = 0; f < block.Factors; f++)
            {
                for (var p = 0; p < block.NumberOfPaths; p += Vector <double> .Count)
                {
                    var s = block.GetStepsForFactor(p, f);

                    for (var i = 0; i < block.NumberOfSteps; i++)
                    {
                        s[i] = _returnVec;
                    }
                }
            }
        }
Exemple #16
0
 public void Process(IPathBlock block)
 {
     for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
     {
         var steps       = block.GetStepsForFactor(path, _assetIndex);
         var finalValues = new Vector <double>(0.0);
         for (var i = 0; i < _dateIndexes.Length; i++)
         {
             finalValues += steps[_dateIndexes[i]];
         }
         finalValues = (new Vector <double>(_strike)) - (finalValues / new Vector <double>(_dateIndexes.Length));
         finalValues = Vector.Max(new Vector <double>(0), finalValues);
         _results.Add(finalValues);
     }
 }
Exemple #17
0
 internal void ConvertToSimCcyIfNeeded(IPathBlock block, int pathId, Vector <double> values, int payDateIndex)
 {
     if (_requiresConversionToSimCcy)
     {
         var stepsFxConv = block.GetStepsForFactor(pathId, _conversionToSimCcyIx);
         if (_requiresConversionToSimCcyInverted)
         {
             values *= stepsFxConv[payDateIndex];
         }
         else
         {
             values /= stepsFxConv[payDateIndex];
         }
     }
 }
        public void Process(IPathBlock block)
        {
            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                //This should be set to the spot price here
                var previousStep = new Vector <double>(_spot);
                var steps        = block.GetStepsForFactor(path, _factorIndex);
                steps[0] = previousStep;
                for (var step = 1; step < block.NumberOfSteps; step++)
                {
                    var drift = _drift * _timesteps.TimeSteps[step] * previousStep;
                    var delta = _scaledVol * steps[step] * previousStep;

                    previousStep = (previousStep + drift + delta);
                    steps[step]  = previousStep;
                }
            }
        }
Exemple #19
0
            public unsafe void Process(IPathBlock block)
            {
                var currentIndex = 0 * block.NumberOfPaths * block.NumberOfSteps;

                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    //This should be set to the spot price here
                    var previousStep = Vector <double> .One;
                    for (var step = 0; step < block.NumberOfSteps; step++)
                    {
                        //ref Vector<double> currentValue = ref block.ReadVectorByRef(currentIndex);


                        //currentValue = Vector.Multiply(previousStep, currentValue);
                        //previousStep = currentValue;
                        //currentIndex += Vector<double>.Count;
                    }
                }
            }
Exemple #20
0
        public void Process(IPathBlock block)
        {
            for (var ix = 0; ix < _regressionIndices.Length; ix++)
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps = block.GetStepsForFactor(path, _regressionIndices[ix]);

                    var targetIx   = 0;
                    var nextTarget = _dateIndexes[targetIx];

                    for (var s = 0; s < steps.Length; s++)
                    {
                        if (s == nextTarget)
                        {
                            if (ix == 0)
                            {
                                for (var v = 0; v < Vector <double> .Count; v++)
                                {
                                    _pathwiseValues[targetIx][block.GlobalPathIndex + path + v] = steps[s][v];
                                }
                            }
                            else
                            {
                                for (var v = 0; v < Vector <double> .Count; v++)
                                {
                                    _pathwiseValues[targetIx][block.GlobalPathIndex + path + v] *= steps[s][v];
                                }
                            }
                            targetIx++;
                            if (targetIx == _dateIndexes.Length)
                            {
                                break;
                            }
                            nextTarget = _dateIndexes[targetIx];
                        }
                    }
                }
            }
        }
Exemple #21
0
        public override void Process(IPathBlock block)
        {
            var blockBaseIx = block.GlobalPathIndex;

            var barrierDownHit = new Vector <double>(0);
            var barrierUpHit   = new Vector <double>(0);
            var expiryValue    = new Vector <double>(0);

            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                var steps    = block.GetStepsForFactor(path, _assetIndex);
                var minValue = new Vector <double>(double.MaxValue);
                var maxValue = new Vector <double>(double.MinValue);
                for (var i = 0; i < _dateIndexes.Length; i++)
                {
                    minValue = Vector.Min(steps[_dateIndexes[i]], minValue);
                    maxValue = Vector.Max(steps[_dateIndexes[i]], maxValue);
                }
                barrierDownHit = Vector.Abs(Vector.ConvertToDouble(Vector.LessThan(minValue, _barrierDownVec)));
                barrierUpHit   = Vector.ConvertToDouble(Vector.GreaterThan(maxValue, _barrierUpVec));


                if (_barrierType == BarrierType.KI)
                {
                    var barrierHit = Vector.BitwiseAnd(barrierDownHit, barrierUpHit);
                    var payoff     = barrierHit * _notional;
                    ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());
                    var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                    _results[resultIx] = payoff;
                }
                else //KO
                {
                    var barrierHit = Vector.BitwiseAnd(Vector <double> .One - barrierDownHit, Vector <double> .One - barrierUpHit);
                    var payoff     = barrierHit * _notional;
                    ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());
                    var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                    _results[resultIx] = payoff;
                }
            }
        }
Exemple #22
0
        public void Process(IPathBlock block)
        {
            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                var previousStep = new Vector <double>(_forwardCurve(0));
                var steps        = block.GetStepsForFactor(path, _factorIndex);
                var c            = 0;
                foreach (var kv in _pastFixings.Where(x => x.Key < _startDate))
                {
                    steps[c] = new Vector <double>(kv.Value);
                    c++;
                }
                steps[c] = previousStep;
                for (var step = c + 1; step < block.NumberOfSteps; step++)
                {
                    var W  = steps[step];
                    var dt = new Vector <double>(_timesteps.TimeSteps[step]);
                    var bm = (_drifts[step] - _vols[step] * _vols[step] / 2.0) * dt + (_vols[step] * _timesteps.TimeStepsSqrt[step] * W);
                    previousStep *= bm.Exp();
                    steps[step]   = previousStep;
                }

                //transform
                for (var step = c + 1; step < block.NumberOfSteps; step++)
                {
                    var ws = new double[Vector <double> .Count];
                    for (var v = 0; v < ws.Length; v++)
                    {
                        var t1 = Log(steps[step][v] / _spot0);
                        var t2 = (_spotDrifts[step] - _spotVols[step] * _spotVols[step] / 2.0) * _timesteps.Times[step];
                        t1   -= t2;
                        t1   /= _spotVols[step] * _spotTimesSqrt[step];
                        t1    = Statistics.NormSDist(t1);
                        ws[v] = _invCdfs[step].Interpolate(t1);
                    }
                    steps[step] = new Vector <double>(ws);
                }
            }
        }
Exemple #23
0
        public void Process(IPathBlock block)
        {
            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                var previousStep = new Vector <double>(_forwardCurve(0));
                var steps        = block.GetStepsForFactor(path, _factorIndex);
                var c            = 0;
                foreach (var kv in _pastFixings.Where(x => x.Key < _startDate))
                {
                    steps[c] = new Vector <double>(kv.Value);
                    c++;
                }
                steps[c] = previousStep;

                if (_siegelInvert)
                {
                    for (var step = c + 1; step < block.NumberOfSteps; step++)
                    {
                        var W  = steps[step];
                        var dt = new Vector <double>(_timesteps.TimeSteps[step]);
                        var bm = (_drifts[step] + _vols[step] * _vols[step] / 2.0) * dt + (_vols[step] * _timesteps.TimeStepsSqrt[step] * W);
                        previousStep *= bm.Exp();
                        steps[step]   = previousStep;
                    }
                }
                else
                {
                    for (var step = c + 1; step < block.NumberOfSteps; step++)
                    {
                        var W  = steps[step];
                        var dt = new Vector <double>(_timesteps.TimeSteps[step]);
                        var bm = (_drifts[step] - _vols[step] * _vols[step] / 2.0) * dt + (_vols[step] * _timesteps.TimeStepsSqrt[step] * W);
                        previousStep *= bm.Exp();
                        steps[step]   = previousStep;
                    }
                }
            }
        }
Exemple #24
0
        public void Process(IPathBlock block)
        {
            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                var steps         = block.GetStepsForFactor(path, _assetIndex);
                var previousIndex = new double[Vector <double> .Count];
                steps[0].CopyTo(previousIndex);
                steps[0] = new Vector <double>(0);
                for (var step = 1; step < block.NumberOfSteps; step++)
                {
                    var currentIndex = new double[Vector <double> .Count];
                    steps[step].CopyTo(currentIndex);

                    if (_logReturns)
                    {
                    }
                    else
                    {
                        steps[step] = new Vector <double>(currentIndex) / new Vector <double>(previousIndex) - new Vector <double>(1.0);
                    }
                    previousIndex = currentIndex;
                }
            }
        }
Exemple #25
0
        public void Process(IPathBlock block)
        {
            var blockBaseIx = block.GlobalPathIndex;

            var barrierHit  = new Vector <double>(0);
            var expiryValue = new Vector <double>(0);

            if (_barrierSide == BarrierSide.Down)
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps    = block.GetStepsForFactor(path, _assetIndex);
                    var minValue = new Vector <double>(double.MaxValue);
                    for (var i = 0; i < _dateIndexes.Length; i++)
                    {
                        minValue = Vector.Min(steps[_dateIndexes[i]], minValue);
                    }
                    barrierHit = Vector.ConvertToDouble(Vector.LessThan(minValue, _barrierVec));

                    if (_barrierType == BarrierType.KI)
                    {
                        var payoff   = -barrierHit * _notional;
                        var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                        _results[resultIx] = payoff;
                    }
                    else //KO
                    {
                        var didntHit = (new Vector <double>(1.0)) + barrierHit;
                        var payoff   = didntHit * _notional;
                        var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                        _results[resultIx] = payoff;
                    }
                }
            }
            else
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps    = block.GetStepsForFactor(path, _assetIndex);
                    var maxValue = new Vector <double>(double.MinValue);
                    for (var i = 0; i < _dateIndexes.Length; i++)
                    {
                        maxValue = Vector.Max(steps[_dateIndexes[i]], maxValue);
                    }
                    barrierHit = Vector.ConvertToDouble(Vector.GreaterThan(maxValue, _barrierVec));

                    if (_barrierType == BarrierType.KI)
                    {
                        var payoff   = barrierHit * _notional;
                        var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                        _results[resultIx] = payoff;
                    }
                    else //KO
                    {
                        var didntHit = (new Vector <double>(1.0)) + barrierHit;
                        var payoff   = didntHit * _notional;
                        var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                        _results[resultIx] = payoff;
                    }
                }
            }
        }
Exemple #26
0
 public abstract void Process(IPathBlock block);
        public void Process(IPathBlock block)
        {
            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                var stepsByFactor = new Dictionary <int, Vector <double>[]>();
                foreach (var ix in _assetIndices)
                {
                    stepsByFactor.Add(ix, block.GetStepsForFactor(path, ix).ToArray());
                }

                var indexCounter = 0;
                var nextIndex    = _calculationDateIndices[indexCounter];

                var steps = stepsByFactor.First().Value;
                var fixingDictionaries = _assetFxModel.FixingDictionaryNames.ToDictionary(x => x, x => (IFixingDictionary) new FixingDictionary(_assetFxModel.GetFixingDictionary(x)));

                for (var i = 0; i < steps.Length; i++)
                {
                    var d = _timeFeature.Dates[i];
                    for (var j = 0; j < Vector <double> .Count; j++)
                    {
                        for (var a = 0; a < _assetIndices.Length; a++)
                        {
                            if (!fixingDictionaries.TryGetValue(_assetIds[a], out var fd))
                            {
                                throw new Exception($"Fixing dictionary not found for asset {_assetIds[a]} ");
                            }
                            fd[d] = stepsByFactor[a][i][j];
                        }
                    }

                    if (i == nextIndex)
                    {
                        var currentDate = _calculationDates[indexCounter];
                        var newModel    = _assetFxModel.Clone();
                        newModel.OverrideBuildDate(currentDate);
                        newModel.AddFixingDictionaries(fixingDictionaries);

                        var spotsByAsset = stepsByFactor.ToDictionary(x => x.Key, x => x.Value[i]);
                        for (var j = 0; j < Vector <double> .Count; j++)
                        {
                            //build on-path price curves in model
                            foreach (var spot in spotsByAsset)
                            {
                                var assetId     = _assetIds[spot.Key];
                                var baseCurve   = _assetFxModel.GetPriceCurve(assetId);
                                var spotOnCurve = baseCurve.GetPriceForFixingDate(currentDate);
                                var ratio       = spot.Value[j] / spotOnCurve;
                                var newCurve    = new FactorPriceCurve(baseCurve, ratio);
                                newModel.AddPriceCurve(assetId, newCurve);
                            }

                            var ead     = _portfolio.SaCcrEAD(newModel, _reportingCurrency, _assetIdToGroupMap);
                            var capital = _counterpartyRiskWeight * ead;
                            if (!_expectedCapital.ContainsKey(currentDate))
                            {
                                lock (_threadLock)
                                {
                                    if (!_expectedCapital.ContainsKey(currentDate))
                                    {
                                        _expectedCapital.Add(currentDate, 0.0);
                                    }
                                }
                            }
                            if (double.IsNaN(capital) || double.IsInfinity(capital))
                            {
                                throw new Exception("Invalid capital generated");
                            }

                            _expectedCapital[currentDate] += capital;
                        }

                        indexCounter++;
                        nextIndex = indexCounter < _calculationDateIndices.Length
                            ? _calculationDateIndices[indexCounter] : int.MaxValue;
                    }
                }
            }
        }
        public void Process(IPathBlock block)
        {
            var blockBaseIx = block.GlobalPathIndex;
            var nTotalVec   = new Vector <double> [_nTotal.Length];

            for (var i = 0; i < nTotalVec.Length; i++)
            {
                nTotalVec[i] = new Vector <double>(_nTotal[i]);
            }

            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                var steps = block.GetStepsForFactor(path, _assetIndex);
                Span <Vector <double> > stepsFx = null;
                if (_fxName != null)
                {
                    stepsFx = block.GetStepsForFactor(path, _fxIndex);
                }
                var avgs   = new Vector <double> [_dateIndexes.Count];
                var avgVec = new Vector <double>((_callPut == OptionType.C) ? double.MaxValue : double.MinValue);
                var setReg = new double[Vector <double> .Count];

                for (var a = 0; a < _dateIndexes.Count; a++)
                {
                    var pastSum = new Vector <double>(0.0);
                    for (var p = 0; p < _dateIndexesPast[a].Length; p++)
                    {
                        pastSum += steps[_dateIndexesPast[a][p]] * (_fxName != null ? stepsFx[_dateIndexesPast[a][p]] : _one);
                    }

                    var spotAtExpiry = steps[_decisionDateIx] * (_fxName != null ? stepsFx[_decisionDateIx] : _one);

                    if (VanillaModel != null && AverageRegressors[a] == null)
                    {
                        avgs[a] = pastSum / nTotalVec[a];

                        for (var i = 0; i < Vector <double> .Count; i++)
                        {
                            setReg[i] = spotAtExpiry[i] * _expiryToSettleCarry;
                        }
                    }
                    else
                    {
                        var futSum = new double[Vector <double> .Count];
                        for (var i = 0; i < Vector <double> .Count; i++)
                        {
                            futSum[i] = AverageRegressors[a] == null ? 0.0 : AverageRegressors[a].Predict(spotAtExpiry[i]) * _nFuture[a];
                            setReg[i] = SettlementRegressor.Predict(spotAtExpiry[i]);
                        }
                        var futVec = new Vector <double>(futSum);
                        avgs[a] = (futVec + pastSum) / nTotalVec[a];
                    }

                    avgVec = (_callPut == OptionType.C) ?
                             Vector.Min(avgs[a], avgVec) :
                             Vector.Max(avgs[a], avgVec);
                }


                var setVec = new Vector <double>(setReg);

                var payoff = (_callPut == OptionType.C) ?
                             Vector.Max(new Vector <double>(0), setVec - avgVec) :
                             Vector.Max(new Vector <double>(0), avgVec - setVec);

                var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                _results[resultIx] = payoff * _notional;
            }
        }
Exemple #29
0
        public override void Process(IPathBlock block)
        {
            var blockBaseIx = block.GlobalPathIndex;

            var barrierHit  = new Vector <double>(0);
            var expiryValue = new Vector <double>(0);

            if (_barrierSide == BarrierSide.Down)
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps    = block.GetStepsForFactor(path, _assetIndex);
                    var minValue = new Vector <double>(double.MaxValue);
                    for (var i = 0; i < _dateIndexes.Length; i++)
                    {
                        minValue = Vector.Min(steps[_dateIndexes[i]], minValue);
                    }
                    barrierHit  = Vector.ConvertToDouble(Vector.LessThan(minValue, _barrierVec));
                    expiryValue = steps[_expiryIx];

                    var vanillaValue = _callPut == OptionType.C
                        ? Vector.Max(expiryValue - _strikeVec, _zero)
                        : Vector.Max(_strikeVec - expiryValue, _zero);

                    if (_barrierType == BarrierType.KI)
                    {
                        var payoff = vanillaValue * barrierHit * _notional;
                        ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());
                        var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                        _results[resultIx] = payoff;
                    }
                    else //KO
                    {
                        var didntHit = (new Vector <double>(1.0)) - barrierHit;
                        var payoff   = vanillaValue * didntHit * _notional;
                        ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());
                        var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                        _results[resultIx] = payoff;
                    }
                }
            }
            else
            {
                for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
                {
                    var steps    = block.GetStepsForFactor(path, _assetIndex);
                    var maxValue = new Vector <double>(double.MinValue);
                    for (var i = 0; i < _dateIndexes.Length; i++)
                    {
                        maxValue = Vector.Max(steps[_dateIndexes[i]], maxValue);
                    }
                    barrierHit  = Vector.ConvertToDouble(Vector.GreaterThan(maxValue, _barrierVec));
                    expiryValue = steps[_expiryIx];

                    var vanillaValue = _callPut == OptionType.C
                        ? Vector.Max(expiryValue - _strikeVec, _zero)
                        : Vector.Max(_strikeVec - expiryValue, _zero);

                    if (_barrierType == BarrierType.KI)
                    {
                        var payoff = vanillaValue * barrierHit * _notional;
                        ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());
                        var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                        _results[resultIx] = payoff;
                    }
                    else //KO
                    {
                        var didntHit = (new Vector <double>(1.0)) + barrierHit;
                        var payoff   = vanillaValue * didntHit * _notional;
                        ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());
                        var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                        _results[resultIx] = payoff;
                    }
                }
            }
        }
Exemple #30
0
        public override void Process(IPathBlock block)
        {
            var blockBaseIx = block.GlobalPathIndex;
            var nTotalVec   = new Vector <double>(_nTotal);

            for (var path = 0; path < block.NumberOfPaths; path += Vector <double> .Count)
            {
                var steps = block.GetStepsForFactor(path, _assetIndex);
                Span <Vector <double> > stepsFx = null;
                if (_fxName != null)
                {
                    stepsFx = block.GetStepsForFactor(path, _fxIndex);
                }

                var pastSum = new Vector <double>(0.0);
                for (var p = 0; p < _dateIndexesPast.Length; p++)
                {
                    pastSum += steps[_dateIndexesPast[p]] * (_fxName != null ? stepsFx[_dateIndexesPast[p]] : _one);
                }

                var spotAtExpiry = steps[_decisionDateIx] * (_fxName != null ? stepsFx[_decisionDateIx] : _one);

                if (VanillaModel != null && AverageRegressor == null)
                {
                    var setReg = new double[Vector <double> .Count];
                    for (var i = 0; i < Vector <double> .Count; i++)
                    {
                        setReg[i] = spotAtExpiry[i] + _expiryToSettleCarry;
                    }

                    var avgVec = pastSum / nTotalVec;
                    var setVec = new Vector <double>(setReg);

                    var payoff = (_callPut == OptionType.C) ?
                                 Vector.Max(new Vector <double>(0), setVec - avgVec) :
                                 Vector.Max(new Vector <double>(0), avgVec - setVec);
                    ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());
                    var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                    _results[resultIx] = payoff * _notional;
                }
                else
                {
                    var futSum = new double[Vector <double> .Count];
                    var setReg = new double[Vector <double> .Count];
                    for (var i = 0; i < Vector <double> .Count; i++)
                    {
                        futSum[i] = AverageRegressor == null ? 0.0 : AverageRegressor.Predict(spotAtExpiry[i]) * _nFuture;
                        setReg[i] = SettlementRegressor.Predict(spotAtExpiry[i]);
                    }
                    var futVec = new Vector <double>(futSum);
                    var avgVec = (futVec + pastSum) / nTotalVec;
                    var setVec = new Vector <double>(setReg);

                    var payoff = (_callPut == OptionType.C) ?
                                 Vector.Max(new Vector <double>(0), setVec - avgVec) :
                                 Vector.Max(new Vector <double>(0), avgVec - setVec);
                    ConvertToSimCcyIfNeeded(block, path, payoff, _dateIndexes.Last());
                    var resultIx = (blockBaseIx + path) / Vector <double> .Count;
                    _results[resultIx] = payoff * _notional;
                }
            }
        }