Your stocks do not deprive anyone else of an essential human need, while owning and renting out a house you do not personally use artificially deprives another of buying that house, which further raises housing prices, making an essential human need an investment vehicle.
Using a different analogy, if you lived in an area with scarce water resources, but happened to purchase land with a particularly abundant spring, you could then profit handsomely by selling that water to the thirsty at an extremely high rate, exploiting the human need for water in for your individual profit.