找到最小标准偏差的最佳方法

时间:2018-10-26 14:25:45

标签: math optimization statistics standard-deviation

我有一个电子表格,其中在书的每个段落上都放置代表经文数量的数字。

我按经文数量手动分配顺序的段落,因此在电子表格中,我将得到以下内容:

Verses  Day
5       1
6       1
3       1
10      2
8       3
4       3
2       3
6       4
3       4
10      5
3       5
2       6
5       6
10      7
        = 2,7080128015

通过将每天(在本例中为7天)的所有经文相加,我得到了标准偏差,并尝试减小该标准偏差以更好地分配段落。

问题是:找到最小标准偏差的最佳方法是什么?

我曾考虑过使用蛮力来生成所有可能的组合,但是如果数量增加,那不是一个好主意。

编辑:标准差基于每天的经文总数,这些经文被顺序标识。第1天总共有14节经文,第2天是10,依此类推。

1   14
2   10
3   14
4   9
5   13
6   7
7   10
    = 2,7080128015

1 个答案:

答案 0 :(得分:1)

由于经文的总数和天数是恒定的,所以您要最小化

sum (avg verse count - verse count of day i)^2
 i

avg verse count是一个常数,只是经文总数除以天数。

可以通过动态程序解决这一问题。让我们建立部分解函数f(days, paragraph),该函数为我们提供了0天内分配段落paragraphdays的最小平方和。我们对该函数的最后一个值感兴趣。

我们可以逐步构建函数。为任何f(1, p)计算p很简单,因为我们只需要计算与平均值和平方的差即可。然后,对于其他所有日子,我们可以计算

f(d, p) = min f(d - 1, i) + (avg verse count -  sum    verse count of paragraph j)^2
          i<p                                 j:i+1..p

这意味着,我们将检查解决方案的时间减少了一天,并用前一天的结束段落和p之间的段落填充了当天的内容。在计算此函数时,我们保持指向所选最小元素的指针(对于动态程序通常如此)。计算完整个函数后,我们只需将指针跟随回到起点即可,这将为我们提供分区。

该算法的运行时间为O(d * p^2),其中d是天数,p是段数。

示例代码

这是一些实现上述算法的示例C#代码:

struct Entry
{
    public double minCost;
    public int predecessor;
}

public static void Main()
{
    //input data
    int[] versesPerParagraph = { 5, 6, 3, 10, 8, 4, 2, 6, 3, 10, 3, 2, 5, 10 };
    int days = 7;

    //calculate constants
    double avgVerses = (double)versesPerParagraph.Sum() / days;

    //set up DP table (f(d,p))
    int paragraphs = versesPerParagraph.Length;
    Entry[,] dp = new Entry[days, paragraphs];

    //initialize table
    int verseCount = 0;
    for(int p = 0; p < paragraphs; ++p)
    {
        verseCount += versesPerParagraph[p];
        double diff = avgVerses - verseCount;
        dp[0, p].minCost = diff * diff;
        dp[0, p].predecessor = -1;
    }

    //run dynamic program
    for(int d = 1; d < days; ++d)
    {
        for(int p = d; p < paragraphs; ++p)
        {
            verseCount = 0;
            dp[d, p].minCost = double.MaxValue;
            for(int i = p; i >= d; --i)
            {
                verseCount += versesPerParagraph[i];
                double diff = avgVerses - verseCount;
                double cost = dp[d - 1, i - 1].minCost + diff * diff;
                if(cost < dp[d, p].minCost)
                {
                    dp[d, p].minCost = cost;
                    dp[d, p].predecessor = i - 1;
                }
            }
        }
    }

    //reconstruct the partitioning
    {
        int p = paragraphs - 1;
        for (int d = days - 1; d >= 0; --d)
        {
            int predecessor = dp[d, p].predecessor;
            //calculate number of verses, just to show them
            verseCount = 0;
            for (int i = predecessor + 1; i <= p; ++i)
                verseCount += versesPerParagraph[i];
            Console.WriteLine($"Day {d} ranges from paragraph {predecessor + 1} to {p} and has {verseCount} verses.");
            p = predecessor;
        }
    }
}

输出为:

Day 6 ranges from paragraph 13 to 13 and has 10 verses.
Day 5 ranges from paragraph 10 to 12 and has 10 verses.
Day 4 ranges from paragraph 9 to 9 and has 10 verses.
Day 3 ranges from paragraph 6 to 8 and has 11 verses.
Day 2 ranges from paragraph 4 to 5 and has 12 verses.
Day 1 ranges from paragraph 2 to 3 and has 13 verses.
Day 0 ranges from paragraph 0 to 1 and has 11 verses.

此分区给出的标准偏差为1.15