prog

Correlation Calculator

Mar 16th, 2010
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1.         public double GetCorrelation(Indice a, Indice b)
  2.         {
  3.             double iSum = 0.0;
  4.  
  5.             for (int i = 0; i < a.Composition.Length; i++)
  6.             {
  7.                 double sigmaA = _underlyingVolatilities[a.Composition.Names[i]];
  8.                 double alphaA = a.Composition[i];
  9.  
  10.                 double jSum = 0.0;
  11.  
  12.                 for (int j = 0; j < b.Composition.Length; j++)
  13.                 {
  14.                     double sigmaB = _underlyingVolatilities[b.Composition.Names[j]];
  15.                     double alphaB = b.Composition[j];
  16.                     double componentsCorrelation = _underlyingCorrelations[a.Composition.Names[i], b.Composition.Names[j]];
  17.  
  18.                     double jTerm = alphaB * sigmaB * componentsCorrelation;
  19.  
  20.                     jSum += jTerm;
  21.                 }
  22.  
  23.                 double iTerm = alphaA * sigmaA * jSum;
  24.  
  25.                 iSum += iTerm;
  26.             }
  27.  
  28.             if (a.Volatility == 0.0)
  29.             {
  30.                 GetVolatility(a);
  31.             }
  32.  
  33.             if (b.Volatility == 0.0)
  34.             {
  35.                 GetVolatility(b);
  36.             }
  37.  
  38.             double correlation = iSum / (a.Volatility * b.Volatility);
  39.            
  40.             return correlation;
  41.         }
Add Comment
Please, Sign In to add comment