Scatter plots in Python

import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

def symbol_to_path(symbol, base_dir="data"):
    """Return CSV file path given ticker symbol."""
    return os.path.join(base_dir, "{}.csv".format(str(symbol)))

def get_data(symbols, dates):
    """Read stock data (adjusted close) for given symbols from CSV files."""
    df = pd.DataFrame(index=dates)
    if 'NSE' not in symbols:  # add SPY for reference, if absent
        symbols.insert(0, 'NSE')

    for symbol in symbols:
        df_temp = pd.read_csv(symbol_to_path(symbol), index_col='Date',
                              parse_dates=True, usecols=['Date', 'Adj Close'], na_values=['nan'])
        df_temp = df_temp.rename(columns={'Adj Close': symbol})
        df = df.join(df_temp)
        if symbol == 'NSE':  # drop dates SPY did not trade
            df = df.dropna(subset=["NSE"])

    return df

def plot_data(df, title, ylabel):
    """Plot stock prices with a custom title and meaningful axis labels."""
    ax = df.plot(title=title, fontsize=12)

def compute_daily_returns(df):
    daily_returns = df.copy()
    # Compute daily rerurns for row 1 onwards
    #daily_returns[1:] = (df[1:] / df[:-1].values) -1
    daily_returns = (df / df.shift(1)) -1
    daily_returns.ix[0, :] = 0 #set daily returns for row 0 to 0
    return daily_returns

def plot_histogram(daily_returns):
    mean = daily_returns.mean()
    std = daily_returns.std()

    plt.axvline(x=mean, color='r', linestyle='--')
    plt.axvline(x=std, color='k', linestyle='--')
    plt.axvline(x=-std, color='k', linestyle='--')

def test_run():
    dates = pd.date_range('2014-01-01', '2014-12-31')
    symbols = ['NSE', 'HDFCBANK', 'ITC']
    df = get_data(symbols, dates)

    daily_returns = compute_daily_returns(df)

    daily_returns.plot(kind='scatter', x='NSE', y='HDFCBANK')
    beta_hdfcbank, aplha_hdfcbank = np.polyfit(daily_returns['NSE'], daily_returns['HDFCBANK'],1)
    plt.plot(daily_returns['NSE'], beta_hdfcbank*daily_returns['NSE'] + aplha_hdfcbank, '-', color='r')
    print("beta_hdfc", beta_hdfcbank)
    print("aplha_hdfc", aplha_hdfcbank)

    daily_returns.plot(kind='scatter', x='NSE', y='ITC')
    beta_itc, aplha_itc = np.polyfit(daily_returns['NSE'], daily_returns['ITC'], 1)
    plt.plot(daily_returns['NSE'], beta_itc * daily_returns['NSE'] + aplha_itc, '-', color='r')
    print("beta_itc", beta_itc)
    print("alpha_itc", aplha_itc)


if __name__ == "__main__":

Scatter plot

(Visited 39 times, 1 visits today)


Eyeing investment. Please note that the views given in this website are meant for reference and guidance of the readers to explore further on the topics and take informed decisions. These should not be construed as investment advice or legal opinion.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
29 − 21 =