Câu 1 (5 điểm) – R 1. Sinh ngẫu nhiên 400 giá trị từ phân phối mũ (λ = 0.5) 2. Tính trung bình, phương sai và vẽ biểu đồ phân phối

set.seed(123)

# Tham số
lambda <- 0.5
n <- 400
# Sinh mẫu phân phối mũ
data_exp <- rexp(n, rate = lambda)

# Tính trung bình và phương sai
mean_exp <- mean(data_exp)
var_exp  <- var(data_exp)

mean_exp
## [1] 1.986143
var_exp
## [1] 3.485842
# Vẽ histogram phân phối
hist(
  data_exp,
  probability = TRUE,
  col = "lightblue",
  border = "white",
  main = "Histogram phân phối mũ (λ = 0.5)",
  xlab = "Giá trị"
)

# Vẽ đường mật độ lý thuyết
curve(dexp(x, rate = lambda),
      col = "red",
      lwd = 2,
      add = TRUE)

Câu 2 (5 điểm) – Python 1. Đọc tập dữ liệu Iris bằng Pandas 2. Tính hệ số tương quan giữa Sepal.Length và Petal.Length 3. Vẽ biểu đồ scatter Petal.Length vs Petal.Width theo từng loài hoa

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Load dữ liệu Iris
iris = sns.load_dataset("iris")

# Hiển thị 5 dòng đầu
iris.head()
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
# Tính hệ số tương quan
corr = iris["sepal_length"].corr(iris["petal_length"])
corr
## np.float64(0.8717537758865831)
# Vẽ scatter plot
plt.figure()
sns.scatterplot(
    data=iris,
    x="petal_length",
    y="petal_width",
    hue="species"
)

plt.title("Petal.Length vs Petal.Width theo loài hoa")
plt.xlabel("Petal Length")
plt.ylabel("Petal Width")
plt.show()