Statistical mechanical basis and algorithms for constructing coarse grained models of molecular liquids and biological structures