Book Image

Haskell Data Analysis cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis cookbook

By: Nishant Shukla

Overview of this book

Step-by-step recipes filled with practical code samples and engaging examples demonstrate Haskell in practice, and then the concepts behind the code. This book shows functional developers and analysts how to leverage their existing knowledge of Haskell specifically for high-quality data analysis. A good understanding of data sets and functional programming is assumed.
Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Calculating a moving average


Summarizing a list of numbers into one representative number can be done by calculating the average. The equation for the arithmetic mean is to add up all the values and divide by the number of values. However, if the values being summed over are extremely large, the total sum may overflow.

In Haskell, the range for Int is at least from -2^29 to 2^29-1. Implementations are allowed to have an Int type with a larger range. If we try to naively average the numbers 2^29-2 and 2^29-3 by first calculating their sum, the sum may overflow, producing an incorrect calculation for the average.

A moving average (or running average) tries to escape this drawback. We will use an exponential smoothing strategy, which means numbers that were seen previously contribute exponentially less to the value of the running mean. An exponential moving average reacts faster to recent data. It can be used in situations for detecting price oscillations or spiking a neuron in a neural network...