[Quantproject-developers] QuantProject/b4_Business/a2_Strategies/TickersRelationships CorrelationP
Brought to you by:
glauco_1
|
From: Glauco S. <gla...@us...> - 2008-08-18 21:13:27
|
Update of /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/TickersRelationships In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv12665/b4_Business/a2_Strategies/TickersRelationships Modified Files: CorrelationProvider.cs Log Message: - the code has been cleaned up to avoid warnings - standard indentation has been applied Index: CorrelationProvider.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b4_Business/a2_Strategies/TickersRelationships/CorrelationProvider.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CorrelationProvider.cs 8 Apr 2008 21:47:28 -0000 1.4 --- CorrelationProvider.cs 18 Aug 2008 21:13:19 -0000 1.5 *************** *** 3,7 **** CorrelationProvider.cs ! Copyright (C) 2008 Marco Milletti --- 3,7 ---- CorrelationProvider.cs ! Copyright (C) 2008 Marco Milletti *************** *** 19,23 **** 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.Data; --- 19,23 ---- 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.Data; *************** *** 34,44 **** namespace QuantProject.Business.Strategies.TickersRelationships { ! /// <summary> ! /// Abstract class that provides correlation's indexes two by two within a ! /// given set of tickers ! /// </summary> ! [Serializable] ! public abstract class CorrelationProvider ! { protected ReturnsManager returnsManager; protected string[] tickers; --- 34,44 ---- namespace QuantProject.Business.Strategies.TickersRelationships { ! /// <summary> ! /// Abstract class that provides correlation's indexes two by two within a ! /// given set of tickers ! /// </summary> ! [Serializable] ! public abstract class CorrelationProvider ! { protected ReturnsManager returnsManager; protected string[] tickers; *************** *** 53,57 **** private void correlationProvider_commonInitialization(string[] tickersToAnalyze, ! float minimumAbsoluteReturnValue, float maximumAbsoluteReturnValue) { this.tickers = tickersToAnalyze; --- 53,57 ---- private void correlationProvider_commonInitialization(string[] tickersToAnalyze, ! float minimumAbsoluteReturnValue, float maximumAbsoluteReturnValue) { this.tickers = tickersToAnalyze; *************** *** 79,101 **** /// <param name="benchmark">The benchmark used for computation /// of returns</param> ! public CorrelationProvider(string[] tickersToAnalyze, ! DateTime startDate, DateTime endDate, int returnIntervalLength, ! float minimumAbsoluteReturnValue, float maximumAbsoluteReturnValue, ! string benchmark) ! { this.correlationProvider_commonInitialization(tickersToAnalyze, ! minimumAbsoluteReturnValue, maximumAbsoluteReturnValue); this.setEndOfDayDatesTime(startDate,endDate); this.returnIntervalLength = returnIntervalLength; this.benchmark = benchmark; ! } public CorrelationProvider(string[] tickersToAnalyze, ! DateTime startDate, DateTime endDate, ! float minimumAbsoluteReturnValue, float maximumAbsoluteReturnValue, ! string benchmark) { this.correlationProvider_commonInitialization(tickersToAnalyze, ! minimumAbsoluteReturnValue, maximumAbsoluteReturnValue); this.setEndOfDayDatesTime(startDate,endDate); this.returnIntervalLength = 1; --- 79,101 ---- /// <param name="benchmark">The benchmark used for computation /// of returns</param> ! public CorrelationProvider(string[] tickersToAnalyze, ! DateTime startDate, DateTime endDate, int returnIntervalLength, ! float minimumAbsoluteReturnValue, float maximumAbsoluteReturnValue, ! string benchmark) ! { this.correlationProvider_commonInitialization(tickersToAnalyze, ! minimumAbsoluteReturnValue, maximumAbsoluteReturnValue); this.setEndOfDayDatesTime(startDate,endDate); this.returnIntervalLength = returnIntervalLength; this.benchmark = benchmark; ! } public CorrelationProvider(string[] tickersToAnalyze, ! DateTime startDate, DateTime endDate, ! float minimumAbsoluteReturnValue, float maximumAbsoluteReturnValue, ! string benchmark) { this.correlationProvider_commonInitialization(tickersToAnalyze, ! minimumAbsoluteReturnValue, maximumAbsoluteReturnValue); this.setEndOfDayDatesTime(startDate,endDate); this.returnIntervalLength = 1; *************** *** 114,126 **** /// have to be less than maximumAbsoluteReturnValue</param> public CorrelationProvider( string[] tickersToAnalyze, ! ReturnsManager returnsManager, ! float minimumAbsoluteReturnValue, float maximumAbsoluteReturnValue ) { this.correlationProvider_commonInitialization(tickersToAnalyze, ! minimumAbsoluteReturnValue, maximumAbsoluteReturnValue); this.returnsManager = returnsManager; } ! protected abstract void setEndOfDayDatesTime(DateTime startDate, DateTime endDate); --- 114,126 ---- /// have to be less than maximumAbsoluteReturnValue</param> public CorrelationProvider( string[] tickersToAnalyze, ! ReturnsManager returnsManager, ! float minimumAbsoluteReturnValue, float maximumAbsoluteReturnValue ) { this.correlationProvider_commonInitialization(tickersToAnalyze, ! minimumAbsoluteReturnValue, maximumAbsoluteReturnValue); this.returnsManager = returnsManager; } ! protected abstract void setEndOfDayDatesTime(DateTime startDate, DateTime endDate); *************** *** 135,163 **** for(int i = 0; i<firstTickerReturns.Length; i++) if( Math.Abs(firstTickerReturns[i]) >= this.minimumAbsoluteReturnValue && ! Math.Abs(firstTickerReturns[i]) <= this.maximumAbsoluteReturnValue && ! Math.Abs(secondTickerReturns[i]) >= this.minimumAbsoluteReturnValue && ! Math.Abs(secondTickerReturns[i]) <= this.maximumAbsoluteReturnValue ) ! { ! countSignificantReturns++; ! tempReturnValue[ countSignificantReturns - 1 ] = firstTickerReturns[ i ]; ! } returnValue = new float[countSignificantReturns]; ! Array.Copy(tempReturnValue, 0, returnValue, 0, countSignificantReturns); return returnValue; } protected double getOrderedTickersPearsonCorrelations_setCorrelations_getValue( ! int indexOfFirstTicker, int indexOfSecondTicker) { double returnValue; ! float[] firstTickerSignificantReturns = this.getOrderedTickersPearsonCorrelations_setCorrelations_getValue_getFilteredReturnsForFirstTicker( ! this.returnsManager.GetReturns(this.tickers[indexOfFirstTicker]), ! this.returnsManager.GetReturns(this.tickers[indexOfSecondTicker]) ); ! float[] secondTickerSignificantReturns = this.getOrderedTickersPearsonCorrelations_setCorrelations_getValue_getFilteredReturnsForFirstTicker( ! this.returnsManager.GetReturns(this.tickers[indexOfSecondTicker]), ! this.returnsManager.GetReturns(this.tickers[indexOfFirstTicker]) ); returnValue = BasicFunctions.PearsonCorrelationCoefficient( --- 135,163 ---- for(int i = 0; i<firstTickerReturns.Length; i++) if( Math.Abs(firstTickerReturns[i]) >= this.minimumAbsoluteReturnValue && ! Math.Abs(firstTickerReturns[i]) <= this.maximumAbsoluteReturnValue && ! Math.Abs(secondTickerReturns[i]) >= this.minimumAbsoluteReturnValue && ! Math.Abs(secondTickerReturns[i]) <= this.maximumAbsoluteReturnValue ) ! { ! countSignificantReturns++; ! tempReturnValue[ countSignificantReturns - 1 ] = firstTickerReturns[ i ]; ! } returnValue = new float[countSignificantReturns]; ! Array.Copy(tempReturnValue, 0, returnValue, 0, countSignificantReturns); return returnValue; } protected double getOrderedTickersPearsonCorrelations_setCorrelations_getValue( ! int indexOfFirstTicker, int indexOfSecondTicker) { double returnValue; ! float[] firstTickerSignificantReturns = this.getOrderedTickersPearsonCorrelations_setCorrelations_getValue_getFilteredReturnsForFirstTicker( ! this.returnsManager.GetReturns(this.tickers[indexOfFirstTicker]), ! this.returnsManager.GetReturns(this.tickers[indexOfSecondTicker]) ); ! float[] secondTickerSignificantReturns = this.getOrderedTickersPearsonCorrelations_setCorrelations_getValue_getFilteredReturnsForFirstTicker( ! this.returnsManager.GetReturns(this.tickers[indexOfSecondTicker]), ! this.returnsManager.GetReturns(this.tickers[indexOfFirstTicker]) ); returnValue = BasicFunctions.PearsonCorrelationCoefficient( *************** *** 167,171 **** if ( Double.IsNaN(returnValue) || Double.IsInfinity(returnValue) ) throw new MissingCorrelationException(this.tickers[indexOfFirstTicker], ! this.tickers[indexOfSecondTicker]); return returnValue; --- 167,171 ---- if ( Double.IsNaN(returnValue) || Double.IsInfinity(returnValue) ) throw new MissingCorrelationException(this.tickers[indexOfFirstTicker], ! this.tickers[indexOfSecondTicker]); return returnValue; *************** *** 176,194 **** if(this.returnsManager == null) this.setReturnsManager(); ! this.pearsonCorrelations = new TickersPearsonCorrelation[ this.numOfCombinationTwoByTwo ]; int index = 0; for (int i = 0; i < this.tickers.Length; i++) for (int j = i + 1; j < this.tickers.Length; j++) ! { ! try{ ! this.pearsonCorrelations[index] = ! new TickersPearsonCorrelation( this.tickers[i], this.tickers[j], ! this.getOrderedTickersPearsonCorrelations_setCorrelations_getValue( i , j) ); ! } ! catch(MissingCorrelationException ex) ! {ex=ex;} ! index++; ! } Array.Sort(this.pearsonCorrelations); } --- 176,196 ---- if(this.returnsManager == null) this.setReturnsManager(); ! this.pearsonCorrelations = new TickersPearsonCorrelation[ this.numOfCombinationTwoByTwo ]; int index = 0; for (int i = 0; i < this.tickers.Length; i++) for (int j = i + 1; j < this.tickers.Length; j++) ! { ! try{ ! this.pearsonCorrelations[index] = ! new TickersPearsonCorrelation( this.tickers[i], this.tickers[j], ! this.getOrderedTickersPearsonCorrelations_setCorrelations_getValue( i , j) ); ! } ! catch(MissingCorrelationException ex) ! { ! string forBreakpoint = ex.Message; forBreakpoint = forBreakpoint + ""; ! } ! index++; ! } Array.Sort(this.pearsonCorrelations); } *************** *** 202,212 **** for(int i = 0; i < this.pearsonCorrelations.Length; i++) System.Console.WriteLine(this.pearsonCorrelations[i].FirstTicker + " " + ! this.pearsonCorrelations[i].SecondTicker + " " + ! this.pearsonCorrelations[i].CorrelationValue.ToString() ); } public TickersPearsonCorrelation[] GetOrderedTickersPearsonCorrelations() { ! if( this.pearsonCorrelations == null ) this.getOrderedTickersPearsonCorrelations_setCorrelations(); this.showOutputToConsole(); --- 204,214 ---- for(int i = 0; i < this.pearsonCorrelations.Length; i++) System.Console.WriteLine(this.pearsonCorrelations[i].FirstTicker + " " + ! this.pearsonCorrelations[i].SecondTicker + " " + ! this.pearsonCorrelations[i].CorrelationValue.ToString() ); } public TickersPearsonCorrelation[] GetOrderedTickersPearsonCorrelations() { ! if( this.pearsonCorrelations == null ) this.getOrderedTickersPearsonCorrelations_setCorrelations(); this.showOutputToConsole(); *************** *** 216,235 **** public double GetPearsonCorrelation(string firstTicker, string secondTicker) { ! double returnValue = double.NaN; if( this.pearsonCorrelations == null ) this.getOrderedTickersPearsonCorrelations_setCorrelations(); ! for(int i = 0; i<this.pearsonCorrelations.Length; i++) ! { ! if( (this.pearsonCorrelations[i].FirstTicker == firstTicker && ! this.pearsonCorrelations[i].SecondTicker == secondTicker) || ! (this.pearsonCorrelations[i].FirstTicker == secondTicker && ! this.pearsonCorrelations[i].SecondTicker == firstTicker) ) ! { returnValue = this.pearsonCorrelations[i].CorrelationValue; i = this.pearsonCorrelations.Length;//exit from for } ! } ! if( double.IsNaN(returnValue) ) throw new MissingCorrelationException(firstTicker, secondTicker); --- 218,237 ---- public double GetPearsonCorrelation(string firstTicker, string secondTicker) { ! double returnValue = double.NaN; if( this.pearsonCorrelations == null ) this.getOrderedTickersPearsonCorrelations_setCorrelations(); ! for(int i = 0; i<this.pearsonCorrelations.Length; i++) ! { ! if( (this.pearsonCorrelations[i].FirstTicker == firstTicker && ! this.pearsonCorrelations[i].SecondTicker == secondTicker) || ! (this.pearsonCorrelations[i].FirstTicker == secondTicker && ! this.pearsonCorrelations[i].SecondTicker == firstTicker) ) ! { returnValue = this.pearsonCorrelations[i].CorrelationValue; i = this.pearsonCorrelations.Length;//exit from for } ! } ! if( double.IsNaN(returnValue) ) throw new MissingCorrelationException(firstTicker, secondTicker); *************** *** 237,240 **** } ! } // end of class } --- 239,242 ---- } ! } // end of class } |