sampler

package module
v0.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

README

Sampler

Go Report Card godoc

This package does two things:

  1. Produces strats. That is, it produces a table of row counts for each stratum of an input source. The strata are defined by the user.
  2. Creates an output table from the input source that is balanced along the desired strata.

Sampling Procedure

The goal is to generate a sample with an equal number of rows for each stratum. A sample rate for each stratum is determined to achieve this. If each stratum has sufficient rows, then we're done. To the extent some strata have insufficient rows, then the total sample will be short. Also, the sample won't be exactly balanced. To achieve balance one would have to do one of these:

  • Target a sample per strata that is equal to the size of the smallest stratum;
  • Resample strata with insufficient rows

Practically, the first is not an option as often there will be a stratum with very few rows. The second is tantamount to up-weighting the small strata. In that case, these observations may become influential observations.

Instead of these, this package adopts the philosophy that an approximate balance goes a long way to reducing the leverage of huge strata which is typical in data. The sampling algorithm used by sampler is:

  1. At the start
    • desired sample = total sample desired / # of strata
    • stratum sample rate = min(desired sample / stratum size, sample rate cap)
    • stratum captured sample = stratum sample rate * stratum size
    • stratum free observations = stratum size - stratum captured sample
  2. Update
    • total sample desired = total sample desired - total captured
    • if this number is "small" stop.
    • stratum desired sample = total sample desired / # of strata with free observations
    • stratum sample rate = min(stratum desired sample + stratum previously captured sample / stratum size, sample rate cap)
    • stratum captured sample = stratum sample rate * stratum size
    • stratum free observations = stratum size - stratum captured sample

Step 2 is repeated (iterations capped at 5) until the target sample size is achieved (within tolerance) or no strata have free observations.

Documentation

Overview

Package sampler produces strats and stratified samples. The package works with ClickHouse tables.

This package does two things:

  1. Produces strats. That is, it produces a table of row counts for each stratum of an input source. The strata are defined by the user.
  2. Creates an output table from the input source that is balanced along the desired strata.

Sampling Procedure

The goal is to generate a sample with an equal number of rows for each stratum. A sample rate for each stratum is determined to achieve this. If each stratum has sufficient rows, then we're done. To the extent some strata have insufficient rows, then the total sample will be short. Also, the sample won't be exactly balanced. To achieve balance one would have to do one of these:

  • Target a sample per strata that is equal to the size of the smallest stratum;
  • Resample strata with insufficient rows

Practically, the first is not an option as often there will be a stratum with very few rows. The second is tantamount to up-weighting the small strata. In that case, these observations may become influential observations.

Instead of these, this package adopts the philosophy that an approximate balance goes a long way to reducing the leverage of huge strata which is typical in data. The sampling algorithm used by sampler is:

1. At the start

  • desired sample = total sample desired / # of strata
  • stratum sample rate = min(desired sample / stratum size, sample rate cap)
  • stratum captured sample = stratum sample rate * stratum size
  • stratum free observations = stratum size - stratum captured sample

2. Update

  • total sample desired = total sample desired - total captured
  • if this number is "small" stop.
  • stratum desired sample = total sample desired / # of strata with free observations
  • stratum sample rate = min(stratum desired sample + stratum previously captured sample / stratum size, sample rate cap)
  • stratum captured sample = stratum sample rate * stratum size
  • stratum free observations = stratum size - stratum captured sample

Step 2 is repeated (iterations capped at 5) until the target sample size is achieved (within tolerance) or no strata have free observations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Max

func Max(a, b int) int

func Min

func Min(a, b int) int

Types

type Generator

type Generator struct {
	// inputs
	Query string // Query to fetch data to sample
	// contains filtered or unexported fields
}

Generator is used to produce stratified samples.

func NewGenerator

func NewGenerator(query, sampleTable, stratTable string, targetTotal int, sortByCount bool, conn *chutils.Connect) *Generator

NewGenerator returns a *Generator. Query is the CH Query to fetch the input data. sampleTable is the output table of the sampled input data. stratTable is the output table of strats & sampling rates of the input data. targetTotal is the target size of sampleTable sortByCount sorts strats by descending count, if true. If false, the table is sorted by the strat fields.

func (*Generator) CalcRates

func (gn *Generator) CalcRates(fields ...string) error

CalcRates calculates the sampling rate for each strat to achieve a balanced sample with a total size of TargetTotal. fields is the set of fields to stratify on.

func (*Generator) MakeQuery

func (gn *Generator) MakeQuery() string

MakeQuery returns the Query used to create sampleTable.

func (*Generator) MakeTable

func (gn *Generator) MakeTable(timeOut int64) error

MakeTable creates sampleTable and stratTable.

func (*Generator) Marginals

func (gn *Generator) Marginals() ([]*Strat, string, error)

Marginals generates the strats of each field we're stratifying on.

func (*Generator) MinCount

func (gn *Generator) MinCount(mc int) uint64

MinCount returns (and optionally sets) the minimum # of obs for a strat to be sampled. The value is not updated if mc < 0.

func (*Generator) SampleCap

func (gn *Generator) SampleCap(sCap float64) float64

SampleCap returns (and optional sets) the maximum sampling rate for strats. The value is not updated if cap <= 0.0 or cap > 1.0

func (*Generator) SampleRates

func (gn *Generator) SampleRates() []float64

SampleRates returns the calculated sample rates. The slice is in the same order as Strats.

func (*Generator) SampleStrats

func (gn *Generator) SampleStrats() *Strat

SampleStrats returns the strats of sampleTable.

func (*Generator) Save

func (gn *Generator) Save() error

Save saves stratTable to the DB.

func (*Generator) Strats

func (gn *Generator) Strats() *Strat

Strats returns the strats of the input data.

func (*Generator) String

func (gn *Generator) String() string

type Strat

type Strat struct {
	Query string // Query to pull data for strats
	// contains filtered or unexported fields
}

Strat produces stratifications.

func NewStrat

func NewStrat(query string, conn *chutils.Connect, sortByCounts bool) *Strat

func (*Strat) Fields

func (strt *Strat) Fields() []string

Fields returns the current set of strat fields

func (*Strat) Make

func (strt *Strat) Make(fields ...string) error

Make generates the strat table for the list of fields.

func (*Strat) MinCount

func (strt *Strat) MinCount(mc int) int

MinCount returns (and optionally sets) the minimum # of obs for a strat to be included

func (*Strat) N

func (strt *Strat) N() uint64

N returns the total number of observations in the strats. This does not include strats dropped if MinCount > 0.

func (*Strat) Plot

func (strt *Strat) Plot(outDir, outFile string, imageTypes []utilities.PlotlyImage, show bool) error

Plot plots the count of observations for each strat from sampleTable

func (*Strat) String

func (strt *Strat) String() string

func (*Strat) Table

func (strt *Strat) Table() (keys [][]any, counts []uint64)

Table returns the elements of the strat table. The table is stored by row. Each row is a stratum.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL