Ejemplo n.º 1
0
        /// <summary>
        /// Given one or more paths, the value of an option is returned.
        /// </summary>
        /// <param name="paths"></param>
        /// <returns></returns>
        public override double Value(Path[] paths)
        {
            // would throw anyways: QL_RequireArgNotNull(paths, "paths", null);
            if (paths.Length != _underlying.Length)
            {
                throw new ArgumentException("TODO: Not enough paths.");
            }
            if (paths[0].Count == 0)
            {
                throw new ArgumentException("TODO: the path cannot be empty.");
            }
            int numAssets    = paths.Length;
            var logDrift     = new SparseVector(numAssets);
            var logDiffusion = new SparseVector(numAssets);

            for (int j = 0; j < numAssets; j++)
            {
                logDrift.Data[j]     = paths[j].Drift.Sum();
                logDiffusion.Data[j] = paths[j].Diffusion.Sum();
            }
            double basketPrice1 = SparseVector.DotProduct(_underlying, SparseVector.Exp(logDrift + logDiffusion));

            if (UseAntitheticVariance)
            {
                double basketPrice2 = SparseVector.DotProduct(_underlying, SparseVector.Exp(logDrift - logDiffusion));
                return(Discount * (
                           Option.ExercisePayoff(_optionType, basketPrice1, _strike) +
                           Option.ExercisePayoff(_optionType, basketPrice2, _strike)
                           ) / 2.0);
            }
            return(Discount * Option.ExercisePayoff(
                       _optionType, basketPrice1, _strike));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Given one or more paths, the value of an option is returned.
        /// </summary>
        /// <param name="paths"></param>
        /// <returns></returns>
        public override double Value(Path[] paths)
        {
            if (paths.Length != _underlying.Length)
            {
                throw new ArgumentException(nameof(paths));
            }
            if (paths[0].Count == 0)
            {
                throw new ArgumentException(nameof(paths));
            }
            int numAssets    = paths.Length;
            var logDrift     = new SparseVector(numAssets);
            var logDiffusion = new SparseVector(numAssets);

            for (int j = 0; j < numAssets; j++)
            {
                logDrift.Data[j]     = paths[j].Drift.Sum();
                logDiffusion.Data[j] = paths[j].Diffusion.Sum();
            }
            double maxPrice1 = (_underlying * SparseVector.Exp(
                                    logDrift + logDiffusion)).Max();

            if (UseAntitheticVariance)
            {
                double maxPrice2 = (_underlying * SparseVector.Exp(
                                        logDrift - logDiffusion)).Max();
                return(Discount * (maxPrice1 + maxPrice2) / 2.0);
            }
            return(Discount * maxPrice1);
        }