Exemple #1
0
        public static double?CalculateTrackingError(
            IEnumerable <double> dailyPerformance1,
            IEnumerable <double> dailyPerformance2,
            out double?informationRatio)
        {
            informationRatio = null;
            var count = dailyPerformance1.Count();

            if (count < 1)
            {
                return(null);
            }

            IList <double> differences = new List <double>();

            for (var i = 0; i < count; i++)
            {
                var value1 = 1 + dailyPerformance1.ElementAt(i);
                var value2 = 1 + dailyPerformance2.ElementAt(i);
                differences.Add((value1 <= 0 ? 0 : Math.Log(value1)) - (value2 <= 0 ? 0 : Math.Log(value2)));
            }

            var average       = differences.Average();
            var sum           = differences.Select(x => Math.Pow(x - average, 2)).Sum();
            var trackingError = Math.Pow(MathematicalFunctions.SafeDivision(sum, count - 1), 0.5) *
                                Math.Pow(260, 0.5);

            informationRatio = MathematicalFunctions.SafeDivision(average * 260, trackingError);
            return(trackingError);
        }
        /// <summary>
        ///     Gets the correlation. (Pearson Method)
        /// </summary>
        /// <param name="valuesX">The values_ X.</param>
        /// <param name="valuesY">The values_ Y.</param>
        /// <returns></returns>
        public static double?GetCorrelation(IEnumerable <double> valuesX, IEnumerable <double> valuesY)
        {
            if (valuesX == null || valuesY == null || !valuesX.Any() || !valuesY.Any())
            {
                return(null);
            }

            var covariance = GetCoVariance(valuesX, valuesY);
            var stdDevX    = GetStandardDeviation(valuesX);
            var stdDevY    = GetStandardDeviation(valuesY);

            return(MathematicalFunctions.SafeDivision(covariance, stdDevX * stdDevY));
        }
        /// <summary>
        ///     Gets the standard deviation per annum.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        public static double?GetStandardDeviationPerAnnum(IDictionary <DateTime, double> values)
        {
            if (values == null)
            {
                return(null);
            }
            if (values.Count < 30)
            {
                return(null);
            }
            var    timeSpan = values.Max(p => p.Key) - values.Min(p => p.Key);
            double days     = values.Count;

            return(GetStandardDeviation(values.Select(p => p.Value)) *
                   Math.Sqrt(MathematicalFunctions.SafeDivision(days, timeSpan.Days) * Constants.DAYS_OF_YEAR));
        }
Exemple #4
0
        public static IDictionary <DateTime, double> GetEntryFromIndexPerformance(IEnumerable <IPerformanceItem> data)
        {
            IDictionary <DateTime, double> result = new Dictionary <DateTime, double>();
            var    index     = 0;
            double lastIndex = 100;

            foreach (var item in data.Where(x => x.Value.HasValue).OrderBy(x => x.Date))
            {
                result.Add(
                    item.Date,
                    index == 0 ? 0 : MathematicalFunctions.SafeDivision(item.Value ?? 100, lastIndex) - 1);
                lastIndex = item.Value ?? 100;
                index++;
            }

            return(result);
        }
        /// <summary>
        ///     Gets the variance.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        public static double GetVariance(IEnumerable <double> values)
        {
            double variance = 0;

            if (values == null)
            {
                return(variance);
            }
            if (!values.Any())
            {
                return(variance);
            }
            var count   = Convert.ToDouble(values.Count());
            var average = values.Average();

            variance = values.Sum(value => Math.Pow(value - average, 2));
            return(MathematicalFunctions.SafeDivision(variance, count));
        }
        /// <summary>
        ///     Gets the item performance values.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns></returns>
        public static IDictionary <DateTime, double> GetItemPerformanceValues(IDictionary <DateTime, double> values)
        {
            var result = new Dictionary <DateTime, double>();

            if (values == null)
            {
                return(result);
            }
            if (values.Count <= 0)
            {
                return(result);
            }

            var lastValue = values.First().Value;

            foreach (var valuePair in values)
            {
                result.Add(valuePair.Key, MathematicalFunctions.SafeDivision(valuePair.Value, lastValue) - 1);
                lastValue = valuePair.Value;
            }

            return(result);
        }
        /// <summary>
        ///     Gets the co variance.
        /// </summary>
        /// <param name="valuesX">The values_ X.</param>
        /// <param name="valuesY">The values_ Y.</param>
        /// <returns></returns>
        public static double GetCoVariance(IEnumerable <double> valuesX, IEnumerable <double> valuesY)
        {
            if (valuesX == null || valuesY == null || !valuesX.Any() || !valuesY.Any())
            {
                return(0);
            }
            if (valuesX.Count() != valuesY.Count())
            {
                return(0);
            }
            var count    = valuesX.Count();
            var averageX = valuesX.Average();
            var averageY = valuesY.Average();

            double covariance = 0;

            for (var i = 0; i < count; i++)
            {
                covariance += (valuesX.ElementAt(i) - averageX) * (valuesY.ElementAt(i) - averageY);
            }

            return(MathematicalFunctions.SafeDivision(covariance, Convert.ToDouble(count)));
        }