[Quantproject-developers] QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WeightedPositio
Brought to you by:
glauco_1
Update of /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WeightedPositionsChoosers/WFLagGeneticFixedPortfolioWithNormalDrivingAndPortfolio In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv3094/b7_Scripts/WalkForwardTesting/WalkForwardLag/WeightedPositionsChoosers/WFLagGeneticFixedPortfolioWithNormalDrivingAndPortfolio Modified Files: WFLagGenomeManagerForFixedPortfolioWithNormalDrivingAndPortfolio.cs Log Message: Bug fixed: now driving positions are actually balanced for equal volatility Index: WFLagGenomeManagerForFixedPortfolioWithNormalDrivingAndPortfolio.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b7_Scripts/WalkForwardTesting/WalkForwardLag/WeightedPositionsChoosers/WFLagGeneticFixedPortfolioWithNormalDrivingAndPortfolio/WFLagGenomeManagerForFixedPortfolioWithNormalDrivingAndPortfolio.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** WFLagGenomeManagerForFixedPortfolioWithNormalDrivingAndPortfolio.cs 7 Aug 2007 16:55:22 -0000 1.4 --- WFLagGenomeManagerForFixedPortfolioWithNormalDrivingAndPortfolio.cs 27 Aug 2007 22:24:21 -0000 1.5 *************** *** 28,31 **** --- 28,32 ---- using QuantProject.ADT.Optimizing.Genetic; using QuantProject.ADT.Statistics; + using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Strategies; using QuantProject.Business.Strategies.ReturnsManagement; *************** *** 60,64 **** // private DateTime lastOptimizationDate; ! private double minimumPositionWeight; // protected WFLagCandidates wFLagCandidates; --- 61,65 ---- // private DateTime lastOptimizationDate; ! // private double minimumPositionWeight; // protected WFLagCandidates wFLagCandidates; *************** *** 114,118 **** this.timeLineForOptimization = timeLineForOptimization; ! this.minimumPositionWeight = 0.2; // TO DO this value should become a constructor parameter this.equityEvaluator = equityEvaluator; --- 115,119 ---- this.timeLineForOptimization = timeLineForOptimization; ! // this.minimumPositionWeight = 0.2; // TO DO this value should become a constructor parameter this.equityEvaluator = equityEvaluator; *************** *** 289,296 **** { int genePosition = firstGenePosition + i; ! genes[ i ] = this.getTickerIndex( genome , genePosition ); } return genes; } private int[] getTickerRelatedGeneValuesForDrivingPositions( Genome genome ) --- 290,298 ---- { int genePosition = firstGenePosition + i; ! genes[ i ] = genome.Genes()[ genePosition ]; } return genes; } + #region getSignedTickersForDrivingPositions private int[] getTickerRelatedGeneValuesForDrivingPositions( Genome genome ) *************** *** 298,308 **** return this.getGenes( genome , 0 , this.numberOfDrivingPositions ); } ! // private int[] getTickerRelatedGeneValuesForPortfolioPositions( ! // Genome genome ) ! // { ! // return this.getGenes( genome , this.numberOfDrivingPositions , ! // this.numberOfPortfolioPositions ); ! // } ! private void decodeTicker_checkParameters( int geneValue , DataTable eligibleTickers ) { if ( geneValue >= eligibleTickers.Rows.Count ) --- 300,305 ---- return this.getGenes( genome , 0 , this.numberOfDrivingPositions ); } ! private void decodeSignedTicker_checkParameters( ! int geneValue , DataTable eligibleTickers ) { if ( geneValue >= eligibleTickers.Rows.Count ) *************** *** 311,344 **** throw new Exception( "geneValue is too (negative) large for eligibleTickers !!" ); } ! private string decodeTicker( int geneValue , DataTable eligibleTickers ) { string ticker; ! decodeTicker_checkParameters( geneValue , eligibleTickers ); if ( geneValue >= 0 ) // long ticker ticker = ( string )eligibleTickers.Rows[ geneValue ][ 0 ]; else // short ticker ticker = ( string )eligibleTickers.Rows[ -(geneValue+1) ][ 0 ]; ! return ticker; } ! private string[] decodeTickers( int[] tickerRelatedGeneValues , DataTable eligibleTickers ) { ! string[] tickers = new string[ tickerRelatedGeneValues.Length ]; for( int i = 0 ; i < tickerRelatedGeneValues.Length ; i++ ) { ! int currentGeneValue = tickerRelatedGeneValues[ i ]; ! tickers[ i ] = decodeTicker( currentGeneValue , eligibleTickers ); ! ! tickers[ i ] = ! ( string )eligibleTickers.Rows[ currentGeneValue ][ 0 ]; } ! return tickers; } ! private string[] getTickersForDrivingPositions( Genome genome ) { int[] geneValues = this.getTickerRelatedGeneValuesForDrivingPositions( genome ); ! return this.decodeTickers( geneValues , this.eligibleTickersForDrivingWeightedPositions ); } #region getTickersForPortfolioPositions --- 308,359 ---- throw new Exception( "geneValue is too (negative) large for eligibleTickers !!" ); } ! private SignedTicker decodeSignedTicker( int geneValue , ! DataTable eligibleTickers ) { + SignedTicker signedTicker; string ticker; ! decodeSignedTicker_checkParameters( geneValue , eligibleTickers ); if ( geneValue >= 0 ) + { // long ticker ticker = ( string )eligibleTickers.Rows[ geneValue ][ 0 ]; + signedTicker = new SignedTicker( ticker , PositionType.Long ); + } else + { // short ticker ticker = ( string )eligibleTickers.Rows[ -(geneValue+1) ][ 0 ]; ! signedTicker = new SignedTicker( ticker , PositionType.Short ); ! } ! return signedTicker; } ! private SignedTicker decodeSignedTickers( int i , ! int[] tickerRelatedGeneValues , DataTable eligibleTickers ) ! { ! int currentGeneValue = tickerRelatedGeneValues[ i ]; ! return this.decodeSignedTicker( currentGeneValue , eligibleTickers ); ! } ! private SignedTickers decodeSignedTickers( int[] tickerRelatedGeneValues , DataTable eligibleTickers ) { ! SignedTickers signedTickers = new SignedTickers(); for( int i = 0 ; i < tickerRelatedGeneValues.Length ; i++ ) { ! SignedTicker signedTicker = this.decodeSignedTickers( ! i , tickerRelatedGeneValues , eligibleTickers ); ! signedTickers.Add( signedTicker ); } ! return signedTickers; } ! private SignedTickers getSignedTickersForDrivingPositions( Genome genome ) { int[] geneValues = this.getTickerRelatedGeneValuesForDrivingPositions( genome ); ! return this.decodeSignedTickers( geneValues , ! this.eligibleTickersForDrivingWeightedPositions ); ! } ! #endregion getSignedTickersForDrivingPositions ! private string[] getTickersForDrivingPositions( Genome genome ) ! { ! return this.getSignedTickersForDrivingPositions( genome ).Tickers; } #region getTickersForPortfolioPositions *************** *** 378,411 **** // return weightRelatedGeneValuesForDrivingPositions; // } ! private int getTickerIndex( Genome genome , int genePosition ) ! { ! int tickerIndex = genome.GetGeneValue( genePosition ); ! if ( tickerIndex < 0 ) ! // the position is short ! tickerIndex += -this.GetMinValueForGenes( genePosition ); ! return tickerIndex; ! } ! private double getWeight( Genome genome , int genePosition ) ! { ! double weight = 1; ! if ( genome.GetGeneValue( genePosition ) < 0 ) ! // the position is short ! weight = -1; ! return weight; ! } ! private double[] getWeights( Genome genome , int firstGenePosition , int numberOfGenes ) ! { ! double[] weights = new double[ numberOfGenes ]; ! for ( int i = 0 ; i < numberOfGenes ; i++ ) ! { ! int genePosition = firstGenePosition + i; ! weights[ i ] = this.getWeight( genome , genePosition ); ! } ! return weights; ! } ! private double[] getWeightsForDrivingPositions( Genome genome ) ! { ! return this.getWeights( genome , 0 , this.numberOfDrivingPositions ); ! } // private double[] getWeightsForPortfolioPositions( Genome genome ) // { --- 393,426 ---- // return weightRelatedGeneValuesForDrivingPositions; // } ! // private int getTickerIndex( Genome genome , int genePosition ) ! // { ! // int tickerIndex = genome.GetGeneValue( genePosition ); ! // if ( tickerIndex < 0 ) ! // // the position is short ! // tickerIndex += -this.GetMinValueForGenes( genePosition ); ! // return tickerIndex; ! // } ! // private double getWeight( Genome genome , int genePosition ) ! // { ! // double weight = 1; ! // if ( genome.GetGeneValue( genePosition ) < 0 ) ! // // the position is short ! // weight = -1; ! // return weight; ! // } ! // private double[] getWeights( Genome genome , int firstGenePosition , int numberOfGenes ) ! // { ! // double[] weights = new double[ numberOfGenes ]; ! // for ( int i = 0 ; i < numberOfGenes ; i++ ) ! // { ! // int genePosition = firstGenePosition + i; ! // weights[ i ] = this.getWeight( genome , genePosition ); ! // } ! // return weights; ! // } ! // private double[] getWeightsForDrivingPositions( Genome genome ) ! // { ! // return this.getWeights( genome , 0 , this.numberOfDrivingPositions ); ! // } // private double[] getWeightsForPortfolioPositions( Genome genome ) // { *************** *** 414,480 **** // } #region decodeWeightedPositions ! #region getNormalizedWeightValues ! private double getAdditionalWeight( int weightRelatedGeneValue ) ! { ! double midrangeValue = ( ! this.GetMinValueForGenes( 0 ) + this.GetMaxValueForGenes( 0 ) ) / 2; ! double singleWeightFreeRange = 1 - this.minimumPositionWeight; ! double scaleRange = Convert.ToDouble( ! this.GetMinValueForGenes( 0 ) - this.GetMaxValueForGenes( 0 ) ); ! double nonScaledAdditionalWeight = Convert.ToDouble( weightRelatedGeneValue ) - ! midrangeValue; ! double scaledAdditionalWeight = ! nonScaledAdditionalWeight * singleWeightFreeRange / scaleRange; ! return scaledAdditionalWeight; ! } ! private double getNonNormalizedWeightValue( int weightRelatedGeneValue ) ! { ! double additionalWeight = this.getAdditionalWeight( weightRelatedGeneValue ); ! double nonNormalizedWeightValue = 0; ! if ( additionalWeight >= 0 ) ! // the gene value represents a long position ! nonNormalizedWeightValue = this.minimumPositionWeight + additionalWeight; ! else ! // additionalWeight < 0 , i.e. the gene value represents a short position ! nonNormalizedWeightValue = -this.minimumPositionWeight + additionalWeight; ! return nonNormalizedWeightValue; ! } ! private double[] getNonNormalizedWeightValues( int[] weightRelatedGeneValues ) ! { ! double[] nonNormalizedWeightValues = new double[ weightRelatedGeneValues.Length ]; ! for ( int i = 0 ; i < weightRelatedGeneValues.Length ; i++ ) ! nonNormalizedWeightValues[ i ] = ! this.getNonNormalizedWeightValue( weightRelatedGeneValues[ i ] ); ! return nonNormalizedWeightValues; ! } ! private double getNormalizingFactor( double[] nonNormalizedWeightValues ) ! { ! // the absolute value for each nonNormalizedWeightValue is between ! // this.minimumPositionWeight and 1 ! double totalForNonNormalizedWeightValues = BasicFunctions.SumOfAbs( nonNormalizedWeightValues ); ! double normalizingFactor = 1 / totalForNonNormalizedWeightValues; ! return normalizingFactor; ! } ! private double[] getNormalizedWeightValues( double[] nonNormalizedWeightValues , ! double normalizingFactor ) ! { ! return BasicFunctions.MultiplyBy( nonNormalizedWeightValues , normalizingFactor ); ! } ! private double[] getNormalizedWeights( double[] nonNormalizedWeights ) ! { ! double normalizingFactor = this.getNormalizingFactor( nonNormalizedWeights ); ! return getNormalizedWeightValues( nonNormalizedWeights , normalizingFactor ); ! } ! // private double[] getNormalizedWeightValues( int[] weightRelatedGeneValues ) // { ! // double[] nonNormalizedWeightValues = ! // this.getNonNormalizedWeightValues( weightRelatedGeneValues ); ! // return this.getNormalizedWeightValues( nonNormalizedWeightValues ); // } ! #endregion private WeightedPositions decodeWeightedPositions( double[] weights , string[] tickers , DataTable eligibleTickers ) { ! double[] normalizedWeights = this.getNormalizedWeights( weights ); WeightedPositions weightedPositions = new WeightedPositions( normalizedWeights , tickers ); --- 429,496 ---- // } #region decodeWeightedPositions ! // #region getNormalizedWeightValues ! // private double getAdditionalWeight( int weightRelatedGeneValue ) // { ! // double midrangeValue = ( ! // this.GetMinValueForGenes( 0 ) + this.GetMaxValueForGenes( 0 ) ) / 2; ! // double singleWeightFreeRange = 1 - this.minimumPositionWeight; ! // double scaleRange = Convert.ToDouble( ! // this.GetMinValueForGenes( 0 ) - this.GetMaxValueForGenes( 0 ) ); ! // double nonScaledAdditionalWeight = Convert.ToDouble( weightRelatedGeneValue ) - ! // midrangeValue; ! // double scaledAdditionalWeight = ! // nonScaledAdditionalWeight * singleWeightFreeRange / scaleRange; ! // return scaledAdditionalWeight; // } ! // private double getNonNormalizedWeightValue( int weightRelatedGeneValue ) ! // { ! // double additionalWeight = this.getAdditionalWeight( weightRelatedGeneValue ); ! // double nonNormalizedWeightValue = 0; ! // if ( additionalWeight >= 0 ) ! // // the gene value represents a long position ! // nonNormalizedWeightValue = this.minimumPositionWeight + additionalWeight; ! // else ! // // additionalWeight < 0 , i.e. the gene value represents a short position ! // nonNormalizedWeightValue = -this.minimumPositionWeight + additionalWeight; ! // return nonNormalizedWeightValue; ! // } ! // private double[] getNonNormalizedWeightValues( int[] weightRelatedGeneValues ) ! // { ! // double[] nonNormalizedWeightValues = new double[ weightRelatedGeneValues.Length ]; ! // for ( int i = 0 ; i < weightRelatedGeneValues.Length ; i++ ) ! // nonNormalizedWeightValues[ i ] = ! // this.getNonNormalizedWeightValue( weightRelatedGeneValues[ i ] ); ! // return nonNormalizedWeightValues; ! // } ! // private double getNormalizingFactor( double[] nonNormalizedWeightValues ) ! // { ! // // the absolute value for each nonNormalizedWeightValue is between ! // // this.minimumPositionWeight and 1 ! // double totalForNonNormalizedWeightValues = BasicFunctions.SumOfAbs( nonNormalizedWeightValues ); ! // double normalizingFactor = 1 / totalForNonNormalizedWeightValues; ! // return normalizingFactor; ! // } ! // private double[] getNormalizedWeightValues( double[] nonNormalizedWeightValues , ! // double normalizingFactor ) ! // { ! // return BasicFunctions.MultiplyBy( nonNormalizedWeightValues , normalizingFactor ); ! // } ! // private double[] getNormalizedWeights( double[] nonNormalizedWeights ) ! // { ! // double normalizingFactor = this.getNormalizingFactor( nonNormalizedWeights ); ! // return getNormalizedWeightValues( nonNormalizedWeights , normalizingFactor ); ! // } ! //// private double[] getNormalizedWeightValues( int[] weightRelatedGeneValues ) ! //// { ! //// double[] nonNormalizedWeightValues = ! //// this.getNonNormalizedWeightValues( weightRelatedGeneValues ); ! //// return this.getNormalizedWeightValues( nonNormalizedWeightValues ); ! //// } ! // #endregion private WeightedPositions decodeWeightedPositions( double[] weights , string[] tickers , DataTable eligibleTickers ) { ! double[] normalizedWeights = ! WeightedPositions.GetNormalizedWeights( weights ); WeightedPositions weightedPositions = new WeightedPositions( normalizedWeights , tickers ); *************** *** 482,496 **** } #endregion private WeightedPositions decodeDrivingWeightedPositions( Genome genome ) { ! string[] tickersForDrivingPositions = this.getTickersForDrivingPositions( genome ); ! double[] weightsForDrivingPositions = this.getWeightsForDrivingPositions( genome ); ! return decodeWeightedPositions( ! weightsForDrivingPositions , ! tickersForDrivingPositions , ! this.eligibleTickersForDrivingWeightedPositions ); } ! private double[] getNormalizedWeightsForPortfolioPositions() { return WeightedPositions.GetBalancedWeights( this.portfolioSignedTickers , --- 498,530 ---- } #endregion + private double[] getNormalizedWeightsForDrivingPositions( + string[] tickersForDrivingPositions ) + { + return WeightedPositions.GetBalancedWeights( this.portfolioSignedTickers , + this.closeToCloseReturnsManager ); + } private WeightedPositions decodeDrivingWeightedPositions( Genome genome ) { ! SignedTickers signedTickers = ! this.getSignedTickersForDrivingPositions( genome ); ! double[] balancedWeightsForDrivingPositions = ! WeightedPositions.GetBalancedWeights( signedTickers , ! this.closeToCloseReturnsManager ); ! WeightedPositions weightedPositions = new WeightedPositions( ! balancedWeightsForDrivingPositions , signedTickers.Tickers ); ! // string[] tickersForDrivingPositions = ! // this.getTickersForDrivingPositions( genome ); ! // double[] weightsForDrivingPositions = ! // this.getWeightsForDrivingPositions( genome ); ! // double[] balancedWeightsForDrivingPositions = ! // WeightedPositions.GetBalancedWeights( qui!!!! , weightsForDrivingPositions ); ! // return this.decodeWeightedPositions( ! // weightsForDrivingPositions , ! // tickersForDrivingPositions , ! // this.eligibleTickersForDrivingWeightedPositions ); ! return weightedPositions; } ! private double[] getBalancedWeightsForPortfolioPositions() { return WeightedPositions.GetBalancedWeights( this.portfolioSignedTickers , *************** *** 502,512 **** string[] tickersForPortfolioPositions = this.getTickersForPortfolioPositions(); double[] portfolioPositionsWeights = ! this.getNormalizedWeightsForPortfolioPositions(); ! return decodeWeightedPositions( portfolioPositionsWeights , tickersForPortfolioPositions , this.eligibleTickersForPortfolioWeightedPositions ); } ! private WFLagWeightedPositions dedcodeDecodableGenome( Genome genome ) { WeightedPositions drivingWeightedPositions = --- 536,546 ---- string[] tickersForPortfolioPositions = this.getTickersForPortfolioPositions(); double[] portfolioPositionsWeights = ! this.getBalancedWeightsForPortfolioPositions(); ! return this.decodeWeightedPositions( portfolioPositionsWeights , tickersForPortfolioPositions , this.eligibleTickersForPortfolioWeightedPositions ); } ! private WFLagWeightedPositions decodeDecodableGenome( Genome genome ) { WeightedPositions drivingWeightedPositions = *************** *** 532,536 **** if ( this.isDecodable( genome ) ) // genome can be decoded to a WFLagWeightedPositions object ! meaning = this.dedcodeDecodableGenome( genome ); else // genome cannot be decoded to a WFLagWeightedPositions object --- 566,570 ---- if ( this.isDecodable( genome ) ) // genome can be decoded to a WFLagWeightedPositions object ! meaning = this.decodeDecodableGenome( genome ); else // genome cannot be decoded to a WFLagWeightedPositions object |