Starting from the first principle I derive an unsupervised learning method named even code to model local statistics of natural images. The first version uses orthogonal bases with independent states to model simple probability distribution of a few pixels. The second version uses a microscopic loss function to learn a nonlinear sparse binary representation of image patches. The distance in the binary representation space reflects image patch similarity. The learned model also has local edge detecting and orientation selective units like early visual systems.