Microscopic analysis of the characteristic energy loss spectra of 3d-and 4d-transition metal carbides and nitrides