在循环中运行一个方法以从数组列表中获取 Max、Min

背景:方法名称是computeStatsMean()。它取初始值 100,并通过从正态分布中随机抽样平均每周利润来添加每周利润。它需要计算 100 的值在 3582 周内会变成多少。我必须对这种方法进行 1000 次试验,以了解正态分布的每周平均利润值的不同值如何使我们获得不同的利润值。我必须将 1000 股股票收益(利润值或“新值”)存储在 ArrayList 中,并从中获取最大值、最小值、中位数。


问题:我能够运行方法并进行采样等等。但是我无法运行它进行 1000 次试验。我尝试在 main 中调用该方法 1000 次(1000 次 for 循环),但它用 ArrayList 中的新值替换了旧值。


代码:


class Sample extends SimulateMarket{ 


public ArrayList<Double> data = new ArrayList<Double>();

public ArrayList<Double> myData = new ArrayList<Double>();

private double mean, stdDev; 

private Random random;

private SimulateMarket mySim = new SimulateMarket(); //mySim is an instance of SimulateMarket class used to access variables of Simulate Class


//Constructor 

public Sample(){ //Constructor used for the Distributional Technique

    random = new Random();

    int size = 3582;

    for(int i=0; i<size ; i++) 

        data.add(0.0016+random.nextGaussian()*0.0205);      //Random sampling from Normal Distribution 

    System.out.println(this); //for printing the results

}


//Getters (Mean, Standard Deviation, Median)

public double getMean(){

    double sum=0;

    for(int i=0; i<mySim.WEEK; i++)

        sum += data.get(i);

    return sum/data.size();

}


public void computeStatsMean(){

    double equity = 100;

    for(int i=0; i<100; i++){ 

        equity = equity*(1+getMean());

    }

    myData.add(equity);

    System.out.println("mean : " + getMean());


    for(Double num: myData){

        System.out.println("with mean:" + num);

    }


}


class SimulateMarket{

    public static void main(String[] args){

        for(int i=0; i<1000; i++){ 

        Sample equity = new Sample();

        equity.computeStatsMean(); 


    }

}


}


繁星淼淼
浏览 204回答 2
2回答

呼如林

您Sample每次都在for-loopin中创建新实例main()。Sample equity = new Sample();因此,在调用equity.computeStatsMean();旧值时会丢失。尝试如下:Sample equity;public static void main(String[] args) {&nbsp; &nbsp; equity = new Sample();&nbsp; &nbsp; for(int i = 0; i < 1000; i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; equity.computeStatsMean();&nbsp;&nbsp; &nbsp; }}在computeStatsMean()变化:equity = equity*(1+getMean());到equity = equity*(i+getMean());&nbsp; // use i instead of 1在 中computeStatsMean(),计数器值从 1 而不是 0 开始。(希望你能找出原因!)

阿波罗的战车

我将 computeStatsMean()重命名为SimulateMarket 类中的Distributional方法,并使用 Sample 类来保存值。它现在正在工作。class SimulateMarket{ //Application Class (it contains the Main Method)//public instance variablespublic double meanWeekly = 0.0016;&nbsp; //Average weekly returnpublic double median = 0.0029; //the middle value is known as Median&nbsp;public final int WEEK = 3582;&nbsp; //Data calculate over the course of 3582 weeks&nbsp;public final int WEEK_YEAR = 52; //The number can never changes; hence, "final."&nbsp;//private instance variablesprivate double meanYearly = meanWeekly * WEEK_YEAR; //Formula for average yearly returnprivate double stdDev = 0.0205;public double Distributional(){&nbsp; &nbsp; Sample mySampleA = new Sample();&nbsp; &nbsp; double equity = 100;&nbsp; &nbsp; for(int i=0; i<3582; i++){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; equity = equity*(1+mySampleA.getMean());&nbsp; &nbsp; }&nbsp; &nbsp; return equity;}public static void main(String[] args){&nbsp; &nbsp; Sample b = new Sample();&nbsp; &nbsp; b.computeStats();}}class Sample{public void computeStats(){&nbsp; &nbsp; for(int i=0; i<4; i++){&nbsp; &nbsp; &nbsp; &nbsp; myDataA.add(super.Distributional());&nbsp; &nbsp; }&nbsp; &nbsp; for(Double num: myDataA){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(num);&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(toStringA());}public String toStringA(){&nbsp; &nbsp; return String.format("\nDistrubution: size = %d, mean = %.2f, stdDev = %.2f, min = %.2f, max = %.2f, median = %.2f", myDataA.size(), getMeanA(), getStdDevA(mean), Collections.min(myDataA), Collections.max(myDataA), getMedianA());&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java