A theory for the economic operation of a smart grid with stochastic renewables, demand response and storage