[Quantproject-developers] QuantProject/b7_Scripts/TickerSelectionTesting GenomeManagerForEfficientPo
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2005-05-04 18:29:09
|
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7317/b7_Scripts/TickerSelectionTesting Modified Files: GenomeManagerForEfficientPortfolio.cs Log Message: the variance and the average rate of return of portfolio are now computed in a different way (that seems to be right, now ...) Index: GenomeManagerForEfficientPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/TickerSelectionTesting/GenomeManagerForEfficientPortfolio.cs,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** GenomeManagerForEfficientPortfolio.cs 26 Apr 2005 19:02:13 -0000 1.7 --- GenomeManagerForEfficientPortfolio.cs 4 May 2005 18:29:00 -0000 1.8 *************** *** 167,188 **** { double returnValue = 0; ! double portfolioVariance = this.getPortfolioVariance(genome.Genes()); ! double portfolioRateOfReturn = this.getPortfolioRateOfReturn(genome.Genes()); ! ! if(!Double.IsInfinity(portfolioVariance) || ! !Double.IsInfinity(portfolioRateOfReturn) || ! !Double.IsNaN(portfolioVariance) || ! !Double.IsNaN(portfolioRateOfReturn)) ! //variance and rate of return are ! //double values computed in the right way { this.variance = portfolioVariance; ! this.rateOfReturn = portfolioRateOfReturn; ! NormalDistribution normal = new NormalDistribution(portfolioRateOfReturn, Math.Sqrt(portfolioVariance)); if(this.portfolioType == PortfolioType.OnlyLong || this.portfolioType == PortfolioType.ShortAndLong) //the genome fitness is evaluated as if //the portfolio was long ! //ALTERNATIVE: returnValue = normal.GetProbability(this.targetPerformance*0.75,this.targetPerformance*1.25); returnValue = 1.0 - normal.GetProbability(this.targetPerformance); else//only short orders are permitted --- 167,197 ---- { double returnValue = 0; ! //OLD IMPLEMENTATION double portfolioRateOfReturn = this.getPortfolioRateOfReturn(genome.Genes()); ! //OLD IMPLEMENTATION double portfolioVariance = this.getPortfolioVariance(genome.Genes()); ! double[] portfolioRatesOfReturn = this.getPortfolioRatesOfReturn(genome.Genes()); ! double averagePortfolioRateOfReturn = ! BasicFunctions.SimpleAverage(portfolioRatesOfReturn); ! ! double portfolioVariance = ! BasicFunctions.Variance(portfolioRatesOfReturn); ! ! if(!Double.IsInfinity(portfolioVariance) && ! !Double.IsInfinity(averagePortfolioRateOfReturn) && ! !Double.IsNaN(portfolioVariance) && ! !Double.IsNaN(averagePortfolioRateOfReturn)) ! //both variance and rate of return are ! //double values computed in the right way: ! // so it's possible to assign fitness using normal distribution class { this.variance = portfolioVariance; ! this.rateOfReturn = averagePortfolioRateOfReturn; ! NormalDistribution normal = ! new NormalDistribution(this.rateOfReturn, ! Math.Sqrt(this.variance)); if(this.portfolioType == PortfolioType.OnlyLong || this.portfolioType == PortfolioType.ShortAndLong) //the genome fitness is evaluated as if //the portfolio was long ! //returnValue = normal.GetProbability(this.targetPerformance*0.75,this.targetPerformance*1.25); returnValue = 1.0 - normal.GetProbability(this.targetPerformance); else//only short orders are permitted *************** *** 250,277 **** protected double getPortfolioVariance(int[] tickerIdx) { ! double sumOfVariances = this.getSumOfVariances(tickerIdx); ! double sumOfCovariances = this.getSumOfCovariances(tickerIdx); double returnValue = sumOfVariances + sumOfCovariances; return returnValue; } ! protected double getSumOfVariances(int[] tickerIdx) { double returnValue = 0; ! double tickerCoeff = 1.0/this.genomeSize; foreach(int idx in tickerIdx) { ! returnValue += BasicFunctions.Variance((float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]); } - returnValue = returnValue * tickerCoeff * tickerCoeff; return returnValue; } ! protected double getSumOfCovariances(int[] tickerIdx) { double returnValue = 0; float[] ticker_i; float[] ticker_j; ! double tickerCoeff = 1.0/this.genomeSize; for(int i = 0; i<this.genomeSize ; i++) { --- 259,286 ---- protected double getPortfolioVariance(int[] tickerIdx) { ! double sumOfVariances = this.getWeightedSumOfVariances(tickerIdx); ! double sumOfCovariances = this.getWeightedSumOfCovariances(tickerIdx); double returnValue = sumOfVariances + sumOfCovariances; return returnValue; } ! protected double getWeightedSumOfVariances(int[] tickerIdx) { double returnValue = 0; ! double tickerCoeff = 1.0/this.genomeSize; foreach(int idx in tickerIdx) { ! returnValue += tickerCoeff * tickerCoeff * ! BasicFunctions.Variance((float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]); } return returnValue; } ! protected double getWeightedSumOfCovariances(int[] tickerIdx) { double returnValue = 0; float[] ticker_i; float[] ticker_j; ! double tickerCoeff = 1.0/this.genomeSize; for(int i = 0; i<this.genomeSize ; i++) { *************** *** 282,290 **** { ticker_j = (float[])this.setOfTickers.Rows[j]["ArrayOfRatesOfReturn"]; ! returnValue += BasicFunctions.CoVariance(ticker_i, ticker_j); } } } - returnValue = returnValue * tickerCoeff * tickerCoeff; return returnValue; } --- 291,299 ---- { ticker_j = (float[])this.setOfTickers.Rows[j]["ArrayOfRatesOfReturn"]; ! returnValue += tickerCoeff * tickerCoeff * ! BasicFunctions.CoVariance(ticker_i, ticker_j); } } } return returnValue; } *************** *** 321,328 **** return ratesOfReturns; } ! ! ! ! protected double getPortfolioRateOfReturn(int[] tickerIdx) { --- 330,334 ---- return ratesOfReturns; } ! protected double getPortfolioRateOfReturn(int[] tickerIdx) { *************** *** 330,338 **** foreach(int idx in tickerIdx) { ! returnValue += BasicFunctions.SimpleAverage((float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]); } //the investment is assumed to be equally divided return (returnValue/this.GenomeSize); } } --- 336,365 ---- foreach(int idx in tickerIdx) { ! returnValue += ! BasicFunctions.SimpleAverage((float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]); } //the investment is assumed to be equally divided return (returnValue/this.GenomeSize); + + } + + protected double[] getPortfolioRatesOfReturn(int[] tickerIdx) + { + int numberOfExaminedReturns = + ((float[])this.setOfTickers.Rows[tickerIdx[0]]["ArrayOfRatesOfReturn"]).Length; + double[] returnValue = new double[numberOfExaminedReturns]; + for(int i = 0; i<returnValue.Length; i++) + { + foreach(int idx in tickerIdx) + { + float[] tickerRatesOfReturn = (float[])this.setOfTickers.Rows[idx]["ArrayOfRatesOfReturn"]; + returnValue[i] += tickerRatesOfReturn[i]/this.genomeSize; + //the investment is assumed to be equally divided for each ticker + } + } + return returnValue; + } + } |