2 回答
TA贡献1798条经验 获得超3个赞
您Sample每次都在for-loopin中创建新实例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
在 中computeStatsMean(),计数器值从 1 而不是 0 开始。(希望你能找出原因!)
TA贡献1862条经验 获得超6个赞
我将 computeStatsMean()重命名为SimulateMarket 类中的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());
}
}
添加回答
举报