1.2.18 Variance for accumulator. Validate that the following code, which adds the methods var () and stddev () to Accumulator, computes both the mean and variance of the numbers presented as arguments to addDataValue(): public class Accumulator { private double m; private double s; private int N; public void addDataValue (double x) { N++; 1.0 (N-1) / N * (x m) * (x

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question
1.2.18 Variance for accumulator. Validate that the following code, which adds the
methods var () and stddev () to Accumulator, computes both the mean and variance
of the numbers presented as arguments to addDataValue():
public class Accumulator
{
}
private double m;
private double s;
private int N;
public void addDataValue (double x)
{
}
N++;
S = s + 1.0 * (N-1) / N * (x - m) * (x - m);
m = m + (x - m) / N;
public double mean()
{ return m; }
public double var()
{ return s/(N - 1); }
public double stddev()
{ return Math.sqrt(this.var()); }
This implementation is less susceptible to roundoff error than the straightforward im-
plementation based on saving the sum of the squares of the numbers.
Transcribed Image Text:1.2.18 Variance for accumulator. Validate that the following code, which adds the methods var () and stddev () to Accumulator, computes both the mean and variance of the numbers presented as arguments to addDataValue(): public class Accumulator { } private double m; private double s; private int N; public void addDataValue (double x) { } N++; S = s + 1.0 * (N-1) / N * (x - m) * (x - m); m = m + (x - m) / N; public double mean() { return m; } public double var() { return s/(N - 1); } public double stddev() { return Math.sqrt(this.var()); } This implementation is less susceptible to roundoff error than the straightforward im- plementation based on saving the sum of the squares of the numbers.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning