numpy in polynomial fit

Polynomial fitting

Introduction: Any differentiable function can be used to estimate a polynomial of degree N, and the N-th power than a higher-order part of infinitely small.

Polynomial fitting and linear fitting difference is:

Linear fit using linear vector spaces to fit the discrete data points

Polynomial curve fit is used to fit the continuous data of discrete points

Key_Function:  

np.polyfit function: input array x-axis, y-axis input array, a series of data points with a polynomial, a coefficient array Returns

np.polyval function: polynomial coefficients an input array, an input x, and returns a value y corresponding to x

np.roots function: polynomial coefficients an input array, represents a return to the root array

np.poluder function: input array polynomial coefficients, the polynomial coefficient array returned after a request guide

Code:

import numpy as np
import matplotlib.pyplot as plt

bhp = np.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True)
vale = np.loadtxt('VALE.csv', delimiter=',', usecols=(6,), unpack=True)

print(bhp)
'''
[ 93.72  95.64  94.56  93.3   93.93  92.39  92.11  92.36  91.76  93.91
  94.6   93.27  94.43  96.02  95.76  94.47  94.34  92.22  88.31  89.59
  89.02  86.95  84.88  87.38  88.56  89.59  88.71  90.02  91.26  90.67]

'''

Print (Vale)
 '' ' 
[34.37 35.13 35.14 35.31 35.57 35.03 33.44 33.94 34.21 34.27 
  34.23 33.76 34.32 34.87 34.5 33.23 33.29 32.88 31.91 32.17 
  32.44 31.91 31.04 31.51 32.14 32.42 32.25 32.36 32.34 32.7] 

' '' 

T = np.arange (len ( BHP))
 Print (T)
 '' ' 
[0. 5. 4. 3. 1 2. 8. 6. 7. 9 10. 11. 17 16 15 14 12 is 13 is 18 is 20 is 21 is 22 is 23 is 24. 19 
 25 27 28 26 is 29] 
' '' 

poly = np.polyfit (t, BHP - Vale,. 5)      # used as a t x, bhp-vale as y, curve fitting 
Print (poly)
 '' ' 
[06 1.01779570e--9.90778257e-03 -3.46268017e-02 4.62600680e- 01
  6.09781428e + 01 + 00 -2.16153196e] 
'' ' 

poly = np.polyfit (T, BHP - Vale,. 3)     # change the order of the polynomial 
Print (poly)
 ' '' 
[03 -5.28581762e-1.11655581e-02 5.79791202e-01 + 01 5.80684638e] 
'' ' 
# 1.11655581e-03 represents 1.11655581 * 10 ^ -3 
# of the four values represent the third power coefficient, quadratic coefficient, first power coefficient, zero square coefficient 


# next predicted value 
Print (np.polyval (poly, T [-1] +. 1 ))
 # 57.9743076081 

# find polynomial roots 
Print (np.roots (poly))
 # [35.48624287 + 30.62717062j 35.48624287- + 0.j -23.63210575 30.62717062j] 
# the present embodiment, there are three complex roots, complex roots, see below Detailed 

# derivation polynomial function 
der = np.polyder (poly)
 Print (der)
 # [.00334967 -0.10571635 0.58068464] 
# the reciprocal of the coefficient array, respectively quadratic term, the first term in the zero-th item 

# requests the function extrema 
Print (np.roots (der))
 # [24.47820054 7.08205278] 

# find the maximum and minimum function 
Vals = np.polyval (poly, T)
 Print (np.argmax (Vals))   # np.argmax seeking the index corresponding to the maximum vals, i.e., the value of x 
# . 7 
Print (np.argmin (vals))   # np.argmin is seeking index corresponding to the minimum vals, i.e., the value of x 
# 24 


plt.plot (T, BHP - Vale) 
plt.plot (T, Vals) 
plt.show ()

* Using a simple moving average, or exponential moving average, may be performed before fitting the data, the data smoothing process.

 

About the complex roots of analysis:

As it can be seen from the following, a plurality of dimensions by extension of the domain, no solution to the original equation solving.

Above cubic polynomial derived, originally in the x dimension it is not a root, but the rotation by adding to x, x expansion of freedom, so that root.

The cubic polynomial, saying only that a mapping from the domain to the range, as to what the domain that function does not care.

 

 

What is imaginary

First, assume that a number of axes, there are two opposite points above: +1 and -1.

The forward part of the root axes may rotate about the origin. Obviously, rotated counterclockwise 180 degrees + 1 becomes -1.

This corresponds to 90 degrees counter-clockwise rotation of the two.

Thus, we get the following relationship:

(+1) * (90 degrees counter-clockwise rotation) * (rotated counterclockwise by 90 degrees) = (-1)

If the +1 eliminated, the equation becomes:

(Rotated 90 degrees counter-clockwise) ^ 2 = (-1)

The "rotated 90 degrees counterclockwise" denoted i:

i^2 = (-1)

This formula is very familiar, it is the definition of formula imaginary numbers.

So, we can know, imaginary number i is rotated 90 degrees counterclockwise, i is not a number, but an amount of rotation.

The definition of complex numbers

Since i represents the amount of rotation, we can use i, shows the rotation state of any real number.

Regarded as the real axis and the horizontal axis, the imaginary longitudinal axes seen, constitutes a two-dimensional plane. Rotated to any positive real number a certain angle, necessarily uniquely correspond to a point in this plane.

As long as the abscissa and the ordinate is determined, such as (1, i), a rotation amount can be determined real number (45 degrees).

Mathematician with a special representation, showing the two-dimensional coordinates: that + the horizontal and vertical coordinates connected. For example, the (1, i) is expressed as 1 + i. This representation is called a complex (complex number), where a is called the real part, i is called the imaginary part.

Why should this happen represented as two-dimensional coordinates, the next section tell you why.

Action imaginary number: addition

The introduction of an imaginary number, greatly facilitate the calculation of the rotation involves.

For example, physics need to calculate the " composition of forces ." Suppose a force is 3 + i, another force is 1 + 3i, may I ask how much their combined force?

根据"平行四边形法则",你马上得到,合成力就是( 3 + i ) + ( 1 + 3i ) = ( 4 + 4i )。

这就是虚数加法的物理意义。

虚数的作用:乘法

如果涉及到旋转角度的改变,处理起来更方便。

比如,一条船的航向是3 + 4i 。

如果该船的航向,逆时针增加45度,请问新航向是多少?

45度的航向就是 1 + i 。计算新航向,只要把这两个航向 3 + 4i 与 1 + i 相乘就可以了(原因在下一节解释):

( 3 + 4i ) * ( 1 + i ) = ( -1 + 7i )

所以,该船的新航向是-1 + 7i。

如果航向逆时针增加90度,就更简单了。因为90度的航向就是 i ,所以新航向等于:

( 3 + 4i ) * i = ( -4 + 3i )

这就是虚数乘法的物理意义:改变旋转角度

虚数乘法的数学证明

为什么一个复数改变旋转角度,只要做乘法就可以了?

下面就是它的数学证明,实际上很简单。

任何复数 a + bi,都可以改写成旋转半径 r 与横轴夹角 θ 的形式。

假定现有两个复数 a + bi 和 c + di,可以将它们改写如下:

a + bi = r1 * ( cosα + isinα )

c + di = r2 * ( cosβ + isinβ )

这两个复数相乘,( a + bi )( c + di ) 就相当于

r1 * r2 * ( cosα + isinα ) * ( cosβ + isinβ )

展开后面的乘式,得到

cosα * cosβ - sinα * sinβ + i( cosα * sinβ + sinα * cosβ )

根据三角函数公式,上面的式子就等于

cos(α+β) + isin(α+β)

所以,

( a + bi )( c + di ) = r1 * r2 * ( cos(α+β) + isin(α+β) )

这就证明了,两个复数相乘,就等于旋转半径相乘、旋转角度相加。

 

Guess you like

Origin www.cnblogs.com/draven123/p/11391782.html