Comprehensive Stock Analysis Using Python: Insights, Statistics, and Visualization

Comprehensive Stock Analysis Using Python: Insights, Statistics, and Visualization


The world of financial analysis is driven by data, insights, and actionable results. This article provides a complete guide to analyzing stock data using Python, focusing on statistical summaries and visualizations. We'll use historical stock data to calculate key metrics like minimum, maximum, median, and quartiles, and visualize them with annotated boxplots. By the end of this article, you'll have a robust, reusable framework for stock price analysis.


1. Introduction

Financial markets are complex systems influenced by multiple factors. Understanding stock price behavior is critical for informed decision-making. With tools like Python, we can automate this process to derive deeper insights. This guide is centered on:

  • Fetching historical stock data.
  • Calculating summary statistics.
  • Visualizing price distributions using boxplots.

We'll work with Apple's (AAPL) stock data, retrieved from Yahoo Finance, covering the period from 2016-01-01 to 2018-01-01.


2. Setting Up the Environment

Required Libraries

To start, install the necessary Python libraries. These tools simplify data retrieval, manipulation, and visualization:

  • yfinance: For fetching stock data.
  • pandas: For data manipulation.
  • matplotlib: For visualizing data.

Install these libraries using:

pip install yfinance pandas matplotlib
        

Import Libraries

Import the required libraries into your Python environment:

import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
        

3. Fetching and Preparing Data

Fetching Stock Data

We fetch historical stock data for Apple (AAPL) from Yahoo Finance for the date range 2016-01-01 to 2018-01-01.

symbol = 'AAPL'
start_date = '2016-01-01'
end_date = '2018-01-01'

# Download stock data
stock_data = yf.download(symbol, start=start_date, end=end_date)
        

Exploring the Data

The data contains columns such as Open, High, Low, Close, Adj Close, and Volume. We'll focus on Adj Close for our analysis.

# Display first and last rows of the dataset
print(stock_data.head())
print(stock_data.tail())
        

Adjusted Close Prices

Extract and round the adjusted closing prices:

stock_data['Adjusted Price'] = stock_data['Adj Close'].round(2)
price = stock_data['Adjusted Price']
        

4. Calculating Summary Statistics

Key Metrics

We calculate the following metrics:

  • Minimum Price: Lowest adjusted close price.
  • Maximum Price: Highest adjusted close price.
  • Median Price: Middle value of the price distribution.
  • Quantiles: 25th, 50th (median), and 75th percentiles.
  • Five-Number Summary: Min, Q1, Median, Q3, and Max.

# Summary statistics
price_min = price.min()
price_max = price.max()
price_median = price.median()
price_quantiles = price.quantile([0.25, 0.5, 0.75])
five_num_summary = price.describe()[['min', '25%', '50%', '75%', 'max']]

# Display results
print("Minimum Price:", price_min)
print("Maximum Price:", price_max)
print("Median Price:", price_median)
print("Quantiles (25%, 50%, 75%):\n", price_quantiles)
print("Five-Number Summary:\n", five_num_summary)
        

Output : Summary Statistics for AAPL Minimum Price: 20.7 Maximum Price: 236.22 Median Price: 85.19 Quantiles (25%, 50%, 75%): 0.25     40.605 0.50     85.190 0.75    155.275 Name: Adjusted Price, dtype: float64 Five-Number Summary: min     20.700 25%     40.605 50%     85.190 75%    155.275 max    236.220          


5. Visualizing Stock Data

Boxplots provide a visual summary of the data, showing distributions, medians, and outliers.

Horizontal and Vertical Boxplots

We create two types of boxplots—horizontal and vertical—to highlight flexibility in data presentation.

plt.figure(figsize=(12, 6))
plt.suptitle(f"{symbol} Stock Price Analysis ({start_date} to {end_date})", fontsize=16)

# Horizontal Boxplot
plt.subplot(1, 2, 1)
plt.boxplot(price, vert=False)
plt.title('Horizontal Boxplot')
plt.xlabel('Price (USD)')
plt.axvline(x=price_min, color='blue', label='Min')
plt.axvline(x=price_max, color='yellow', label='Max')
plt.axvline(x=price_median, color='green', label='Median')
plt.legend()

# Vertical Boxplot
plt.subplot(1, 2, 2)
plt.boxplot(price)
plt.title('Vertical Boxplot')
plt.ylabel('Price (USD)')
plt.axhline(y=price_min, color='blue', label='Min')
plt.axhline(y=price_max, color='yellow', label='Max')
plt.axhline(y=price_median, color='green', label='Median')
plt.legend()

plt.tight_layout(rect=[0, 0, 1, 0.95])
plt.show()
        


Article content

Annotated Boxplots

To provide additional context, we annotate the boxplot with statistical values.

plt.figure(figsize=(8, 4))
plt.boxplot(price, vert=False, patch_artist=True)
plt.title('Boxplot Summary with Annotations', fontsize=14)
plt.xlabel('Price (USD)')

# Annotate statistical values
five_num = price.describe()[['min', '25%', '50%', '75%', 'max']]
plt.text(x=five_num['min'], y=1.1, s=f"Min: {five_num['min']:.2f}")
plt.text(x=five_num['25%'], y=1.1, s=f"Q1: {five_num['25%']:.2f}")
plt.text(x=five_num['50%'], y=1.1, s=f"Median: {five_num['50%']:.2f}")
plt.text(x=five_num['75%'], y=1.1, s=f"Q3: {five_num['75%']:.2f}")
plt.text(x=five_num['max'], y=1.1, s=f"Max: {five_num['max']:.2f}")

plt.show()
        


Article content


6. Insights from the Analysis

Observations

  • The minimum price highlights the lowest value during the given period, representing potential support levels.
  • The maximum price shows the highest value, indicating resistance levels.
  • The boxplot provides a clear overview of price variability and outliers.

Applications

  1. Trading Strategies: Use historical prices and boxplots to identify patterns and anomalies.
  2. Risk Management: Quantiles and five-number summaries can aid in assessing potential losses or gains.
  3. Portfolio Analysis: Combine insights with other stocks for diversification.


7. Conclusion

This Python-based framework provides a structured approach to stock price analysis:

  1. Fetch data effortlessly using yfinance.
  2. Calculate meaningful statistics to understand price distributions.
  3. Visualize data effectively with annotated boxplots.

For any inquiries or to collaborate on advanced financial modeling, feel free to reach out to me, Anand Damdiyal, founder of Spacewink, at founder@spacewink.com. Let’s build the future of finance together!



Interesting information da

Impressive use of Python for stock analysis! Leveraging data-driven insights is a game-changer for traders. Looking forward to seeing more innovative approaches like this—collaboration sounds exciting! Anand Damdiyal

Hema Bhatt

Creative content writer and designer with a flair for storytelling and impactful visual communication.

5mo

Very informative

To view or add a comment, sign in

More articles by Anand Damdiyal

Insights from the community

Others also viewed

Explore topics