Monotonic functions

Monotonic function:

A function, whose value is constant or increases or decreases as the input to the function increases or decreases.

Example:

import pandas as pds

 

sequence = [1,1,1,1,1,1];

series   = pds.Series(sequence);

 

# Check the is_monotonic attribute

isMonotonic = series.is_monotonic;

 

result = "non-monotonic";

if isMonotonic:

    result = "monotonic";

 

print("The series:");

print(series);

print("is %s."%result);

 

Output:

The series:

0    1

1    1

2    1

3    1

4    1

5    1

dtype: int64

is monotonic.

 

Monotonically increasing function:

A monotonically increasing function is a function whose output increases in value as the input value to the function increases. This rate of increase may not be constant over the distribution. Real world examples of monotonically increasing functions include knowledge available in the universe and the age of the universe.

Example:

import pandas as pds

 

# Function to print the result of whether

# the series is monotonically increasing

def printResults(series, testResult):

    print("The series:");

    print(series);

    result = "monotonically not-increasing.";

    if testResult:

        result = "monotonically increasing.";

    print("is %s"%result);

 

# Sequences as Python lists

numberSequence1 = [4,5,5.1,5.2,7.01,8.95];

numberSequence2 = [1,2,1.9,3,4];

 

# Create pandas series instances

series1 = pds.Series(numberSequence1);

series2 = pds.Series(numberSequence2);

 

# Get the value of the monotonically-increasing property/attribute

monoIncS1 = series1.is_monotonic_increasing;

monoIncS2 = series2.is_monotonic_increasing;

 

# Print the results

printResults(series1, monoIncS1);

printResults(series2, monoIncS2);

 

Output:

The series:

0    4.00

1    5.00

2    5.10

3    5.20

4    7.01

5    8.95

dtype: float64

is monotonically increasing.

The series:

0    1.0

1    2.0

2    1.9

3    3.0

4    4.0

dtype: float64

is monotonically not-increasing.

 

Monotonically decreasing function:

A monotonically decreasing function is a function whose output decreases in value as the input value to the function decreases. This rate of decrease may not be constant over the distribution. Real world example of monotonically decreasing functions is life available for living beings to acquire knowledge.

Example:

import pandas as pds

 

# Create pandas series instance with number sequences

s1 = pds.Series([1, 0.75, 0.7499, 0.2, -.5, -2.5]);

s2 = pds.Series([10, 9, 8, 11, 7, 6, 12]);

 

# Check for the property: monotonically decreasing

monoDecr_s1 = s1.is_monotonic_decreasing;

monoDecr_s2 = s2.is_monotonic_decreasing;

 

print("The series:");

print(s1);

print("is monotonically decreasing: %s" % monoDecr_s1);

 

print("The series:");

print(s2);

print("is monotonically decreasing: %s" % monoDecr_s2);

 

Output:

The series:

0    1.0000

1    0.7500

2    0.7499

3    0.2000

4   -0.5000

5   -2.5000

dtype: float64

is monotonically decreasing: True

The series:

0    10

1     9

2     8

3    11

4     7

5     6

6    12

dtype: int64

is monotonically decreasing: False

 


Copyright 2024 © pythontic.com