0

有人可以帮我找到以下代码中的错误。我收到一个 Java 异常,但我不知道它为什么会发生。感谢您的关注!异常描述如下:

>    Exception in thread "main" java.lang.ExceptionInInitializerError
>    Caused by: java.lang.ArrayIndexOutOfBoundsException
>       at java.lang.System.arraycopy(Native Method)
>       at org.ejml.data.DenseMatrix64F.set(Unknown Source)
>       at org.ejml.data.DenseMatrix64F.<init>(Unknown Source)
>       at org.ejml.simple.SimpleMatrix.<init>(Unknown Source)
>       at test1.<clinit>(test1.java:14)

对于此代码:

    import org.ejml.simple.SimpleMatrix;
    import java.lang.Math;

    import java.util.Scanner;

    public class test1 {
        class testcase3 {
            public testcase3() {
                Mkt = 100000;
                E1name = new SimpleMatrix(2, 5, true, -10, -5, 0, 5, 10, 1, 2, 3,
                        4, 5);
                E1name2 = new SimpleMatrix(2, 5, true, 1, 2, 3, 4, 5, -10, -5, 0,
                        5, 10);
                E2name = new SimpleMatrix(2, 2, true, 1, 3, 1, 2);
                E2name2 = new SimpleMatrix(2, 2, true, 1, 2, 1, 3);
                EM1 = new SimpleMatrix(2, 5, true, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
                EM2 = new SimpleMatrix(2, 2, true, 0, 0, 0, 0);
                T = new SimpleMatrix(2, 2, true, 0, 0, 0, 0);
                Pa = new SimpleMatrix(1, 2, true, 0, 0);
                Position = 0;
            }

            public double Mkt;
            public SimpleMatrix E1name;
            public SimpleMatrix E2name;
            public SimpleMatrix E1name2;
            public SimpleMatrix E2name2;
            public SimpleMatrix EM1;
            public SimpleMatrix EM2;
            public SimpleMatrix T;
            public SimpleMatrix Pa;
            public int Position;

            public int newBidAsk(double bidPrice, double askPrice) {
                // log("I received a new Top-of-Book update, bid=" + bidPrice +
                // ", offer=" + askPrice);

                // data transformation
                double newMkt = (bidPrice + askPrice) / 2;
                double Grow = newMkt - Mkt;
                double Spread = (askPrice - bidPrice) / 2;

                SimpleMatrix seq = new SimpleMatrix(1, 2, true, Grow, Spread);
                SimpleMatrix z = new SimpleMatrix(1, 2, true, 0, 0);

                z.set(0, 0, Nametrans(seq.getIndex(0, 0), E1name));
                z.set(0, 1, Nametrans(seq.getIndex(0, 1), E2name));
                int act = 0;

                // parameter updating
                SimpleMatrix Px = Hmmupdate(Pa, z, EM1, EM2);
                SimpleMatrix newPa = Hmmpredict(Px, T);
                SimpleMatrix E1n = Hmmempredict(newPa, EM1, EM2, 5);
                SimpleMatrix E2n = Hmmempredict(newPa, EM1, EM2, 2);
                SimpleMatrix E = Eab(newMkt, E1n, E2n, E1name2, E2name2);
                int E1 = E.getIndex(0, 0);
                int E2 = E.getIndex(0, 1);

                // action
                if (Position == 0) {
                    int L = (int) Math.max(0,
                            Math.max(E1 - askPrice, bidPrice - E2));
                    switch (L) {
                    case 1:
                        act = 0;
                        break;
                    case 2:
                        act = 1;
                        break;
                    case 3:
                        act = -1;
                        break;
                    }
                } else if (Position == -1) {
                    int L = (int) Math.max(-askPrice, -E1);
                    switch (L) {
                    case 1:
                        act = 1;
                        break;
                    case 2:
                        act = 0;
                        break;
                    }
                } else if (Position == 1) {
                    int L = (int) Math.max(bidPrice, E2);
                    switch (L) {
                    case 1:
                        act = -1;
                        break;
                    case 2:
                        act = 0;
                        break;
                    }
                }

                Position = act + Position;
                // log("The act is " + Position);
                return Position;
            }

            public SimpleMatrix Eab(double Mkt, SimpleMatrix E1, SimpleMatrix E2,
                    SimpleMatrix E1name2, SimpleMatrix E2name2) {
                SimpleMatrix Easkbid = new SimpleMatrix(1, 2, true, Mkt, Mkt);
                for (int i = 0; i < 5; i++) {
                    for (int j = 0; j < 2; j++) {
                        Easkbid.set(0, 0,
                                Easkbid.getIndex(0, 0) + (Nametrans(i, E1name2))
                                        * E1.getIndex(0, i) * E2.getIndex(0, j));
                        Easkbid.set(0, 1,
                                Easkbid.getIndex(0, 1) + (Nametrans(i, E2name2))
                                        * E1.getIndex(0, i) * E2.getIndex(0, j));
                    }
                }
                return Easkbid;
            }

            public SimpleMatrix Hmmempredict(SimpleMatrix Pxkla, SimpleMatrix EM1,
                    SimpleMatrix EM2, int length) {
                SimpleMatrix E = new SimpleMatrix(1, length, true);
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < length; j++) {
                        E.set(1,
                                j,
                                E.getIndex(1, j) + EM2.getIndex(i, j)
                                        * Pxkla.getIndex(1, i));
                    }
                }
                E = normalize(E);
                return E;
            }

            public SimpleMatrix Hmmpredict(SimpleMatrix Pxk, SimpleMatrix T) {
                SimpleMatrix Pxkla = new SimpleMatrix(1, 2, true, 0, 0);
                for (int i = 0; i < 2; i++) {
                    for (int j = 0; j < 2; j++) {
                        Pxkla.set(0, i, Pxkla.getIndex(0, i) + Pxkla.getIndex(0, j)
                                * T.getIndex(j, i));
                    }
                }
                Pxkla = normalize(Pxkla);
                return Pxkla;
            }

            public SimpleMatrix Hmmupdate(SimpleMatrix Pxka, SimpleMatrix zk,
                    SimpleMatrix EM1, SimpleMatrix EM2) {
                SimpleMatrix Pxk = new SimpleMatrix(1, 2, true, 0, 0);
                for (int i = 0; i < 2; i++) {
                    Pxk.set(0,
                            i,
                            (Pxka.getIndex(0, i)
                                    * EM1.getIndex(i, zk.getIndex(0, 0)) * EM2
                                    .getIndex(i, zk.getIndex(0, 1))));
                }
                Pxk = normalize(Pxk);
                return Pxk;
            }

            public SimpleMatrix normalize(SimpleMatrix B) {
                double temp = 0;
                for (int i = 0; i < B.numCols(); i++) {
                    temp = B.getIndex(0, i);
                }
                SimpleMatrix A = B;
                for (int j = 0; j < A.numCols(); j++) {
                    A.set(0, j, B.getIndex(0, j) / temp);
                }
                return A;
            }

            public double Nametrans(double a, SimpleMatrix Nama) {
                int n = Nama.numCols();
                double b = 0;
                for (int i = 0; i < n; i++) {
                    if (a == Nama.getIndex(0, i)) {
                        b = Nama.getIndex(1, i);
                    }
                }
                return b;
            }
        }

        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            double bid = s.nextDouble();
            double ask = s.nextDouble();
            testcase3 tc3;
            int act = tc3.newBidAsk(bid, ask);
            System.out.printf("%d", act);

        }
    }
4

1 回答 1

0

堆栈跟踪很棒,它们会告诉您发生错误的那一行 - 或之后的那一行。关于您的代码,我唯一能看到的可能是负数;尝试在没有它们的情况下制作矩阵,看看是否可以清除错误。如果是这样,您可以尝试在那里传递真正的双精度数,而不是必须由运行时转换的整数。

而且,如果确实如此,库作者应该在他们的 javadoc 中提到它,或者您可能在他们的代码中发现了一个(可怕的!)主要错误。

于 2013-04-03T02:05:24.073 回答