Skip to main content

sengbao_mle

翻新的最大似然估计框架

copied and renewed maximum likelihood estimation package from python-mle


搬运与翻新说明

  • 本仓库搬运自ibab/python-mle
  • 由于旧仓库最后一次更新于2016年2月19日,其中一些结构安排和依赖库已经发生了变化,因此笔者将其翻新后,重新发布到pypi上,便于直接安装调用

安装&使用指南🎯

  1. 安装

使用pip install sengbao_mle命令进行安装

  1. 使用
  • 导入框架
import sengbao_mle as m
  • 使用实例1——原库示例(翻新后)
import numpy as np
import sengbao_mle as m

# Define model
x = m.var('x', observed=True, vector=True)
y = m.var('y', observed=True, vector=True)

a = m.var('a')
b = m.var('b')
sigma = m.var('sigma')

model = m.Normal(y, a * x + b, sigma)

# Generate data
xs = np.linspace(0, 2, 20)
ys = 0.5 * xs + 0.3 + np.random.normal(0, 0.1, 20)

# Fit model to data
result = model.fit({'x': xs, 'y': ys}, {'a': 1, 'b': 1, 'sigma': 1})
print(result)
  • 使用实例2——自定义分布
import numpy as np
import sengbao_mle as m
import theano.tensor as T

# 以Subbotin分布为例
class Subbotin(m.Model):
  def __init__(self,x,af,*args,**kwargs):
      # 自定义的分布类型,均需继承自m.Model
      super(Subbotin, self).__init__(*args,**kwargs)
      # 运算都需要采用theano.tensor中的运算函数
      mm=T.mean(x)
      xg=T.mean(T.abs_(x-mm)**af)**(1/af)
      # 写入该分布的对数概率密度函数
      self._logp=m.distributions.bound(T.log(
          T.exp(-((T.abs_((x-mm)/xg))**af)/af)/(2*xg*(af**(1/af))*T.gamma(1+1/af))
      ))
      # 添加样本点
      self._add_expr('x',x)
      # 添加要拟合的参数
      self._add_expr('af',af)

# 样本点
x = m.var('x', observed=True, vector=True)
# 目标参数
af=m.var('af')
model=Subbotin(x,af)

xs=np.random.normal(-3,3,(1000,))
# 给目标参数设置初始值
result = model.fit({'x': xs}, {'af': 1})
print(result)
  • 使用实例3——遍历初始值
# 由于目标参数的初始值的设定,对能否拟合成功有着较大影响,因此在拟合失败时,可以考虑便利初始值,以寻找能拟合成功的初始参数
import numpy as np
import sengbao_mle as m
import theano.tensor as T

# 以Subbotin分布为例
class Subbotin(m.Model):
  def __init__(self,x,af,*args,**kwargs):
   	 # 自定义的分布类型,均需继承自m.Model
      super(Subbotin, self).__init__(*args,**kwargs)
      # 运算都需要采用theano.tensor中的运算函数
      mm=T.mean(x)
      xg=T.mean(T.abs_(x-mm)**af)**(1/af)
      # 写入该分布的对数概率密度函数
      self._logp=m.distributions.bound(T.log(
      T.exp(-((T.abs_((x-mm)/xg))**af)/af)/(2*xg*(af**(1/af))*T.gamma(1+1/af))
      ))
      # 添加样本点
      self._add_expr('x',x)
      # 添加要拟合的参数
      self._add_expr('af',af)

# 样本点
x = m.var('x', observed=True, vector=True)
# 目标参数
af=m.var('af')
model=Subbotin(x,af)

xs=np.random.normal(-3,3,(1000,))
# 遍历能拟合成功的初始参数值
rs=[]
for af in range(-100,100):
  	result = model.fit({'x': xs}, {'af': af})
		if result.success is True:
  			rs.append({af:result})
af=np.mean([i.values().x['af'] for i in rs])      	
print(af)

相关链接🔗

Metadata

Release files for sengbao-mle 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sengbao-mle 0.0.1
File Size Uploaded
sengbao_mle-0.0.1.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sengbao-mle 0.0.1
File Interpreter ABI Platform
sengbao_mle-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / sengbao_mle-0.0.1.tar.gz

Download URL sengbao_mle-0.0.1.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1ee5f5aac0ff5fd52bf1054d2d0b8bab4871622906133e80b85e97b6e8480cef
BLAKE2b-256 checksum
How to use checksums
ae38207531c36993ebc65c5b1e3547c66f95dfa20b7fda80901d91771844e57c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / sengbao_mle-0.0.1-py3-none-any.whl

Download URL sengbao_mle-0.0.1-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
faf5d34e5a4ff7c81201fe04617fa4c4b594955cbca960b1a289e0f01f631de2
BLAKE2b-256 checksum
How to use checksums
72faecc146c03790372789be5a3ec4e8e3347605c1d525d5af956a99b2e5189e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page