Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function called exp_average that computes the “exponentially weighted mo

ID: 3834259 • Letter: W

Question

Write a function called exp_average that computes the “exponentially weighted moving average,” or “exponential average” for short, of a sequence of scalars. The input sequence is provided to the function one element at a time and the function returns the current average each time. If we denote the nth element of the input sequence, that is, the function input at the nth invocation, by inn, then the rule for calculating the corresponding average outn that is to be returned by the function is: out1 = in1 outn = b inn + (1 - b) outn-1 where b is a coefficient between 0 and 1. You do not need to check b. In plain English, the current average depends on the current input and the previously computed average weighted by b and (1 - b), respectively. Here is how the function is expected to work. When called by two input arguments, the input sequence is reset, the first input argument is considered to be in1 and the second input argument is the value of the coefficient b. When called with a single input argument, it is considered to be int, that is, the current value of the input sequence. In both cases, the output is calculated according to the formula above. If the function is called for the very first time with a single input argument, the value of the coefficient b must default to 0.1. Hint: you should use two persistent variables to store the output and the coefficient b.

Explanation / Answer

public class MovingAvg {
    private static double b = 0.1;
    private static double out = 0;
    private static boolean start = true;
  
    static double exp_average(double in){
        if(start){
            out = in;
            start = false;
        }
        else {
            out = b*in + (1-b)*out;
        }
        return out;
    }
  
    static double exp_avg(double in, double b){
        MovingAvg.b = b;
        out = in;
        return out;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote