|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | + |
| 4 | +namespace Core.Services |
| 5 | +{ |
| 6 | + public class AverageService |
| 7 | + { |
| 8 | + /// <summary> |
| 9 | + /// Simple moving average |
| 10 | + /// </summary> |
| 11 | + /// <param name="collection"></param> |
| 12 | + /// <param name="index"></param> |
| 13 | + /// <param name="interval"></param> |
| 14 | + public virtual double SimpleAverage(IEnumerable<double> collection, int index, int interval) |
| 15 | + { |
| 16 | + var response = collection.ElementAtOrDefault(index); |
| 17 | + |
| 18 | + if (interval > 0 && index >= interval - 1) |
| 19 | + { |
| 20 | + response = collection |
| 21 | + .Skip(index - interval + 1) |
| 22 | + .Take(interval) |
| 23 | + .Average(); |
| 24 | + } |
| 25 | + |
| 26 | + return response; |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Exponential moving average |
| 31 | + /// </summary> |
| 32 | + /// <param name="collection"></param> |
| 33 | + /// <param name="index"></param> |
| 34 | + /// <param name="interval"></param> |
| 35 | + /// <param name="previous"></param> |
| 36 | + /// <returns></returns> |
| 37 | + public virtual double ExponentialAverage(IEnumerable<double> collection, int index, int interval, double previous) |
| 38 | + { |
| 39 | + var pr = 2.0 / (interval + 1.0); |
| 40 | + var response = collection.ElementAtOrDefault(index); |
| 41 | + |
| 42 | + if (interval > 0) |
| 43 | + { |
| 44 | + response = collection.ElementAt(index) * pr + previous * (1 - pr); |
| 45 | + } |
| 46 | + |
| 47 | + return response; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Smooth moving average |
| 52 | + /// </summary> |
| 53 | + /// <param name="collection"></param> |
| 54 | + /// <param name="index"></param> |
| 55 | + /// <param name="interval"></param> |
| 56 | + /// <param name="previous"></param> |
| 57 | + /// <returns></returns> |
| 58 | + public virtual double SmoothAverage(IEnumerable<double> collection, int index, int interval, double previous) |
| 59 | + { |
| 60 | + var response = collection.ElementAtOrDefault(index); |
| 61 | + |
| 62 | + if (interval > 0) |
| 63 | + { |
| 64 | + if (index == interval - 1) |
| 65 | + { |
| 66 | + response = collection |
| 67 | + .Skip(index - interval + 1) |
| 68 | + .Take(interval) |
| 69 | + .Average(); |
| 70 | + } |
| 71 | + |
| 72 | + if (index >= interval) |
| 73 | + { |
| 74 | + response = (previous * (interval - 1) + collection.ElementAt(index)) / interval; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return response; |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Linear weighted moving average |
| 83 | + /// </summary> |
| 84 | + /// <param name="collection"></param> |
| 85 | + /// <param name="index"></param> |
| 86 | + /// <param name="interval"></param> |
| 87 | + /// <returns></returns> |
| 88 | + public virtual double LinearWeightAverage(IEnumerable<double> collection, int index, int interval) |
| 89 | + { |
| 90 | + var sum = 0.0; |
| 91 | + var wsum = 0.0; |
| 92 | + var response = collection.ElementAtOrDefault(index); |
| 93 | + |
| 94 | + if (interval > 0 && index >= interval - 1) |
| 95 | + { |
| 96 | + for (var i = interval; i > 0; i--) |
| 97 | + { |
| 98 | + wsum += i; |
| 99 | + sum += collection.ElementAt(index - i + 1) * (interval - i + 1); |
| 100 | + } |
| 101 | + |
| 102 | + response = sum / wsum; |
| 103 | + } |
| 104 | + |
| 105 | + return response; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments