c# - 时间序列统计 algrothim 在 C# 中生成递归数据结构

标签 c# algorithm statistics time-series recursive-datastructures

我有一个值列表,可以是 double 值或 DateTimes。

15、36、-7、12、8

此数据是一个 TimeSeries,因此顺序很重要。列表中也只有 3 到 6 个值,所以我们不是在谈论大数据集。

假设我想生成关于这些的统计数据,例如比率。

15/36、36/-7、-7/12、12/8 == .417、-5.14、-.583、1.5

然后是比率的比率

.417/-5.14, -5.14/-.583, -.583/1.5

..等等。

我还需要针对过去的每个值生成每个值的统计信息。

12/8、-7/8、36/8、15/8

12/-7、12/36、12/15

...

还需要每个值与先前值的平均值的比率。

平均(12,-7)/8 , 平均(12,-7,36)/8

当数据为 DateTime 时,将使用 TimeSpan。还需要斜率、平均斜​​率、比率趋势、斜率趋势等。

基本上是尝试获取尽可能多的相关数据。由于它是一个时间序列,因此相关数据仅限于每个值左侧的统计信息,以及第一个和最后一个值。

不确定我是在寻找设计模式、数学公式还是 TimeSeries 分析概念。

我目前的设计是分步进行。每对比率的类,然后是比率的比率类..等等。寻找更抽象的东西。

是否有设计模式、数学公式或 TimeSeries 概念可以让我为我的问题编写更抽象的解决方案?

感谢 Stack Overflow!

最佳答案

我认为您需要从抽象时间序列数字列表开始。似乎每组计算都要以不同的方式遍历列表。

interface IMyList<T>
{
    void SetList(IList<T> series);

    bool IsDone();
    T GetOperand1();
    T GetOperand2();
    T Calculate(T op1, T op2);
    void SetResult(T result);
    void Next();

    Dictionary<int, IList<T>> GetResults();
}

当您在每个类中实现每个 IMyList 时,您将准确地构建到类中应该如何遍历列表。我已经实现了您的第一个示例。另请注意,我没有使用递归。对于每种类型的遍历和计算,您可以创建一个这样的类:

public class Ratio : IMyList<double>
{
    private Dictionary<int, IList<double>> _results;
    private int _currentSeries;
    private int _seriesResults;
    private int _op1Index;
    private int _op2Index;
    private bool _bDone;

    public Ratio()
    {
        _op1Index = 0;
        _op2Index = 1;
        _currentSeries = 0;
        _seriesResults = 1;
    }

    public void SetList(IList<double> series)
    {
        // the zero entry is the first result set
        _results = new Dictionary<int, IList<double>>();
        _results.Add(_currentSeries, series);
        _results.Add(_seriesResults, new List<double>());
    }

    public bool IsDone()
    {
        return _bDone;
    }

    public double GetOperand1()
    {
        return _results[_currentSeries][_op1Index];
    }

    public double GetOperand2()
    {
        return _results[_currentSeries][_op2Index];
    }

    public double Calculate(double op1, double op2)
    {
        return op1 / op2;
    }

    public void SetResult(double result)
    {
        _results[_seriesResults].Add(result);
    }

    public void Next()
    {
        _op1Index++;
        _op2Index++;

        if (_op2Index >= _results[_currentSeries].Count())
        {
            if (_results[_seriesResults].Count == 1)
            {
                _bDone = true;
            }
            else
            {
                _currentSeries++;
                _seriesResults++;
                _results.Add(_seriesResults, new List<double>());
                _op1Index = 0;
                _op2Index = 1;
            }
        }
    }

    public Dictionary<int, IList<double>> GetResults()
    {
        return _results;
    }
}

要将其付诸实践,代码为:

        List<double> firstList = new List<double>() { 15, 36, -7, 12, 8 };

        // the following section could be refactored further by putting the classes
        // in a list of IMyList and then looping through it
        var rat = new Ratio();
        rat.SetList(firstList);
        while (!rat.IsDone())
        {
            double op1 = rat.GetOperand1();
            double op2 = rat.GetOperand2();
            rat.SetResult(rat.Calculate(op1, op2);
            rat.Next();
        }
        var results = rat.GetResults();

关于c# - 时间序列统计 algrothim 在 C# 中生成递归数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9724884/

相关文章:

c - 正确括号的最长子串

c++ - 高效积累

c++ - 如何测试某些数字是否沿区间均匀分布?

algorithm - 使用 Welford 方法计算单程方差时删除先验样本

r - 带有 Gamma 分布的fitdist中的错误

c# - 使用 C# sql 对表的特定列的行进行求和

c# - WPF:将变量从父 xaml 传递给用户控件

c# - 将项目添加到 Canvas Unity C#

c# - 如何在 Visual Studio 中测试运行需要 SSL 的 ASP.NET MVC 网站?

java - 有关如何改进当前模糊搜索实现的建议