VBA:生成模拟特定参数的数据(Avg,StdDev..etc)

时间:2011-01-10 14:20:57

标签: excel vba excel-2007

我在这里修改了一个给我的VBA数组函数:Excel Generate Normalized Data

这个问题将解释我的目标。


下载我正在使用的excel以完全理解:

http://www.mediafire.com/?smq5tl9poitdacc


我正在使用以下数据(左侧是我为数据输入的值,右侧是生成数据的结果):

alt text

正如您所看到的,%Diff对Avg Click非常有用,但是当Day StdDev(Day +/-)很高时,Click / Time会关闭。 Day Stddev较低时的差异接近0。

我认为这是因为var NoClickDaysPerClick_Running_Avg 变得不准确,因为 NoClickDays_Total (间接用于确定正在运行的平均值)在开始时被“猜到”,并且需要重新评估每次点击,因为高StdDev会增加随机性,而原始的“猜测”会变得越来越不准确。

我不确定这是不是问题,或者我是否能解决它。

我只是在寻找最好的方法来做我想要的建议。我不确定为什么stdDev也离这么远,但这不是什么大问题。我宁愿拥有比其他任何东西更准确的点击/时间 - 无论Day StdDev是什么。


这是我的VBA中的功能:

Function ClickSpacer(Total_Days As Long, ClicksPerDay_Desired_Avg As Double, Clicks_Desired_Deviation As Double, Clicks_Min As Integer, Clicks_Max As Integer, TotalClicksOverTotalDays_Desired_Avg As Double, NoClickDays_Desired_Deviation As Double, NoClickDays_Min As Integer, NoClickDays_Max As Integer)


    Dim Day_Array() As Integer
    ReDim Day_Array(1 To Total_Days, 1 To 1)

    Dim NumDaysToGetClicks As Double
    Dim ClickOffset As Long

    Dim Clicks_Total As Long
    Dim Clicks_SoFar As Long
    Dim Clicks_Remaining As Long


    Dim NoClickDaysPerClick_Desired_Avg As Double





    ' Number of clicks that are needed to Achieved desired Avg of clicks over time
    Clicks_Total = Round(Total_Days * TotalClicksOverTotalDays_Desired_Avg, 0)

    ' Number of days in which the user has to click atleast once to achieve desired Avg. clicks per day
    NumDaysToGetClicks = Round(Clicks_Total / ClicksPerDay_Desired_Avg, 0)

    ' The number of non-click days in order fill out the total days
    NoClickDays_Total = Round(Total_Days - NumDaysToGetClicks, 0)





    ' The guessimated average of non-click days per click to fill out total non-click days
    ' This is never used, just used for comparsion of the running Avg
    NoClickDaysPerClick_Desired_Avg = NoClickDays_Total / NumDaysToGetClicks





    'This variable is here to achieved closer results to the desired StdDev.
    'a higher multiplyer will not limit the deviation but just give an average deviation
    'For example, if the Average was 3 with a +/- 2, then with a StdDevMulti of 1
    'ALL numbers will be 1 (3-2) through 5  (3+2) with an avg of 3 and stddev of 2, the numbers will NEVER exceed the StdDev.
    'With a StdDevMulti of 2, the numbers will be 0 through 6, but should still have an
    'Avg deviation of 2.
    StdDevMulti = 1

    NoClickDays_Desired_Deviation = NoClickDays_Desired_Deviation * StdDevMulti
    Clicks_Desired_Deviation = Clicks_Desired_Deviation * StdDevMulti


    'Set the obvious defaults
    ClickedDaysSoFar = 0
    Clicks_SoFar = 0
    NoClickDays_SoFar = 0

    'Give the ClickOffset a starting value
    ClickOffset = NoClickDaysPerClick_Desired_Avg

    Do

        'used to find the "running" average of days not clicked
        NoClickDays_Remaining = NoClickDays_Total - NoClickDays_SoFar

        'used to find the "running" average of clicks per day
        Clicks_Remaining = (Clicks_Total - Clicks_SoFar)

        'used in both "running" averages mentioned above and also will
        'mark the end of the while loop.
        RemainingClickedDays = (NumDaysToGetClicks - ClickedDaysSoFar)


        ' Find what the average num. click should be based on the remaining
        ' and then apply the deviation. Only accept a click below its max
        ' above its min.
        Do

            ' Generate a random number between -1 and 1
            SignChanger = Rnd() - Rnd()

            ' Apply the randomized StdDev
            Clicks_Deviation = Clicks_Desired_Deviation * SignChanger

            'Figure out the "running" average
            ClicksPerDay_Running_Avg = Clicks_Remaining / RemainingClickedDays

            'Figure out a click value and round to the nearest whole number
            Generated_Clicks = Round(ClicksPerDay_Running_Avg + Clicks_Deviation, 0)

        ' Make sure it meets the requirements, if not, try again
        Loop While Generated_Clicks < Clicks_Min Or Generated_Clicks > Clicks_Max


        ' Set the click value to the spaced-out array index
        Day_Array(ClickOffset, 1) = Generated_Clicks


         'Find a random space based upon the "running" avg. and desired deviation
         'Make sure it between the min and max required.
         Do
             ' Generate a random number between -1 and 1
            SignChanger = Rnd() - Rnd()

            ' Apply the randomized StdDev
            NoClickDays_Deviation = NoClickDays_Desired_Deviation * SignChanger


            'Figure out the "running" average
            NoClickDaysPerClick_Running_Avg = NoClickDays_Remaining / RemainingClickedDays

            'Figure out a space value and round to the nearest whole number
            Generated_NoClickDays = Round(NoClickDaysPerClick_Running_Avg + NoClickDays_Deviation, 0)

        ' Make sure it meets the requirements, if not, try again
        Loop While Generated_NoClickDays < NoClickDays_Min Or Generated_NoClickDays >= NoClickDays_Max



        'Define the array index based upon the spacing previously generated.
        ' Make sure to "add" upon the already known index. Add 1 because you
        'have to account for the index the click occupies
        ClickOffset = ClickOffset + Generated_NoClickDays + 1




        'These should be self-explaintory
        ClickedDaysSoFar = ClickedDaysSoFar + 1
        Clicks_SoFar = Clicks_SoFar + Generated_Clicks
        NoClickDays_SoFar = NoClickDays_SoFar + Generated_NoClickDays

    Loop While ClickOffset < Total_Days And RemainingClickedDays > 0



    'Set the array equal to the clicks so that it returns the array as
    'we want. Ideally this will be just replace Total_Days fields under
    'the base, so not to require a array-function. Neither of these work:
    'ClickSpacer = Range("P1:P" & UBound(Day_Array) + 1).Value
    'Range("P1:P" & UBound(Day_Array) + 1) = Application.Transpose(Day_Array)


    ClickSpacer = Day_Array



End Function

1 个答案:

答案 0 :(得分:0)

我认为你的假设是正确的。上面代码的“问题”是它使用StdDev作为生成随机数的基础,因此标准差将趋于准确,平均值将不太准确。

如果你想要更准确的平均值和更少的标准偏差,那么你将不得不“翻转”数字的生成方式:它们需要以你想要的平均值为中心并使用所需的标准差作为指导,而不是相反。

我对如何做到这一点有所了解,但它会比我在工作中应用更加集中,所以我将不得不回来编辑它。我会看到我能做些什么。