java拟合_如何在java中拟合正态分布

前言

最近在工作中需要拟合高斯曲线,在python中可以使用 scipy,相关代码如下:

#!/usr/bin/env python

# -*- coding=utf-8 -*-

%matplotlib inline

import numpy as np

import pylab as plt

from scipy.optimize import curve_fit

x = range(10)

y = [25,68,144,220,335,199,52,14,5,2]

def gaussian2(x,*param):

return param[0]*np.exp(-np.power(x - param[1], 2.) / (2 * np.power(param[2], 2.)))

def use_fit_gaussian2():

try:

popt,pcov = curve_fit(gaussian2,x,y,p0=[1,1,1])

except Exception,e:

print e

return

print popt

yhat = gaussian2(x, *popt) # or [p(z) for z in x]

ybar = np.sum(y)/len(y) # or sum(y)/len(y)

ssreg = np.sum((yhat-ybar)**2) # or sum([ (yihat - ybar)**2 for yihat in yhat])

sstot = np.sum((y - ybar)**2) # or sum([ (yi - ybar)**2 for yi in y])

error = ssreg / sstot

print ssreg, sstot, error

plt.plot(x,y,'b+:',label='data', linewidth =3)

plt.plot(x,gaussian2(x,*popt),'ro:',label='fit', linewidth =3)

plt.legend()

plt.show()

use_fit_gaussian2()

生成的结果如下图所示:

16b177884b700ac41fc206838b603369.png

java ?

由于线上用的java,所以需要使用java实现,需要使用到 apache 的 commons-math3 jar包

org.apache.commons

commons-math3

3.6.1

代码

import org.apache.commons.math3.fitting.GaussianCurveFitter;

import org.apache.commons.math3.fitting.WeightedObservedPoints;

/**

* Created by xingxing.dxx on 2016/11/14.

*/

public class CurveFittingTest {

public static void main(String[] args) {

WeightedObservedPoints obs = new WeightedObservedPoints();

obs.add(0, 25);

obs.add(1, 68);

obs.add(2, 144);

obs.add(3, 220);

obs.add(4, 335);

obs.add(5, 199);

obs.add(6, 52);

obs.add(7, 14);

obs.add(8, 5);

obs.add(9, 2);

double[] parameters = GaussianCurveFitter.create().fit(obs.toList());

for (double i : parameters) {

System.out.println(i);

}

}

}

最开始测试的时候非常完美,可是马上就悲剧了,在我运行如下case的时候,抛了一个错

[0, 0, 0, 0, 4, 1, 0, 2, 0]

Exception in thread "main" org.apache.commons.math3.exception.ConvergenceException: illegal state: unable to perform Q.R decomposition on the 9x3 jacobian matrix

瞬间懵逼了,有没有。然后开始找错误是哪报的,发现了报错的代码,

if (Double.isInfinite(norm2) || Double.isNaN(norm2)) {

throw new ConvergenceException(LocalizedFormats.UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN,

nR, nC);

}

看着代码貌似是矩阵求逆的时候,矩阵中有1.0/0.0的情况,猜测是不是输入的数据都大于0就ok了,果然,下面的测试样本就ok了

[0.01, 0.01, 0.01, 0.01, 4.01, 1.01, 0.01, 2.01, 0.01]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值