Java: 实现自回归分析/线性回归分析/基金各项指标计算等

版权声明:本文为博主原创文章,未经博主允许不得转载。
需Jama矩阵运算库.

java版源码:
包含自回归分析/线性回归分析/基金各项指标计算

import Jama.Matrix;
public class test {
    public static void main(String[] args){

        double rf = 1.0;
        double[] rm = {1,2,3,4,3,2,5,6,8,8};
        double[] rp = {2,2,3,4,4,3,5,6,9,8};

        //线性回归
        Linear l = linearRegression(rp,rm,rf);
        System.out.println("线性回归");
        System.out.println("alpha: "+l.alpha+"\nbeta: "+l.beta+"\nr2: "+l.rsquare);

        //自回归
        Autoregressive a = autoRegression(rp,2);
        System.out.println("自回归");
        //参数
        for (double ci :a.ratios){
            System.out.println("ratio: "+ci);
        }
        //拟合值
        for (double ci :a.estimates){
            System.out.println("estimates: "+ci);
        }
        //噪声
        for (double ci :a.noises){
            System.out.println("noises: "+ci);
        }
        //噪声均值
        System.out.println("exp noises: "+exp(a.noises));
        //噪声方差
        System.out.println("dev noises: "+dev(a.noises));

    }

    //求均值
     static double exp(double[] rp){
        int len = rp.length;
        if (len > 0){
            double output = 0.0;
            for (double p: rp){
                output +=p;
            }
            output /= len;
            return output;
        }else {
            return -9999;
        }
    }

    //求标准差
    static double dev(double[] rp){
        int len = rp.length;
        if (len > 0){
            double output = 0.0;
            double exp = exp(rp);
            for (double p: rp){
                output += Math.pow((p -exp),2);
            }
            output = Math.sqrt(output/(len -1));
            return output;
        }else {
            return -9999;
        }
    }

    //求下行风险标准差
    static double downRisk(double[] rp, double rf){
        int len = rp.length;
        if (len > 0){
            double output = 0.0;
            int count = 0;
            for (double p: rp){
                if (p < rf){
                    count ++;
                    output += Math.pow((p - rf),2);
                }
            }
            if (count > 1){
                output = Math.sqrt(output/(count -1));
                return output;
            }else {
                System.out.println("益率小于无风险利率的天数刚好为1");
                return -9999;
            }
        }else {
            return -9999;
        }
    }

    //求索提诺比率
    static double sortinoRatio(double exp, double rf, double dr){
        if (dr != 0){
            return (exp - rf)/dr;
        }else {
            System.out.println("下行风险标准差有误");
            return -9999;
        }
    }

    //求夏普比率
    static double sharpRatio(double exp, double rf, double dp){
        if (dp != 0){
            return (exp - rf)/dp;
        }else {
            System.out.println("标准差为0");
            return -9999;
        }
    }

    //求线性回归 alpha beta R2
    static Linear linearRegression(double[] rp,double[] rm,double rf){
        Linear output = new Linear(-9999,-9999,-9999);
        int len = rp.length;
        int lenrm = rm.length;
        if (len > 0){
            if (len == lenrm){
                double xexp = 0.0;
                double yexp = 0.0;
                double xsqura = 0.0;
                double ysqura = 0.0;
                double xy = 0.0;
                for (int i = 0; i 

结果示例:

=线性回归=
alpha: 0.5611510791366894
beta: 0.9496402877697845
r2: 0.9568894502718442

=自回归=
参数
ratio: 0.5716829919857536
ratio: 0.7043633125556548
估计值
estimates: 2.5520926090828167
estimates: 3.12377560106857
estimates: 4.399821905609978
estimates: 5.104185218165633
estimates: 4.532502226179879
estimates: 4.971504897595732
estimates: 6.951914514692795
estimates: 9.37132680320571
噪声
noises: 0.44790739091718335
noises: 0.8762243989314298
noises: -0.3998219056099783
noises: -2.1041852181656333
noises: 0.4674977738201207
noises: 1.0284951024042677
noises: 2.0480854853072046
noises: -1.3713268032057098
噪声均值
exp noises: 0.12410952804986058
噪声方差
dev noises: 1.3514101374682685

你可能感兴趣的:(Java: 实现自回归分析/线性回归分析/基金各项指标计算等)