Is there a formula for heat retained by an atmosphere, based on density and composition?
Earth's atmosphere is composed of a number of layers: troposphere, stratosphere, mesosphere, thermosphere and exosphere, ascending in order from the surface.

Each layer has different characteristics and within each layer the temperature profile is different. The red line in the following US standard atmosphere graph shows the temperature profile of the atmosphere.

The temperature profile is complex and is not related to air density (the amber line on the graph) above the tropopause. 

There are higher temperatures on the Earth's surface and at the stratopause but low temperatures at the tropopause and mesopause.



As for carbon dioxide, it is fairly well mixed in the atmosphere, particularly at altitude. Concentrations of carbon dioxide vary laterally near the Earth's surface, with the concentrated regions occurring in areas of major human activity, as can be seen from the NASA produced picture below.