Joining Time Series

I am developing a simple internal structure for processing time series data. Given that LINQ is my real toy hammer, I want to hit everyone with it.

I want to implement methods in the TimeSeries class (Select (), Where (), etc.) so that I can use LINQ syntax to process time series data

Some things are straightforward, for example. (from x to select x + 10), giving a new time series.

What is the best syntax design for combining two or more time series? (from a to b to B choose a + b) is small because it expresses a nested loop. Maybe some join? This should match the union over an implicit time variable. (What I mean corresponds to the lisp 'zip' function)


EDIT: Some clarification is required.

A time series is a kind of function that depends on time, for example. stock quotes. The combination of time series can be the difference between two stock prices as a function of time.

Stock1.MyJoin(Stock2, (a,b)=>a-b)

possible, but can this be expressed neatly with some LINQ syntax? I expect to implement LINQ methods in class MyTimeSeriesmyself.

+3
source share
4 answers

If I understand the question correctly, do you want to join several sequences based on their position in the sequence?

System.Linq.Enumerable , Join GroupJoin . PositionalJoin , :

sequenceA.PositionalJoin(sequenceB, (a, b) => new { a, b });

, , , , , . , , .

public static IEnumerable<TResult> PositionalJoin<T1, T2, TResult>(
    this IEnumerable<T1> source1, 
    IEnumerable<T2> source2, 
    Func<T1, T2, int, TResult> selector)
{
    // argument checking here
    return PositionalJoinIterator(source1, source2, selector);
}

private static IEnumerable<TResult> PositionalJoinIterator<T1, T2, TResult>(
    IEnumerable<T1> source1, 
    IEnumerable<T2> source2, 
    Func<T1, T2, TResult> selector)
{
    using (var enumerator1 = source1.GetEnumerator())
    using (var enumerator2 = source2.GetEnumerator())
    {
        bool gotItem;
        do
        {
            gotItem = false;

            T1 item1;
            if (enumerator1.MoveNext())
            {
                item1 = enumerator1.Current;
                gotItem = true;
            }
            else
            {
                item1 = default(T1);
            }

            T2 item2;
            if (enumerator2.MoveNext())
            {
                item2 = enumerator2.Current;
                gotItem = true;
            }
            else
            {
                item2 = default(T2);
            }

            if (gotItem)
            {
                yield return selector(item1, item2);
            }
        }
        while (gotItem);
    }
}

, , , , , .

0

Union - , , , .

, , MiscUtil, . :

foreach (DateTime day in 19.June(1976).To(DateTime.Today).Step(1.Day()))
{
    Console.WriteLine("I'm alive!");
}

, , , , . :)

+1

NExtension:

public static IEnumerable<TResult> Zip<T1, T2, TResult>(
    this IEnumerable<T1> source1, 
    IEnumerable<T2> source2, 
    Func<T1, T2, TResult> combine)
{
    if (source1 == null)
        throw new ArgumentNullException("source1");
    if (source2 == null)
        throw new ArgumentNullException("source2");
    if (combine == null)
        throw new ArgumentNullException("combine");

    IEnumerator<T1> data1 = source1.GetEnumerator();
    IEnumerator<T2> data2 = source2.GetEnumerator();
    while (data1.MoveNext() && data2.MoveNext())
    {
        yield return combine(data1.Current, data2.Current);
    }
}

:

Stock1.Zip(Stock2, (a,b)=>a-b)
+1

Bjarke, NEsper, , , , , SQL. , , , , . http://esper.codehaus.org/about/nesper/nesper.html

+1

Source: https://habr.com/ru/post/1698774/


All Articles