Beispiel #1
0
        // factory
        public object make_sequence_generator(int dimension, ulong seed)
        {
            RandomSequenceGenerator <URNG> g = new RandomSequenceGenerator <URNG>(dimension, seed);

            return(icInstance != null ? new InverseCumulativeRsg <RandomSequenceGenerator <URNG>, IC>(g, icInstance)
                                       : new InverseCumulativeRsg <RandomSequenceGenerator <URNG>, IC>(g));
        }
 public HaltonSequence(int dimensionality,
                       ulong seed       = 0,
                       bool randomStart = true,
                       bool randomShift = false)
 {
     _dimensionality  = dimensionality;
     _sequenceCounter = 0;
     _sequence        = new Sample <List <double> >(new List <double>(dimensionality), 1.0);
     _randomStart     = new List <ulong>(dimensionality);
     _randomShift     = new List <double>(dimensionality);
     //dimensionality must be greater than 0"
     if (randomStart || randomShift)
     {
         RandomSequenceGenerator <MersenneTwister> uniformRsg =
             new RandomSequenceGenerator <MersenneTwister>(_dimensionality, seed);
         if (randomStart)
         {
             _randomStart = uniformRsg.NextInt32Sequence();
         }
         if (randomShift)
         {
             _randomShift = uniformRsg.NextSequence().Value;
         }
     }
 }
        public HaltonRsg(int dimensionality,
                         ulong seed ,
                         bool randomStart,
                         bool randomShift)
        {
            if(seed ==null) seed = 0;
            if(randomStart ==null) randomStart = true;
            if(randomShift ==null) randomShift = false;
            dimensionality_=dimensionality;
            sequenceCounter_ = 0;
            sequence_ = new Sample<List<double>>(new InitializedList<double>(dimensionality), 1.0);
            randomStart_= new InitializedList<ulong>(dimensionality, 0UL);
            randomShift_ = new InitializedList<double>(dimensionality, 0.0);

            if(!(dimensionality>0)) 
                throw new ArgumentException("dimensionality must be greater than 0");

            if (randomStart || randomShift) 
            {
                RandomSequenceGenerator<MersenneTwisterUniformRng>uniformRsg = 
                    new RandomSequenceGenerator<MersenneTwisterUniformRng>(dimensionality_, seed);
                if (randomStart)
                    randomStart_ = uniformRsg.nextInt32Sequence();
                if (randomShift)
                    randomShift_ = uniformRsg.nextSequence().value;
            }
        }
Beispiel #4
0
        public Grid GetRandomGrid(int rows = 5, int columns = 2)
        {
            var grid = new Grid(rows, columns);
            var sequenceGenerator = new RandomSequenceGenerator();

            grid.PopulateCells(sequenceGenerator);
            grid.RandomiseCells();
            return(grid);
        }
Beispiel #5
0
        public void Random_Sequence_Generates_Correct_Values()
        {
            var sequenceGenerator = new RandomSequenceGenerator();
            var actual1           = sequenceGenerator.Generate(4);
            var actual2           = sequenceGenerator.Generate(4);

            actual1.Items.Should().NotBeEquivalentTo(actual2.Items);
            actual1.Items.Select(s => s.Value).Should().OnlyHaveUniqueItems();
            actual1.Items.Select(s => int.Parse(s.Value)).Should().BeInAscendingOrder();
        }
Beispiel #6
0
        public IObservable <TradeDto> GetEventStream()
        {
            var quotes = _stockQuoteProvider.GetEventStream();

            var quantities = new RandomSequenceGenerator(10, 1000).CreateIntegerSequence(TimeSpan.FromMilliseconds(50)).Publish().RefCount();

            return(quotes.SelectMany(quote =>
            {
                return quantities
                .Take(1)
                .Select(qty => new TradeDto(quote.Code, qty, quote.QuoteValue * qty));
            }));
        }
 public double[] getPath(double s0, double drift, double sigma, double maturity, int numOfSteps
                             , RandomSequenceGenerator randomSequenceGenerator)
 {
     double dt = maturity / numOfSteps;
     double[] stockPath = new double[numOfSteps + 1];
     stockPath[0] = s0;
     double[] randomSeq = randomSequenceGenerator.getSequence(numOfSteps);
     for (int i = 1; i < numOfSteps + 1; ++i)
     {
         stockPath[i] = stockPath[i - 1] + drift * stockPath[i - 1] * dt + sigma * stockPath[i - 1] * Math.Sqrt(dt) * randomSeq[i - 1];
     }
     return stockPath;
 }
 public static void MCPriceOption(double s0, double drift, double sigma, double maturity, double strike
                               , PutCallOption optionType, int numOfSteps
                               , StockPathGenerator stockpathGenerator, PayoffCalculator payoffCalculator
                               , RandomSequenceGenerator randomSequenceGenerator)
 {
     StatisticTracker tracker = new StatisticTracker();
     while (tracker.needMoreSimulation())
     {
         tracker.putNewResult(payoffCalculator.computePayoff(strike
         , stockpathGenerator.getPath(s0, drift, sigma, maturity, numOfSteps, randomSequenceGenerator)
         , optionType));
     }
     Console.Write("option price is ");
     Console.WriteLine(tracker.getMean());
     Console.Write("option price standard deviation is ");
     Console.WriteLine(tracker.getStdDev());
 }
Beispiel #9
0
        public ScenarioCube(double r, double[] spots, double[] q, double[] vols, double[,] correlations, int nSteps, double dt)
        {
            nAssets     = spots.Length;
            this.nSteps = nSteps;
            this.dt     = dt;

            Processes    = new List <StochasticProcess1D>(nAssets);
            Correlations = new Matrix(nAssets, nAssets);

            Date       now = new Date(DateTime.Now.Date);
            DayCounter dc  = new SimpleDayCounter();

            yc = new Handle <YieldTermStructure>(new FlatForward(now, r, dc));
            for (int i = 0; i < nAssets; ++i)
            {
                Handle <Quote> x0 = new Handle <Quote>(new SimpleQuote(spots[i]));
                Handle <YieldTermStructure>    dyc = new Handle <YieldTermStructure>(new FlatForward(now, q[i], dc));
                Handle <BlackVolTermStructure> bv  = new Handle <BlackVolTermStructure>(new BlackConstantVol(now, new Calendar(), vols[i], dc));

                Processes.Add(new GeneralizedBlackScholesProcess(x0, dyc, yc, bv));

                for (int j = 0; j < nAssets; ++j)
                {
                    if (i == j)
                    {
                        Correlations[i, j] = 1;
                    }
                    else
                    {
                        Correlations[i, j] = Correlations[j, i] = correlations[i, j];
                    }
                }
            }

            spa = new StochasticProcessArray(Processes, Correlations);
            rsg = new RandomSequenceGenerator <MersenneTwisterUniformRng>(nSteps * Processes.Count, new MersenneTwisterUniformRng());
            tg  = new TimeGrid(nSteps * dt, nSteps);

            var rsg2 = new InverseCumulativeRsg <RandomSequenceGenerator <MersenneTwisterUniformRng>,
                                                 InverseCumulativeNormal>(rsg, new InverseCumulativeNormal());

            mpg = new MultiPathGenerator <InverseCumulativeRsg <RandomSequenceGenerator <MersenneTwisterUniformRng>, InverseCumulativeNormal> >(spa, tg, rsg2, false);
        }
Beispiel #10
0
        public JsonResult LoadPage(int number)
        {
            var firstElemId = number * PageSize;
            var tuchPointId = firstElemId / RandomSequenceGenerator.PartSize;
            var skipCount   = firstElemId - tuchPointId * RandomSequenceGenerator.PartSize;
            var takeCount   = GeneratingResults.Period - firstElemId < PageSize ?
                              (int)(GeneratingResults.Period - firstElemId + 1) :
                              PageSize;

            var generator   = new RandomSequenceGenerator(Data.A, Data.C, Data.M, GeneratingResults.TuchPoints[tuchPointId]);
            var pageContent = new StringBuilder();
            var sequence    = generator.GetNextSequencePart().Skip(skipCount).Take(takeCount).ToList();

            if (sequence.Count < PageSize && GeneratingResults.Period - firstElemId > PageSize)
            {
                sequence.AddRange(generator.GetNextSequencePart().Skip(sequence.Count).Take(PageSize - sequence.Count).ToList());
            }
            sequence.ForEach(num => pageContent.Append(num).Append(" "));

            return(Json(new { PageContent = pageContent.ToString() }, JsonRequestBehavior.AllowGet));
        }
Beispiel #11
0
        public IObservable <Quote> GetEventStream()
        {
            var googleStock = new RandomSequenceGenerator(GoogleMin, GoogleMax)
                              .Create(TimeSpan.FromMilliseconds(200))
                              .Select(s => new Quote("GOOGL", s));

            var ibmStock = new RandomSequenceGenerator(IbmMin, IbmMax)
                           .Create(TimeSpan.FromMilliseconds(705))
                           .Select(s => new Quote("IBM", s));

            var hpStock = new RandomSequenceGenerator(HpMin, HpMax)
                          .Create(TimeSpan.FromMilliseconds(602))
                          .Select(s => new Quote("HPQ", s));

            var appleStock = new RandomSequenceGenerator(AppleMin, AppleMax)
                             .Create(TimeSpan.FromMilliseconds(253))
                             .Select(s => new Quote("AAPL", s));

            var microsoftStock = new RandomSequenceGenerator(MicrosoftMin, MicrosoftMax)
                                 .Create(TimeSpan.FromMilliseconds(407))
                                 .Select(s => new Quote("MSFT", s));

            return(Observable.Merge(googleStock, ibmStock, hpStock, appleStock, microsoftStock).Publish().RefCount());
        }