Created
October 18, 2022 21:36
-
-
Save MichalBrylka/46e44412377d82f3eae964babc25edbe to your computer and use it in GitHub Desktop.
GetAvgSentiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string[] symbolsUniverse = new[] { "AAPL", "VOD", "ADBE", "GRPN", "META", "ACER", "GOOG", "BP" }; | |
List<string> GenerateSymbols(int number) | |
{ | |
var symbols = new List<string>(); | |
for (int i = 0; i < symbolsUniverse.Length; i++) | |
{ | |
var bit = 1 << i; | |
if ((number & bit) == bit) | |
symbols.Add(symbolsUniverse[i]); | |
} | |
return symbols; | |
} | |
var rand = new Random(42); | |
var articles = Enumerable.Range(1, 255).Select(i => new ArticleWithSentiment( | |
GenerateSymbols(i), | |
new SentimentResponse(rand.NextDouble(), rand.NextDouble(), (SentimentResult)(i % 4)) | |
)).ToList(); | |
var symbol = "AAPL"; | |
var articlesAboutSymbol = articles.Where(a => a.Symbols.Contains(symbol)).ToList(); | |
var weights = articlesAboutSymbol.Select(a => a.Weight); | |
var positivesAvg = articlesAboutSymbol.Select(a => a.Response.PositiveScore * a.Weight).Sum() / weights.Sum(); | |
var negativesAvg = articlesAboutSymbol.Select(a => a.Response.NegativeScore * a.Weight).Sum() / weights.Sum(); | |
var sentimentsAvg = articlesAboutSymbol.Select(a => (int)a.Response.Sentiment * a.Weight).Sum() / weights.Sum(); | |
var sentimentsAvgEnum = (SentimentResult)(int)Math.Round(sentimentsAvg); | |
Console.Read(); | |
public record ArticleWithSentiment(IReadOnlyList<string> Symbols, SentimentResponse Response) | |
{ | |
public double Weight => 1.0 / (Symbols.Count * Symbols.Count); | |
} | |
public record SentimentResponse(double PositiveScore, double NegativeScore, SentimentResult Sentiment); | |
public enum SentimentResult { Neutral = 0, Positive = 1, Negative = 2, Mixed = 3, } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment