I have some distribution X of values (which I don't know exactly but I can sample many times). I also have a function $f : X \to Y$ which may be complicated. I want to approximate $f$ with a piecewise constant function $g$, where the number of pieces is constant but I can choose the intervals, and where I minimize $|f - g|^2$.
Is this a studied problem? Are there some relatively simple ways of doing this in a good way?