model

package
v0.0.0-...-4f40efb Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2015 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

modelling routines.

focused on gps algorithm from http://www-stat.stanford.edu/~jhf/ftp/GPSpub.pdf

Index

Constants

View Source
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 Sgn

func Sgn(a float64) float64

func ValidateRegressionProblem

func ValidateRegressionProblem(rp *RegressionProblem) error

make sure regression problem is self-consistent

Types

type CalcAdvisor

type CalcAdvisor struct {
	DontNeedValue      bool
	DontNeedDerivative bool
}

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

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

type FoldDecider func(oos gmath.Function) Continue

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 (*GpsResults) String

func (i *GpsResults) String() string

type IndexSet

type IndexSet struct {
	Length int
	Max    *KeyValue
}

func NewKVList

func NewKVList() *IndexSet

func (*IndexSet) Add

func (f *IndexSet) Add(kv *KeyValue)

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 KeyValue

type KeyValue struct {
	Index int
	Value float64
}

type LinearRegressionRisk

type LinearRegressionRisk struct {
}

func (*LinearRegressionRisk) CalcRisk

func (lr *LinearRegressionRisk) CalcRisk(model []float64, rowMask []float64, rp *RegressionProblem, a CalcAdvisor) ValueAndDerivative

func (*LinearRegressionRisk) Norm

type LogisticRegressionRisk

type LogisticRegressionRisk struct {
}

func (*LogisticRegressionRisk) CalcRisk

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

only normalize X matrix, since Y's are in {-1,1} set by definition

type MonitorFactory

type MonitorFactory func() Continue

type Normalization

type Normalization struct {
	Mean, Sd float64
	IsNone   bool `json:",omitempty"`
}

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 ObservationAssignments struct {
	TrainingIndicies []int
	TestingIndicies  []int
}

type PassiveMonitor

type PassiveMonitor func(v float64, jstar int, m []float64, trainingRisk, testRisk float64)

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

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

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

type ValueAndDerivative

type ValueAndDerivative struct {
	Value      float64
	Derivative []float64
}

Directories

Path Synopsis
use modelling code on well-known prostate cancer data, check we get same results.
use modelling code on well-known prostate cancer data, check we get same results.
logistic modelling example.
logistic modelling example.

Jump to

Keyboard shortcuts

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