首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中生成递归数据结构的时序统计算法

在C#中生成递归数据结构的时序统计算法
EN

Stack Overflow用户
提问于 2012-03-16 01:20:40
回答 1查看 363关注 0票数 0

我有一个值列表,可以是doubles或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!

EN

回答 1

Stack Overflow用户

发布于 2012-03-16 02:15:18

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

代码语言:javascript
复制
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时,您将准确地将遍历列表的方式构建到类中。我已经实现了你的第一个例子。还要注意,我没有使用递归。对于每种类型的遍历和计算,您可以像这样创建一个类:

代码语言:javascript
复制
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;
    }
}

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

代码语言:javascript
复制
        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();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9724884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档