public virtual AbstractModel trainModel(int iterations, SequenceStream sequenceStream, int cutoff,
                                                bool useAverage)
        {
            this.iterations     = iterations;
            this.sequenceStream = sequenceStream;
            DataIndexer di = new OnePassDataIndexer(new SequenceStreamEventStream(sequenceStream), cutoff, false);

            numSequences = 0;
            foreach (Sequence <Event> s in sequenceStream)
            {
                numSequences++;
            }
            outcomeList = di.OutcomeList;
            predLabels  = di.PredLabels;
            pmap        = new IndexHashTable <string>(predLabels, 0.7d);

            display("Incorporating indexed data for training...  \n");
            this.useAverage = useAverage;
            numEvents       = di.NumEvents;

            this.iterations = iterations;
            outcomeLabels   = di.OutcomeLabels;
            omap            = new Dictionary <string, int?>();
            for (int oli = 0; oli < outcomeLabels.Length; oli++)
            {
                omap[outcomeLabels[oli]] = oli;
            }
            outcomeList = di.OutcomeList;

            numPreds    = predLabels.Length;
            numOutcomes = outcomeLabels.Length;
            if (useAverage)
            {
                updates = RectangularArrays.ReturnRectangularIntArray(numPreds, numOutcomes, 3);
            }

            display("done.\n");

            display("\tNumber of Event Tokens: " + numEvents + "\n");
            display("\t    Number of Outcomes: " + numOutcomes + "\n");
            display("\t  Number of Predicates: " + numPreds + "\n");


            parameters = new MutableContext[numPreds];
            if (useAverage)
            {
                averageParams = new MutableContext[numPreds];
            }

            allOutcomesPattern = new int[numOutcomes];
            for (int oi = 0; oi < numOutcomes; oi++)
            {
                allOutcomesPattern[oi] = oi;
            }

            for (int pi = 0; pi < numPreds; pi++)
            {
                parameters[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);
                if (useAverage)
                {
                    averageParams[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);
                }
                for (int aoi = 0; aoi < numOutcomes; aoi++)
                {
                    parameters[pi].setParameter(aoi, 0.0);
                    if (useAverage)
                    {
                        averageParams[pi].setParameter(aoi, 0.0);
                    }
                }
            }
            modelDistribution = new double[numOutcomes];

            display("Computing model parameters...\n");
            findParameters(iterations);
            display("...done.\n");

            /// <summary>
            ///************* Create and return the model ***************** </summary>
            string[] updatedPredLabels = predLabels;

            /*
             *  String[] updatedPredLabels = new String[pmap.size()];
             *  for (String pred : pmap.keySet()) {
             *    updatedPredLabels[pmap.get(pred)]=pred;
             *  }
             */
            if (useAverage)
            {
                return(new PerceptronModel(averageParams, updatedPredLabels, outcomeLabels));
            }
            else
            {
                return(new PerceptronModel(parameters, updatedPredLabels, outcomeLabels));
            }
        }
        public AbstractModel TrainModel(int trainIterations, ISequenceStream trainStream, int cutoff,
                                        bool trainUseAverage)
        {
            iterations     = trainIterations;
            useAverage     = trainUseAverage;
            sequenceStream = trainStream;

            info.Append("Trained using Perceptron Sequence algorithm.\n\n");

            var di = new OnePassDataIndexer(new SequenceStreamEventStream(trainStream), cutoff, false);

            trainStream.Reset();

            numSequences = 0;

            while (trainStream.Read() != null)
            {
                numSequences++;
            }

            outcomeList = di.GetOutcomeList();
            predLabels  = di.GetPredLabels();

            pMap = new IndexHashTable <string>(predLabels, 0.7d);

            // Incorporation indexed data for training...


            numEvents = di.GetNumEvents();

            outcomeLabels = di.GetOutcomeLabels();
            oMap          = new Dictionary <string, int>();
            for (var i = 0; i < outcomeLabels.Length; i++)
            {
                oMap.Add(outcomeLabels[i], i);
            }

            outcomeList = di.GetOutcomeList();
            numPreds    = predLabels.Length;
            numOutcomes = outcomeLabels.Length;

            if (trainUseAverage)
            {
                updates = new int[numPreds][][];
                for (var i = 0; i < numPreds; i++)
                {
                    updates[i] = new int[numOutcomes][];
                    for (var j = 0; j < numOutcomes; j++)
                    {
                        updates[i][j] = new int[3];
                    }
                }
            }

            // done.
            Display("done.\n");

            info.Append("Number of Event Tokens: {0}\n" +
                        "    Number of Outcomes: {1}\n" +
                        "  Number of Predicates: {2}\n", numEvents, numOutcomes, numPreds);

            Display("\tNumber of Event Tokens: " + numEvents);
            Display("\t    Number of Outcomes: " + numOutcomes);
            Display("\t  Number of Predicates: " + numPreds);

            param = new MutableContext[numPreds];
            if (trainUseAverage)
            {
                averageParams = new MutableContext[numPreds];
            }

            allOutcomesPattern = new int[numOutcomes];
            for (var i = 0; i < numOutcomes; i++)
            {
                allOutcomesPattern[i] = i;
            }

            for (var pi = 0; pi < numPreds; pi++)
            {
                param[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);
                if (trainUseAverage)
                {
                    averageParams[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]);
                }

                for (var aoi = 0; aoi < numOutcomes; aoi++)
                {
                    param[pi].SetParameter(aoi, 0.0d);
                    if (trainUseAverage)
                    {
                        averageParams[pi].SetParameter(aoi, 0.0d);
                    }
                }
            }

            Display("Computing model parameters...");
            FindParameters();
            Display("...done.");

            /*************** Create and return the model ******************/

            // ReSharper disable CoVariantArrayConversion
            if (trainUseAverage)
            {
                return new PerceptronModel(averageParams, predLabels, outcomeLabels)
                       {
                           info = info
                       }
            }
            ;

            return(new PerceptronModel(param, predLabels, outcomeLabels)
            {
                info = info
            });
            // ReSharper restore CoVariantArrayConversion
        }