Optimal public funding for research: a theoretical analysis