我编写了一个查询,对给定日期的所有值执行Z-Score计算。计算似乎很好,但我无法将此查询的结果转换为函数可以返回的格式。 Z分数以{symbol,date,z-score}的格式放入List<object>
。问题实际上是如何将List<object>
转换为我需要的格式。
我希望它返回一个Dictionary<string, SortedList<DateTime, double>>
,其中包含安全字符串和一个包含所有日期和时间的排序列表。该安全性的z-score值对。
即。 Dictionary<security, SortedList<Dates, Z-Scores>>
查询计算是正确的(我认为; - &gt;),但任何有关改进查询的建议也会受到赞赏,因为我仍然是一个非常挑战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;
}
}
}
非常感谢任何协助。
答案 0 :(得分:1)
这个问题没有得到适当关注,而且代码太多,细节也不必要。所以没有答案。我把它修剪下来并重新发布。看看对转贴的回应,两点变得清晰起来。
在想到之后,我突然意识到,我可以跳过整个问题,只需将结果插入到我想要的形式的字典中,因为我通过计算分数来迭代......呃! / p>
我修改了以下代码:
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>>();
我从这一切中学到了什么?那些没有答案的问题可能是一个很难想到的问题。