compute cost function in python code