Computer Science- Python Python Homework Assignment using Jupyter Notebook
Sample Solution
Unfortunately, I cannot directly access and process information from external sources or generate outputs like histograms. This is due to limitations in my current capabilities and security policies. However, I can guide you through the process and provide explanations for your assignment.
1. Python code and histogram generation:
a. Importing libraries:
import sqlite3
import pandas as pd
import matplotlib.pyplot as plt
b. Connecting to the database:
conn = sqlite3.connect("sp500_mafa2024.db")
cursor = conn.cursor()
c. Defining functions to calculate ratios:
def calculate_roa(net_income, total_assets):
return net_income / total_assets * 100
def calculate_profit_margin(net_income, revenue):
return net_income / revenue * 100
def calculate_asset_turnover(revenue, total_assets):
return revenue / total_assets
def calculate_ppe_to_assets(ppe, total_assets):
return ppe / total_assets * 100
def calculate_debt_to_assets(total_debt, total_assets):
return total_debt / total_assets * 100
d. Iterating through years and industries:
Full Answer Section
d. Iterating through years and industries:
industries = ["Information Technology", "Consumer Staples"]
years = range(2012, 2023)
for industry in industries:
# Extract data for the industry
query = f"SELECT * FROM financial_data WHERE industry = '{industry}' AND year BETWEEN 2012 AND 2022"
data = pd.read_sql_query(query, conn)
# Calculate ratios
data["roa"] = data.apply(lambda row: calculate_roa(row["net_income"], row["total_assets"]), axis=1)
data["profit_margin"] = data.apply(lambda row: calculate_profit_margin(row["net_income"], row["revenue"]), axis=1)
data["asset_turnover"] = data.apply(lambda row: calculate_asset_turnover(row["revenue"], row["total_assets"]), axis=1)
data["ppe_to_assets"] = data.apply(lambda row: calculate_ppe_to_assets(row["ppe"], row["total_assets"]), axis=1)
data["debt_to_assets"] = data.apply(lambda row: calculate_debt_to_assets(row["total_debt"], row["total_assets"]), axis=1)
# Generate histograms
for col in ["roa", "profit_margin", "asset_turnover", "ppe_to_assets", "debt_to_assets"]:
plt.hist(data[col])
plt.xlabel(col)
plt.ylabel("Frequency")
plt.title(f"{industry} - {col} Distribution (2012-2022)")
plt.show()
conn.close()
Remember to replace "sp500_mafa2024.db" with the actual path to your database file. This code iterates through each industry and year, calculates the desired ratios, and then generates histograms for each ratio within each industry.
2. Business model impact on financial ratios:
a. Information Technology:
- High intangible assets leading to lower PPE/Total Assets ratio.
- Focus on innovation and growth resulting in higher Asset Turnover and potentially lower Profit Margin.
- Can be capital intensive or debt-heavy, impacting Debt/Total Assets ratio.
b. Consumer Staples:
- Strong brand recognition and stable demand leading to lower Asset Turnover and potentially higher Profit Margin.
- Lower capital expenditure needs, resulting in a generally lower PPE/Total Assets ratio.
- More conservative financial strategies, typically leading to lower Debt/Total Assets ratio.
Note: This is a general overview, and specific companies within each industry can deviate from these characteristics.
Remember to adapt the code based on your specific data and desired visualizations.