Stats riddles, Covariance Matrix, etc.

What I did

What I learned

A B
1 3
2 1
-1 -2

First find the deviation score sums.

A B
(1 - 1/3)^2 (3 - 3/3)^2
(2 - 2/3)^2 (1 - 1/3)^2
(-1 + 1/3)^2 (2 + 2/3)^2

Now average these values to get the covariance matrix

A B
[(1 - 1/3)^2]/3 [(3 - 3/3)^2]/3
[(2 - 2/3)^2]/3 [(1 - 1/3)^2]/3
[(-1 + 1/3)^2]/3 [(2 + 2/3)^2]/3

In code:

import numpy as np
A = np.array([1,2,-1])
B = np.array([3,1,-2])

def make_covariance_matrix(A, B)
  matrix = np.stack((A, B))
  n = size(matrix, axis=1)
  temp = matrix - A/n
  matrix = matrix - temp
  matrix = matrix.T @ matrix
  matrix = matrix/n

* Note, the stack function gives a dimension like this 
matrix = array([[ 1,  2, -1],
                [ 3,  1, -2]])

NLP

#string s, string t, len(s), m = len(s), n = len(t)
def min_edit_distance(s, t, m, n):
# for dynamic programming, you need a table to hold the values for
# each subproblem.
  dp = [[0 for x in range(n + 1)] for x in range(m + 1)]

  for i in range(m+1):
    for j in range(n+1):
      # If first string is empty, only option is to 
      # insert all characters of second string
      if i == 0:
        dp[i][j] = j # j=min num of operations
      # If 2nd string is empty, only option is to
      # remove all the characters
      elif j == 0:
        dp[i][j] = i

      # If last characters are same, ignore last char
      # and recur for remaining string
      elif str1[i-1] == s[j-1]:
          dp[i][j] = dp[i-1][j-1]

      # If last character are different, consider all
      # possibilities and find minimum
      else:
          dp[i][j] = 1 + min(dp[i][j-1],        # Insert
                             dp[i-1][j],        # Remove
                             dp[i-1][j-1])      # Replace
  
    return dp[m][n]


What I will do Next