背景:方法名称为computeStatsMean()。它采用100的初始值,并通过从正态分布中随机抽样平均每周利润为其增加每周利润。它需要计算3582周内100的价值会变成多少。我必须对该方法进行1000次试验,以查看正态分布的平均每周利润值的不同值如何使我们获得不同的利润值。我必须将1000个股本回报率(利润值或“新值”)存储在ArrayList中,并从中获取最大,最小和中位数。
问题:我能够运行method并进行采样和全部操作。但是我无法进行1000次试用。我尝试在主要方法中调用该方法1000次(循环执行1000次),但是它用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();
}
}
}
答案 0 :(得分:1)
您每次在Sample
的{{1}}内部创建for-loop
的新实例。
main()
调用Sample equity = new Sample();
旧值时将丢失。
尝试如下:
equity.computeStatsMean();
在Sample equity;
public static void main(String[] args) {
equity = new Sample();
for(int i = 0; i < 1000; i++) {
equity.computeStatsMean();
}
}
中进行更改:
computeStatsMean()
到
equity = equity*(1+getMean());
在equity = equity*(i+getMean()); // use i instead of 1
中,从1而不是0开始计数。(希望您能找出原因!)
答案 1 :(得分:0)
我在SimulateMarket类中放置了computeStatsMean()重命名为Distributional 方法,并使用Sample类保存值。现在正在工作。
class SimulateMarket{ //Application Class (it contains the Main Method)
//public instance variables
public double meanWeekly = 0.0016; //Average weekly return
public double median = 0.0029; //the middle value is known as Median
public final int WEEK = 3582; //Data calculate over the course of 3582 weeks
public final int WEEK_YEAR = 52; //The number can never changes; hence, "final."
//private instance variables
private double meanYearly = meanWeekly * WEEK_YEAR; //Formula for average yearly return
private double stdDev = 0.0205;
public double Distributional(){
Sample mySampleA = new Sample();
double equity = 100;
for(int i=0; i<3582; i++){
equity = equity*(1+mySampleA.getMean());
}
return equity;
}
public static void main(String[] args){
Sample b = new Sample();
b.computeStats();
}
}
class Sample{
public void computeStats(){
for(int i=0; i<4; i++){
myDataA.add(super.Distributional());
}
for(Double num: myDataA){
System.out.println(num);
}
System.out.println(toStringA());
}
public String toStringA(){
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());
}
}