A. Calculation of Information Gain:
To calculate the information gain for each feature (X1, X2, X3), we need to calculate the entropy of the dependent feature (Y) and the conditional entropy of the dependent feature given each independent feature. The information gain is then calculated as the difference between the entropy and the conditional entropy.
Step 1: Calculate the entropy of the dependent feature (Y)
Count the occurrences of each class (Up and Down) in the dependent feature (Y):
Count(Up) = 4
Count(Down) = 6
Total count = 4 + 6 = 10
Entropy(Y) = - (Count(Up)/Total count) * log2(Count(Up)/Total count) - (Count(Down)/Total count) * log2(Count(Down)/Total count)
Entropy(Y) = - (4/10) * log2(4/10) - (6/10) * log2(6/10)
Entropy(Y) = - (0.4) * (-1.322) - (0.6) * (-0.737)
Entropy(Y) = 0.529 + 0.442
Entropy(Y) = 0.971
Step 2: Calculate the conditional entropy for each independent feature (X1, X2, X3)
Conditional Entropy(X) = Sum(Probability(X = xi) * Entropy(Y | X = xi))
a. For feature X1 (Positive, Negative):
Count(X1 = Positive) = 6
Count(X1 = Negative) = 4
Entropy(Y | X1 = Positive):
Count(Y = Up | X1 = Positive) = 3
Count(Y = Down | X1 = Positive) = 3
Total count (X1 = Positive) = 3 + 3 = 6
Entropy(Y | X1 = Positive) = - (Count(Y = Up | X1 = Positive)/Total count) * log2(Count(Y = Up | X1 = Positive)/Total count) - (Count(Y = Down | X1 = Positive)/Total count) * log2(Count(Y = Down | X1 = Positive)/Total count)
Entropy(Y | X1 = Positive) = - (3/6) * log2(3/6) - (3/6) * log2(3/6)
Entropy(Y | X1 = Positive) = - (0.5) * (-1) - (0.5) * (-1) = 1
Entropy(Y | X1 = Negative):
Count(Y = Up | X1 = Negative) = 1
Count(Y = Down | X1 = Negative) = 3
Total count (X1 = Negative) = 1 + 3 = 4
Entropy(Y | X1 = Negative) = - (Count(Y = Up | X1 = Negative)/Total count) * log2(Count(Y = Up | X1 = Negative)/Total count) - (Count(Y = Down | X1 = Negative)/Total count) * log2(Count(Y = Down | X1 = Negative)/Total count)
Entropy(Y | X1 = Negative) = - (1/4) * log2(1/4) - (3/4) * log2(3/4)
Entropy(Y | X1 = Negative) = - (0.25) * (-2) - (0.75) * (-0.415) = 0.5 + 0.311
Entropy(Y | X1 = Negative) = 0.811
Probability(X1 = Positive) = Count(X1 = Positive)/Total count = 6/10 = 0.6
Probability(X1 = Negative) = Count(X1 = Negative)/Total count = 4/10 = 0.4
Conditional Entropy(X1) = Probability(X1 = Positive) * Entropy(Y | X1 = Positive) + Probability(X1 = Negative) * Entropy(Y | X1 = Negative)
Conditional Entropy(X1) = 0.6 * 1 + 0.4 * 0.811
Conditional Entropy(X1) = 0.6 + 0.324
Conditional Entropy(X1) = 0.924
b. For feature X2 (Low, High):
Count(X2 = Low) = 5
Count(X2 = High) = 5
Entropy(Y | X2 = Low):
Count(Y = Up | X2 = Low) = 2
Count(Y = Down | X2 = Low) = 3
Total count (X2 = Low) = 2 + 3 = 5
Entropy(Y | X2 = Low) = - (Count(Y = Up | X2 = Low)/Total count) * log2(Count(Y = Up | X2 = Low)/Total count) - (Count(Y = Down | X2 = Low)/Total count) * log2(Count(Y = Down | X2 = Low)/Total count)
Entropy(Y | X2 = Low) = - (2/5) * log2(2/5) - (3/5) * log2(3/5)
Entropy(Y | X2 = Low) = - (0.4)(-1.322) - (0.6)(-0.737) = 0.529 + 0.442
Entropy(Y | X2 = Low) = 0.971
Entropy(Y | X2 = High):
Count(Y = Up | X2 = High) = 2
Count(Y = Down | X2 = High) = 3
Total count (X2 = High) = 2 + 3 = 5
Entropy(Y | X2 = High) = - (Count(Y = Up | X2 = High)/Total count) * log2(Count(Y = Up | X2 = High)/Total count) - (Count(Y = Down | X2 = High)/Total count) * log2(Count(Y = Down | X2 = High)/Total count)
Entropy(Y | X2 = High) = - (2/5) * log2(2/5) - (3/5) * log2(3/5)
Entropy(Y | X2 = High) = (0.4)(-1.322) + (0.6)(-0.737) = 0.529 + 0.442
Entropy(Y | X2 = High) = 0.971
Probability(X2 = Low) = Count(X2 = Low)/Total count = 5/10 = 0.5
Probability(X2 = High) = Count(X2 = High)/Total count = 5/10 = 0.5
Conditional Entropy(X2) = Probability(X2 = Low) * Entropy(Y | X2 = Low) + Probability(X2 = High) * Entropy(Y | X2 = High)
Conditional Entropy(X2) = 0.5 * 0.971 + 0.5 * 0.971
Conditional Entropy(X2) = 0.971 + 0.971
Conditional Entropy(X2) = 1.942
c. For feature X3 (High, Low):
Count(X3 = High) = 10
Entropy(Y | X3 = High):
Count(Y = Up | X3 = High) = 4
Count(Y = Down | X3 = High) = 6
Total count (X3 = High) = 4 + 6 = 10
Entropy(Y | X3 = High) = - (Count(Y = Up | X3 = High)/Total count) * log2(Count(Y = Up | X3 = High)/Total count) - (Count(Y = Down | X3 = High)/Total count) * log2(Count(Y = Down | X3 = High)/Total count)
Entropy(Y | X3 = High) = - (4/10) * log2(4/10) - (6/10) * log2(6/10)
Entropy(Y | X3 = High) = - (0.4) * (-1.322) - (0.6) * (-0.737)
Entropy(Y | X3 = High) = 0.529 + 0.442
Entropy(Y | X3 = High) = 0.971
Probability(X3 = High) = Count(X3 = High)/Total count = 10/10 = 1
Conditional Entropy(X3) = Probability(X3 = High) * Entropy(Y | X3 = High)
Conditional Entropy(X3) = 1 * 0.971
Conditional Entropy(X3) = 0.971
Step 3: Calculate the Information Gain
Information Gain(X) = Entropy(Y) - Conditional Entropy(X)
a. Information Gain(X1) = Entropy(Y) - Conditional Entropy(X1)
Information Gain(X1) = 0.971 - 0.924
Information Gain(X1) = 0.047
b. Information Gain(X2) = Entropy(Y) - Conditional Entropy(X2)
Information Gain(X2) = 0.971 - 1.942
Information Gain(X2) = -0.971
c. Information Gain(X3) = Entropy(Y) - Conditional Entropy(X3)
Information Gain(X3) = 0.971 - 0.971
Information Gain(X3) = 0
Based on the information gain, the feature with the highest information gain is X1. Therefore, X1 will be selected as the root node.
B. Calculation of Gini Index:
To calculate the Gini Index for each feature (X1, X2, X3), we need to calculate the Gini Index of the dependent feature (Y) and the conditional Gini Index of the dependent feature given each independent feature. The Gini Index is then calculated as the weighted average of the conditional Gini Index.
Step 1: Calculate the Gini Index of the dependent feature (Y)
Count the occurrences of each class (Up and Down) in the dependent feature (Y):
Count(Up) = 4
Count(Down) = 6
Total count = 4 + 6 = 10
Gini Index(Y) = 1 - ((Count(Up)/Total count)^2 + (Count(Down)/Total count)^2)
Gini Index(Y) = 1 - ((4/10)^2 + (6/10)^2)
Gini Index(Y) = 1 - (0.16 + 0.36)
Gini Index(Y) = 1 - 0.52
Gini Index(Y) = 0.48
Step 2: Calculate the conditional Gini Index for each independent feature (X1, X2, X3)
a. For feature X1 (Positive, Negative):
Count(X1 = Positive) = 6
Count(X1 = Negative) = 4
Gini Index of X1 = Probability(X1 = Positive) * Gini Index(Y | X1 = Positive) + Probability(X1 = Negative) * Gini Index(Y | X1 = Negative)
Probability(X1 = Positive) = Count(X1 = Positive)/Total count = 6/10 = 0.6
Probability(X1 = Negative) = Count(X1 = Negative)/Total count = 4/10 = 0.4
Gini Index(Y | X1 = Positive):
Count(Y = Up | X1 = Positive) = 3
Count(Y = Down | X1 = Positive) = 3
Total count (X1 = Positive) = 3 +