【问题标题】:Java:Variance Issue in SimulationJava:模拟中的差异问题
【发布时间】:2016-06-18 12:16:46
【问题描述】:

我最近写了一个小模拟,找不到方差,我的课程如下:

    class aQuarter {
    public int ordered; 
    public int leftOver;
    public int profit;
    public int profit2;
    public double variance;
    public int demand;
    public int demandTotal;
    public int counter;
    public int sold;
    public int averageProfit;
    public double standardDev;
    public int soldTotal;
    public int orderedTotal;



    public int getCounter() {
        return counter;
    }
    public void incrementCounter() { //increments the counter for every week
        counter++;
    }
    public int getDemand() {
        return demand;
    }
    public void setDemand(int demand) { //sets the demand and adds that demand to the total demand for stat keeping
        this.demand = demand;
        demandTotal+=demand;
    }
    public int getDemandAverage() {
        return demandTotal/counter; //returns the average demand
    }
    public int getProfitAverage() {
        return profit/counter;     //returns the average profit
    }

    public int getOrderedAverage() { //returns how many ordered on average for that quarter
        return orderedTotal/counter;
    }
    public void setOrdered(int ordered) { //sets how much was ordered
        this.ordered = ordered;
        orderedTotal+=ordered;
    }
    public int getleftOver() { //testing purposes
        return leftOver;
    }
    public void setleftOver() { //sets how many gallons we have left over to deduct from the profit made
        int x=ordered-demand; 
        if (x<0) {              //if the demand was more than we ordered, none leftover
            leftOver=0;
        }
        else
            leftOver=x;         //if we ordered more than was demanded, leftover = ordered-demand

    }
    public int getSoldAverage() {
        return soldTotal/counter; //returns how many was sold average
    }
    public void setSold() {
        int x = ordered-demand;
        if (x<=0) {             //sets how many we sold
            sold=ordered;       //if the demand was more than we ordered, we sold how much was demanded
            soldTotal+=sold;    //add that total to the soldTotal accumulator
        }
        else {
            sold=demand;        //else, we ordered more than was demanded, making us sell how much was demanded
            soldTotal+=sold;    //add that total to the soldTotal accumulator
        }

    }
    public int getProfit() {
        return profit;
    }
    public void setProfit() {               //sets the profit made for the week
        int baseProfit=sold*10 - leftOver*7;  
        profit+= sold*10 - leftOver*7;      //For every gallon we sold, we made 10 dollars,but for every gallon that
        profit2+=(baseProfit*baseProfit);   //was leftover, we lost 7 dollars
    }                                       //add the profit squared to profit2

    public double getVariance() {           //gets the variance of the simulation for the quarter
        averageProfit=profit/counter;
        variance=profit2/counter -averageProfit*averageProfit;
        return variance;
    }
    public double getStandardDev() {        //gets the standard deviation for the quarter
        standardDev=Math.sqrt(variance);
        return standardDev;
    }

}

以及在“for”时间循环中对类的调用。

            firstQuarter.incrementCounter(); //add 1 to the counter
            firstQuarter.setOrdered(50+gallonsDelivered()); //set how many gallons ordered for quarter+ randomness
            firstQuarter.setDemand(gallonsleftOver1()); //sets demand using the demands for the first quarter
            firstQuarter.setSold(); //sets sold
            firstQuarter.setleftOver(); //sets how many leftover to deduct from the profit made
            firstQuarter.setProfit(); //sets the profit

一切都按预期打印出来,除了这个世界数字给我的方差。

For Quarter 1:
 Average Profit Per Week: 444
 Average Demand Per Week: 54
 Average Ordered Per Week: 51
 Average Sold Per Week:47
 Average Variance of Profit: 8915.0
 Standard Deviation: 94.41927769264072

我按照程序在过去的作业中完成了差异,所以我不确定我在哪里出错了,希望另一双眼睛可以得出一些答案。感谢您的宝贵时间!

【问题讨论】:

  • 为什么你认为方差是错误的?它有什么问题?

标签: java class object simulation variance


【解决方案1】:

您在计算方差时使用的所有变量都是int 变量!这就是问题所在!因此,公式中的除法无法按预期运行。分配给double 变量并不意味着将发生的计算将是浮点计算。计算作为整数计算执行,然后隐式转换为double

【讨论】:

  • 我将班级中的所有变量都更改为双倍,但仍然遇到同样的问题
  • 真的吗?那么你在这里给出的场景的正确值是多少?
  • 大概在 20 年代左右
  • 哦,我明白了。好吧,从编程的角度来看,除了变量问题,我看不到任何其他错误。你确定计算方差的方式是正确的吗?对我来说看起来不错,但我的统计知识有点土气..
猜你喜欢
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
相关资源
最近更新 更多