[Quantproject-developers] QuantProject/b4_Business/a2_Strategies/InSample/InSampleFitnessDistributi
Brought to you by:
glauco_1
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/InSample/InSampleFitnessDistributionEstimation In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv22168 Added Files: BasicInSampleFitnessDistributionEstimator.cs IInSampleFitnessDistributionEstimator.cs Log Message: Added IInSampleFitnessDistributionEstimator interface. --- NEW FILE: IInSampleFitnessDistributionEstimator.cs --- /* QuantProject - Quantitative Finance Library IInSampleFitnessDistributionEstimator.cs Copyright (C) 2009 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; //using QuantProject.ADT.Optimizing.Genetic; using QuantProject.Business.Strategies.Eligibles; using QuantProject.Business.Strategies.ReturnsManagement; using QuantProject.Business.Strategies.InSample; namespace QuantProject.Business.Strategies.InSample.InSampleFitnessDistributionEstimation { /// <summary> /// Interface for classes performing /// estimations of the distribution's features (mainly, /// average and variance) for the fitness of the population /// regarded by an optimization process. /// The classes should be used for Hypothesis tests /// on given fitnesses /// </summary> public interface IInSampleFitnessDistributionEstimator { /// <summary>Returns an estimation of the average fitness /// for a given population of fitnesses /// </summary> /// <param name="geneticChooser">Genetic chooser that targets the population /// through the way fitness has to be computed</param> /// <param name="eligibleTickers">eligible tickers used for the in sample optimization</param> /// <param name="returnsManager">manager to efficiently handle in sample optimization</param> /// <param name="sampleLength">number of samples that have to be drawn from /// the population for the estimation</param> /// <returns>Estimation of the average fitness of the population</returns> double GetAverage( GeneticChooser geneticChooser, EligibleTickers eligibleTickers , ReturnsManager returnsManager, int sampleLength ); double GetVariance( GeneticChooser geneticChooser, EligibleTickers eligibleTickers , ReturnsManager returnsManager, int sampleLength ); } } --- NEW FILE: BasicInSampleFitnessDistributionEstimator.cs --- /* QuantProject - Quantitative Finance Library BasicInSampleFitnessDistributionEstimator.cs Copyright (C) 2009 Marco Milletti This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Collections; using QuantProject.ADT; using QuantProject.ADT.Statistics; using QuantProject.ADT.Optimizing.Genetic; using QuantProject.Business.Strategies.InSample; //using QuantProject.Business.DataProviders; //using QuantProject.Business.Strategies; using QuantProject.Business.Strategies.Eligibles; using QuantProject.Business.Strategies.ReturnsManagement; namespace QuantProject.Business.Strategies.InSample.InSampleFitnessDistributionEstimation { /// <summary> /// Basic IInSampleFitnessDistributionEstimator for estimation /// of average and variance of the fitness for the population from /// which best fitnesses are retrieved through an optimization process /// </summary> [Serializable] public class BasicInSampleFitnessDistributionEstimator : IInSampleFitnessDistributionEstimator { protected IGenomeManager genomeManager; protected GeneticOptimizer geneticOptimizer; protected ArrayList sampleFitnesses; protected EligibleTickers currentEligibleTickers; protected ReturnsManager currentReturnsManager; protected int sampleLength; /// <summary> /// Basic IInSampleFitnessDistributionEstimator for estimation /// of average and variance of the fitness for the population from /// which best fitnesses are retrieved through an optimization process /// </summary> public BasicInSampleFitnessDistributionEstimator() { } private void newGenerationEventHandler( object sender , NewGenerationEventArgs e ) { ; } private void runGeneticOptimizerAndSetSampleFitnesses_setSampleFitnesses() { this.sampleFitnesses = new ArrayList(); for(int i = 0; i < this.geneticOptimizer.CurrentGeneration.Count; i++) if ( this.geneticOptimizer.CurrentGeneration[i] is Genome && !double.IsInfinity( ((Genome)this.geneticOptimizer.CurrentGeneration[i]).Fitness ) && !double.IsNaN( ((Genome)this.geneticOptimizer.CurrentGeneration[i]).Fitness ) && double.MinValue != ((Genome)this.geneticOptimizer.CurrentGeneration[i]).Fitness ) this.sampleFitnesses.Add( ((Genome)this.geneticOptimizer.CurrentGeneration[i]).Fitness); } private void runGeneticOptimizerAndSetSampleFitnesses(GeneticChooser geneticChooser , EligibleTickers eligibleTickers , ReturnsManager returnsManager , int sampleLength) { if( this.genomeManager == null || !ReferenceEquals(this.currentEligibleTickers, eligibleTickers) || !ReferenceEquals(this.currentReturnsManager, returnsManager) || this.sampleLength != sampleLength) { this.sampleLength = sampleLength; this.currentEligibleTickers = eligibleTickers; this.currentReturnsManager = returnsManager; this.genomeManager = geneticChooser.GetGenomeManager(eligibleTickers, returnsManager); this.geneticOptimizer = new GeneticOptimizer(this.genomeManager, sampleLength, 0, ConstantsProvider.SeedForRandomGenerator); this.geneticOptimizer.NewGeneration += new NewGenerationEventHandler( this.newGenerationEventHandler ); this.geneticOptimizer.Run( false ); this.runGeneticOptimizerAndSetSampleFitnesses_setSampleFitnesses(); } } /// <summary> /// Returns an estimation of the average fitness /// for the population of fitnesses targeted /// by the given geneticChooser (through IGenomeManager) /// </summary> public double GetAverage(GeneticChooser geneticChooser, EligibleTickers eligibleTickers , ReturnsManager returnsManager, int sampleLength) { this.runGeneticOptimizerAndSetSampleFitnesses(geneticChooser , eligibleTickers , returnsManager , sampleLength); double average = BasicFunctions.GetSimpleAverage(this.sampleFitnesses); return average; } /// <summary> /// Returns an estimation of the variance /// for the population of fitnesses targeted /// by the given geneticChooser (through IGenomeManager) /// </summary> public double GetVariance(GeneticChooser geneticChooser, EligibleTickers eligibleTickers , ReturnsManager returnsManager, int sampleLength) { this.runGeneticOptimizerAndSetSampleFitnesses(geneticChooser , eligibleTickers , returnsManager , sampleLength); double variance = BasicFunctions.GetVariance(this.sampleFitnesses); return variance; } } } |