Optical excitations in lanthanide and actinide compounds