我有一个时间序列的形式SortedList<dateTime,double>.我想计算一下这个系列的移动平均线.我可以使用简单的for循环来做到这一点.我想知道是否有更好的方法来使用linq.
我的版本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var mySeries = new SortedList<DateTime, double>();
mySeries.Add(new DateTime(2011, 01, 1), 10);
mySeries.Add(new DateTime(2011, 01, 2), 25);
mySeries.Add(new DateTime(2011, 01, 3), 30);
mySeries.Add(new DateTime(2011, 01, 4), 45);
mySeries.Add(new DateTime(2011, 01, 5), 50);
mySeries.Add(new DateTime(2011, 01, 6), 65);
var calcs = new calculations();
var avg = calcs.MovingAverage(mySeries, 3);
foreach (var item in avg)
{
Console.WriteLine("{0} {1}", item.Key, item.Value);
} …Run Code Online (Sandbox Code Playgroud) 我们正处于f#项目的开端,该项目涉及流数据的实时和历史分析.数据包含在ac#对象中(见下文),并作为标准.net事件的一部分发送.实时地,我们通常接收的事件的数量可以从每个仪器每秒大约800个事件的不到1 /秒到更高的变化很大,因此可以是非常突发的.典型的一天可能会累积500万行/每个元素
C#事件的数据结构的通用版本如下所示:
public enum MyType { type0 = 0, type1 = 1}
public class dataObj
{
public int myInt= 0;
public double myDouble;
public string myString;
public DateTime myDataTime;
public MyType type;
public object myObj = null;
}
Run Code Online (Sandbox Code Playgroud)
我们计划以两种方式在f#中使用这个数据结构:
随着我们添加更多事件,数据结构需要能够增长.这排除了array<t>因为它不允许调整大小,尽管它可以用于历史分析.数据结构还需要能够快速访问最近的数据,理想情况下需要能够跳回到数据x点.这排除了Lists<T>因为线性查找时间,并且因为没有随机访问元素,只是"仅向前"遍历.
根据这篇文章,Set<T>可能是个不错的选择......
编辑:尹祝的回应让我更清楚地知道我在问什么.我已经编辑了帖子的其余部分以反映这一点.此外,通过引入历史分析的要求,这个问题的先前版本变得混乱.我省略了它们.
以下是实时流程步骤的细分:
Set<T>还是其他结构?所以问题是什么是用于存储我们将用于生成功能的实时流事件的最佳数据结构.
我有一个函数创建一个本地List<object>,其中对象是一个匿名类型.我需要返回这些结果Dictionary<string, SortedList<DateTime, double>>.
数据列表如下所示.
{ Security = "6752 JT", Date = {1/17/2011 12:00:00 AM}, zScore = 1 }
{ Security = "6753 JT", Date = {1/17/2011 12:00:00 AM}, zScore = 2 }
{ Security = "6754 JT", Date = {1/17/2011 12:00:00 AM}, zScore = 3 }
{ Security = "6752 JT", Date = {1/18/2011 12:00:00 AM}, zScore = 1 }
{ Security = "6753 JT", Date = {1/18/2011 12:00:00 AM}, zScore = 2 }
{ Security …Run Code Online (Sandbox Code Playgroud) 我在F#中使用第三方供应商的API.在初始化时,API返回一个嵌套的msg容器的C#对象.它填充了状态消息,可能包含错误消息.供应商提供了一个C#样本解析例程,我已经移植了F#.
代码示例循环通过嵌套的msg容器提取致命和非致命错误,然后返回类型的元组列表 BBResponseType * string
回复枚举:
type BBResponseType =
| Status = 0
| Data = 1
| Error = 2
| FatalError = -1
Run Code Online (Sandbox Code Playgroud)
我到F#的端口看起来像这样:
member private this.ProcessStatusMsg(eventObj: Blpapi.Event) =
let responseLst = List<(BBResponseType * string)>()
for msg in eventObj do
if msg.MessageType.Equals(SUBSTARTED) then
if msg.GetElement(EXCEPTIONS).NumValues > 0 then // <- check for errors/exceptions
let e = msg.GetElement(EXCEPTIONS)
for i in 0..e.NumValues-1 do
let error = e.GetValueAsElement(i)
let field = error.GetElementAsString(FieldID)
let reason = error.GetElement(REASON)
let message = sprintf …Run Code Online (Sandbox Code Playgroud) 我有一套约2000个独立的时间序列形式SortedList<DateTime,double>.每个系列对应于给定证券的每日流动性.我想创建这些值的每日排名.如果我用for循环执行此操作,我会执行以下操作:
SortedList<DateTime,double>.简而言之,这是每日流动性从最大到最小的排名.我可以让linq从对象中提取数据并按日期分组,但其余的超出了我的linq技能.
任何linq大师都会照顾到这一点?
下面概述了对象结构的简化版本.
注意:我有意创建了一个日期(2011,01,18),其中值(30)是相同的.在这种情况下,符号名称的子排名是可接受的.所以他们将排名第一名6753 JT,第二名6754 JT.6752 JT没有该日期的值,因此不会包含它.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ranking_Query
{
class Program
{
static void Main(string[] args)
{
// created an instance of the datasource and add 3 securities to it
Datasource ds = new Datasource() { Name = "test" };
ds.securities.Add("6752 JT", new Security() {
timeSeries = new Dictionary<string, SortedList<DateTime, double>>() {
{ "liquidity", new SortedList<DateTime, double>() {
{new …Run Code Online (Sandbox Code Playgroud) c# ×3
linq ×3
f# ×2
c#-to-f# ×1
dictionary ×1
f#-3.0 ×1
f#-data ×1
nested-loops ×1
ranking ×1
recursion ×1
stream ×1
time-series ×1