<s> from cvxopt import matrix , spmatrix , sparse , exp , uniform <EOL> import numpy as np <EOL> import math as math <EOL> from so_interface import SOInterface <EOL> class SOHMM ( SOInterface ) : <EOL> """<STR_LIT>""" <EOL> ninf = - <NUM_LIT> ** <NUM_LIT:15> <EOL> start_p = [ ] <EOL> states = - <NUM_LIT:1> <EOL> hotstart_tradeoff = <NUM_LIT:0.1> <EOL> def __init__ ( self , X , y = [ ] , num_states = <NUM_LIT:2> , hotstart_tradeoff = <NUM_LIT:0.1> ) : <EOL> SOInterface . __init__ ( self , X , y ) <EOL> self . states = num_states <EOL> self . start_p = matrix ( <NUM_LIT:1.0> , ( self . states , <NUM_LIT:1> ) ) <EOL> self . hotstart_tradeoff = hotstart_tradeoff <EOL> def get_hotstart_sol ( self ) : <EOL> sol = uniform ( self . get_num_dims ( ) , <NUM_LIT:1> , a = - <NUM_LIT:1> , b = + <NUM_LIT:1> ) <EOL> sol [ <NUM_LIT:0> : self . states * self . states ] = self . hotstart_tradeoff <EOL> print ( '<STR_LIT>' . format ( self . hotstart_tradeoff ) ) <EOL> return sol <EOL> def calc_emission_matrix ( self , sol , idx , augment_loss = False , augment_prior = False ) : <EOL> T = len ( self . X [ idx ] [ <NUM_LIT:0> , : ] ) <EOL> N = self . states <EOL> F = self . dims <EOL> em = matrix ( <NUM_LIT:0.0> , ( N , T ) ) ; <EOL> for t in xrange ( T ) : <EOL> for s in xrange ( N ) : <EOL> for f in xrange ( F ) : <EOL> em [ s , t ] += sol [ N * N + s * F + f ] * self . X [ idx ] [ f , t ] <EOL> if ( augment_loss == True ) : <EOL> loss = matrix ( <NUM_LIT:1.0> , ( N , T ) ) <EOL> for t in xrange ( T ) : <EOL> loss [ self . y [ idx ] [ t ] , t ] = <NUM_LIT:0.0> <EOL> em += loss <EOL> if ( augment_prior == True ) : <EOL> prior = matrix ( <NUM_LIT:0.0> , ( N , T ) ) <EOL> em += prior <EOL> return em <EOL> def get_transition_matrix ( self , sol ) : <EOL> N = self . states <EOL> A = matrix ( <NUM_LIT:0.0> , ( N , N ) ) <EOL> for i in xrange ( N ) : <EOL> for j in xrange ( N ) : <EOL> A [ i , j ] = sol [ i * N + j ] <EOL> return A <EOL> def argmax ( self , sol , idx , add_loss = False , add_prior = False , opt_type = '<STR_LIT>' ) : <EOL> T = len ( self . X [ idx ] [ <NUM_LIT:0> , : ] ) <EOL> N = self . states <EOL> F = self . dims <EOL> A = self . get_transition_matrix ( sol ) <EOL> em = self . calc_emission_matrix ( sol , idx , augment_loss = add_loss , augment_prior = add_prior ) <EOL> delta = matrix ( <NUM_LIT:0.0> , ( N , T ) ) ; <EOL> psi = matrix ( <NUM_LIT:0> , ( N , T ) ) ; <EOL> for i in xrange ( N ) : <EOL> delta [ i , <NUM_LIT:0> ] = self . start_p [ i ] + em [ i , <NUM_LIT:0> ] <EOL> for t in xrange ( <NUM_LIT:1> , T ) : <EOL> for i in xrange ( N ) : <EOL> ( delta [ i , t ] , psi [ i , t ] ) = max ( [ ( delta [ j , t - <NUM_LIT:1> ] + A [ j , i ] + em [ i , t ] , j ) for j in xrange ( N ) ] ) ; <EOL> states = matrix ( <NUM_LIT:0> , ( <NUM_LIT:1> , T ) ) <EOL> ( prob , states [ T - <NUM_LIT:1> ] ) = max ( [ delta [ i , T - <NUM_LIT:1> ] , i ] for i in xrange ( N ) ) ; <EOL> for t in reversed ( xrange ( <NUM_LIT:1> , T ) ) : <EOL> states [ t - <NUM_LIT:1> ] = psi [ states [ t ] , t ] ; <EOL> psi_idx = self . get_joint_feature_map ( idx , states ) <EOL> val = sol . trans ( ) * psi_idx <EOL> return ( val , states , psi_idx ) <EOL> def get_jfm_norm2 ( self , idx , y = [ ] ) : <EOL> y = np . array ( y ) <EOL> if ( y . size == <NUM_LIT:0> ) : <EOL> y = np . array ( self . y [ idx ] ) <EOL> jfm = self . get_joint_feature_map ( idx , y ) <EOL> return jfm . trans ( ) * jfm <EOL> def calc_loss ( self , idx , y ) : <EOL> return float ( sum ( [ np . uint ( self . y [ idx ] [ i ] ) != np . uint ( y [ i ] ) for i in xrange ( len ( y ) ) ] ) ) <EOL> def get_scores ( self , sol , idx , y = [ ] ) : <EOL> y = np . array ( y ) <EOL> if ( y . size == <NUM_LIT:0> ) : <EOL> y = np . array ( self . y [ idx ] ) <EOL> ( foo , T ) = y . shape <EOL> N = self . states <EOL> F = self . dims <EOL> scores = matrix ( <NUM_LIT:0.0> , ( <NUM_LIT:1> , T ) ) <EOL> anom_score = sol . trans ( ) * self . get_joint_feature_map ( idx ) <EOL> A = self . get_transition_matrix ( sol ) <EOL> em = self . calc_emission_matrix ( sol , idx , augment_loss = False , augment_prior = False ) ; <EOL> scores [ <NUM_LIT:0> ] = self . start_p [ int ( y [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ] + em [ int ( y [ <NUM_LIT:0> , <NUM_LIT:0> ] ) , <NUM_LIT:0> ] <EOL> for t in range ( <NUM_LIT:1> , T ) : <EOL> scores [ t ] = A [ int ( y [ <NUM_LIT:0> , t - <NUM_LIT:1> ] ) , int ( y [ <NUM_LIT:0> , t ] ) ] + em [ int ( y [ <NUM_LIT:0> , t ] ) , t ] <EOL> if max ( abs ( scores ) ) > <NUM_LIT> ** ( - <NUM_LIT:15> ) : <EOL> scores = exp ( - abs ( <NUM_LIT> * scores / max ( abs ( scores ) ) ) ) <EOL> else : <EOL> scores = matrix ( <NUM_LIT:0.0> , ( <NUM_LIT:1> , T ) ) <EOL> return ( float ( np . single ( anom_score ) ) , scores ) <EOL> def get_joint_feature_map ( self , idx , y = [ ] ) : <EOL> y = np . array ( y ) <EOL> if ( y . size == <NUM_LIT:0> ) : <EOL> y = np . array ( self . y [ idx ] ) <EOL> ( foo , T ) = y . shape <EOL> N = self . states <EOL> F = self . dims <EOL> jfm = matrix ( <NUM_LIT:0.0> , ( self . get_num_dims ( ) , <NUM_LIT:1> ) ) <EOL> for i in range ( N ) : <EOL> ( foo , inds ) = np . where ( [ y [ <NUM_LIT:0> , <NUM_LIT:1> : T ] == i ] ) <EOL> for j in range ( N ) : <EOL> ( foo , indsj ) = np . where ( [ y [ <NUM_LIT:0> , inds ] == j ] ) <EOL> jfm [ j * N + i ] = len ( indsj ) <EOL> for t in range ( T ) : <EOL> for f in range ( F ) : <EOL> jfm [ int ( y [ <NUM_LIT:0> , t ] ) * F + f + N * N ] += self . X [ idx ] [ f , t ] <EOL> return jfm <EOL> def get_num_dims ( self ) : <EOL> return self . dims * self . states + self . states * self . states <EOL> def evaluate ( self , pred ) : <EOL> """<STR_LIT>""" <EOL> N = self . samples <EOL> if len ( pred ) != N : <EOL> print len ( pred ) <EOL> raise Exception ( '<STR_LIT>' ) <EOL> all_tp = all_fp = all_tn = all_fn = <NUM_LIT:0.0> <EOL> err_exm = { } <EOL> err_exm [ '<STR_LIT>' ] = [ ] <EOL> err_exm [ '<STR_LIT>' ] = [ ] <EOL> err_exm [ '<STR_LIT>' ] = [ ] <EOL> err_exm [ '<STR_LIT>' ] = [ ] <EOL> for i in xrange ( N ) : <EOL> loss1 = self . calc_loss ( i , pred [ i ] ) <EOL> loss2 = self . calc_loss ( i , - pred [ i ] + <NUM_LIT:1> ) <EOL> seq_true = np . uint ( np . sign ( self . y [ i ] ) ) <EOL> seq_pred = np . uint ( np . sign ( pred [ i ] ) ) <EOL> if loss2 < loss1 : <EOL> seq_pred = np . uint ( np . sign ( - pred [ i ] + <NUM_LIT:1> ) ) <EOL> lens = len ( seq_pred [ <NUM_LIT:0> , : ] ) <EOL> tp = fp = tn = fn = <NUM_LIT:0.0> <EOL> isPosAvail = False <EOL> for t in xrange ( lens ) : <EOL> if ( seq_true [ <NUM_LIT:0> , t ] == <NUM_LIT:1> ) : <EOL> isPosAvail = True <EOL> fp += float ( seq_true [ <NUM_LIT:0> , t ] == <NUM_LIT:0> and seq_pred [ <NUM_LIT:0> , t ] == <NUM_LIT:1> ) <EOL> fn += float ( seq_true [ <NUM_LIT:0> , t ] == <NUM_LIT:1> and seq_pred [ <NUM_LIT:0> , t ] == <NUM_LIT:0> ) <EOL> tp += float ( seq_true [ <NUM_LIT:0> , t ] == <NUM_LIT:1> and seq_pred [ <NUM_LIT:0> , t ] == <NUM_LIT:1> ) <EOL> tn += float ( seq_true [ <NUM_LIT:0> , t ] == <NUM_LIT:0> and seq_pred [ <NUM_LIT:0> , t ] == <NUM_LIT:0> ) <EOL> if tp + fp + tn + fn != lens : <EOL> print '<STR_LIT:error>' <EOL> all_fn += fn <EOL> all_tn += tn <EOL> all_fp += fp <EOL> all_tp += tp <EOL> if tp + fn > <NUM_LIT:0> : <EOL> sensitivity = float ( tp ) / float ( tp + fn ) <EOL> else : <EOL> sensitivity = <NUM_LIT:1.0> <EOL> if isPosAvail : <EOL> sensitivity = <NUM_LIT:0.0> <EOL> if tn + fp > <NUM_LIT:0> : <EOL> specificity = float ( tn ) / float ( tn + fp ) <EOL> else : <EOL> specificity = <NUM_LIT:0.0> <EOL> if tp + fp > <NUM_LIT:0> : <EOL> precision = float ( tp ) / float ( tp + fp ) <EOL> else : <EOL> precision = <NUM_LIT:1.0> <EOL> if isPosAvail : <EOL> precision = <NUM_LIT:0.0> <EOL> if precision + sensitivity > <NUM_LIT:0.0> : <EOL> fscore = <NUM_LIT> * precision * sensitivity / float ( precision + sensitivity ) <EOL> else : <EOL> fscore = <NUM_LIT:0.0> <EOL> err_exm [ '<STR_LIT>' ] . append ( fscore ) <EOL> err_exm [ '<STR_LIT>' ] . append ( sensitivity ) <EOL> err_exm [ '<STR_LIT>' ] . append ( specificity ) <EOL> err_exm [ '<STR_LIT>' ] . append ( precision ) <EOL> if all_tp + all_fn > <NUM_LIT:0> : <EOL> sensitivity = float ( all_tp ) / float ( all_tp + all_fn ) <EOL> else : <EOL> sensitivity = <NUM_LIT:0.0> <EOL> if all_tn + all_fp > <NUM_LIT:0> : <EOL> specificity = float ( all_tn ) / float ( all_tn + fp ) <EOL> else : <EOL> specificity = <NUM_LIT:0.0> <EOL> if all_tp + all_fp > <NUM_LIT:0> : <EOL> precision = float ( all_tp ) / float ( all_tp + all_fp ) <EOL> else : <EOL> precision = <NUM_LIT:0.0> <EOL> err = { } <EOL> if precision + sensitivity > <NUM_LIT:0.0> : <EOL> err [ '<STR_LIT>' ] = <NUM_LIT> * precision * sensitivity / float ( precision + sensitivity ) <EOL> else : <EOL> err [ '<STR_LIT>' ] = <NUM_LIT:0.0> <EOL> err [ '<STR_LIT>' ] = sensitivity <EOL> err [ '<STR_LIT>' ] = specificity <EOL> err [ '<STR_LIT>' ] = precision <EOL> return ( err , err_exm ) </s>
