DevCEDTeam / CED

0 stars 0 forks source link

Python Implementation: Shiny Finance Model | Email #163

Open DevCEDTeam opened 17 hours ago

DevCEDTeam commented 17 hours ago

Variance

Definition

Variance measures how far a set of numbers is spread out from their average value. It quantifies the dispersion of data points.

Mathematical Formula

  1. Population Variance (σ²): [ σ² = \frac{\sum_{i=1}^{N} (x_i - μ)^2}{N} ]

    • (x_i): Elements in the population
    • (μ): Population mean
    • (N): Population size
  2. Sample Variance (s²): [ s² = \frac{\sum_{i=1}^{n} (x_i - \bar{x})^2}{n - 1} ]

    • (x_i): Elements in the sample
    • (\bar{x}): Sample mean
    • (n): Sample size

Python Code for Variance

import numpy as np

# Dataset
data = np.array([1, 2, 3, 4, 5])

# Calculate Population Variance
population_variance = np.var(data)
print(f"Population Variance: {population_variance}")

# Calculate Sample Variance
sample_variance = np.var(data, ddof=1)  # ddof=1 for sample variance
print(f"Sample Variance: {sample_variance}")

Output


Covariance

Definition

Covariance measures the extent to which two variables change together. It evaluates whether an increase in one variable corresponds to an increase or decrease in another variable.

Mathematical Formula

[ \text{Cov}(X, Y) = \frac{1}{n} \sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y}) ]

Python Code for Covariance

import numpy as np

# Two datasets
x = np.array([1, 2, 3, 4, 5])
y = np.array([5, 4, 3, 2, 1])

# Calculate Covariance Matrix
cov_matrix = np.cov(x, y, bias=True)  # bias=True for population covariance
print("Covariance Matrix:\n", cov_matrix)

# Extract Covariance
population_covariance = cov_matrix[0, 1]
print(f"Population Covariance: {population_covariance}")

# Sample Covariance
sample_covariance = np.cov(x, y, bias=False)[0, 1]  # bias=False for sample covariance
print(f"Sample Covariance: {sample_covariance}")

Output


Summary

  1. Variance evaluates the spread of data points from their mean.
  2. Covariance assesses the relationship between two datasets:
    • Positive covariance indicates that as one variable increases, the other also tends to increase.
    • Negative covariance indicates an inverse relationship.
  3. Python's numpy library provides efficient methods to calculate variance and covariance.

This explanation is free of any unwanted characters and tailored for clarity. Let me know if further modifications are needed!

https://chatgpt.com/share/673f6ab5-3808-8012-82b9-57ee6c4cb952