[Quantproject-developers] QuantProject/b4_Business/a2_Strategies/Optimizing/Decoding BasicDecoderF
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2008-02-06 23:11:33
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/Optimizing/Decoding In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv30261/a2_Strategies/Optimizing/Decoding Added Files: BasicDecoderForTestingPositions.cs DecoderForTestingPositionsWithBalancedWeights.cs Log Message: Added two general IDecoderForTestingPositions, that should be inherited by IDecoderForTestingPositions specific to strategies --- NEW FILE: DecoderForTestingPositionsWithBalancedWeights.cs --- /* QuantProject - Quantitative Finance Library DecoderForTestingPositionsWithBalancedWeights.cs Copyright (C) 2008 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.Business.Strategies; namespace QuantProject.Business.Strategies.Optimizing.Decoding { /// <summary> /// Decodes optimization candidates to a /// TestingPositions /// In this implementation, weights are balanced on volatility base /// </summary> public class DecoderForTestingPositionsWithBalancedWeights : BasicDecoderForTestingPositions { public DecoderForTestingPositionsWithBalancedWeights() { } protected override double[] getWeights() { return WeightedPositions.GetBalancedWeights( this.decodeSignedTickers(), this.returnsManager ); } } } --- NEW FILE: BasicDecoderForTestingPositions.cs --- /* QuantProject - Quantitative Finance Library BasicDecoderForTestingPositions.cs Copyright (C) 2008 Glauco Siliprandi 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.Decoding; using QuantProject.Business.Financial.Accounting; using QuantProject.Business.Strategies.Eligibles; using QuantProject.Business.Strategies.OutOfSample; using QuantProject.Business.Strategies.ReturnsManagement; namespace QuantProject.Business.Strategies.Optimizing.Decoding { /// <summary> /// Decodes optimization candidates to a plain /// TestingPositions /// In this implementation, with the encoded items /// can be decoded only tickers /// </summary> public class BasicDecoderForTestingPositions : IDecoderForTestingPositions { protected int[] tickerRelatedGeneValues; protected int[] encoded; protected EligibleTickers eligibleTickers; protected ReturnsManager returnsManager; public BasicDecoderForTestingPositions() { } #region decodeSignedTickers private void decodeSignedTicker_checkParameters( int geneValue ) { if ( geneValue >= this.eligibleTickers.Count ) throw new Exception( "geneValue is too (positive) large for eligibleTickers !!" ); if ( geneValue < -this.eligibleTickers.Count ) throw new Exception( "geneValue is too (negative) large for eligibleTickers !!" ); } private SignedTicker decodeSignedTickers( int i ) { int signedTickerCode = this.tickerRelatedGeneValues[ i ]; SignedTicker signedTicker; string ticker; decodeSignedTicker_checkParameters( signedTickerCode ); if ( signedTickerCode >= 0 ) { // long ticker ticker = this.eligibleTickers[ signedTickerCode ]; signedTicker = new SignedTicker( ticker , PositionType.Long ); } else { // short ticker ticker = this.eligibleTickers[ -(signedTickerCode+1) ]; signedTicker = new SignedTicker( ticker , PositionType.Short ); } return signedTicker; } protected SignedTickers decodeSignedTickers() { SignedTickers signedTickers = new SignedTickers(); for( int i = 0 ; i < this.tickerRelatedGeneValues.Length ; i++ ) { SignedTicker signedTicker = this.decodeSignedTickers( i ); signedTickers.Add( signedTicker ); } return signedTickers; } #endregion decodeSignedTickers #region isDecodable private string[] getTickersForPositions() { SignedTickers signedTickersForPositions = this.decodeSignedTickers(); return signedTickersForPositions.Tickers; } private bool isDecodable() { return ( WeightedPositions.AreValidTickers( this.getTickersForPositions() ) ); } #endregion isDecodable #region decodeDecodable protected virtual double[] getWeights() { //in this implementation encoded doesn't contain //information for weights: so weights are all the same double[] weights = new double[this.encoded.Length]; for(int i = 0; i<weights.Length; i++) weights[i] = 1.0 / weights.Length; return weights; } protected virtual TestingPositions decodeDecodable() { SignedTickers signedTickers = this.decodeSignedTickers(); TestingPositions testingPositions = new TestingPositions( new WeightedPositions( this.getWeights(), signedTickers) ); return testingPositions; } #endregion decodeDecodable protected virtual void setTickerRelatedGeneValues() { this.tickerRelatedGeneValues = this.encoded; //in this implementation all encoded contains //information only for tickers } private void decode_updateProtectedMembers(int[] encoded , EligibleTickers eligibleTickers , ReturnsManager returnsManager) { this.encoded = encoded; this.eligibleTickers = eligibleTickers; this.returnsManager = returnsManager; } /// <summary> /// A positive array value means a long position. /// A negative array value means a short position. /// The positive value n means the same ticker as the value -(n+1). /// Thus, if there are p (>0) eligible tickers, array values should /// range from -p to p-1 /// </summary> /// <param name="encoded"></param> /// <returns></returns> public TestingPositions Decode(int[] encoded , EligibleTickers eligibleTickers , ReturnsManager returnsManager) { this.setTickerRelatedGeneValues(); this.decode_updateProtectedMembers(encoded , eligibleTickers , returnsManager); TestingPositions meaning = new TestingPositions(); if ( this.isDecodable() ) // encoded, normally a Genome, can be decoded to a TestingPositions meaning = this.decodeDecodable(); return meaning; } } } |