Generalize g(x) correctly for n samples