public AccuracyEstimates CalculateEstimates(ICollection <Record> records) { var accuracyEstimates = new AccuracyEstimates(); var polynomial = _regressionService.CalculateRegression(records); var approximatedPairs = records.Select((record) => { double output = 0.0; for (int i = 1; i <= record.Inputs.Count; i++) { output += record.Inputs.ElementAt(i - 1) * polynomial.ElementAt(i); } return(new Record { Inputs = record.Inputs, Output = output + polynomial.ElementAt(0) }); }).ToArray(); accuracyEstimates.ApproximationOutputs = approximatedPairs.Select(pair => pair.Output).ToArray(); accuracyEstimates.SquareSumMax = records.Join(approximatedPairs, r => r.Inputs, est => est.Inputs, (r, est) => new { DiscreteOutput = r.Output, EstimatedOutput = est.Output }).Max(pair => Math.Pow(pair.DiscreteOutput - pair.EstimatedOutput, 2)); var outputsAvg = records.Select(record => record.Output).Average(); var approximatedPairsAvg = approximatedPairs.Select(record => record.Output).Average(); var outputCov = records .Zip(approximatedPairs, (o, a) => (o.Output - outputsAvg) * (a.Output - approximatedPairsAvg)) .Sum(); var outputDispX = records.Sum(x => Math.Pow(x.Output - outputsAvg, 2)); var outputDispY = approximatedPairs.Sum(x => Math.Pow(x.Output - approximatedPairsAvg, 2)); var outputDispMult = Math.Sqrt(outputDispX * outputDispY); accuracyEstimates.Correlation = outputCov / outputDispMult; return(accuracyEstimates); }
public IActionResult ProcessData([FromBody] DatasetProcessingModel dataset, [FromQuery] int?digits) { _logger.LogInformation($"[{DateTime.Now}] Params: statistic size: {dataset.Records.Count()}"); ICollection <double> poly = _regressionService.CalculateRegression( dataset.Records.Select(row => new Record { Inputs = row.Inputs, Output = row.Output }).ToArray()); if (digits.HasValue) { poly = poly.Select(number => Math.Round(number, digits.Value)).ToArray(); } return(Ok(new PolynomialModel { Koeficients = poly })); }
public async Task <IActionResult> CalculateEquation(Guid id, [FromQuery] int?digits) { _logger.LogInformation($"[{DateTime.Now}] Calculate equation for dataset #{id}"); var dataset = await _datasetService.GetDatasetById(id); ICollection <double> poly = _regressionService.CalculateRegression( dataset.Records.Select(row => new Record { Inputs = row.Inputs, Output = row.Output }).ToArray()); if (digits.HasValue) { poly = poly.Select(number => Math.Round(number, digits.Value)).ToArray(); } return(Ok(new PolynomialModel { Koeficients = poly })); }
public IActionResult Recommendations() { var userId = _jwt.GetUserIdFromContext(HttpContext.User); if (userId == null) { return(Unauthorized()); } try { var player = _context .Players .Include(p => p.Likes) .ThenInclude(l => l.Game) .ThenInclude(g => g.Profile) .ThenInclude(p => p.Conflict) .Include(p => p.Likes) .ThenInclude(l => l.Game) .ThenInclude(g => g.Profile) .ThenInclude(p => p.Agency) .Include(p => p.Likes) .ThenInclude(l => l.Game) .ThenInclude(g => g.Profile) .ThenInclude(p => p.Appearance) .Include(p => p.Likes) .ThenInclude(l => l.Game) .ThenInclude(g => g.Profile) .ThenInclude(p => p.Investment) .Include(p => p.Likes) .ThenInclude(l => l.Game) .ThenInclude(g => g.Profile) .ThenInclude(p => p.Rules) .First(p => p.User.Id == userId); var randomNotLikedGames = _context.Games .FromSql("SELECT TOP (31) * from Games " + " WHERE Id NOT IN (SELECT g.Id FROM Games g " + "INNER JOIN Likes l ON g.Id = l.GameId " + "INNER JOIN Players p ON l.PlayerId = p.Id " + "INNER JOIN Users u ON p.Id = u.PlayerId " + "WHERE u.Id = {0})", userId) .Include(g => g.Profile) .ThenInclude(g => g.Agency) .Include(g => g.Profile) .ThenInclude(g => g.Appearance) .Include(g => g.Profile) .ThenInclude(g => g.Conflict) .Include(g => g.Profile) .ThenInclude(g => g.Investment) .Include(g => g.Profile) .ThenInclude(g => g.Rules) .ToList(); _regressionService.CalculateRegression(player, randomNotLikedGames); _context.SaveChanges(); var notLikedGames = _context.Games .FromSql("SELECT * from Games " + " WHERE Id NOT IN (SELECT g.Id FROM Games g " + "INNER JOIN Likes l ON g.Id = l.GameId " + "INNER JOIN Players p ON l.PlayerId = p.Id " + "INNER JOIN Users u ON p.Id = u.PlayerId " + "WHERE u.Id = {0})", userId) .Include(g => g.Profile) .ThenInclude(g => g.Agency) .Include(g => g.Profile) .ThenInclude(g => g.Appearance) .Include(g => g.Profile) .ThenInclude(g => g.Conflict) .Include(g => g.Profile) .ThenInclude(g => g.Investment) .Include(g => g.Profile) .ThenInclude(g => g.Rules) .ToList(); var recommendedGames = _regressionService .OrderGameListByRegressionScore(player, notLikedGames) .ToArray(); var recommendations = new long[recommendedGames.Length]; for (var i = 0; i < recommendedGames.Length; i++) { recommendations[i] = recommendedGames[i].Id; } return(Ok(recommendations)); } catch (Exception e) { Console.WriteLine(e); return(BadRequest(e)); } }