Magnetic and valency instabilities in 4f compounds