Question

So this is quite a complex problem and I googled alot about it but haven't really come up with anything. So the problem, I need to find the Standard Deviation of some variables within files. So let me state what I need to do: I have the code to find the average value of some numbers extracted from files. What I need to do with that average value is subtract it from the value in the files and then take that new value and square it.

Code to find the average value:

var query5 = from file in fileEntries             
 doc = XDocument.Load(file)              
let x = doc.Descendants("").Single()              
let y = doc.Descendants("").Single()              
let z = doc.Descendants("")Single()             
select new             
{                
 X1 = x.Element("Max").Value,                  
 X2 = x.Element("Min").Value,                  
 Y1 = y.Element("Max").Value,                  
 Y2 = y.Element("Min").Value,                  
 Z1 = z.Element("Max").Value,                 
 Z2 = z.Element("Min").Value             
 }; 
Was it helpful?

Solution

Given your above, if you want the population variance and standard deviation, you could do:

// ... code from above
double averageMaximumX = query.Average(t => double.Parse(t.XMax)); 
double varianceMaximumX = query.Sum(t => 
                Math.Pow(double.Parse(t.XMax) - averageMaximumX, 2)));

double stdDevMaximumX = Math.Sqrt(varianceMaximumX);
varianceMaximumX /= query.Count();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top