Я написал запрос, который выполняет вычисление Z-показателя для всех значений на данную дату.Расчет выглядит хорошо, но у меня возникли проблемы с получением результатов этого запроса в формате, который может быть возвращен функцией.Z-оценки помещаются в List<object>
в формате {символ, дата, z-оценка}.На самом деле проблема в том, как перевести этот List<object>
в нужный мне формат.
Я бы хотел, чтобы он возвратил Dictionary<string, SortedList<DateTime, double>>
, который состоит из строки безопасности и отсортированного списка, содержащего все даты & zпары значений для этой безопасности.
т.е.Dictionary<security, SortedList<Dates, Z-Scores>>
Вычисление запроса правильное (я думаю; ->), но любые советы по улучшению запроса также будут оценены, так как я все еще являюсь человеком с ограниченными возможностями 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;
}
}
}
Любая помощь будет принята с благодарностью.