function to calculate g matrix added to inference.jl