Ocean Waves (Sinusoidal) Regression
By: Pratik Shukla
Tags: Machine Learning, Mathematics, Artificial Intelligence, Data Science, Data Visualization

Definition :

A Sine wave or sinusoidal wave is a mathematical curve that describes a smooth periodic oscillation. A Sine wave is a continuous wave, it goes from 0 to 360 degrees.

Table Representing Sine Values

Generation of Sine Wave

Sinusoidal function is given by,

Sine Function Formula

The period of the sine curve is the length of one cycle of the curve. The natural period of the sine curve is 2π. So, a coefficient of b=1 is equivalent to a period of 2π. To get the period of the sine curve for any coefficient B, just divide 2π by the coefficient b to get the new period of the curve.

Real Life Application Of Sine Function :

(1) Generation of music waves.

(2) Sound travels in waves.

(3) Trigonometric functions in constructions.

(4) Used in space flights.

(5) GPS location calculations.

(6) Architecture.

(7) Electrical current.

(8) Radio broadcasting.

(9) Low and high tides of the ocean.

(10) Buildings.

Now, I’m going to show you different kinds of sine waves that can be generated by modifying its parameters. My ultimate goal is to show you how modification of parameters affects the shape of the graph. After that, I’m going to take an example that will show how we can implement sinusoidal regression in python.

First of all, we are going to have a look at different graphs of sine waves by modifying the parameter values.

Why are we going to do this?

As we know data visualization has a major role in data science. While working with data (regression) we need to find the best fit curve for it. For that, we’ll have a lot of parameters in our function. Now if we don’t know what happens when we change these parameters then it’s going to be a cumbersome journey to go through it, right? So here we’ll take examples to understand what happens when we change the parameter values.

How we should understand it?

We will take our main sine function and then we’ll modify the parameter values then there will be a graph for that to visualize it. What I want you to do is take a pen and paper and try to plot the sine graph while going through examples. I think that’ll help you understand better.

Let’s have a look at different sine graphs! ☀️

Example : 1

Y = 1*Sin(1(X+0))+0

Y = SinX

A = 1

B = 1

C = 0

D = 0

Period = 2*pi/1 = 2*pi

Y = SinX

Here we can see that the sine wave has the amplitude of 1 and the length of cycle for the sine wave goes from 0 to 2pi.

Example 2 :

Y = 2*Sin(1(X+0))+0

Y = 2SinX

A = 2

B = 1

C = 0

D = 0

Period = 2*pi/1 = 2*pi

Y = 2SinX

Here we can see that the sine wave has an amplitude of 2. As we can see that it increases the height of our sine wave. The length of cycle for the sine wave goes from 0 to 2pi.

Example 3 :

Y = 1*Sin(2(X+0))+0

Y = Sin2X

A = 1

B = 2

C = 0

D = 0

Period = 2*pi/2 = pi

Y = Sin2X

Here we can see that the sine wave has an amplitude of 1. The length of cycle for the sine wave goes from 0 to pi.

Example 4:

Y = 2*Sin(2(X+0))+0

Y = 2Sin2X

A = 2

B = 2

C = 0

D = 0

Period = 2*pi/2 = pi

Y = 2Sin2X

Here we can see that the sine wave has an amplitude of 2. The length of cycle for the sine wave goes from 0 to pi. As we can see from the graph it has increased the height of our wave and one cycle completer at pi.

Example 5:

Y = 2*Sin(1(X+1))+0

Y = 2Sin(X+1)

A = 2

B = 1

C = 1 (Shift to Left)

D =0

Period = 2*pi/1 = 2*pi

Y = 2Sin(X+1)

Here we have shifted our curve to the left by 1. We took the amplitude value as 1. Notice that here we have the period of 2*pi. That means one cycle has a length of 2*pi. Since we have shifted it to the left by one unit, the first cycle will be shifted 1 unit to the left from 2pi.

Example 6 :

Y = 2*Sin(1(X-1))+0

Y = 2Sin(X-1)

A = 2

B = 1

C = -1 (Shift to Right)

Period = 2*pi/1 = 2*pi

Y = 2Sin(X-1)

Here we have shifted our curve to the right by 1. We took the amplitude value as 1. Notice that here we have the period of 2*pi. That means one cycle has a length of 2*pi. Since we have shifted it to the right by one unit, the first cycle will be shifted 1 unit to the right from 2pi.

Example 7:

Y = 1*Sin(1(X+0))+2

Y = SinX +2

A = 1

B = 1

C = 0

D =2

Period = 2*pi/1 = 2*pi

Y = SinX +2

Here notice that we have shifted our curve 2 points on the positive y-axis. The amplitude of the curve is 1. The period as you can see is also 2*pi.

Example 8 :

Y = 1*Sin(1(X+0)) — 2

Y = SinX — 2

A = 1

B = 1

C =0

D =-2

Period = 2*pi/1 = 2*pi

Y = SinX — 2

Here notice that we have shifted our curve 2 points on the negative y-axis. The amplitude of the curve is 1. The period as you can see is also 2*pi.

Example 9:

Y = -1*Sin(1(X+0))+0

Y = -SinX

A = -1

B = 1

C = 0

D =0

Period = 2*pi/1 = 2*pi

Y = -SinX

Here we have changed the amplitude value to -1. From the illustration above, we can see that our graph is inverted from the previous version which has amplitude of 1. It means the positive y-axis is replaced by the negative y-axis.

Example 10 :

Y = -2*Sin(1(X+0))+0

Y = -2SinX

A = -2

B = 1

C = 0

D = 0

Period = 2*pi/1 = 2*pi

Y = -2SinX

Here we are going to set the value of amplitude to -2. So it’s just like our previous graph but the height of sine curve is increased. Also notice that the period of sine curve is 2*pi.

Example 11 :

Y = -2*Sin(1(X-1))+0

Y = -2Sin(X-1)

A = -2

B = 1

C = -1

D = 0

Period = 2*pi/1 = 2*pi

Y = -2Sin(X-1)

Here we have shifted the curve to the right by 1 point and also changed the amplitude value to -1. The period of the sine curve is 2*pi.

Example 12 :

Y = -2*Sin(1(X+1))+0

Y = -2Sin(X+1)

A = -2

B = 1

C = 1

D = 0

Period = 2*pi/1 = 2*pi

Y = -2Sin(X+1)

Here we have shifted the curve to the left by 1 point and also changed the amplitude value to -1. So it’s going to negative y-axis first. The period of the sine curve is 2*pi.

Example 13 :

Y = 2*Sin(-1(X+1))+0

Y = 2Sin(-1(X+1))

A = 2

B = -1

C = 1

D = 0

Period = 2*pi/-1 = -2*pi

Y = 2Sin(-1(X+1))

Here we have shifted the curve to the left by 1 unit. One thing to notice is that, since we have period of -2*pi, our graph is going to the left side or we can say on the negative x-axis. When we have a positive value of the period it goes to the positive x-axis.

Example :14

Y = -2*Sin(-1(X-1))+0

Y = -2Sin(-1(X-1))

A = -2

B = -1

C = -1

D = 0

Period = 2*pi/-1 = -2*pi

Y = -2Sin(-1(X-1))

Here we have shifted the curve to the right by 1 unit. One thing to notice is that, since we have period of -2*pi, our graph is going to the left side or we can say on the negative x-axis.

Example 15 :

Y = 1*Sin(1(X+1))+1

Y = 1*Sin(X+1) + 1

A =1

B = 1

C =1

D=1

Period = 2*pi/1 = 2*pi

Y = 1*Sin(X+1) + 1

Here we have the amplitude value of 1 and we have also shifted the curve to the left by 1 unit. Here notice that the period of our curve is 2*pi. One more thing to notice is that we have shifted our curve by 1 on the positive y-axis.

Example 16 :

Y = -1*Sin(-1(X-1))-1

A = -1

B = -1

C = -1

D =-1

Period = 2*pi/-1 = -2*pi

Y = -1*Sin(-1(X-1))-1

Here we have the amplitude value of -1 and we have also shifted the curve to the right by 1 unit. Here notice that the period of our curve is -2*pi. So it’s going to go to the left first. One more thing to notice is that we have shifted our curve by 1 on the negative y-axis.

Credits : Unsplash

Let’s code :

(1) Import required libraries :

Here we are going to import four libraries.

numpy : for calculations.

for calculations. matplotlib : to plot our dataset and curves.

to plot our dataset and curves. curve_fit : to find the optimal parameters values for our sine curve.

to find the optimal parameters values for our sine curve. r2_score : to calculate the accuracy of our model.

# Import required libraries : import numpy as np

import matplotlib.pyplot as plt

from scipy.optimize import curve_fit # For curve fitting

from sklearn.metrics import r2_score # To check accuracy

(2) Generate Dataset :

Since we don’t have an actual dataset that represents a sine wave pattern, what we are going to do is make our own dataset.

Here we are going to use linspace function to get the value of X. For Y value we’ll use 2*pi*X. Now our real-life dataset isn’t going to follow the exact since pattern, right? There will be some noise in the dataset. We’re also going to add some noise in our dataset to make it look more realistic!

After then we are just going to scatter the X, Y points on the plane. That way we can visualize the dataset we have created.

# Generating dataset : # Y = A*sin(B(X + C)) + D

# A = Amplitude

# Period = 2*pi/0B

# Period = Length of One Cycle

# C = Phase Shift (In Radian)

# D = Vertical Shift X = np.linspace(0,1,100) #(Start,End,Points) # Here…

# A = 1

# B= 2*pi

# B = 2*pi/Period

# Period = 1

# C = 0

# D = 0 Y = 1*np.sin(2*np.pi*X) # Adding some Noise :

Noise = 0.4*np.random.normal(size=100) Y_data = Y + Noise plt.scatter(X,Y_data,c=”r”)

(3) Finding the best fit line for our dataset :

Here I’m going to show you, how we can fit a “Regression Line” to our dataset. Here we’ll calculate the error and in the next part, we’ll plot the sine curve that best fits our dataset. From the accuracy of both models, we can be sure that why we should use sinusoidal regression in this case.

# Function to calculate the value :

def calc_line(X,m,b):

return b + X*m # It returns optimized parameters for our function :

# popt stores optimal parameters

# pcov stores the covariance between each parameters.

popt,pcov = curve_fit(calc_line,X,Y_data) # Plot the main data :

plt.scatter(X,Y_data) # Plot the best fit line :

plt.plot(X,calc_line(X,*popt),c=”r”) # Check the accuracy of model :

Accuracy =r2_score(Y_data,calc_line(X,*popt))

print (“Accuracy of Linear Model : “,Accuracy)

Here notice that for our data-set which follows sine wave pattern, we have found the best fit line for it. Also notice that the accuracy of our model is only around 40%. So here we can conclude that for data-sets that follows sine wave pattern if we use simple linear regression then we may not achieve higher accuracy. That’s the reason to use sine wave regression.

(4) Finding the optimal sine curve that fits our data :

Now it’s time to find the best fit curve for it. Here we can see that our data follows sine wave pattern. So we are going to find the optimal parameters using curve_fit method for sine wave. After then are going to plot the data with the best fit curve to visualize it.

# Calculate the value :

def calc_sine(x,a,b,c,d):

return a * np.sin(b* ( x + np.radians(c))) + d # Finding optimal parameters :

popt,pcov = curve_fit(calc_sine,X,Y_data) # Plot the main data :

plt.scatter(X,Y_data) # Plot the best fit curve :

plt.plot(X,calc_sine(X,*popt),c=”r”) # Check the accuracy :

Accuracy =r2_score(Y_data,calc_sine(X,*popt))

print (Accuracy)

Here notice that our best fit curve is in the shape of the sine wave. Also, notice that the accuracy of our model has increased to around 79%. So here we can conclude that for sine regression helped us achieve higher accuracy.

Putting it all together :

Okay. So I think that covers almost everything we are going to need in Machine Learning from sinusoidal waves. If you enjoyed reading this, then please hit the clap icon, that’ll motivate me to write such comprehensive articles on various machine learning algorithms.

Thank you for reading this article. I hope it helped!

I regularly post my articles on : patrickstar0110.blogspot.com

All my articles are available on: medium.com/@shuklapratik22

If you have any doubts then feel free to contact me: shuklapratik22@gmail.com