[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting GenomeManagerForEfficientPo
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2005-07-25 22:48:05
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24585/b7_Scripts/TickerSelectionTesting Modified Files: GenomeManagerForEfficientPortfolio.cs Log Message: Added array containing CandidateProperties (information on tickers to be optimized). Code has been simplified in many points. More relevant changes: - Decode method has been updated (and it is now used by inherited classes); - methods for getting portfolio rates of return have been changed (now they are based on the new CandidateProperties class) Index: GenomeManagerForEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientPortfolio.cs,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** GenomeManagerForEfficientPortfolio.cs 13 Jul 2005 16:37:04 -0000 1.12 --- GenomeManagerForEfficientPortfolio.cs 25 Jul 2005 22:47:57 -0000 1.13 *************** *** 43,48 **** protected int maxValueForGenes; ! protected DataTable setOfTickers; protected int originalNumOfTickers; protected DateTime firstQuoteDate; protected DateTime lastQuoteDate; --- 43,52 ---- protected int maxValueForGenes; ! protected DataTable setOfTickers;//used only for keeping ! //the same signature for ! //protected retrieveData() method ! protected CandidateProperties[] setOfCandidates; protected int originalNumOfTickers; + protected int constToDiscoverDuplicateGenes; protected DateTime firstQuoteDate; protected DateTime lastQuoteDate; *************** *** 55,67 **** protected int numberOfExaminedReturns; ! static public string GetCleanTickerCode(string tickerModifiedCode) { ! if(tickerModifiedCode.StartsWith("-")) ! //if the first char is "-" ! //each element of the array of rates of return is ! //multiplied by -1 ! return tickerModifiedCode.Substring(1,tickerModifiedCode.Length -1); else ! return tickerModifiedCode; } --- 59,68 ---- protected int numberOfExaminedReturns; ! static public string GetCleanTickerCode(string tickerCodeForLongOrShortTrade) { ! if(tickerCodeForLongOrShortTrade.StartsWith("-")) ! return tickerCodeForLongOrShortTrade.Substring(1,tickerCodeForLongOrShortTrade.Length -1); else ! return tickerCodeForLongOrShortTrade; } *************** *** 99,102 **** --- 100,106 ---- } + //setOfInitialTickers has to contain the + //ticker's symbol in the first column ! + public GenomeManagerForEfficientPortfolio(DataTable setOfInitialTickers, DateTime firstQuoteDate, *************** *** 107,114 **** { ! this.setOfTickers = setOfInitialTickers; ! this.originalNumOfTickers = setOfInitialTickers.Rows.Count; ! if(!this.setOfTickers.Columns.Contains("ArrayOfRatesOfReturn")) ! this.setOfTickers.Columns.Add("ArrayOfRatesOfReturn", System.Type.GetType("System.Array")); this.firstQuoteDate = firstQuoteDate; this.lastQuoteDate = lastQuoteDate; --- 111,117 ---- { ! this.setOfTickers = setOfInitialTickers; ! this.originalNumOfTickers = setOfInitialTickers.Rows.Count; ! this.constToDiscoverDuplicateGenes = this.originalNumOfTickers + 1; this.firstQuoteDate = firstQuoteDate; this.lastQuoteDate = lastQuoteDate; *************** *** 117,171 **** this.portfolioType = portfolioType; this.setMinAndMaxValueForGenes(); - this.set_SetOfInitialTickers(); - - } - - - private void set_SetOfInitialTickers() - { - - if(this.portfolioType == PortfolioType.ShortAndLong) - { - for(int i = 0;i<this.originalNumOfTickers;i++) - { - string ticker = (string)this.setOfTickers.Rows[i][0]; - DataRow newRow = this.setOfTickers.NewRow(); - newRow[0] = "-" + ticker; - this.setOfTickers.Rows.Add(newRow); - //so, if row[i][0]="TICKER" - //row[i+originalNumOfTickers][0]="-TICKER" - } - } } private void setMinAndMaxValueForGenes() { - //each genes is the index for the setOfTickers table this.minValueForGenes = 0; ! ! if(this.portfolioType == PortfolioType.OnlyLong || ! this.portfolioType == PortfolioType.OnlyShort) ! this.maxValueForGenes = this.setOfTickers.Rows.Count - 1; ! else//ShortAndLong ! this.maxValueForGenes = this.setOfTickers.Rows.Count*2 - 1; } ! ! ! protected float getCoefficient(string ticker) { ! float returnValue; ! if(ticker.StartsWith("-")) ! //if the first char is "-" ! //each element of the array of rates of return is ! //multiplied by -1 ! returnValue = -1; ! else ! returnValue = 1; ! ! return returnValue; ! } ! protected virtual double getFitnessValue_calculate() { --- 120,154 ---- this.portfolioType = portfolioType; this.setMinAndMaxValueForGenes(); } private void setMinAndMaxValueForGenes() { this.minValueForGenes = 0; ! this.maxValueForGenes = this.originalNumOfTickers - 1; ! ! if(this.portfolioType == PortfolioType.ShortAndLong) ! this.minValueForGenes = - this.originalNumOfTickers; ! //if gene g is negative, it refers to the ticker Abs(g+1) to be shorted } ! //this protected method has to be called by inherited genome ! //managers (open to close or close to close) ! //only after all initializations provided ! //by their respective constructors ! protected void retrieveData() { ! this.setOfCandidates = new CandidateProperties[setOfTickers.Rows.Count]; ! for(int i = 0; i<setOfTickers.Rows.Count; i++) ! { ! string ticker = (string)setOfTickers.Rows[i][0]; ! this.setOfCandidates[i] = new CandidateProperties(ticker, ! this.getArrayOfRatesOfReturn(ticker)); ! } } ! ! //implementation of IGenomeManager ! ! #region GetFitnessValue ! protected virtual double getFitnessValue_calculate() { *************** *** 216,224 **** } public Genome[] GetChilds(Genome parent1, Genome parent2) { return GenomeManagement.MixGenesWithoutDuplicates(parent1, parent2, ! this.originalNumOfTickers); } --- 199,209 ---- } + #endregion + public Genome[] GetChilds(Genome parent1, Genome parent2) { return GenomeManagement.MixGenesWithoutDuplicates(parent1, parent2, ! this.constToDiscoverDuplicateGenes); } *************** *** 228,237 **** // the others already stored in the given genome int returnValue = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, ! genome.MaxValueForGenes + 1); while(genome.HasGene(returnValue) || ! genome.HasGene(returnValue + this.originalNumOfTickers) || ! genome.HasGene(returnValue - this.originalNumOfTickers) ) ! //the portfolio can't have a long position and a short position ! // for the same ticker { returnValue = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, --- 213,221 ---- // the others already stored in the given genome int returnValue = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, ! genome.MaxValueForGenes + 1); while(genome.HasGene(returnValue) || ! genome.HasGene(returnValue + this.constToDiscoverDuplicateGenes) || ! genome.HasGene(returnValue - this.constToDiscoverDuplicateGenes) ) ! //the portfolio can't have a long position and a short one for the same ticker { returnValue = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, *************** *** 241,245 **** return returnValue; } - public void Mutate(Genome genome, double mutationRate) --- 225,228 ---- *************** *** 251,256 **** int genePositionToBeMutated = GenomeManagement.RandomGenerator.Next(genome.Size); while(genome.HasGene(newValueForGene) || ! genome.HasGene(newValueForGene + this.originalNumOfTickers) || ! genome.HasGene(newValueForGene - this.originalNumOfTickers) ) //the efficient portfolio, in this implementation, // can't have a long position and a short position --- 234,239 ---- int genePositionToBeMutated = GenomeManagement.RandomGenerator.Next(genome.Size); while(genome.HasGene(newValueForGene) || ! genome.HasGene(newValueForGene + this.constToDiscoverDuplicateGenes) || ! genome.HasGene(newValueForGene - this.constToDiscoverDuplicateGenes) ) //the efficient portfolio, in this implementation, // can't have a long position and a short position *************** *** 258,284 **** { newValueForGene = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, ! genome.MaxValueForGenes +1); } GenomeManagement.MutateOneGene(genome, mutationRate, ! genePositionToBeMutated, newValueForGene); } public virtual object Decode(Genome genome) { ! string sequenceOfTickers = ""; ! object returnValue; ! foreach(int index in genome.Genes()) { ! sequenceOfTickers += (string)this.setOfTickers.Rows[index][0] + ";" ; } ! returnValue = sequenceOfTickers; ! returnValue += "(rate: " + this.RateOfReturn + " std: " + ! System.Math.Sqrt(this.Variance) + ")"; ! return returnValue; } // end of implementation of IGenomeManager #region old implementation for variance computation ! protected double getPortfolioVariance(int[] tickerIdx) { --- 241,282 ---- { newValueForGene = GenomeManagement.RandomGenerator.Next(genome.MinValueForGenes, ! genome.MaxValueForGenes + 1); } GenomeManagement.MutateOneGene(genome, mutationRate, ! genePositionToBeMutated, newValueForGene); } + #region Decode + + private string decode_getTickerCodeForLongOrShortTrade(int geneValue) + { + string initialCharForTickerCode = ""; + int position = geneValue; + if(geneValue<0) + { + position = Math.Abs(geneValue + 1); + initialCharForTickerCode = "-"; + } + return initialCharForTickerCode + this.setOfCandidates[position].Ticker; + } + public virtual object Decode(Genome genome) { ! string[] arrayOfTickers = new string[genome.Genes().Length]; ! int indexOfTicker; ! for(int index = 0; index < genome.Genes().Length; index++) { ! indexOfTicker = (int)genome.Genes().GetValue(index); ! arrayOfTickers[index] = this.decode_getTickerCodeForLongOrShortTrade(indexOfTicker); } ! return arrayOfTickers; ! } + #endregion + // end of implementation of IGenomeManager #region old implementation for variance computation ! /* protected double getPortfolioVariance(int[] tickerIdx) { *************** *** 322,346 **** return returnValue; } ! #endregion ! ! ! protected void retrieveData() ! { ! foreach(DataRow row in this.setOfTickers.Rows) ! { ! // ! float[] arrayOfRatesOfReturn = this.getArrayOfRatesOfReturn((string)row[0]); ! if(arrayOfRatesOfReturn == null) ! row["ArrayOfRatesOfReturn"] = DBNull.Value; ! else ! row["ArrayOfRatesOfReturn"] = arrayOfRatesOfReturn; ! } ! } ! //this protected method must be overriden by inherited classes //specifing the type of rates of return that have to //be analyzed - protected virtual float[] getArrayOfRatesOfReturn(string ticker) { --- 320,330 ---- return returnValue; } ! */ #endregion ! ! //this protected method must be overriden by inherited classes //specifing the type of rates of return that have to //be analyzed protected virtual float[] getArrayOfRatesOfReturn(string ticker) { *************** *** 348,378 **** return returnValue; } ! ! ! protected double[] getPortfolioRatesOfReturn(int[] tickerIdx) { double[] returnValue = new double[this.numberOfExaminedReturns]; - float[] tickerRatesOfReturn; for(int i = 0; i<returnValue.Length; i++) { ! foreach(int idx in tickerIdx) ! { ! if(this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"] is System.DBNull) ! //the idx points to a ticker for which short returns are ! //to be examined ! tickerRatesOfReturn = ! (float[])this.setOfTickers.Rows[idx - this.originalNumOfTickers]["ArrayOfRatesOfReturn"]; ! else ! tickerRatesOfReturn = (float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]; ! ! returnValue[i] += ! this.getCoefficient((string)this.setOfTickers.Rows[idx][0])* ! tickerRatesOfReturn[i]/this.genomeSize; ! //the investment is assumed to be equally divided for each ticker ! } } return returnValue; - } } --- 332,373 ---- return returnValue; } ! ! #region getPortfolioRatesOfReturn ! ! private int getPortfolioRatesOfReturn_getRateOfTickerToBeAddedToTheArray_getPositionInArray(int geneValueForTickerIdx) ! { ! int position = geneValueForTickerIdx; ! if(geneValueForTickerIdx<0) ! position = Math.Abs(geneValueForTickerIdx + 1); ! return position; ! } ! ! private float getPortfolioRatesOfReturn_getRateOfTickerToBeAddedToTheArray(int tickerIdx, ! int arrayElementPosition) ! { ! bool longReturns = false; ! if(tickerIdx > 0) ! //the tickerIdx points to a ticker for which long returns are to be examined ! longReturns = true; ! int position = this.getPortfolioRatesOfReturn_getRateOfTickerToBeAddedToTheArray_getPositionInArray(tickerIdx); ! this.setOfCandidates[position].LongRatesOfReturn = longReturns; ! float[] arrayOfRatesOfReturn = this.setOfCandidates[position].ArrayOfRatesOfReturn; ! return (arrayOfRatesOfReturn[arrayElementPosition]/this.GenomeSize); ! //the investment is assumed to be equally divided for each ticker ! } ! ! protected double[] getPortfolioRatesOfReturn(int[] tickersIdx) { double[] returnValue = new double[this.numberOfExaminedReturns]; for(int i = 0; i<returnValue.Length; i++) { ! foreach(int tickerIdx in tickersIdx) ! returnValue[i] += ! this.getPortfolioRatesOfReturn_getRateOfTickerToBeAddedToTheArray(tickerIdx,i); } return returnValue; } + + #endregion } |