Python
Python
readability. It emphasizes code readability with its clear and expressive syntax, making it
an ideal language. Python offers:
• Libraries: Python boasts powerful libraries like NumPy, SciPy, and Pandas, which
provide efficient data structures and functions for numerical computing, scientific
computing, and data manipulation, respectively.
• Data Visualization: Libraries such as Matplotlib, Seaborn, and Plotly enable users
to create visually appealing plots and charts to explore and represent statistical
data effectively.
• Statistical Modeling: Python offers libraries like Statsmodels and Scikit-learn for
statistical modeling, hypothesis testing, regression analysis, machine learning, and
predictive analytics.
• Data Analysis: Pandas, a widely-used library in Python, provides data structures
like DataFrames that facilitate data manipulation, cleaning, and analysis. It allows
statisticians to perform various operations such as filtering, grouping, aggregating,
and merging datasets efficiently.
• Probability Distributions: Python has built-in functions and libraries (e.g.,
SciPy.stats) for working with probability distributions, generating random
numbers, calculating cumulative distribution functions (CDFs), probability density
functions (PDFs), and conducting statistical tests based on different distributions.
import pandas as pd
• SciPy: SciPy builds on NumPy and provides a large number of functions that
operate on NumPy arrays and are useful for scientific and technical computing.
Commonly Used in Advanced statistical functions, optimization, integration, and
signal processing. Code-
from scipy import stats
data = [1, 2, 3, 4, 5]
t_stat, p_val = stats.ttest_1samp(data, 3)
• Statsmodels: Statsmodels is a module that provides classes and functions for the
estimation of many different statistical models, as well as for conducting statistical
tests and data exploration. Commonly Used in Linear regression, logistic
regression, time series analysis. Code-
import statsmodels.api as sm
data = sm.datasets.get_rdataset("mtcars").data
X = data[['mpg', 'hp']]
Y = data['wt']
X = sm.add_constant(X)
model = sm.OLS(Y, X).fit()
print(model.summary())
Code:
import numpy as np
# Vector u
u = np.array([1, 1, 2, 3, 5, 8])
# Column vector v
v = np.array([[1], [1], [2], [3], [5], [8]])
# Identity matrix x
x = np.array([[1, 0], [0, 1]])
# Matrix y
y = np.array([[1, 2], [3, 4]])
# Matrix z
z = np.array([
[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2]
])
# Matrix w formed by placing x and y side by side
# First, create block matrix with repeated x and y matrices
w = np.block([
[x, x],
[y, y]
])
# Print the arrays to verify
print("u:", u)
print("v:\n", v)
print("x:\n", x)
print("y:\n", y)
print("z:\n", z)
print("w:\n", w)
What command would select x from w?
Code:
import numpy as np
# Identity matrix x
x = np.array([[1, 0], [0, 1]])
# Matrix y
y = np.array([[1, 2], [3, 4]])
# Constructing matrix w
w = np.block([
[x, x],
[y, y]
])
# Selecting x from w (top left 2x2 submatrix)
x_from_w = w[:2, :2]
# Print the extracted x to verify
print("w:\n", w)
print("Extracted x from w:\n", x_from_w)
Output: w=
[[1 0 1 0]
[0 1 0 1]
[1 2 1 2]
[3 4 3 4]]
Extracted x from w:
[[1 0]
[0 1]]
What command would select [x′ y’]′ from w? Is there more than one? If there
are, list all alternatives.
Code:
import numpy as np
# Identity matrix x
x = np.array([[1, 0], [0, 1]])
# Matrix y
y = np.array([[1, 2], [3, 4]])
# Constructing matrix w
w = np.block([
[x, x],
[y, y]
])
# Selecting [x'y']' from w
xy_prime = w[:, [0, 2]]
# Print the extracted [x'y']' to verify
print("w:\n", w)
print("Extracted [x'y']' from w:\n", xy_prime)
Output: w:
[[1 0 1 0]
[0 1 0 1]
[1 2 1 2]
[3 4 3 4]]
Extracted [x'y']' from w:
[[1 1]
[0 0]
[1 1]
[3 3]]
Alternate:
Explanation
In this case, the two expressions (𝑥𝑥 + 𝑦𝑦)2 and 𝑥𝑥 2 + 𝑥𝑥 ⋅ 𝑦𝑦 + 𝑦𝑦 ⋅ 𝑥𝑥 + 𝑦𝑦 2 are indeed the
same. This is because matrix addition and multiplication follow the distributive property
similar to scalar arithmetic. Here's why:
[[ 6. 7. 8.]
[ 9. 10. 11.]]]
Original shape of x: (3, 4)
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]]
Code:
# Define the array x
x = np.reshape(np.arange(12.0), (4, 3))
# Use ravel, flatten, and flat to extract elements 1, 3, ..., 11
indices = np.arange(1, 12, 2)
extracted_elements = [x.ravel()[i] for i in indices]
print("Extracted elements:", extracted_elements)
Output:
Extracted elements: [1.0, 3.0, 5.0, 7.0, 9.0, 11.0]
Code:
# Define arrays x, y, and z
x = np.array([[1, 2], [3, 4]])
y = np.array([[5]])
z = np.array([[6, 7], [8, 9], [10, 11]])
# Construct w using hstack, vstack, and tile
w_top = np.hstack([x, np.tile(y, (2, 1)), np.tile(y, (2, 1))])
w_middle = np.tile(y, (3, 1))
w_bottom = np.vstack([z, np.transpose(z)])
w = np.vstack([w_top, w_middle, w_bottom])
print("Array w:")
print(w)
Output:
Array w:
[[ 1 2 5 5 5]
[ 3 4 5 5 5]
[ 5 5 5 5 5]
[ 6 7 5 5 5]
[ 8 9 5 5 5]
[10 11 5 5 5]
[ 6 8 10]
[ 7 9 11]]
Code:
# Define the array x
x = np.reshape(np.arange(12.0), (2, 2, 3))
# Use squeeze on x
x_squeezed = np.squeeze(x)
print("Original shape of x:", x.shape)
print("Shape of x after squeezing:", x_squeezed.shape)
Output:
Original shape of x: (2, 2, 3)
Shape of x after squeezing: (2, 2, 3)
Code:
# Define the array y
y = np.array([[2, 0.5], [0.5, 4]])
# Construct the diagonal array containing the diagonal elements of y
diagonal_array = np.diag(np.diag(y))
print("Diagonal array:")
print(diagonal_array)
Output:
Diagonal array:
[[2. 0. ]
[0. 4. ]]
Code:
import numpy as np
# Define the array y
y = np.array([[2, 0.5], [0.5, 4]])
# Compute the eigenvalues and eigenvectors of y
eigenvalues, eigenvectors = np.linalg.eig(y)
# Construct the diagonal array D containing the eigenvalues
D = np.diag(eigenvalues)
# Compute VDV'
VDV_prime = np.dot(np.dot(eigenvectors, D), np.transpose(eigenvectors))
# Verify if VDV' equals the inverse of y
inverse_y = np.linalg.inv(y)
print("VDV' equals the inverse of y:", np.allclose(VDV_prime, inverse_y))
Output:
VDV' equals the inverse of y: True
Code:
from numpy.random import randn
# Simulate data
x = randn(100, 2)
e = randn(100, 1)
B = np.array([[1], [0.5]])
y = np.dot(x, B) + e
# Use lstsq to estimate beta from x and y
beta_estimated, residuals, rank, singular_values = np.linalg.lstsq(x, y, rcond=None)
print("Estimated beta:")
print(beta_estimated)
Output:
Estimated beta:
[[1.00699029]
[0.50443957]]
Code:
# Define the array y
y = np.array([[5, -1.5, -3.5],
[-1.5, 2, -0.5],
[-3.5, -0.5, 4]])
# Determine the rank of y
rank_y = np.linalg.matrix_rank(y)
# Compute the eigenvalues of y
eigenvalues_y, _ = np.linalg.eig(y)
# Compute the determinant of y
det_y = np.linalg.det(y)
print("Rank of y:", rank_y)
print("Eigenvalues of y:", eigenvalues_y)
print("Determinant of y:", det_y)
Output:
Rank of y: 3
Eigenvalues of y: [7.64486772 3.18862264 0.16650964]
Determinant of y: 0.0