Processing math: 100%

Java 初体验: 线性回归系数的检验

翻阅了 官方 tutorial.

与 Python 比较

  • new 相当于在 Python 中显式地调用 __init__ 方法.
  • import 好像没有 Python 灵活, 比如 a.b.c, 只能导入 fully qualified name (一个具体的类) 或者一个库下所有的类, 而不能像 Python 一样 from a import b 然后再用 b.c.
  • 没有默认参数 (可以用函数重载实现), 没有位置参数 (传参感觉不方便读 raw text?).
  • 方法命名习惯用动词开头, camelCase.
  • “only public methods are allowed to be called on class instance.” Why subclass in another package cannot access a protected method?

线性回归系数检验

Apache Commons Math 库的线性回归类没有直接实现各种假设检验. 考虑线性回归 (满足一般假设)

y=Xβ+ε,εN(0,σ2I).

其中 Xn×p 设计矩阵 (第一列全为 1), βp×1 参数向量. 检验估计参数 ˆβ 的每一项,

H0:β1==βp1=0,

其中 β0 为截距. 由于

ˆβNp(β,σ2(XX)1),

如果 βi=0 成立, 则 t-统计量

τi=ˆβici,iuu/(np)tnp,

其中 ci,i 表示 (XX)1 的第 i 行第 i 列, 残差 u=yXˆβ (右下角这一坨是 ˆσ), 符号 tnp 表示自由度为 np 的 t 分布. 最后 2P(Z>|τi|) 则为第 i 项对应的 p-value, 其中随机变量 Ztnp.

实现见 这里, 其中

  • TDistribution 构造函数中显式传入 rng=null 避免创建随机数生成器;
  • 用了 Apache 的 FastMath: Faster, more accurate, portable alternative to Math and StrictMath for large scale computation.;
  • 用函数重载实现了类似默认参数的效果, 避免重复计算 beta;
  • Javadoc 的写法参考了 Apache Commons Math 官方文档风格.

其他

Python 可以用 JPype 调 jar 包, 但是一个 已知缺陷

Because of lack of JVM support, you cannot shutdown the JVM and then restart it. Nor can you start more than one copy of the JVM.

Gitalking ...