Learning the Dynamics for Anomaly Detection in Wireless Sensor Networks