public CovarianceItem GetCovariance(DateTime markDate_, bool forceRefresh_ = false)
    {
      // for speed, hard-code to to where we know cov data currently starts
      if (markDate_.Year < 2001) return null;

      if (!forceRefresh_ && _dict.ContainsKey(markDate_))
        return _dict[markDate_];

      string fileName = getFileName(markDate_);

      if (!forceRefresh_)
      {
        var cached = Singleton<DiscCache>.Instance.Read<CovarianceItem>(fileName,true);

        if (cached != null)
        {
          _dict[markDate_] = cached;
          return cached;
        }
      }

      try
      {
        var ds = Singleton<ConnMngr>.Instance.Execute(DBNames.SQLSERVER_DBAGHLA_HK, getSql(markDate_));

        if (ConnMngr.HasResult(ds))
        {
          var ccys = Singleton<ComIDs>.Instance.ToArray();

          var arr = new double[ccys.Length, ccys.Length];

          foreach (DataRow row in ds.Tables[0].Rows)
          {
            int id1 = Convert.ToInt32(row["ID1"]);
            int id2 = Convert.ToInt32(row["ID2"]);
            double value = Convert.ToDouble(row["cov"]);

            arr[id1 - 1, id2 - 1] = value;
            arr[id2 - 1, id1 - 1] = value;
          }

          var covItem = new CovarianceItem(arr, new List<int>(ccys.Select(x => x.ID)));

          _dict[markDate_] = covItem;
          Singleton<DiscCache>.Instance.Write(fileName, covItem);

          return covItem;

        }
      }
      catch (Exception ex_)
      {
        this.LogError(string.Format("Error getting Covariance. ({0})", ex_.Message));
      }
      return null;
    }
    public CovarianceItem GetCovariance(DateTime markDate_, bool forceRefresh_=false)
    {
      var key = getKey(markDate_);

      if (!forceRefresh_ && _dict.ContainsKey(key))
        return _dict[key];

      if (!forceRefresh_)
      {
        var cached = Singleton<DiscCache>.Instance.Read<CovarianceItem>(key.DiskFileName,"covariances",true);

        if (cached != null)
        {
          _dict[key] = cached;
          return cached;
        }
      }

      try
      {
        var ds = Singleton<ConnMngr>.Instance.Execute(DBNames.SQLSERVER_DBAGHLA_HK, key.SQL);

        if (ConnMngr.HasResult(ds))
        {
          var ccys = Singleton<FXIDs>.Instance.ToArray();

          var arr = new double[ccys.Length, ccys.Length];

          foreach (DataRow row in ds.Tables[0].Rows)
          {
            int id1 = Convert.ToInt32(row["ID1"]);
            int id2 = Convert.ToInt32(row["ID2"]);
            double value = Convert.ToDouble(row["cov"]);

            arr[id1 - 1, id2 - 1] = value;
            arr[id2 - 1, id1 - 1] = value;
          }

          var covItem = new CovarianceItem(arr, new List<int>(ccys.Select(x => x.ID)));

          _dict[key] = covItem;
          Singleton<DiscCache>.Instance.Write(key.DiskFileName, "covariances", covItem);

          return covItem;

        }
      }
      catch(Exception ex_)
      {
        this.LogError(string.Format("Error getting Covariance. ({0})", ex_.Message));
      }
      return null;
    }
    internal void Create(string stratName_, string[] columnHeadings_, double[] wts_, CovarianceItem cov_)
    {
      lblWtsHeading.Text = string.Format("Weights for '{0}'", stratName_);

      if (columnHeadings_ == null || wts_ == null)
      {
        Logger.Warn("WtsContVar.Create called without any weights - won't display anything", typeof(WtsContToVar));
        return;
      }

      simpleWtsColumnChart1.Create(columnHeadings_, wts_);

      if (cov_ == null)
      {
        Logger.Warn("WtsContVar.Create called without a covariance - won't be able to display risk parts of analysis", typeof(WtsContToVar));
        return;
      }

      simpleWtsColumnChart2.Create(columnHeadings_, cov_.GetContToVar(wts_));

      lblTargetVol.Text = string.Format("TargetVol={0}", cov_.GetVol(wts_).ToString("##0.0#%"));
    }
    public static CovarianceItem SquashIntoUsedCells(CovarianceItem item_)
    {
      List<int> cellsWithValue = new List<int>();

      for (int i = 0; i < item_.ComponentIds.Count; ++i)
      {
        int index = item_.ComponentIds[i] - 1;

        if (item_.Data[0, index] == 0.0 || double.IsNaN(item_.Data[0, index]))
          continue;

        cellsWithValue.Add(item_.ComponentIds[i]);
      }

      double[,] grid = new double[cellsWithValue.Count, cellsWithValue.Count];

      for (int i = 0; i < cellsWithValue.Count; ++i)
      {
        int iIndex = cellsWithValue[i] - 1;
        for (int j = i; j < cellsWithValue.Count; ++j)
        {
          int jIndex = cellsWithValue[j] - 1;

          grid[i, j] = grid[j, i] = item_.Data[iIndex, jIndex];
        }
      }

      return new CovarianceItem(grid, cellsWithValue);
    }