SoFunction
Updated on 2024-11-21

python how to realize MK mutation test method, code copy modification available

demand (economics)

The maximum permafrost depth is known for the year and calendar year, and the maximum permafrost depth Mk mutation test is calculated.

principle

请添加图片描述

请添加图片描述

请添加图片描述

Tools and languages

  • python
  • jupter notebook

coding process

Defining Functions

def mktest(inputdata):
    import numpy as np
    inputdata = (inputdata)
    n=[0]
    Sk = (n)
    UFk = (n)
    r = 0
    for i in range(1,n):
        for j in range(i):
            if inputdata[i] > inputdata[j]:
                r = r+1
        Sk[i] = r
        E = (i+1)*i/4
        Var = (i+1)*i*(2*(i+1)+5)/72
        UFk[i] = (Sk[i] - E)/(Var)
    Sk2 = (n)
    UBk = (n)
    inputdataT = inputdata[::-1]
    r = 0
    for i in range(1,n):
        for j in range(i):
            if inputdataT[i] > inputdataT[j]:
                r = r+1
        Sk2[i] = r
        E = (i+1)*(i/4)
        Var = (i+1)*i*(2*(i+1)+5)/72
        UBk[i] = -(Sk2[i] - E)/(Var)
    UBk2 = UBk[::-1]
    return UFk, UBk2
Defining a function to compute a variable
```python
def mktest(inputdata):
    import numpy as np
    inputdata = (inputdata)
    n=[0]
    s              =  0
    Sk = (n)
    UFk = (n)
    for i in range(1,n):
        for j in range(i):
            if inputdata[i] > inputdata[j]:
                s = s+1
            else:
                s = s+0
        Sk[i] = s
        E = (i+1)*(i/4)
        Var = (i+1)*i*(2*(i+1)+5)/72
        UFk[i] = (Sk[i] - E)/(Var)
    Sk2 = (n)
    UBk = (n)
    s  =  0
    inputdataT = inputdata[::-1]
    for i in range(1,n):
        for j in range(i):
            if inputdataT[i] > inputdataT[j]:
                s = s+1
            else:
                s = s+0
        Sk2[i] = s
        E = (i+1)*(i/4)
        Var = (i+1)*i*(2*(i+1)+5)/72
        UBk[i] = -(Sk2[i] - E)/(Var)
    UBk2 = UBk[::-1]
    return UFk, UBk2

Introduce the variable , form a mutation test plot

import  as mdates    # Processing Date
import  as plt
import numpy as np
from pylab import mpl
from  import MultipleLocator
['-serif'] = ['SimHei'] #Prevent the title from being garbled.
['axes.unicode_minus'] = False   #Prevent the negative numbers on the graph from appearing as boxes.
# y-values and x-values Enter the maximum depth of permafrost values for each of the six sites, importing the values as a list
a = [150,150,114,109,96,95,83,76,109,80,115,80,94,86,133,91,110,116,114,128,172,172,
162,121,175,151,110,92,116,156,134,110,89,97,109,157,153,105,76,87,122,78,97,93,141,162,
123,133,161,128,138,104,133,102,140,109,118,86,126,92,121,149,116]  # This partial value can be replaced with the value of the temperature, hydrology, etc. to be examined
x_values=list(range(1961,2022))
uf,ub = mktest(a)
(figsize=(8,4))   # Image size
(uf,'r',label='UFk')
(ub,'b',label='UBk')
([0,5,10,15,20,25,30,35,40,45,50,55,60],['1960','1965','1970','1975','1980','1985','1990','1995','2000','2005','2010','2015','2020',])
# Replace the default x-axis values with the year's x-axis, which defaults to 0-61, a total of 62 values representing the x-axis contents.
# 0.01 significance test
()
(1.96)
(-1.96)
# Setting the label (title) of the image
("Results of the sudden change in maximum permafrost depth test at Fuyun Point.")#Names on the x-axis
("Years (1960-2022)")#Names on the x-axis
("Parameters for the fluctuation of mutation values.")#Names on the y-axis
() # Forming gridlines for output
x_major_locator=MultipleLocator(5)
()

How it will look after it's finally in the picture.

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.