1

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             
 }; 
  • See [this][1] question for an extension method to do what you want [1]: http://stackoverflow.com/questions/2253874/linq-equivalent-for-standard-deviation – Bob Vale Sep 21 '11 at 01:23

1 Answers1

0

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();
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks!! Wrapping my mind around what to do with the number each time just confused me and feeding it where. –  Sep 21 '11 at 01:37
  • I think you got your x^y backwards in your `Math.Pow`. It goes `Math.Pow(base, power)` – Brad Sep 21 '11 at 01:38
  • Also isn't the variance the (value-average) squared divided by the count? –  Sep 21 '11 at 01:42
  • yes, so his `varianceMaximumX` should be a `varianceMaximumXNumerator` variable then divide that by your degrees of freedom (usually not count unless you are doing a population stdev) to get the variance. – Brad Sep 21 '11 at 01:44