Interact Exercise 4

Imports


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

In [2]:
from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display


:0: FutureWarning: IPython widgets are experimental and may change in the future.

Line with Gaussian noise

Write a function named random_line that creates x and y data for a line with y direction random noise that has a normal distribution $N(0,\sigma^2)$:

$$ y = m x + b + N(0,\sigma^2) $$

Be careful about the sigma=0.0 case.


In [3]:
def random_line(m, b, sigma, size=10):
    """Create a line y = m*x + b + N(0,sigma**2) between x=[-1.0,1.0]
    
    Parameters
    ----------
    m : float
        The slope of the line.
    b : float
        The y-intercept of the line.
    sigma : float
        The standard deviation of the y direction normal distribution noise.
    size : int
        The number of points to create for the line.
    
    Returns
    -------
    x : array of floats
        The array of x values for the line with `size` points.
    y : array of floats
        The array of y values for the lines with `size` points.
    """
    
    x = np.linspace(-1.0,1.0,size)
    def N(mu, sigma):
        if sigma == 0:
            N = 0
        else:
            N = np.exp(-1*((x-mu)**2)/(2*(sigma**2)))/(sigma*((2*np.pi)**0.5))
        return N
    y = m*x + b + N(0,sigma)
    return x, y

In [4]:
random_line(0.0,0.0,1.0,500)


Out[4]:
(array([-1.        , -0.99599198, -0.99198397, -0.98797595, -0.98396794,
        -0.97995992, -0.9759519 , -0.97194389, -0.96793587, -0.96392786,
        -0.95991984, -0.95591182, -0.95190381, -0.94789579, -0.94388778,
        -0.93987976, -0.93587174, -0.93186373, -0.92785571, -0.9238477 ,
        -0.91983968, -0.91583166, -0.91182365, -0.90781563, -0.90380762,
        -0.8997996 , -0.89579158, -0.89178357, -0.88777555, -0.88376754,
        -0.87975952, -0.8757515 , -0.87174349, -0.86773547, -0.86372745,
        -0.85971944, -0.85571142, -0.85170341, -0.84769539, -0.84368737,
        -0.83967936, -0.83567134, -0.83166333, -0.82765531, -0.82364729,
        -0.81963928, -0.81563126, -0.81162325, -0.80761523, -0.80360721,
        -0.7995992 , -0.79559118, -0.79158317, -0.78757515, -0.78356713,
        -0.77955912, -0.7755511 , -0.77154309, -0.76753507, -0.76352705,
        -0.75951904, -0.75551102, -0.75150301, -0.74749499, -0.74348697,
        -0.73947896, -0.73547094, -0.73146293, -0.72745491, -0.72344689,
        -0.71943888, -0.71543086, -0.71142285, -0.70741483, -0.70340681,
        -0.6993988 , -0.69539078, -0.69138277, -0.68737475, -0.68336673,
        -0.67935872, -0.6753507 , -0.67134269, -0.66733467, -0.66332665,
        -0.65931864, -0.65531062, -0.65130261, -0.64729459, -0.64328657,
        -0.63927856, -0.63527054, -0.63126253, -0.62725451, -0.62324649,
        -0.61923848, -0.61523046, -0.61122244, -0.60721443, -0.60320641,
        -0.5991984 , -0.59519038, -0.59118236, -0.58717435, -0.58316633,
        -0.57915832, -0.5751503 , -0.57114228, -0.56713427, -0.56312625,
        -0.55911824, -0.55511022, -0.5511022 , -0.54709419, -0.54308617,
        -0.53907816, -0.53507014, -0.53106212, -0.52705411, -0.52304609,
        -0.51903808, -0.51503006, -0.51102204, -0.50701403, -0.50300601,
        -0.498998  , -0.49498998, -0.49098196, -0.48697395, -0.48296593,
        -0.47895792, -0.4749499 , -0.47094188, -0.46693387, -0.46292585,
        -0.45891784, -0.45490982, -0.4509018 , -0.44689379, -0.44288577,
        -0.43887776, -0.43486974, -0.43086172, -0.42685371, -0.42284569,
        -0.41883768, -0.41482966, -0.41082164, -0.40681363, -0.40280561,
        -0.3987976 , -0.39478958, -0.39078156, -0.38677355, -0.38276553,
        -0.37875752, -0.3747495 , -0.37074148, -0.36673347, -0.36272545,
        -0.35871743, -0.35470942, -0.3507014 , -0.34669339, -0.34268537,
        -0.33867735, -0.33466934, -0.33066132, -0.32665331, -0.32264529,
        -0.31863727, -0.31462926, -0.31062124, -0.30661323, -0.30260521,
        -0.29859719, -0.29458918, -0.29058116, -0.28657315, -0.28256513,
        -0.27855711, -0.2745491 , -0.27054108, -0.26653307, -0.26252505,
        -0.25851703, -0.25450902, -0.250501  , -0.24649299, -0.24248497,
        -0.23847695, -0.23446894, -0.23046092, -0.22645291, -0.22244489,
        -0.21843687, -0.21442886, -0.21042084, -0.20641283, -0.20240481,
        -0.19839679, -0.19438878, -0.19038076, -0.18637275, -0.18236473,
        -0.17835671, -0.1743487 , -0.17034068, -0.16633267, -0.16232465,
        -0.15831663, -0.15430862, -0.1503006 , -0.14629259, -0.14228457,
        -0.13827655, -0.13426854, -0.13026052, -0.12625251, -0.12224449,
        -0.11823647, -0.11422846, -0.11022044, -0.10621242, -0.10220441,
        -0.09819639, -0.09418838, -0.09018036, -0.08617234, -0.08216433,
        -0.07815631, -0.0741483 , -0.07014028, -0.06613226, -0.06212425,
        -0.05811623, -0.05410822, -0.0501002 , -0.04609218, -0.04208417,
        -0.03807615, -0.03406814, -0.03006012, -0.0260521 , -0.02204409,
        -0.01803607, -0.01402806, -0.01002004, -0.00601202, -0.00200401,
         0.00200401,  0.00601202,  0.01002004,  0.01402806,  0.01803607,
         0.02204409,  0.0260521 ,  0.03006012,  0.03406814,  0.03807615,
         0.04208417,  0.04609218,  0.0501002 ,  0.05410822,  0.05811623,
         0.06212425,  0.06613226,  0.07014028,  0.0741483 ,  0.07815631,
         0.08216433,  0.08617234,  0.09018036,  0.09418838,  0.09819639,
         0.10220441,  0.10621242,  0.11022044,  0.11422846,  0.11823647,
         0.12224449,  0.12625251,  0.13026052,  0.13426854,  0.13827655,
         0.14228457,  0.14629259,  0.1503006 ,  0.15430862,  0.15831663,
         0.16232465,  0.16633267,  0.17034068,  0.1743487 ,  0.17835671,
         0.18236473,  0.18637275,  0.19038076,  0.19438878,  0.19839679,
         0.20240481,  0.20641283,  0.21042084,  0.21442886,  0.21843687,
         0.22244489,  0.22645291,  0.23046092,  0.23446894,  0.23847695,
         0.24248497,  0.24649299,  0.250501  ,  0.25450902,  0.25851703,
         0.26252505,  0.26653307,  0.27054108,  0.2745491 ,  0.27855711,
         0.28256513,  0.28657315,  0.29058116,  0.29458918,  0.29859719,
         0.30260521,  0.30661323,  0.31062124,  0.31462926,  0.31863727,
         0.32264529,  0.32665331,  0.33066132,  0.33466934,  0.33867735,
         0.34268537,  0.34669339,  0.3507014 ,  0.35470942,  0.35871743,
         0.36272545,  0.36673347,  0.37074148,  0.3747495 ,  0.37875752,
         0.38276553,  0.38677355,  0.39078156,  0.39478958,  0.3987976 ,
         0.40280561,  0.40681363,  0.41082164,  0.41482966,  0.41883768,
         0.42284569,  0.42685371,  0.43086172,  0.43486974,  0.43887776,
         0.44288577,  0.44689379,  0.4509018 ,  0.45490982,  0.45891784,
         0.46292585,  0.46693387,  0.47094188,  0.4749499 ,  0.47895792,
         0.48296593,  0.48697395,  0.49098196,  0.49498998,  0.498998  ,
         0.50300601,  0.50701403,  0.51102204,  0.51503006,  0.51903808,
         0.52304609,  0.52705411,  0.53106212,  0.53507014,  0.53907816,
         0.54308617,  0.54709419,  0.5511022 ,  0.55511022,  0.55911824,
         0.56312625,  0.56713427,  0.57114228,  0.5751503 ,  0.57915832,
         0.58316633,  0.58717435,  0.59118236,  0.59519038,  0.5991984 ,
         0.60320641,  0.60721443,  0.61122244,  0.61523046,  0.61923848,
         0.62324649,  0.62725451,  0.63126253,  0.63527054,  0.63927856,
         0.64328657,  0.64729459,  0.65130261,  0.65531062,  0.65931864,
         0.66332665,  0.66733467,  0.67134269,  0.6753507 ,  0.67935872,
         0.68336673,  0.68737475,  0.69138277,  0.69539078,  0.6993988 ,
         0.70340681,  0.70741483,  0.71142285,  0.71543086,  0.71943888,
         0.72344689,  0.72745491,  0.73146293,  0.73547094,  0.73947896,
         0.74348697,  0.74749499,  0.75150301,  0.75551102,  0.75951904,
         0.76352705,  0.76753507,  0.77154309,  0.7755511 ,  0.77955912,
         0.78356713,  0.78757515,  0.79158317,  0.79559118,  0.7995992 ,
         0.80360721,  0.80761523,  0.81162325,  0.81563126,  0.81963928,
         0.82364729,  0.82765531,  0.83166333,  0.83567134,  0.83967936,
         0.84368737,  0.84769539,  0.85170341,  0.85571142,  0.85971944,
         0.86372745,  0.86773547,  0.87174349,  0.8757515 ,  0.87975952,
         0.88376754,  0.88777555,  0.89178357,  0.89579158,  0.8997996 ,
         0.90380762,  0.90781563,  0.91182365,  0.91583166,  0.91983968,
         0.9238477 ,  0.92785571,  0.93186373,  0.93587174,  0.93987976,
         0.94388778,  0.94789579,  0.95190381,  0.95591182,  0.95991984,
         0.96392786,  0.96793587,  0.97194389,  0.9759519 ,  0.97995992,
         0.98396794,  0.98797595,  0.99198397,  0.99599198,  1.        ]),
 array([ 0.24197072,  0.24294054,  0.24391033,  0.24488005,  0.24584968,
         0.24681918,  0.24778853,  0.24875769,  0.24972663,  0.25069531,
         0.25166371,  0.25263179,  0.25359952,  0.25456686,  0.2555338 ,
         0.25650028,  0.25746629,  0.25843178,  0.25939672,  0.26036109,
         0.26132484,  0.26228795,  0.26325037,  0.26421209,  0.26517305,
         0.26613324,  0.26709261,  0.26805114,  0.26900878,  0.26996551,
         0.27092129,  0.27187609,  0.27282986,  0.27378259,  0.27473423,
         0.27568475,  0.27663411,  0.27758228,  0.27852923,  0.27947492,
         0.28041931,  0.28136238,  0.28230408,  0.28324439,  0.28418326,
         0.28512066,  0.28605656,  0.28699093,  0.28792371,  0.28885489,
         0.28978443,  0.29071229,  0.29163843,  0.29256283,  0.29348544,
         0.29440623,  0.29532516,  0.2962422 ,  0.29715732,  0.29807048,
         0.29898164,  0.29989077,  0.30079783,  0.30170278,  0.3026056 ,
         0.30350624,  0.30440468,  0.30530087,  0.30619478,  0.30708637,
         0.30797561,  0.30886247,  0.3097469 ,  0.31062888,  0.31150836,
         0.31238531,  0.31325971,  0.3141315 ,  0.31500066,  0.31586715,
         0.31673093,  0.31759197,  0.31845024,  0.3193057 ,  0.32015832,
         0.32100805,  0.32185487,  0.32269874,  0.32353963,  0.32437749,
         0.3252123 ,  0.32604402,  0.32687262,  0.32769806,  0.32852031,
         0.32933932,  0.33015508,  0.33096754,  0.33177667,  0.33258244,
         0.3333848 ,  0.33418374,  0.3349792 ,  0.33577117,  0.3365596 ,
         0.33734447,  0.33812573,  0.33890336,  0.33967732,  0.34044758,
         0.3412141 ,  0.34197686,  0.34273581,  0.34349093,  0.34424219,
         0.34498955,  0.34573297,  0.34647243,  0.3472079 ,  0.34793933,
         0.34866671,  0.34939   ,  0.35010916,  0.35082416,  0.35153498,
         0.35224158,  0.35294393,  0.353642  ,  0.35433576,  0.35502517,
         0.35571022,  0.35639086,  0.35706706,  0.35773881,  0.35840605,
         0.35906878,  0.35972695,  0.36038054,  0.36102952,  0.36167385,
         0.36231352,  0.36294848,  0.36357872,  0.3642042 ,  0.3648249 ,
         0.36544078,  0.36605183,  0.366658  ,  0.36725928,  0.36785564,
         0.36844704,  0.36903347,  0.36961489,  0.37019128,  0.37076262,
         0.37132887,  0.37189002,  0.37244602,  0.37299687,  0.37354253,
         0.37408298,  0.37461819,  0.37514815,  0.37567282,  0.37619217,
         0.3767062 ,  0.37721487,  0.37771815,  0.37821604,  0.37870849,
         0.37919549,  0.37967703,  0.38015306,  0.38062358,  0.38108856,
         0.38154798,  0.38200181,  0.38245004,  0.38289265,  0.38332961,
         0.3837609 ,  0.38418651,  0.38460641,  0.38502059,  0.38542902,
         0.38583168,  0.38622856,  0.38661964,  0.3870049 ,  0.38738432,
         0.38775788,  0.38812556,  0.38848736,  0.38884324,  0.3891932 ,
         0.38953722,  0.38987527,  0.39020736,  0.39053345,  0.39085353,
         0.3911676 ,  0.39147562,  0.3917776 ,  0.39207351,  0.39236334,
         0.39264708,  0.39292471,  0.39319622,  0.3934616 ,  0.39372083,
         0.3939739 ,  0.3942208 ,  0.39446153,  0.39469605,  0.39492438,
         0.39514648,  0.39536237,  0.39557201,  0.39577541,  0.39597255,
         0.39616342,  0.39634802,  0.39652634,  0.39669837,  0.39686409,
         0.3970235 ,  0.3971766 ,  0.39732338,  0.39746382,  0.39759793,
         0.39772569,  0.3978471 ,  0.39796216,  0.39807085,  0.39817318,
         0.39826914,  0.39835872,  0.39844192,  0.39851873,  0.39858916,
         0.39865319,  0.39871083,  0.39876208,  0.39880692,  0.39884536,
         0.3988774 ,  0.39890303,  0.39892225,  0.39893507,  0.39894148,
         0.39894148,  0.39893507,  0.39892225,  0.39890303,  0.3988774 ,
         0.39884536,  0.39880692,  0.39876208,  0.39871083,  0.39865319,
         0.39858916,  0.39851873,  0.39844192,  0.39835872,  0.39826914,
         0.39817318,  0.39807085,  0.39796216,  0.3978471 ,  0.39772569,
         0.39759793,  0.39746382,  0.39732338,  0.3971766 ,  0.3970235 ,
         0.39686409,  0.39669837,  0.39652634,  0.39634802,  0.39616342,
         0.39597255,  0.39577541,  0.39557201,  0.39536237,  0.39514648,
         0.39492438,  0.39469605,  0.39446153,  0.3942208 ,  0.3939739 ,
         0.39372083,  0.3934616 ,  0.39319622,  0.39292471,  0.39264708,
         0.39236334,  0.39207351,  0.3917776 ,  0.39147562,  0.3911676 ,
         0.39085353,  0.39053345,  0.39020736,  0.38987527,  0.38953722,
         0.3891932 ,  0.38884324,  0.38848736,  0.38812556,  0.38775788,
         0.38738432,  0.3870049 ,  0.38661964,  0.38622856,  0.38583168,
         0.38542902,  0.38502059,  0.38460641,  0.38418651,  0.3837609 ,
         0.38332961,  0.38289265,  0.38245004,  0.38200181,  0.38154798,
         0.38108856,  0.38062358,  0.38015306,  0.37967703,  0.37919549,
         0.37870849,  0.37821604,  0.37771815,  0.37721487,  0.3767062 ,
         0.37619217,  0.37567282,  0.37514815,  0.37461819,  0.37408298,
         0.37354253,  0.37299687,  0.37244602,  0.37189002,  0.37132887,
         0.37076262,  0.37019128,  0.36961489,  0.36903347,  0.36844704,
         0.36785564,  0.36725928,  0.366658  ,  0.36605183,  0.36544078,
         0.3648249 ,  0.3642042 ,  0.36357872,  0.36294848,  0.36231352,
         0.36167385,  0.36102952,  0.36038054,  0.35972695,  0.35906878,
         0.35840605,  0.35773881,  0.35706706,  0.35639086,  0.35571022,
         0.35502517,  0.35433576,  0.353642  ,  0.35294393,  0.35224158,
         0.35153498,  0.35082416,  0.35010916,  0.34939   ,  0.34866671,
         0.34793933,  0.3472079 ,  0.34647243,  0.34573297,  0.34498955,
         0.34424219,  0.34349093,  0.34273581,  0.34197686,  0.3412141 ,
         0.34044758,  0.33967732,  0.33890336,  0.33812573,  0.33734447,
         0.3365596 ,  0.33577117,  0.3349792 ,  0.33418374,  0.3333848 ,
         0.33258244,  0.33177667,  0.33096754,  0.33015508,  0.32933932,
         0.32852031,  0.32769806,  0.32687262,  0.32604402,  0.3252123 ,
         0.32437749,  0.32353963,  0.32269874,  0.32185487,  0.32100805,
         0.32015832,  0.3193057 ,  0.31845024,  0.31759197,  0.31673093,
         0.31586715,  0.31500066,  0.3141315 ,  0.31325971,  0.31238531,
         0.31150836,  0.31062888,  0.3097469 ,  0.30886247,  0.30797561,
         0.30708637,  0.30619478,  0.30530087,  0.30440468,  0.30350624,
         0.3026056 ,  0.30170278,  0.30079783,  0.29989077,  0.29898164,
         0.29807048,  0.29715732,  0.2962422 ,  0.29532516,  0.29440623,
         0.29348544,  0.29256283,  0.29163843,  0.29071229,  0.28978443,
         0.28885489,  0.28792371,  0.28699093,  0.28605656,  0.28512066,
         0.28418326,  0.28324439,  0.28230408,  0.28136238,  0.28041931,
         0.27947492,  0.27852923,  0.27758228,  0.27663411,  0.27568475,
         0.27473423,  0.27378259,  0.27282986,  0.27187609,  0.27092129,
         0.26996551,  0.26900878,  0.26805114,  0.26709261,  0.26613324,
         0.26517305,  0.26421209,  0.26325037,  0.26228795,  0.26132484,
         0.26036109,  0.25939672,  0.25843178,  0.25746629,  0.25650028,
         0.2555338 ,  0.25456686,  0.25359952,  0.25263179,  0.25166371,
         0.25069531,  0.24972663,  0.24875769,  0.24778853,  0.24681918,
         0.24584968,  0.24488005,  0.24391033,  0.24294054,  0.24197072]))

In [5]:
m = 0.0; b = 1.0; sigma=0.0; size=3
x, y = random_line(m, b, sigma, size)
assert len(x)==len(y)==size
assert list(x)==[-1.0,0.0,1.0]
assert list(y)==[1.0,1.0,1.0]
sigma = 1.0
m = 0.0; b = 0.0
size = 500
x, y = random_line(m, b, sigma, size)
assert np.allclose(np.mean(y-m*x-b), 0.0, rtol=0.1, atol=0.1)
assert np.allclose(np.std(y-m*x-b), sigma, rtol=0.1, atol=0.1)


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-5-dce728037167> in <module>()
      8 size = 500
      9 x, y = random_line(m, b, sigma, size)
---> 10 assert np.allclose(np.mean(y-m*x-b), 0.0, rtol=0.1, atol=0.1)
     11 assert np.allclose(np.std(y-m*x-b), sigma, rtol=0.1, atol=0.1)

AssertionError: 

Write a function named plot_random_line that takes the same arguments as random_line and creates a random line using random_line and then plots the x and y points using Matplotlib's scatter function:

  • Make the marker color settable through a color keyword argument with a default of red.
  • Display the range $x=[-1.1,1.1]$ and $y=[-10.0,10.0]$.
  • Customize your plot to make it effective and beautiful.

In [6]:
def ticks_out(ax):
    """Move the ticks to the outside of the box."""
    ax.get_xaxis().set_tick_params(direction='out', width=1, which='both')
    ax.get_yaxis().set_tick_params(direction='out', width=1, which='both')

In [7]:
def plot_random_line(m, b, sigma, size=10, color='red'):
    """Plot a random line with slope m, intercept b and size points."""
    x, y = random_line(m, b, sigma, size)
    plt.scatter(x,y,color=color)
    plt.xlim(min(x),max(x))
    plt.ylim(min(y),max(y))
    plt.tick_params(direction='out', width=1, which='both')

In [8]:
plot_random_line(5.0, -1.0, 2.0, 50)



In [9]:
assert True # use this cell to grade the plot_random_line function

Use interact to explore the plot_random_line function using:

  • m: a float valued slider from -10.0 to 10.0 with steps of 0.1.
  • b: a float valued slider from -5.0 to 5.0 with steps of 0.1.
  • sigma: a float valued slider from 0.0 to 5.0 with steps of 0.01.
  • size: an int valued slider from 10 to 100 with steps of 10.
  • color: a dropdown with options for red, green and blue.

In [10]:
interact(plot_random_line, m=(-10.0,10.0,0.1), b=(-5.0,5.0,0.1), sigma=(0.0,5.0,0.01), size=(10,100,10), color=['red','green','blue'])



In [11]:
#### assert True # use this cell to grade the plot_random_line interact