<jupyter_start><jupyter_text>The Maximum Likelihood Function is $\theta ^{MLE}=max\prod_{i=1}^{n}\frac{1}{\sqrt{2\pi g_{\theta }(\mathbf{x_{i}})^{2} }}exp(\frac{-(y_{i}-f_{\theta }(\mathbf{x_{i}})^{2})}{2g_{\theta }(\mathbf{x_{i}})^{2}})$. And the loss function of the neural network is $\theta ^{MLE}=min\sum_{i=1}^{n} (log( g_{\theta }(\mathbf{x_{i}}))+\frac{(y_{i}-f_{\theta }(\mathbf{x_{i}}))^{2}}{2g_{\theta }(\mathbf{x_{i}})^{2}})$<jupyter_code>class GRUModel(tf.keras.Model):
    def __init__(self, seq_length, cell_size,batchsize,var):
        super().__init__()
        self.batchsize=batchsize
        self.var=var
        self.seq_length = seq_length
        self.cell_size = cell_size
    
        self.layer1 = tf.keras.layers.Reshape((self.seq_length,self.var), batch_size = self.batchsize)
        #self.layer2=  tf.keras.layers.Dense(10,input_shape=(self.seq_length,self.var),activation='relu',
                                          #kernel_initializer=tf.keras.initializers.TruncatedNormal(mean=1,stddev=1),
                                           #bias_initializer=tf.keras.initializers.Constant(1))
        self.layer_GRU = tf.keras.layers.GRU(self.cell_size,activation='relu', return_sequences=False)
        self.layer_dense = tf.keras.layers.Dense(12,activation='softplus',bias_initializer=tf.keras.initializers.Constant(1),
                                                kernel_initializer=tf.keras.initializers.TruncatedNormal(mean=2,stddev=1))
        self.layer_dense1 = tf.keras.layers.Dense(12,activation='linear')
  
    def call(self, inputs):
        x1 = self.layer1(inputs)
        #x1 = self.layer2(x1)
        x2 = self.layer_GRU(x1)
        output1=self.layer_dense(x2)
        output2=self.layer_dense1(x2)
        
        return output1,output2
def loss_fun(y_pred1,y_pred2,y_test):
    #loss=tf.math.log(y_pred1)+tf.clip_by_value((tf.square(y_test-y_pred2))/(tf.square(y_pred1)*2),0,100)
    loss=tf.math.log(y_pred1)+tf.square(y_test-y_pred2)/(tf.square(y_pred1)*2)
    #loss=tf.math.log(tf.reduce_mean(y_pred1,axis=0))+tf.reduce_mean(tf.square(y_test-y_pred2),axis=0)/(tf.square(tf.reduce_mean(y_pred1,axis=0))*2)
    return tf.square(tf.reduce_mean(loss))
model = GRUModel(SEQLEN,RNN_CELLSIZE,BATCHSIZE, VAR)
optimizer = tf.keras.optimizers.Adam(learning_rate = LR)
for epoch in range(100):
    with tf.GradientTape() as tape:
        y_pred = model(x_train)

        loss=loss_fun(y_pred[0],y_pred[1],y_train)
        #loss=tf.reduce_mean((y_pred-y_train)**2)
        
        if epoch%1 == 0:
            print("epoch: {}, loss: {}".format(epoch, loss))
  
    grads = tape.gradient(loss, model.variables)
    #print(grads)
    optimizer.apply_gradients(zip(grads,model.variables))
ytestyear1=[2015,2016,2017,2018]
test_x=[]
for i in range(len(ncode)):
    temp=df[df['secID'].isin([ncode[i]])].copy()
    temp1=temp[temp['year'].isin(ytestyear1)].copy()
    for j in mon:
        temp2=temp1[temp1['mon'].isin([j])].copy()
        values=temp2.loc[:,'revenue':'marketValue'].copy()

        val=values.values
        test_x.append(val)
x_test=np.array(test_x)
x_test.shape
result=model.predict(x_test)
EBIT_mkt=result[1][:,3]<jupyter_output><empty_output><jupyter_text>risk-adjusted EBIT<jupyter_code>adje=EBIT_mkt/result[0][:,3]
codelist=[]
for i in range(len(ncode)):
    code=ncode[i]
    for i in range(4):
        codelist.append(code)
codelist    
monlist=mon*len(ncode)
da=pd.DataFrame()
da['code']=codelist
da['adjEBIT']=adje
da['fo_EBIT']=result[1][:,3]
da['mon']=monlist
da['year']=2019
da
mul=(len(da['year']))/4
mul
date=['2018-12-28','2019-03-29','2019-06-28','2019-09-30']*181
date
da['date']=date
da
da.to_excel('2019diffdf.xlsx')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18315902386698846, 'fraction_numerical': 0.038348082595870206, 'mean_word_length': 3.973333333333333, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '31799', 'license': 'MIT', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1432, 'n_words': 154, 'path': 'GRU Neural Network.ipynb', 'repo_name': 'YuhaoLiuox/Project-2-', 'size': '3729'}