Analyzing stock market data using Hidden Markov Models

Let's analyze stock market data using Hidden Markov Models. Stock market data is a good example of time series data where the data is organized in the form of dates. In the dataset that we will use, we can see how the stock values of various companies fluctuate over time. Hidden Markov Models are generative models that are used to analyze such time series data. In this recipe, we will use these models to analyze stock values.

How to do it…

  1. Create a new Python file, and import the following packages:
    import datetime
    
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.finance import quotes_historical_yahoo_ochl
    from hmmlearn.hmm import GaussianHMM
  2. Get the stock quotes from Yahoo finance. There is a method available in matplotlib to load this directly:
    # Get quotes from Yahoo finance
    quotes = quotes_historical_yahoo_ochl("INTC", 
            datetime.date(1994, 4, 5), datetime.date(2015, 7, 3))
  3. There are six values in each quote. Let's extract the relevant data such as the closing value of the stock and the volume of stock that is traded along with their corresponding dates:
    # Extract the required values
    dates = np.array([quote[0] for quote in quotes], dtype=np.int)
    closing_values = np.array([quote[2] for quote in quotes])
    volume_of_shares = np.array([quote[5] for quote in quotes])[1:]
  4. Let's compute the percentage change in the closing value of each type of data. We will use this as one of the features:
    # Take diff of closing values and computing rate of change
    diff_percentage = 100.0 * np.diff(closing_values) / closing_values[:-1]
    
    dates = dates[1:]
  5. Stack the two arrays column-wise for training:
    # Stack the percentage diff and volume values column-wise for training
    X = np.column_stack([diff_percentage, volume_of_shares])
  6. Train the HMM using five components:
    # Create and train Gaussian HMM 
    print "
    Training HMM...."
    model = GaussianHMM(n_components=5, covariance_type="diag", n_iter=1000)
    
    model.fit(X)
  7. Generate 500 samples using the trained HMM and plot this, as follows:
    # Generate data using model
    num_samples = 500 
    samples, _ = model.sample(num_samples) 
    plt.plot(np.arange(num_samples), samples[:,0], c='black')
    
    plt.show()
  8. The full code is given in hmm_stock.py that is already provided to you. If you run this code, you will see the following figure:
    How to do it…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.151.153