Documentation ¶
Overview ¶
modelling routines.
focused on gps algorithm from http://www-stat.stanford.edu/~jhf/ftp/GPSpub.pdf
Index ¶
- Constants
- func NewLassoPenalty(predictors int) *lassoPenalty
- func NewRidgePenalty() *ridgePenalty
- func Sgn(a float64) float64
- func ValidateRegressionProblem(rp *RegressionProblem) error
- type CalcAdvisor
- type Continue
- type CrossValResults
- type ElasticNet1Minus
- type ElasticNet1Plus
- type FixedRoundsMonitor
- type FixedVMonitor
- type FoldDecider
- type GpsResults
- type IndexSet
- type IntersectionMonitor
- type KeyValue
- type LinearRegressionRisk
- type LogisticRegressionRisk
- type MonitorFactory
- type Normalization
- type NormalizationType
- type ObservationAssigner
- type ObservationAssignments
- type PassiveMonitor
- type PenaltyCalculator
- type RandomAssigner
- type RegressionProblem
- type RiskCalculator
- type SignalDimInfo
- type TrainingOnlyAssigner
- type ValueAndDerivative
Constants ¶
const ( X_AND_Y X_ONLY )
Variables ¶
This section is empty.
Functions ¶
func NewLassoPenalty ¶
func NewLassoPenalty(predictors int) *lassoPenalty
func NewRidgePenalty ¶
func NewRidgePenalty() *ridgePenalty
func ValidateRegressionProblem ¶
func ValidateRegressionProblem(rp *RegressionProblem) error
make sure regression problem is self-consistent
Types ¶
type CalcAdvisor ¶
advisory flags as to whether we need various components
var ( ValueOnly CalcAdvisor = CalcAdvisor{DontNeedValue: false, DontNeedDerivative: true} DerivOnly CalcAdvisor = CalcAdvisor{DontNeedValue: true, DontNeedDerivative: false} CalcAll CalcAdvisor = CalcAdvisor{} )
type Continue ¶
type Continue func(v float64, jstar int, m []float64, trainingRisk, testRisk float64, cn []Normalization, rn Normalization) bool
monitors gps iterations, decided whether or not to continue
type CrossValResults ¶
type CrossValResults struct { TestRisk gmath.Function GpsResults *GpsResults }
func RunCrossVal ¶
func RunCrossVal(folds int, dv float64, rp *RegressionProblem, rc RiskCalculator, pc PenaltyCalculator, mf MonitorFactory, oa ObservationAssigner, fd FoldDecider) *CrossValResults
runs a cross-validation
folds: number of folds in the cross-validation rp: the regression problem per se rc: the risk measure pc: penalty term mf: oa: called each time to assign observations to folds fd: decides how to regulate the final model run after cross-val
type ElasticNet1Minus ¶
type ElasticNet1Minus struct {
Beta float64
}
elastic net for beta < 1 branch
func (*ElasticNet1Minus) CalcPenalty ¶
func (en *ElasticNet1Minus) CalcPenalty(a []float64, advisor CalcAdvisor) ValueAndDerivative
type ElasticNet1Plus ¶
type ElasticNet1Plus struct {
Beta float64
}
elastic net for beta > 1 branch
func (*ElasticNet1Plus) CalcPenalty ¶
func (en *ElasticNet1Plus) CalcPenalty(a []float64, advisor CalcAdvisor) ValueAndDerivative
type FixedRoundsMonitor ¶
type FixedRoundsMonitor struct { Vmax float64 Debug PassiveMonitor }
func (*FixedRoundsMonitor) Continue ¶
func (m *FixedRoundsMonitor) Continue(v float64, jstar int, a []float64, i, o float64, cn []Normalization, rn Normalization) bool
type FixedVMonitor ¶
type FixedVMonitor struct {
Max float64
}
func (*FixedVMonitor) Continue ¶
func (x *FixedVMonitor) Continue(v float64, jstar int, m []float64, i, o float64, cn []Normalization, rn Normalization) bool
type FoldDecider ¶
return monitor for final gps run after cross-val
type GpsResults ¶
type GpsResults struct { Predictors []SignalDimInfo Response SignalDimInfo }
func RunGps ¶
func RunGps(rp *RegressionProblem, testSet *ObservationAssignments, dv float64, mon Continue) *GpsResults
func RunGpsFull ¶
func RunGpsFull(rp *RegressionProblem, tt *ObservationAssignments, dv float64, rc RiskCalculator, pc PenaltyCalculator, proceed Continue) *GpsResults
func (*GpsResults) String ¶
func (i *GpsResults) String() string
type IntersectionMonitor ¶
type IntersectionMonitor struct { A Continue B PassiveMonitor }
func (*IntersectionMonitor) Continue ¶
func (m *IntersectionMonitor) Continue(v float64, jstar int, a []float64, i, o float64, cn []Normalization, rn Normalization) bool
type LinearRegressionRisk ¶
type LinearRegressionRisk struct { }
func (*LinearRegressionRisk) CalcRisk ¶
func (lr *LinearRegressionRisk) CalcRisk(model []float64, rowMask []float64, rp *RegressionProblem, a CalcAdvisor) ValueAndDerivative
func (*LinearRegressionRisk) Norm ¶
func (lr *LinearRegressionRisk) Norm() NormalizationType
type LogisticRegressionRisk ¶
type LogisticRegressionRisk struct { }
func (*LogisticRegressionRisk) CalcRisk ¶
func (lr *LogisticRegressionRisk) CalcRisk(beta []float64, rowMask []float64, rp *RegressionProblem, a CalcAdvisor) ValueAndDerivative
Adapted from:
public IRiskOutput calcRisk(double[] beta) { final int n = getN(); final int p = getP(); final double[][] x = ds.getPredictors(); final double[] y = ds.getResponses(); double risk = 0; final double[] g = new double[p]; for (int i = 0; i < n; i++) { double f = 0; for (int j = 0; j < p; j++) { f += x[i][j] * beta[j]; } double exp = Math.exp(-y[i] * f); double eyf = y[i] * exp / (1 + exp); risk += Math.log(1 + exp); for (int j = 0; j < p; j++) { g[j] += -eyf * x[i][j]; } } final double finalRisk = risk / n; return new IRiskOutput() { @Override public double[] getRiskDerivative() { return g; } @Override public double getRisk() { return finalRisk; } }; } // outputs the probability per observation public double[][] predict(double[] beta) { final int n = getN(); final int p = getP(); double[][] out = new double[n][1]; final double[][] x = ds.getPredictors(); for (int i = 0; i < n; i++) { double f = 0; for (int j = 0; j < p; j++) { f += x[i][j] * beta[j]; } double e = Math.exp(f); out[i][0] = e / (1 + e); } return out; }
func (*LogisticRegressionRisk) Norm ¶
func (lr *LogisticRegressionRisk) Norm() NormalizationType
only normalize X matrix, since Y's are in {-1,1} set by definition
type MonitorFactory ¶
type MonitorFactory func() Continue
type Normalization ¶
func NormalizeColumnsInPlace ¶
func NormalizeColumnsInPlace(m *la.Matrix, rowMask []float64) (out []Normalization)
type NormalizationType ¶
type NormalizationType int
type ObservationAssigner ¶
type ObservationAssigner interface {
Assign() *ObservationAssignments
}
type ObservationAssignments ¶
type PassiveMonitor ¶
type PenaltyCalculator ¶
type PenaltyCalculator interface {
CalcPenalty(model []float64, a CalcAdvisor) ValueAndDerivative
}
calculate a model penalty and its derivative; note that derivative is respect to absolute values of model coefficients
func NewElasticNetFamily ¶
func NewElasticNetFamily(beta float64, n int) PenaltyCalculator
create a new elastic net for 0<beta<=2
type RandomAssigner ¶
type RandomAssigner struct { N int // total number of observations P float64 // probability that observation is a training example }
func (*RandomAssigner) Assign ¶
func (x *RandomAssigner) Assign() *ObservationAssignments
type RegressionProblem ¶
type RegressionProblem struct {
N, P int // number of observations and predictors, respectively
Data *la.Matrix // i.e., the NxP design matrix
RowNames []string // N-dim slice of row names
ColumnNames []string
Response *la.Vector
ResponseName string
}
the data and metadata for a regression problem
func (*RegressionProblem) Copy ¶
func (r *RegressionProblem) Copy() *RegressionProblem
type RiskCalculator ¶
type RiskCalculator interface { // rowMask is comprised of 1.0 and 0.0 elements, to respectively mask in or out various rows CalcRisk(model []float64, rowMask []float64, rp *RegressionProblem, a CalcAdvisor) ValueAndDerivative Norm() NormalizationType }
type SignalDimInfo ¶
type SignalDimInfo struct { Name string Norm Normalization Model float64 `json:",omitempty"` }
type TrainingOnlyAssigner ¶
type TrainingOnlyAssigner struct {
N int
}
func (*TrainingOnlyAssigner) Assign ¶
func (x *TrainingOnlyAssigner) Assign() *ObservationAssignments