/// <summary>
        /// Calculate the efficient frontier for a given covariance matrix and mean vector
        /// </summary>
        /// <param name="covariance">Covariance Matrix of assets included in portfolio</param>
        /// <param name="mean">Expected return of assets in portfolio</param>
        public IPortfolio Calculate()
        {
            // MVO using quadratic programming
            // Optimal portfolio is:
            //         min(-dvec'bvec+1/2x'Dmat*x) - where ' is the transpose of vector/matrix
            // Dmat - Covariance Matrix - quadratic terms
            // dvec - [0..nAssets] - linear terms
            // Amat - variable weigths in constraints equation
            // bvec - constraint equalities
            // with constraints
            //         A'x >= b
            // meq = eqsumW

            // Get mean returns of all instruments in portfolio
            var meanReturns = from sp in _samplePortfolio
                              select sp.Mean;

            // Get covariance matrix for all instruments in portfolio
            var cov = from sp in _samplePortfolio
                      select new KeyValuePair <string, Dictionary <string, double> >(sp.ID, sp.Covariance);

            var covariance = CovarianceMatrix.Create(cov.ToDictionary(a => a.Key, b => b.Value));
            var portfConf  = new ConfigurationManager(_samplePortfolio);

            OptimizationResult result;

            double[] dvec = new double[_samplePortfolio.Count].Zeros(_samplePortfolio.Count);
            try
            {
                result = QuadProg.Solve(covariance, dvec, portfConf.Amat, portfConf.Bvec, portfConf.NumEquals);
            }
            catch (Exception e)
            {
                // Solution not found - create NaN Portfolio
                result = new OptimizationResult(new double[] { double.NaN }, double.NaN);
                // Log error
                Console.WriteLine(e.Message);
            }

            int    q   = 0;
            double sum = 0;

            foreach (var m in meanReturns)
            {
                sum = sum + m * result.Solution[q];
                q++;
            }

            var portf = PortfolioFactory.Create(_samplePortfolio, _samplePortfolio.Name, sum, result.Value);

            for (int c = 0; c < result.Solution.Length; c++)
            {
                portf[_samplePortfolio[c].ID].Weight = result.Solution[c];
            }

            return(portf);
        }
        /// <summary>
        /// Calculate the efficient frontier for a given covariance matrix and mean vector
        /// </summary>
        /// <param name="covariance">Covariance Matrix of assets included in portfolio</param>
        /// <param name="mean">Expected return of assets in portfolio</param>
        public IEnumerable <IPortfolio> Calculate(int digits = 4)
        {
            // MVO using quadratic programming
            // Optimal portfolio is:
            //         min(-dvec'bvec+1/2x'Dmat*x) - where ' is the transpose of vector/matrix
            // Dmat - Covariance Matrix - quadratic terms
            // dvec - [0..nAssets] - linear terms
            // Amat - variable weigths in constraints equation
            // bvec - constraint equalities
            // with constraints
            //         A'x >= b
            // meq = eqsumW

            var meanReturns = from sp in _samplePortfolio
                              select sp.Mean;

            var cov = from ins in _samplePortfolio
                      select new KeyValuePair <string, Dictionary <string, double> >(ins.ID, ins.Covariance);

            var covariance = CovarianceMatrix.Create(cov.ToDictionary(a => a.Key, b => b.Value));
            List <IPortfolio> portfolios = new List <IPortfolio>();

            // generate sequence of target returns for the efficient frontier and minimum variance locus
            var targetReturns = new double[_numPortfolios].Seq(meanReturns.Min(), meanReturns.Max(), (int)_numPortfolios, digits);

            double[] dvec = new double[_samplePortfolio.Count].Zeros(_samplePortfolio.Count);
            // For every target return find the minimum variance portfolio
            for (int i = 0; i < _numPortfolios; i++)
            {
                var portfConf = new ConfigurationManager(_samplePortfolio, targetReturns[i]);
                OptimizationResult result;
                try
                {
                    result = QuadProg.Solve(covariance, dvec, portfConf.Amat, portfConf.Bvec, portfConf.NumEquals);
                }
                catch (Exception e)
                {
                    // Solution not found - create NaN Portfolio
                    result = new OptimizationResult(new double[] { double.NaN }, double.NaN);
                    // Log error
                    Console.WriteLine(e.Message);
                }
                var portf = PortfolioFactory.Create(_samplePortfolio, i.ToString(), targetReturns[i], result.Value);
                for (int c = 0; c < result.Solution.Length; c++)
                {
                    portf[_samplePortfolio[c].ID].Weight = result.Solution[c];
                }
                portfolios.Add(portf);
            }

            return(portfolios);
        }