In [ ]:
%matplotlib inline

'''
Guess the correlation game
'''
import numpy as np
import matplotlib.pyplot as plt

def generate_data():
    samples = 200
    r = np.random.rand()
    # http://stats.stackexchange.com/questions/83172/generate-two-variables-with-precise-pre-specified-correlation
    data = np.random.multivariate_normal([0, 0], [[1, r], [r, 1]], size=samples)
    x, y = data[:,0], data[:,1]

    plt.plot(x, y, 'o')
    plt.show()
    
    return r

current_streak = 0

while True:
    actual_r = generate_data()
    guessed_r = float(input('Guess the correlation: '))
    error = abs(actual_r - guessed_r)
    if abs(error - 1e-2) < 1e-10:
        current_streak = current_streak + 1
    print('Error: {0} Current streak: {1}'.format(error, current_streak))


Guess the correlation: 0.3
Error: 0.11510766697397762 Current streak: 0
Guess the correlation: 0.8
Error: 0.10379113931402717 Current streak: 0
Guess the correlation: 0.9
Error: 0.049964460240174646 Current streak: 0

In [ ]:


In [ ]: