def _generateFileFromProb(filename, numRecords, categoryList, initProb, <EOL>firstOrderProb, secondOrderProb, seqLen, numNoise=<NUM_LIT:0>, resetsEvery=None):
<EOL>print("<STR_LIT>" % (filename))<EOL>fields = [('<STR_LIT>', '<STR_LIT:int>', '<STR_LIT:R>'), <EOL>('<STR_LIT>', '<STR_LIT:string>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT:float>', '<STR_LIT>')]<EOL>scriptDir = os.path.dirname(__file__)<EOL>pathname = os.path.join(scriptDir, '<STR_LIT>', filename)<EOL>outFile = FileRecordStream(pathname, write=True, fields=fields)<EOL>initCumProb = initProb.cumsum()<EOL>firstOrderCumProb = dict()<EOL>for (key,value) in firstOrderProb.items():<EOL><INDENT>firstOrderCumProb[key] = value.cumsum()<EOL><DEDENT>if secondOrderProb is not None:<EOL><INDENT>secondOrderCumProb = dict()<EOL>for (key,value) in secondOrderProb.items():<EOL><INDENT>secondOrderCumProb[key] = value.cumsum()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>secondOrderCumProb = None<EOL><DEDENT>elementsInSeq = []<EOL>numElementsSinceReset = <NUM_LIT:0><EOL>maxCatIdx = len(categoryList) - <NUM_LIT:1><EOL>for _ in range(numRecords):<EOL><INDENT>if numElementsSinceReset == <NUM_LIT:0>:<EOL><INDENT>reset = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>reset = <NUM_LIT:0><EOL><DEDENT>rand = numpy.random.rand()<EOL>if secondOrderCumProb is None:<EOL><INDENT>if len(elementsInSeq) == <NUM_LIT:0>:<EOL><INDENT>catIdx = numpy.searchsorted(initCumProb, rand)<EOL><DEDENT>elif len(elementsInSeq) >= <NUM_LIT:1> and(seqLen is None or len(elementsInSeq) < seqLen-numNoise):<EOL><INDENT>catIdx = numpy.searchsorted(firstOrderCumProb[str(elementsInSeq[-<NUM_LIT:1>])], <EOL>rand)<EOL><DEDENT>else:   <EOL><INDENT>catIdx = numpy.random.randint(len(categoryList))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if len(elementsInSeq) == <NUM_LIT:0>:<EOL><INDENT>catIdx = numpy.searchsorted(initCumProb, rand)<EOL><DEDENT>elif len(elementsInSeq) == <NUM_LIT:1>:<EOL><INDENT>catIdx = numpy.searchsorted(firstOrderCumProb[str(elementsInSeq)], rand)<EOL><DEDENT>elif (len(elementsInSeq) >=<NUM_LIT:2>) and(seqLen is None or len(elementsInSeq) < seqLen-numNoise):<EOL><INDENT>catIdx = numpy.searchsorted(secondOrderCumProb[str(elementsInSeq[-<NUM_LIT:2>:])], rand)<EOL><DEDENT>else:   <EOL><INDENT>catIdx = numpy.random.randint(len(categoryList))<EOL><DEDENT><DEDENT>catIdx = min(maxCatIdx, catIdx)<EOL>outFile.appendRecord([reset, categoryList[catIdx], catIdx])    <EOL>elementsInSeq.append(catIdx)<EOL>numElementsSinceReset += <NUM_LIT:1><EOL>if resetsEvery is not None and numElementsSinceReset == resetsEvery:<EOL><INDENT>numElementsSinceReset = <NUM_LIT:0><EOL>elementsInSeq = []<EOL><DEDENT>if seqLen is not None and (len(elementsInSeq) == seqLen+numNoise):<EOL><INDENT>elementsInSeq = []<EOL><DEDENT><DEDENT>outFile.close()<EOL>
Generate a set of records reflecting a set of probabilities.

    Parameters:
    ----------------------------------------------------------------
    filename:         name of .csv file to generate
    numRecords:       number of records to generate
    categoryList:     list of category names
    initProb:         Initial probability for each category. This is a vector
                        of length len(categoryList).
    firstOrderProb:   A dictionary of the 1st order probabilities. The key
                        is the 1st element of the sequence, the value is
                        the probability of each 2nd element given the first. 
    secondOrderProb:  A dictionary of the 2nd order probabilities. The key
                        is the first 2 elements of the sequence, the value is
                        the probability of each possible 3rd element given the 
                        first two. If this is None, then the sequences will be
                        first order only. 
    seqLen:           Desired length of each sequence. The 1st element will
                        be generated using the initProb, the 2nd element by the
                        firstOrder table, and the 3rd and all successive 
                        elements by the secondOrder table. None means infinite
                        length. 
    numNoise:         Number of noise elements to place between each 
                        sequence. The noise elements are evenly distributed from 
                        all categories. 
    resetsEvery:      If not None, generate a reset every N records


    Here is an example of some parameters:

    categoryList:     ['cat1', 'cat2', 'cat3']

    initProb:         [0.7, 0.2, 0.1]

    firstOrderProb:   {'[0]': [0.3, 0.3, 0.4],
                       '[1]': [0.3, 0.3, 0.4],
                       '[2]': [0.3, 0.3, 0.4]}

    secondOrderProb:  {'[0,0]': [0.3, 0.3, 0.4],
                       '[0,1]': [0.3, 0.3, 0.4],
                       '[0,2]': [0.3, 0.3, 0.4],
                       '[1,0]': [0.3, 0.3, 0.4],
                       '[1,1]': [0.3, 0.3, 0.4],
                       '[1,2]': [0.3, 0.3, 0.4],
                       '[2,0]': [0.3, 0.3, 0.4],
                       '[2,1]': [0.3, 0.3, 0.4],
                       '[2,2]': [0.3, 0.3, 0.4]}