Ejemplo n.º 1
0
def test_02():

    coeff = np.array([1.0])
    # ==> f(x) = 1.0

    xVals = np.array([0.0])
    res = polynomial_horner(xVals, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)

    xVals = np.array([0.25])
    res = polynomial_horner(xVals, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)

    xVals = np.array([0.6666667])
    res = polynomial_horner(xVals, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)

    xVals = np.array([1.6666667e-9])
    res = polynomial_horner(xVals, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)

    xVals = np.array([1.0])
    res = polynomial_horner(xVals, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)
def polynomialCurveFitting(mOrder, Xt, X):

    nTest = X.shape[0]

    res = np.zeros((len(mOrder), 3))

    for m in mOrder:

        # create coefficient vector (containing all fit parameters)
        w = np.ones((m + 1, ))

        # curve fitting
        popt, pcov = curve_fit(polynomial_horner, Xt[:, 0], Xt[:, 1], p0=w)

        yPredict = polynomial_horner(Xt[:, 0], *popt)

        # test data set prediction
        yPredictTest = polynomial_horner(X[:, 0], *popt)

        # compute sum of squares deviation
        sum_of_squares_error = 0.5 * np.sum(np.square(yPredict - Xt[:, 1]))
        sum_of_squares_error_test = 0.5 * np.sum(
            np.square(yPredictTest - X[:, 1]))

        RMS = np.sqrt(2.0 * sum_of_squares_error / N)
        RMS_test = np.sqrt(2.0 * sum_of_squares_error_test / nTest)

        res[m, 0] = m
        res[m, 1] = RMS
        res[m, 2] = RMS_test

    return res
Ejemplo n.º 3
0
def test_10():

    coeff = np.array([1.0])  # i.e. f(x) = 1.0

    yValue_ref = 1.0
    yValue = polynomial_horner(0.0, *coeff)
    assert np.isclose(yValue, yValue_ref)

    yValue_ref = 1.0
    yValue = polynomial_horner(-1.23456789, *coeff)
    assert np.isclose(yValue, yValue_ref)

    yValue_ref = 1.0
    yValue = polynomial_horner(1.2e4, *coeff)
    assert np.isclose(yValue, yValue_ref)
Ejemplo n.º 4
0
def test_01():

    coeff = np.array([1.0])
    # ==> f(x) = 1.0

    res = polynomial_horner(0.0, *coeff)
    assert np.isclose(res, 1.0)

    res = polynomial_horner(0.1, *coeff)
    assert np.isclose(res, 1.0)

    res = polynomial_horner(-99.1, *coeff)
    assert np.isclose(res, 1.0)

    res = polynomial_horner(1.0e-3, *coeff)
    assert np.isclose(res, 1.0)
Ejemplo n.º 5
0
def test_06():

    nVals = 130
    xVals = np.linspace(0.0, 1.0, nVals)
    coeff = np.array([1.0])
    yVals = polynomial_horner(xVals, *coeff)
    assert xVals.shape == yVals.shape
    assert np.array_equal(np.ones(nVals), yVals)
Ejemplo n.º 6
0
def test_05():

    nVals = 120
    xVals = np.linspace(0.0, 1.0, nVals)
    coeff = np.array([0.0, 1.0])
    yVals = polynomial_horner(xVals, *coeff)
    assert xVals.shape == yVals.shape
    assert np.array_equal(xVals, yVals)
Ejemplo n.º 7
0
def test_09():
    # return type test
    coeff = np.array([1.0, 1.0])  # i.e. f(x) = 1 + x

    # vector in / vector out
    xVals = np.array([0.15])
    yVals = polynomial_horner(xVals, *coeff)
    assert xVals.shape == yVals.shape
    assert type(xVals) == type(yVals)
    yVals_ref = np.array([1.0 + x for x in xVals])
    assert np.array_equal(yVals, yVals_ref)

    # scalar in / scalar out
    xVals = 0.15
    yVals = float(polynomial_horner(xVals, *coeff))
    assert type(xVals) == type(yVals)
    yVals_ref = 1.0 + 1.0 * 0.15
    assert np.array_equal(yVals, yVals_ref)
Ejemplo n.º 8
0
def test_08():
    # single value test
    xVals = np.array([0.91264712])
    coeff = np.array([0.15, 2.83, 3.5, -0.224, 2.9971])
    yVals = polynomial_horner(xVals, *coeff)
    assert xVals.shape == yVals.shape
    yVals_ref = np.array([0.15 + 2.83 * x + 3.5 * x ** 2 - 0.224 * x ** 3 \
        + 2.9971 * x ** 4 for x in xVals])
    assert np.array_equal(yVals, yVals_ref)
Ejemplo n.º 9
0
def test_07():

    nVals = 140
    xVals = np.linspace(0.0, 1.0, nVals)
    coeff = np.array([1.0, 0.0, 0.5])
    yVals = polynomial_horner(xVals, *coeff)
    assert xVals.shape == yVals.shape
    yVals_ref = np.array([1.0 + 0.5 * x**2 for x in xVals])
    assert np.array_equal(yVals, yVals_ref)
Ejemplo n.º 10
0
def test_04():

    coeff = np.array([1.0, 1.0])
    # ==> f(x) = 1 + x
    xVals = np.array([1.0, 0.0])
    reference = np.array([2.0, 1.0])
    res = polynomial_horner(xVals, *coeff)

    assert np.array_equal(res, reference)
Ejemplo n.º 11
0
def test_03():

    coeff = np.array([1.0])
    # ==> f(x) = 1.0

    res = polynomial_horner(0.0, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)

    res = polynomial_horner(0.25, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)

    res = polynomial_horner(0.6666667, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)

    res = polynomial_horner(1.6666667e-9, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)

    res = polynomial_horner(1.0, *coeff)
    reference = 1.0
    assert np.isclose(res, reference)
def polynomialCurveFitting(mOrder, Xt):

    res = np.zeros((len(mOrder), 2))

    for m in mOrder:

        # create coefficient vector (containing all fit parameters)
        w = np.ones((m + 1, ))

        # curve fitting
        popt, pcov = curve_fit(polynomial_horner, Xt[:, 0], Xt[:, 1], p0=w)

        yPredict = polynomial_horner(Xt[:, 0], *popt)

        # compute sum of squares deviation
        sum_of_squares_error = 0.5 * np.sum(np.square(yPredict - Xt[:, 1]))

        RMS = np.sqrt(2.0 * sum_of_squares_error / N)

        res[m, 0] = m
        res[m, 1] = RMS

    return res
Ejemplo n.º 13
0
    outname = f'prml_ch_01_figure_1.2_training_data_PRNG-seed_{seedValue}.txt'
    np.savetxt(os.path.join(RAWDIR, outname), Xt, fmt = '%.8f')
    ######################################################################################

    ######################################################################################
    # polynomial least squares fitting
    m = 3
    w = polyLeastSquares(m, Xt)
    print("fitted weights =", w)
    ######################################################################################

    ######################################################################################
    # create fitted model
    nModelPoints = 800
    xVals = np.linspace(0.0, 1.0, nModelPoints)
    yVals = polynomial_horner(xVals, *w)
    Xm = np.zeros((nModelPoints, 2))
    Xm[:, 0] = xVals
    Xm[:, 1] = yVals

    ######################################################################################
    # file i/o
    outname = f'prml_ch_01_figure_1.2_training_data_PRNG-seed_{seedValue}_m_{m}_fit.txt'
    np.savetxt(os.path.join(RAWDIR, outname), Xm, fmt = '%.8f')
    ######################################################################################

    ######################################################################################
    # call the plotting function

    outname = f'prml_ch_01_figure_1.4_PRNG-seed_{seedValue}_m_{m}_fit_polynomial_leastSq'
Ejemplo n.º 14
0
    ######################################################################################
    # polynomial curve fitting (learning the model)

    m_order = np.arange(0, 10, 1).astype('int')
    res = np.zeros((len(m_order), 3))

    for m in m_order:

        # create coefficient vector (containing all fit parameters)
        w = np.ones((m + 1, ))

        # curve fitting
        popt, pcov = curve_fit(polynomial_horner, Xt[:, 0], Xt[:, 1], p0=w)

        yPredict = polynomial_horner(Xt[:, 0], *popt)

        # test data set prediction
        yPredictTest = polynomial_horner(X[:, 0], *popt)

        # compute sum of squares deviation

        sum_of_squares_error = 0.5 * np.sum(np.square(yPredict - Xt[:, 1]))
        sum_of_squares_error_test = 0.5 * np.sum(
            np.square(yPredictTest - X[:, 1]))

        RMS = np.sqrt(2.0 * sum_of_squares_error / n_train)
        RMS_test = np.sqrt(2.0 * sum_of_squares_error_test / n_test)

        res[m, 0] = m
        res[m, 1] = RMS
Ejemplo n.º 15
0
    ######################################################################################
    # file i/o
    outname = f'figure_1.6_training_data_N_{n_train}_PRNG_seed_{seed_value}.txt'
    np.savetxt(os.path.join(RAWDIR, outname), Xt, fmt = '%.8f')
    ######################################################################################

    ######################################################################################
    # polynomial curve fitting (learning the model)
    m = 9
    w = np.ones((m + 1,))
    popt, pcov = curve_fit(polynomial_horner, Xt[:, 0], Xt[:, 1], p0 = w)

    # create fitted model
    n_modelpoints = 800
    xVals = np.linspace(0.0, 1.0, n_modelpoints)
    yVals = polynomial_horner(xVals, *popt)
    Xm = np.zeros((n_modelpoints, 2))
    Xm[:, 0] = xVals
    Xm[:, 1] = yVals

    ######################################################################################
    # file i/o
    outname = f'figure_1.6_fitted_model_N_{n_train}_PRNG_seed_{seed_value}.txt'
    np.savetxt(os.path.join(RAWDIR, outname), X, fmt = '%.8f')
    ######################################################################################

    ######################################################################################
    # call the plotting function

    outname = f'figure_1.6_N_{n_train}_PRNG_seed_{seed_value}'
    outname += '_Python_' + platform.python_version() + \