Many farm animals live in non natural environments – grouped in high density, confined space, single boxes, etc. (Bergeron et al., 2006). There is a positive correlation between the poor quality of the environment in which the animals are in and the observation of stereotypies (Bergeron et al., 2006). The reason why the tendency of stereotypies increases in poorly managed environments is because of the animals’ frustration that motivates them to attempt to escape from their environment (Bergeron et al., 2006). In fact, the environments in which they live in usually discourage natural behaviors such as: inability to forage, interact with mates etc. (Bergeron et al., 2006). When the animals are switched from low quality environment to a better quality one, stereotypies can decrease by up to 50% (Bergeron et al., 2006).
Please take a moment and observe the following images, these images are images of common housing systems of farm animals:

PC: University of Massachusetts Amherst

PC: StockClip

PC: University of Minnesota

PC: Pet Care ASPCA