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

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

作者: 疯狂的向日葵 | 来源:发表于2017-03-07 15:22 被阅读1387次

    版权声明:本文为博主原创文章,未经博主允许不得转载。
    需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 <len; i++){
                        double yi = rp[i] - rf;
                        double xi = rm[i] - rf;
                        xexp += xi;
                        yexp += yi;
                        xy += xi * yi;
                        xsqura += Math.pow(xi,2);
                        ysqura += Math.pow(yi,2);
                    }
                    xexp /= len;
                    yexp /= len;
                    double lxy = xy - len * xexp * yexp;
                    double lxx = xsqura - len * Math.pow(xexp,2);
                    double lyy = ysqura - len * Math.pow(yexp,2);
                    output.beta = lxy / lxx;
                    output.alpha = yexp - output.beta * xexp;
                    output.rsquare = Math.pow(lxy,2)/(lxx * lyy);
                    return output;
                }else {
                    System.out.println("市场收益序列长度不匹配");
                }
            }else {
                System.out.println("收益输入为空");
            }
            return output;
        }
    
        //求詹森系数
        static double jensen(double eRp, double eRm, double rf, double beta){
            return eRp - rf - beta *(eRm - rf);
        }
    
        //求特雷诺系数
        static double treynorRatio(double exp, double rf, double beta){
            if (beta != 0){
                return (exp - rf)/beta;
            }else {
                System.out.println("系统风险beta为0");
                return -9999;
            }
        }
    
        //自回归分析 求系数 拟合值 噪声序列
        static Autoregressive autoRegression(double[] rp, int p){
            double[] op = {-9999};
            Autoregressive output = new Autoregressive(op,op,op);
            int len = rp.length;
            if (len < 3 || p < 1 || len <= p +1){
                System.out.println("输入有误");
            }else {
                int leny = len - p;
                double[][] y = new double[leny][1];
                for (int i = p; i <len; i ++){
                    y[i-p][0] = rp[i];
                }
                double[][] a = new double[leny][p];
                for (int i = 0; i < leny ; i ++){
                    for (int j = 0 ; j < p; j ++){
                        a[i][j] = rp[p -1 - j + i];
                    }
                }
                Matrix mY = new Matrix(y);
                Matrix mA = new Matrix(a);
                Matrix mR = (mA.transpose().times(mA)).inverse().times(mA.transpose()).times(mY);
                output.ratios = mR.getColumnPackedCopy();
                Matrix mYhat = mA.times(mR);
                output.estimates = mYhat.getColumnPackedCopy();
                Matrix mNs = mY.minus(mYhat);
                output.noises = mNs.getColumnPackedCopy();
            }
            return output;
        }
    }
    
    class Linear {
        double alpha;
        double beta;
        double rsquare;
    
        public Linear(double alpha, double beta, double rsquare){
            this.alpha = alpha;
            this.beta = beta;
            this.rsquare = rsquare;
        }
    }
    
    class Autoregressive {
        double[] ratios;
        double[] estimates;
        double[] noises;
    
        public Autoregressive(double[] ratios, double[] estimates, double[] noises){
            this.ratios = ratios;
            this.noises = noises;
            this.estimates = estimates;
        }
    }
    

    结果示例:

    =线性回归=
    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: 实现自回归分析/线性回归分析/基金各项指标计算等

          本文链接:https://www.haomeiwen.com/subject/lummgttx.html