C#LINQ Z-Score查询输出到字典<;字符串,分类列表<;日期时间,双精度>&燃气轮机;

C#LINQ Z-Score查询输出到字典<;字符串,分类列表<;日期时间,双精度>&燃气轮机;,c#,linq,dictionary,C#,Linq,Dictionary,我编写了一个查询,该查询对给定日期的所有值执行Z分数计算。计算似乎很好,但我很难将此查询的结果转换为函数可以返回的格式。Z分数以{symbol,date,Z分数}的格式放入列表。问题是如何将列表转换成我需要的格式 我希望它返回一个字典,该字典由安全字符串和一个包含该安全性的所有日期和z值对的排序列表组成 例如:Dictionary 查询计算是正确的(我想;->),但是任何关于改进查询的建议都将不胜感激,因为我仍然是一个非常有挑战性的LINQ个人 下面是一个示例实现: using System;

我编写了一个查询,该查询对给定日期的所有值执行Z分数计算。计算似乎很好,但我很难将此查询的结果转换为函数可以返回的格式。Z分数以{symbol,date,Z分数}的格式放入
列表
。问题是如何将
列表
转换成我需要的格式

我希望它返回一个
字典
,该字典由安全字符串和一个包含该安全性的所有日期和z值对的排序列表组成

例如:
Dictionary

查询计算是正确的(我想;->),但是任何关于改进查询的建议都将不胜感激,因为我仍然是一个非常有挑战性的LINQ个人

下面是一个示例实现:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Ranking_Query
{
    class Program
    {
        static void Main(string[] args)
        {
            // created an instance of the datasource and add 4 securities and their time-series to it
            Datasource ds = new Datasource() { Name = "test" };

            ds.securities.Add("6752 JT", new Security()
            {
                timeSeries = new Dictionary<string, SortedList<DateTime, double>>() {
                        { "Mkt_Cap", new SortedList<DateTime, double>() { 
                            {new DateTime(2011,01,16),300},
                            {new DateTime(2011,01,17),303},
                            {new DateTime(2011,01,18),306},
                            {new DateTime(2011,01,19),309} } } ,
                        { "Liquidity_Rank", new SortedList<DateTime, double>() { 
                            {new DateTime(2011,01,16),1},
                            {new DateTime(2011,01,17),2},
                            {new DateTime(2011,01,18),3},
                            {new DateTime(2011,01,19),4} } }
                }
            });

            ds.securities.Add("6753 JT", new Security()
            {
                timeSeries = new Dictionary<string, SortedList<DateTime, double>>() {
                        { "Mkt_Cap", new SortedList<DateTime, double>() { 
                            {new DateTime(2011,01,16),251},
                            {new DateTime(2011,01,17),252},
                            {new DateTime(2011,01,18),253}, 
                            {new DateTime(2011,01,19),254} } } ,
                        { "Liquidity_Rank", new SortedList<DateTime, double>() { 
                            {new DateTime(2011,01,16),2},
                            {new DateTime(2011,01,17),3},
                            {new DateTime(2011,01,18),4},
                            {new DateTime(2011,01,19),1} } }
                }
            });

            ds.securities.Add("6754 JT", new Security()
            {
                timeSeries = new Dictionary<string, SortedList<DateTime, double>>() {
                        { "Mkt_Cap", new SortedList<DateTime, double>() { 
                            {new DateTime(2011,01,16),203},
                            {new DateTime(2011,01,17),205},
                            {new DateTime(2011,01,18),207},
                            {new DateTime(2011,01,19),209}  } },
                        { "Liquidity_Rank", new SortedList<DateTime, double>() { 
                            {new DateTime(2011,01,16),3},
                            {new DateTime(2011,01,17),4},
                            {new DateTime(2011,01,18),1},
                            {new DateTime(2011,01,19),2} } }
                }
            });

            ds.securities.Add("6755 JT", new Security()
            {
                timeSeries = new Dictionary<string, SortedList<DateTime, double>>() {
                        { "Mkt_Cap", new SortedList<DateTime, double>() { 
                            {new DateTime(2011,01,16),100},
                            {new DateTime(2011,01,17),101},
                            {new DateTime(2011,01,18),103},
                            {new DateTime(2011,01,19),104} } },
                        { "Liquidity_Rank", new SortedList<DateTime, double>() { 
                            {new DateTime(2011,01,16),4},
                            {new DateTime(2011,01,17),1},
                            {new DateTime(2011,01,18),2},
                            {new DateTime(2011,01,19),3} } }
                }
            });

            // set minimum liquidty rank
            int MinLiqRank = 2;

            // Initial query to get a sequence of { Symbol, Date, Mkt_Cap } entries that meet minimum liquidty rank.
            var entries = from securityPair in ds.securities
                          from valuation_liq in securityPair.Value.timeSeries["Liquidity_Rank"]
                          from valuation_MC in securityPair.Value.timeSeries["Mkt_Cap"]
                          where (valuation_liq.Key == valuation_MC.Key) && (valuation_liq.Value >= MinLiqRank)
                          select new
                          {
                              Symbol = securityPair.Key,
                              Date = valuation_liq.Key,
                              MktCap = valuation_MC.Value
                          };      


            // Now group by date 
            var groupedByDate = from entry in entries
                                group entry by entry.Date into date
                                select date.OrderByDescending(x => x.MktCap)
                                           .ThenBy(x => x.Symbol)
                                           .Select(x  => new
                                           {
                                               x.Symbol,
                                               x.MktCap,
                                               x.Date
                                           });


            // final results should populate the following Dictionary of symbols and their respective Z-score time series
            var zScoreResult = new Dictionary<string, SortedList<DateTime, double>>();

            // Calculate the Z-scores for each day
            bool useSampleStdDev = true;
            var results = new List<object>();
            foreach (var sec in groupedByDate)
            {
                // calculate the average value for the date
                double total = 0;
                foreach (var secRank in sec)
                    total += secRank.MktCap;
                double avg = total/ sec.Count();


                // calculate the standard deviation
                double SumOfSquaredDev = 0;
                foreach (var secRank in sec)
                    SumOfSquaredDev += ((secRank.MktCap - avg) * (secRank.MktCap - avg));

                double stdDev;
                if (useSampleStdDev)
                    // sample standard deviation 
                    stdDev = Math.Sqrt(SumOfSquaredDev / (sec.Count() - 1));                    
                else
                    // population standard deviation
                    stdDev = Math.Sqrt(SumOfSquaredDev / sec.Count());


                Console.WriteLine("{0} AvgMktCap {1}, StdDev {2}", sec.First().Date, Math.Round(avg,2), Math.Round(stdDev,2));

                // calculate the Z-score
                double zScore;
                foreach (var secRank in sec)
                {
                    zScore = ((secRank.MktCap - avg) / stdDev);
                    results.Add(new { Symbol = secRank.Symbol, Date = sec.First().Date, zScore = zScore });
                    Console.WriteLine("  {0} MktCap {1} Z-Score {2}", secRank.Symbol, secRank.MktCap, Math.Round(zScore, 2));
                }                
            }


        }

        class Datasource
        {
            public string Name { get; set; }
            public Dictionary<string, Security> securities = new Dictionary<string, Security>();
        }

        class Security
        {
            public string symbol { get; set; }
            public Dictionary<string, SortedList<DateTime, double>> timeSeries;
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间排序查询
{
班级计划
{
静态void Main(字符串[]参数)
{
//创建了一个数据源实例,并向其中添加了4种证券及其时间序列
Datasource ds=new Datasource(){Name=“test”};
ds.securities.Add(“6752 JT”,新证券()
{
timeSeries=newdictionary(){
{“Mkt_Cap”,新分类列表(){
{新日期时间(2011,01,16),300},
{新日期时间(2011,01,17),303},
{新日期时间(2011,01,18),306},
{新日期时间(2011,01,19),309}},
{“流动性排名”,新分类列表(){
{新日期时间(2011,01,16),1},
{新日期时间(2011,01,17),2},
{新日期时间(2011,01,18),3},
{新日期时间(2011,01,19),4}}
}
});
ds.securities.Add(“6753 JT”,新证券()
{
timeSeries=newdictionary(){
{“Mkt_Cap”,新分类列表(){
{新日期时间(2011,01,16),251},
{新日期时间(2011,01,17),252},
{新日期时间(2011,01,18),253},
{新日期时间(2011,01,19),254}},
{“流动性排名”,新分类列表(){
{新日期时间(2011,01,16),2},
{新日期时间(2011,01,17),3},
{新日期时间(2011,01,18),4},
{新日期时间(2011,01,19),1}}
}
});
ds.securities.Add(“6754 JT”,新证券()
{
timeSeries=newdictionary(){
{“Mkt_Cap”,新分类列表(){
{新日期时间(2011,01,16),203},
{新日期时间(2011,01,17),205},
{新日期时间(2011,01,18),207},
{新日期时间(2011,01,19),209}},
{“流动性排名”,新分类列表(){
{新日期时间(2011,01,16),3},
{新日期时间(2011,01,17),4},
{新日期时间(2011,01,18),1},
{新日期时间(2011,01,19),2}}
}
});
ds.securities.Add(“6755 JT”,新证券()
{
timeSeries=newdictionary(){
{“Mkt_Cap”,新分类列表(){
{新日期时间(2011,01,16),100},
{新日期时间(2011,01,17),101},
{新日期时间(2011,01,18),103},
{新日期时间(2011,01,19),104}},
{“流动性排名”,新分类列表(){
{新日期时间(2011,01,16),4},
{新日期时间(2011,01,17),1},
{新日期时间(2011,01,18),2},
{新日期时间(2011,01,19),3}}
}
});
//设置最小流动性秩
int MinLiqRank=2;
//初始查询以获取满足最小流动性等级的{Symbol,Date,Mkt_Cap}条目的序列。
var分录=来自ds.securities中的securityPair
来自securityPair.Value.timeSeries[“流动性等级”]中的估值
来自securityPair.Value.timeSeries[“Mkt_Cap”]中的估值
其中(估值=估值>最小利基值)&(估值>最小利基值)
选择新的
{
Symbol=securityPair.Key,
日期=估价_liq.Key,
MktCap=估价值
};      
//现在按日期分组
var groupedByDate=来自条目中的条目
按条目分组条目。日期输入日期
选择date.OrderByDescending(x=>x.MktCap)
.ThenBy(x=>x.Symbol)
.选择(x=>new
{
x、 符号,
x、 MktCap,
x、 日期
});
//最终结果应填入以下符号字典及其各自的Z分数时间序列
var zscoresult=新字典();
//计算每天的Z分数
bool useSamplestdev=true;
results.Add(new { Symbol = secRank.Symbol, Date = sec.First().Date, zScore = zScore });
if (!results.ContainsKey(secRank.Symbol))
   results.Add(secRank.Symbol,new SortedList<DateTime,double>()); 

results[secRank.Symbol].Add(sec.First().Date, zScore); 
var results = new List<object>();
var results = new Dictionary<string, SortedList<DateTime, double>>();