Python Math.erfc()方法
在Python中,math
模块提供了许多数学实用函数,其中一个方法是math.erfc()
。这个方法的目的是找到指定参数的互补误差函数。
这个互补的误差函数也被称为高斯误差函数。如果参数不是一个数字,这个方法会抛出一个错误。
这种方法主要是为了找到重要性丧失的原因。该方法取值范围为-无穷大到+无穷大,返回值范围为0到2。
这个方法是一个误差函数,通常在统计学、概率学和偏微分方程中使用。
Pythonmath.erfc()
方法的语法
math.erfc(number)
参数
number |
这个方法接受一个单一的参数作为浮动值。如果给出任何其他数据,该方法会抛出一个错误。 |
返回值
该方法以浮动值的形式返回一个数字的错误函数。
示例代码:使用math.erfc()
方法
要使用math.erfc()
方法,我们必须使用Python 3.2或以上的版本。如果参数的数据类型不是数字或浮点数,该方法会抛出一个错误。
下面的例子显示了传递给math.erfc()
方法的四个参数,它们是正/负的整数和小数。结果数值在0到2之间。
# math import for the use of built-in mathematical functions
import math
# returns the complementary error function
def complementary_error_function(number):
return math.erfc(number)
print("The complementary error function for 20 is", complementary_error_function(20))
print("The complementary error function for 9.88 is", complementary_error_function(9.88))
print("The complementary error function for -6 is", complementary_error_function(-6))
print("The complementary error function for -2.44 is", complementary_error_function(-2.44))
输出:
# result shows that the value range between 0 to 2.
# result when +integer number is passed as a parameter
The complementary error function for 20 is 5.395865611607901e-176
# result when +decimal number is passed as a parameter
The complementary error function for 9.88 is 2.296551005681906e-44
# result when -integer number is passed as a parameter
The complementary error function for -6 is 2.0
# result when a -decimal number is passed as a parameter
The complementary error function for -2.44 is 1.9994408261164485
示例代码:使用math.erfc()
方法时,TypeError
的原因
在Python 3.2及以上版本中,如果math.erf()
方法的参数不是浮动值,该方法会返回一个TypeError
。TypeError
通常发生在方法需要一些其他类型的数据,与提供的数据不同。
math.erfc()
方法只接受浮动值作为参数。
import math
# methods returns the TypeError exception error
def complementary_error_function(number):
return math.erfc(number)
# the passing parameter is a string, not a number
print("The complementary error function for 20 is", complementary_error_function('a'))
输出:
# Not a valid argument. We need to change the type to float value
TypeError: must be a real number, not str
示例代码:使用math.erfc()
方法来确定高斯分布的量值
math.erfc()
方法可用于不同的数学工作,因为它可以确定误差函数。下面的实现是math.erfc()
方法的一个使用案例,我们有一个名为gaussian_qualites()
的函数。
该函数需要四个变量:获取函数参数、当前最小值、平均值和标准差的矢量。
import math
# numpy module for using NumPy array
import numpy as np
# returns the acquisition function values
def gaussian_qualites(acqui, mi, means, sd):
if isinstance(sd, np.ndarray):
sd[sd<1e-10] = 1e-10
elif sd< 1e-10:
sd = 1e-10
u = (mi - means - acqui)/sd
phi = np.exp(-0.5 * u**2) / np.sqrt(2*np.pi)
# here, we have the vector form of erfc
Phi = 0.5 * math.erfc(-u / np.sqrt(2))
return (phi, Phi, u)
print("The quantiles of the Gaussian distribution are ",gaussian_qualites(10, 2.4, 6.62, 99))
输出:
# return output as a vector
The quantiles of the Gaussian distribution are (0.39484806419518365, 0.44289381217966217, -0.14363636363636365)
示例代码:使用math.erfc()
方法来寻找高斯概率函数下的面积
我们经常使用Python的math
模块来寻找复杂的数学问题解决方案。最新的Python版本,如3.2或以上版本,往往能用其自动解决方案解决任何问题。
在下面的例子中,我们使用math.erfc()
方法来寻找高斯概率密度函数下的面积。
该方法gaussian_probability()
,参数从-无穷大到指定值n
。我们将向该方法传递一个正数和一个负数的浮动值来验证它。
下面代码中的输入-4.0
,没有返回一个负值,因为该值首先经过abs()
方法,在进入math.erfc()
方法之前,该方法使其成为一个正数。
import math
# method returns the area under the probability density function
def gaussian_probability(number):
square_root = math.sqrt(2) / 2
a = float(number) * square_root
# abs() method always returns the positive number
absolute_value = abs(a)
if absolute_value < square_root:
b = 0.5 + 0.5 * math.erf(a)
else:
b = 0.5 * math.erfc(absolute_value)
if a > 0:
b = 1 - b
return b
# positive and negative float number as an argument
print("The area under the Gaussian probability for a positive number is",gaussian_probability(2.8))
print("The area under the Gaussian probability for a negative number is",gaussian_probability(-4.0))
输出:
The area under the Gaussian probability for a positive number is 0.997444869669572
The area under the Gaussian probability for a negative number is 3.167124183311989e-05