logistical regression

package com.gridnt.offline;

public class offcal4 {

    static final double E = 2.718281828;
    private final int M = 6;
    private final int N = 4;

    double X[][] = new double[][] {
            { 1, 47, 76, 24 }, // include x0=1
            { 1, 46, 77, 23 }, { 1, 48, 74, 22 }, { 1, 34, 76, 21 }, { 1, 35, 75, 24 },
            { 1, 34, 77, 25 }, };

    double Y[] = { 1, 1, 1, 0, 0, 0 };
    double theta[] = { 1, 1, 1, 1 }; // include theta0
    double h[] = new double[M];

    double sigmodFun(double x) {
        double ex = Math.pow(2.718281828, x);
        return ex / (1 + ex);
    }

    public void calclikelihood_error() {
        double likelihood = 0;
        // 需重新根据theta计算,不要根据h算
        for (int i = 0; i < M; i++) {
            likelihood += Y[i] * Math.log(h[i]) + (1 - Y[i]) * Math.log(1 - h[i]);
        }
        System.err.println(likelihood);
    }

    public void calclikelihood() {
        double likelihood = 0;
        for (int i = 0; i < M; i++) {
            double sum = 0;
            for (int j = 0; j < N; j++) {
                sum += X[i][j] * theta[j];
            }
            h[i] = sigmodFun(sum);
            likelihood += Y[i] * Math.log(sigmodFun(sum)) + (1 - Y[i])
                    * Math.log(1 - sigmodFun(sum));
        }
        System.err.println(likelihood);
    }

    public void calc() {
        for (int t = 0; t < 1000; t++) {
            int i = t % 6;
            // for (int i = 0; i < M; i++) {
            double sum = 0; // 不能定义为Int
            for (int j = 0; j < N; j++) {
                sum += X[i][j] * theta[j];
            }
            h[i] = sigmodFun(sum);
            for (int j = 0; j < N; j++) {
                theta[j] += 0.001 * X[i][j] * (Y[i] - h[i]); // 不是(Y[j]-h[i])
            }
            // }
            calclikelihood();
        }
    }

    public static void main(String[] args) {
        offcal4 of = new offcal4();
        // for (int i = 0; i < 1000; i++) {
        of.calc();
        // }
        System.err.println(of);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值