Neurons in the primary visual cortex (V1) predominantly respond to a patch of the visual input, their classical receptive field. These responses are modulated by the visual input in the surround [2]. This reflects the fact that features in natural scenes do not occur in isolation: lines, surfaces are generally continuous, and the surround provides context for the information in the classical receptive field. It is generally assumed that the information in the near surround is transmitted via lateral connections between neurons in the same area [2]. A series of large scale efforts have recently described the relation between lateral connectivity and visual evoked responses and found like-to-like connectivity between excitatory neurons [16,18]. Additionally, specific cell type connectivity for inhibitory neuron types has been described [11,31]. Current normative models of cortical function relying on sparsity [27], saliency [4] predict functional inhibition between similarly tuned neurons. What computations are consistent with the observed structure of the lateral connections between the excitatory and diverse types of inhibitory neurons?We combined natural scene statistics [24] and mouse V1 neuron responses [7] to compute the lateral connections and computations of individual neurons which optimally integrate information from the classical receptive field with that from the surround by directly implementing Bayes rule. This increases the accuracy of representation of a natural scene under noisy conditions. We show that this network has like-to-like connectivity between excitatory neurons, similar to the observed one [16,18,11], and has three types of inhibition: local normalization, surround inhibition and gating of inhibition from the surround -that can be attributed to three classes of inhibitory neurons. We hypothesize that this computation: optimal integration of contextual cues with a gate to ignore context when necessary is a general property of cortical circuits, and the rules constructed for mouse V1 generalize to other areas and species.