Rational herd behavior and informationally efficient security prices have long been considered to be mutually exclusive but for exceptional cases. In this paper we describe the conditions on the underlying information structure that are necessary and sufficient for informational herding and contrarianism. In a standard sequential security trading model, subject to sufficient noise trading, people herd if and only if, loosely, their information is sufficiently dispersed so that they consider extreme outcomes more likely than moderate ones. Likewise, people act as contrarians if and only if their information leads them to concentrate on middle values. Both herding and contrarianism generate more volatile prices, and they lower liquidity. They are also resilient phenomena, although by themselves herding trades are self-enforcing whereas contrarian trades are self-defeating. We complete the characterization by providing conditions for the absence of herding and contrarianism.