Convex optimization for nonparametric estimation and test