Previous theoretical approaches relating neural activity in the primary visual cortex to formal computational goals that V1 might be optimized for focused mostly on the filter-like properties of cells or on related features such as contrast invariance. The key insights gained from these studies were that V1 neurons (simple cells, in particular) can be seen as implementing an efficient code for statistical estimates of natural scene stimuli and that this code can be learned from a set of natural image patches. However, this approach only accounts for the mean responses of cells averaged across multiple presentations of the same (set of) stimuli, and therefore completely neglects the rich within-trial dynamical interactions between cells. For the same reason, it is also incapable of accounting for the richly structured spontaneous activity in V1. In order to better understand the relation of the intrinsic dynamics of V1 to its computational role, we explored the idea that activity in V1 reflects sampling of the ‘recognition distribution’, the probability distribution of possible hypotheses that are congruent with both the present and past inputs to the system. We also used this sampled approximation to the true recognition distribution in a variant of the expectation-maximization algorithm to adapt the synaptic weights between cells so that they form the efficient code hypothesized in earlier work. Beyond reproducing the linear filter properties of simple cells, our results also account for temporal and spatial correlations between cells as seen in multielectrode recordings, and give a normative account of the experimentally observed close correspondence between spontaneous and stimulus-driven network activity in V1.