00001 #include "MusimatChapter9.h" 00002 MusimatChapter9Section(C091406) { 00003 Print("*** 9.14.6 Cumulative Distribution Function ***"); 00004 /***************************************************************************** 00005 00006 9.14.6 Cumulative Distribution Function 00007 00008 Let's rotate each of the weights in figure 9.21 and then concatenate them. Their sum is 78, so we divide 00009 the length of each weight by 78 so that the weights sum to a length of 1.0 (figure 9.21). We have effec- 00010 tively divided up the x-axis in the unit interval into 12 areas that are proportional to the weights in the 00011 original distribution. Now we pick a random number in the unit interval with the Random() function, 00012 see which interval the number would fall in, and then determine the chosen pitch. The probability that 00013 a particular interval will be chosen is proportional to the extent of its footprint on the x-axis. 00014 00015 How can we represent this formally so that a computer can do this? First, the definition 00016 of list f shown below defines the weights for each pitch, lowest to highest, left to right. 00017 Note that the type of list f is RealList. 00018 *****************************************************************************/ 00019 para1(); // Step into this function to continue. 00020 para2(); // Step into this function to continue. 00021 para3(); // Step into this function to continue. 00022 } 00023 00024 RealList f(12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0); 00025 00026 Static Void para1() { 00027 Print("*** Cumulative Distribution Function ***"); 00028 Print("f=", f); 00029 /***************************************************************************** 00030 00031 Next, we normalize the weights so that they sum to 1.0 (see appendix A, A.3). 00032 Normalizing is done in two steps: 00033 00034 1. Find the sum of all weights with the sum() function: 00035 *****************************************************************************/ 00036 } 00037 00038 Real sum(RealList L) { 00039 Real s = 0.0; 00040 For (Integer i = 0; i < Length(L); i = i + 1) { 00041 s = s + L[i]; 00042 } 00043 Return(s); 00044 } 00045 00046 Static Void para2() { 00047 /***************************************************************************** 00048 Given the definition of RealList f above, 00049 *****************************************************************************/ 00050 00051 Print("Sum of f: ", sum(f)); 00052 00053 /***************************************************************************** 00054 prints 78. 00055 00056 2. Divide each weight by sum(f) so that the sum of the weights equals 1.0: 00057 *****************************************************************************/ 00058 } 00059 00060 RealList normalize(RealList L, Real s){ 00061 For (Integer i = 0; i < Length(L); i = i + 1) { 00062 L[i] = L[i]/s; 00063 } 00064 Return(L); 00065 } 00066 00067 Static Void para3() { 00068 /***************************************************************************** 00069 The normalize() function can be replaced with operations performed directly on the list. 00070 This statement: 00071 00072 RealList r = normalize(f, sum(f)); 00073 00074 can be replaced with: 00075 00076 RealList r = f / sum(f); 00077 00078 So we don't really need the normalize() function. 00079 Given the definition of RealList f above, the statements: 00080 *****************************************************************************/ 00081 00082 RealList r = f / sum(f); 00083 Print("Normalized f=", r); 00084 RationalList x = RealListToRationalList(r); // RealListToRationalList is a built-in function 00085 Print(x); 00086 00087 /***************************************************************************** 00088 print 00089 {{2,13}, {11,78}, {5,39}, {3,26}, {4,39}, {7,78}, {1,13}, {5,78}, {2,39}, {1,26}, {1,39}, {1,78} 00090 which correspond to the reduced form of the following ratios, as we'd expect: 00091 {12/78, 11/78, 10/78, 9/78, 8/78, 7/78, 6/78, 5/78, 4/78, 3/78, 2/78, 1/78}. 00092 00093 After these two steps, r will look like figure 9.20 except that all values are scaled down by 78. (The 00094 built-in RealToRational() function is described in appendix B, B.2.2.) 00095 00096 Next, we create a function such that each step along the x-axis accumulates all the weights to 00097 its left with its own weight (figure 9.22). The first column has a height of 12/78, the second of 00098 12/78 + 11/78, the next of 12/78 + 11/78 + 10/78, and so on. This function is called a cumulative 00099 distribution function. 00100 00101 *****************************************************************************/ 00102 } 00103 00105 /* $Revision: 1.4 $ $Date: 2006/09/07 08:38:15 $ $Author: dgl $ $Name: $ $Id: C091406.cpp,v 1.4 2006/09/07 08:38:15 dgl Exp $ */ 00106 // The Musimat Tutorial © 2006 Gareth Loy 00107 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00108 // and published exclusively by The MIT Press. 00109 // This program is released WITHOUT ANY WARRANTY; without even the implied 00110 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00111 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00112 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00113 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00114 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00115 // The Musimat website: http://www.musimat.com/ 00116 // This program is released under the terms of the GNU General Public License 00117 // available here: http://www.gnu.org/licenses/gpl.txt 00118