/// <summary>
        /// Simulate a realization of the stochastic process driven by the noise matrix noise.
        /// </summary>
        /// <remarks>
        /// This function is called once for realization.
        /// </remarks>
        /// <param name="dates">
        /// The dates (in years fractions) at which the process must be simulated.
        /// </param>
        /// <param name="noise">The matrix of IID normal realizations.</param>
        /// <param name="outDynamic">Where the dynamic should be written.</param>
        public void Simulate(double[] dates, IReadOnlyMatrixSlice noise, IMatrixSlice outDynamic)
        {
            switch (OperatingMode)
            {
            case OperatingMode.TranslateHistoricalRealizationsForward:

                for (int i = 0; i < dates.Length; i++)
                {
                    int dateIndex;
                    if (!this.simulationDateIndexes.TryGetValue(dates[i], out dateIndex))
                    {
                        dateIndex = 0;
                    }

                    Tuple <DateTime, Vector> value = this.fileContent[dateIndex];
                    for (int j = 0; j < value.Item2.Length - 1; j++)
                    {
                        outDynamic[i, j] = value.Item2[j];
                    }
                }

                break;

            case OperatingMode.Bootstrap:
                this.bootstrap.Simulate(dates, outDynamic);
                break;
            }
        }
 /// <summary>
 /// Handles the conversion, after the simulation, from y to the rate r.
 /// </summary>
 /// <param name="dates">The parameter is not used.</param>
 /// <param name="outDynamic">The input and output components of the transformation.</param>
 public void Transform(double[] dates, IMatrixSlice outDynamic)
 {
     for (int j = 0; j < dates.Length; j++)
     {
         outDynamic[j, 0] = Math.Pow(outDynamic[j, 0] + this.alphaT0[j], 2);
     }
 }
 public void Simulate(double[] Dates, IReadOnlyMatrixSlice Noise, IMatrixSlice OutDynamic)
 {
     OutDynamic[0, 0] = spot.fV();
     for (int i = 1; i < Dates.Length; i++)
     {
         OutDynamic[i, 0] = a * OutDynamic[i - 1, 0] + b + c * Noise[i - 1, 0];
     }
 }
Exemple #4
0
 /// <summary>
 /// Calculates the probabilities for one scenario.
 /// </summary>
 /// <param name="dates">Dates (in Year fractions).</param>
 /// <param name="outDynamic">Input and output matrix.</param>
 public void Transform(double[] dates, IMatrixSlice outDynamic)
 {
     for (int j = 0; j < dates.Length; j++)
     {
         // Truncate to zero the first two components.
         outDynamic[j, 0] = Math.Max(0.0000000001, outDynamic[j, 0]);
         outDynamic[j, 1] = Math.Max(0.0000000001, outDynamic[j, 1]);
     }
 }
Exemple #5
0
        /// <summary>
        /// Handles the conversion, after the simulation, from y to the short rate r.
        /// </summary>
        /// <param name="dates">Simulation dates.</param>
        /// <param name="outDynamic">The input and output components of the transformation.</param>
        public virtual void Transform(double[] dates, IMatrixSlice outDynamic)
        {
#if TFORWARDFORMULATION
            for (int j = 0; j < dates.Length; j++)
            {
                outDynamic[j, 0] = outDynamic[j, 0] + this.alphaT[j];
            }
#endif
        }
Exemple #6
0
 public void Simulate(double[] Dates, IReadOnlyMatrixSlice Noise, IMatrixSlice OutDynamic)
 {
     OutDynamic[0, 0] = x0[0];
     for (int i = 1; i < Dates.Length; i++)
     {
         double dt  = Dates[i] - Dates[i - 1];
         double rdt = Math.Sqrt(dt);
         double th  = theta(Dates[i], dt);
         OutDynamic[i, 0] = OutDynamic[i - 1, 0] + (th - alpha1Temp * OutDynamic[i - 1, 0]) * dt + sigma1Temp * Noise[i - 1, 0] * rdt;
     }
 }
Exemple #7
0
        /// <summary>
        /// Manage the simulation of the variance gamma process
        /// </summary>
        /// <param name="Dates">Simulation dates</param>
        /// <param name="Noise">Gaussian noise for a single path</param>
        /// <param name="OutDynamic">Single path process realization</param>
        public void Simulate(double[] Dates, IReadOnlyMatrixSlice Noise, IMatrixSlice OutDynamic)
        {
            int    steps = OutDynamic.R;
            double GammaNoise, dt;

            OutDynamic[0, 0] = this.s0.fV();
            for (int i = 1; i < steps; i++)
            {
                dt               = Dates[i] - Dates[i - 1];
                this.gamma       = new Fairmat.Statistics.Gamma(dt / this.nu.fV(), 1.0 / this.nu.fV());
                GammaNoise       = this.gamma.Draw(Engine.Generator);
                OutDynamic[i, 0] = OutDynamic[i - 1, 0] * Math.Exp(this.drift * dt +
                                                                   this.theta.fV() * GammaNoise + this.sigma.fV() * Math.Sqrt(GammaNoise) * Noise[i - 1, 0]);
            }
        }
Exemple #8
0
        /// <summary>
        /// Simulate a realization of the stochastic process by bootstrapping historical data.
        /// </summary>
        /// <remarks>
        /// This function is called once for realization.
        /// </remarks>
        /// <param name="dates">
        /// The dates (in years fractions) at which the process must be simulated.
        /// </param>
        /// <param name="outDynamic">Where the dynamic should be written.</param>
        internal void Simulate(double[] dates, IMatrixSlice outDynamic)
        {
            // Number of components.
            int components = this.returns[0].Length;
            for (int c = 0; c < components; c++)
                outDynamic[0, c] = 1;

            // Assumes equispaced dates, otherwise we can normalize returns.
            for (int i = 1; i < dates.Length; i++)
            {
                double dt = dates[i] - dates[i - 1];

                // Selects a vector of returns.
                double u = Engine.Generator.Uniform();
                int z = (int)(u * this.returns.Length);
                for (int c = 0; c < components; c++)
                    outDynamic[i, c] = outDynamic[i - 1, c] * Math.Exp(this.returns[z][c] * dt);
            }
        }
Exemple #9
0
        /// <summary>
        /// Simulate a realization of the stochastic process by bootstrapping historical data.
        /// </summary>
        /// <remarks>
        /// This function is called once for realization.
        /// </remarks>
        /// <param name="dates">
        /// The dates (in years fractions) at which the process must be simulated.
        /// </param>
        /// <param name="outDynamic">Where the dynamic should be written.</param>
        internal void Simulate(double[] dates, IMatrixSlice outDynamic)
        {
            // Number of components.
            int components = this.returns[0].Length;

            for (int c = 0; c < components; c++)
            {
                outDynamic[0, c] = 1;
            }

            // Assumes equispaced dates, otherwise we can normalize returns.
            for (int i = 1; i < dates.Length; i++)
            {
                double dt = dates[i] - dates[i - 1];

                // Selects a vector of returns.
                double u = Engine.Generator.Uniform();
                int    z = (int)(u * this.returns.Length);
                for (int c = 0; c < components; c++)
                {
                    outDynamic[i, c] = outDynamic[i - 1, c] * Math.Exp(this.returns[z][c] * dt);
                }
            }
        }
        /// <summary>
        /// Simulate a realization of the stochastic process driven by the noise matrix noise.
        /// </summary>
        /// <remarks>
        /// This function is called once for realization.
        /// </remarks>
        /// <param name="dates">
        /// The dates (in years fractions) at which the process must be simulated.
        /// </param>
        /// <param name="noise">The matrix of IID normal realizations.</param>
        /// <param name="outDynamic">Where the dynamic should be written.</param>
        public void Simulate(double[] dates, IReadOnlyMatrixSlice noise, IMatrixSlice outDynamic)
        {
            switch (OperatingMode)
            {
                case OperatingMode.TranslateHistoricalRealizationsForward:

                    for (int i = 0; i < dates.Length; i++)
                    {
                        int dateIndex;
                        if (!this.simulationDateIndexes.TryGetValue(dates[i], out dateIndex))
                            dateIndex = 0;

                        Tuple<DateTime, Vector> value = this.fileContent[dateIndex];
                        for (int j = 0; j < value.Item2.Length - 1; j++)
                        {
                            outDynamic[i, j] = value.Item2[j];
                        }
                    }

                    break;
                case OperatingMode.Bootstrap:
                    this.bootstrap.Simulate(dates, outDynamic);
                    break;
            }
        }
 /// <summary>
 /// Handles the conversion, after the simulation, from y to the rate r.
 /// </summary>
 /// <param name="dates">The parameter is not used.</param>
 /// <param name="outDynamic">The input and output components of the transformation.</param>
 public void Transform(double[] dates, IMatrixSlice outDynamic)
 {
     for (int j = 0; j < dates.Length; j++)
     {
         outDynamic[j, 0] = Math.Pow(outDynamic[j, 0] + this.alphaT0[j], 2);
     }
 }
Exemple #12
0
 /// <summary>
 /// Calculates the probabilities for one scenario.
 /// </summary>
 /// <param name="dates">Dates (in Year fractions).</param>
 /// <param name="outDynamic">Input and output matrix.</param>
 public void Transform(double[] dates, IMatrixSlice outDynamic)
 {
     for (int j = 0; j < dates.Length; j++)
     {
         // Truncate to zero the first two components.
         outDynamic[j, 0] = Math.Max(0.0000000001, outDynamic[j, 0]);
         outDynamic[j, 1] = Math.Max(0.0000000001, outDynamic[j, 1]);
     }
 }
Exemple #13
0
 /// <summary>
 /// Manage the simulation of the variance gamma process
 /// </summary>
 /// <param name="Dates">Simulation dates</param>
 /// <param name="Noise">Gaussian noise for a single path</param>
 /// <param name="OutDynamic">Single path process realization</param>
 public void Simulate(double[] Dates, IReadOnlyMatrixSlice Noise, IMatrixSlice OutDynamic)
 {
     int steps = OutDynamic.R;
     double GammaNoise, dt;
     OutDynamic[0, 0] = this.s0.fV();
     for (int i = 1; i < steps; i++)
     {
         dt = Dates[i] - Dates[i - 1];
         this.gamma = new Fairmat.Statistics.Gamma(dt / this.nu.fV(), 1.0 / this.nu.fV());
         GammaNoise = this.gamma.Draw(Engine.Generator);
         OutDynamic[i, 0] = OutDynamic[i - 1, 0] * Math.Exp(this.drift * dt +
             this.theta.fV() * GammaNoise + this.sigma.fV() * Math.Sqrt(GammaNoise) * Noise[i - 1, 0]);
     }
 }
Exemple #14
0
 public void Simulate(double[] Dates, IReadOnlyMatrixSlice Noise, IMatrixSlice OutDynamic)
 {
     OutDynamic[0, 0] = x0[0];
     for (int i = 1; i < Dates.Length; i++)
     {
         double dt= Dates[i]-Dates[i-1];
         double rdt=Math.Sqrt(dt);
         double th = theta(Dates[i], dt);
         OutDynamic[i, 0] = OutDynamic[i-1, 0]+(th - alpha1Temp * OutDynamic[i - 1, 0]) * dt + sigma1Temp * Noise[i - 1, 0] * rdt;
     }
 }
Exemple #15
0
        /// <summary>
        /// Handles the conversion, after the simulation, from y to the short rate r.
        /// </summary>
        /// <param name="dates">Simulation dates.</param>
        /// <param name="outDynamic">The input and output components of the transformation.</param>
        public virtual void Transform(double[] dates, IMatrixSlice outDynamic)
        {
            
#if TFORWARDFORMULATION
            for (int j = 0; j < dates.Length; j++)
            {
                outDynamic[j, 0] = outDynamic[j, 0] + this.alphaT[j];
            }
#endif
             
        }