Interpolate()

The Interpolate class is a utility for generating interpolated values between two numerical points using various interpolation methods. It allows users to specify the start and end values, the number of interpolation steps, and the desired interpolation method to achieve smooth transitions suitable for quantitative analysis.

Import Statement

from marketquant.math import Interpolate

Usage Overview

To use the Interpolate class, you need to initialize it with the start and end values, specify the number of interpolation steps, choose an interpolation method, and then call the interpolate method to generate the interpolated values.

Class: Interpolate

Method: interpolate

Interpolate.interpolate()

This is the primary method used for generating interpolated values between the specified start and end points using the chosen interpolation method.

Arguments:

  • This method does not take any additional arguments beyond those provided during class initialization.

Returns:

  • List[float]: A list of interpolated numerical values based on the specified parameters.

How to Use Interpolate()

  1. Import the Interpolate Class

    Begin by importing the Interpolate class from the marketquant.interpolation module.

    from marketquant.math import Interpolate
  2. Initialize the Interpolate Class

    Create an instance of the Interpolate class by providing the necessary parameters:

    • start (float): The starting value of the interpolation.

    • end (float): The ending value of the interpolation.

    • steps (int, optional): The number of interpolation steps. Default is 10.

    • method (str, optional): The interpolation method to use. Options include 'linear', 'ease-in', 'ease-out', 'ease-in-out', and 'cubic'. Default is 'linear'.

    interpolator = Interpolate(start=0, end=10, steps=5, method='ease-in')
  3. Generate Interpolated Values

    Call the interpolate method on the instance to obtain the list of interpolated values.

    interpolated_values = interpolator.interpolate()

Examples

Example 1: Basic Linear Interpolation

Note: If you are interpolating two points, use steps=2.

from marketquant.math import Interpolate

# Initialize the Interpolate class for linear interpolation from 0 to 10 with 5 steps
interpolator = Interpolate(start=0, end=10, steps=5, method='linear')

# Generate interpolated values
interpolated_values = interpolator.interpolate()

print(interpolated_values)

Output:

[0.0, 2.5, 5.0, 7.5, 10.0]

Example 2: Ease-In Interpolation

from marketquant.math import Interpolate

# Initialize the Interpolate class for ease-in interpolation from 0 to 10 with 10 steps
interpolator = Interpolate(start=0, end=10, steps=10, method='ease-in')

# Generate interpolated values
interpolated_values = interpolator.interpolate()

print(interpolated_values)

Output:

[0.0, 0.1111111111111111, 0.4444444444444444, 1.0, 1.7777777777777777, 
 2.7777777777777777, 4.0, 5.444444444444445, 7.111111111111111, 9.0]

Example 3: Ease-Out Interpolation

from marketquant.math import Interpolate

# Initialize the Interpolate class for ease-out interpolation from 5 to 15 with 6 steps
interpolator = Interpolate(start=5, end=15, steps=6, method='ease-out')

# Generate interpolated values
interpolated_values = interpolator.interpolate()

print(interpolated_values)

Output:

[5.0, 6.666666666666667, 9.444444444444445, 13.333333333333334, 18.333333333333332, 25.0]

Note: The ease-out interpolation may produce values that extend beyond the specified end value depending on the interpolation function's behavior.

Example 4: Cubic Interpolation

from marketquant.math import Interpolate

# Initialize the Interpolate class for cubic interpolation from 100 to 200 with 8 steps
interpolator = Interpolate(start=100, end=200, steps=8, method='cubic')

# Generate interpolated values
interpolated_values = interpolator.interpolate()

print(interpolated_values)

Output:

[100.0, 101.78515625, 104.0, 107.5390625, 112.5, 118.90625, 126.7578125, 136.0]

Note: The cubic interpolation provided here is a simple cubic function. Depending on your requirements, you might want to implement a more sophisticated cubic interpolation (like spline interpolation) which typically requires more control points.

Customization Options for Interpolation

When performing interpolation, you can control various aspects of the process using the following parameters:

  • start (float): Set the starting value of the interpolation.

  • end (float): Set the ending value of the interpolation.

  • steps (int): Define the number of interpolation steps. Increasing the number of steps results in a smoother transition.

  • method (str): Choose the interpolation method. Available options are:

    • 'linear': Progresses at a constant rate.

    • 'ease-in': Starts slowly and accelerates (quadratic).

    • 'ease-out': Starts quickly and decelerates (quadratic).

    • 'ease-in-out': Combines ease-in and ease-out for a smooth start and end (cubic).

    • 'cubic': Pure cubic interpolation.

Example of Customization:

from marketquant.math import Interpolate

# Custom interpolation with 20 steps using ease-in-out method
interpolator = Interpolate(start=50, end=150, steps=20, method='ease-in-out')
interpolated_values = interpolator.interpolate()

print(interpolated_values)

Output

The output of the Interpolate class will be:

  • A List of Interpolated Values: A List[float] containing the interpolated numerical values based on the specified parameters.

Example Output:

[0.0, 2.5, 5.0, 7.5, 10.0]

This output corresponds to linear interpolation from 0 to 10 with 5 steps.

Last updated