Stochastic theory for classical and quantum mechanical systems