<jupyter_start><jupyter_text>MLE of Cox model on simple dataset above has closed-form solution, which is ln(2)/2. The result above should equal this, which reassuringly it does.<jupyter_code>np.log(2)/2<jupyter_output><empty_output><jupyter_text>Now we will compare our result against the R package 'survival' on another dataset.<jupyter_code>coxmod = TorchCox.TorchCox()

tname = 'tyears'
Xnames = ['Karn', 'Ascites']
dname = 'd'
%load_ext rpy2.ipython
%%R
library(readr)
library(survival)
library(dplyr)
library(tidyr)

df2 = read_csv("/home/ilan/Desktop/TorchCox/data/ovarian_deduplicated.csv")

#df2 = df2 %>% arrange(tyears)

starttime = Sys.time()

rmod = coxph(Surv(tyears, d) ~ Karn + Ascites, df2, ties="breslow")
print(coef(rmod))

endtime = Sys.time()

print(endtime-starttime)
%%time

coxmod.fit(df_tied, Xnames=Xnames, tname=tname, dname=dname, basehaz=False)<jupyter_output>[-0.23638311  0.4170416 ]
CPU times: user 895 ms, sys: 3.37 ms, total: 899 ms
Wall time: 262 ms<jupyter_text>We indeed match that result as well!  We are about 10x slower than the R package (which runs in C). But simplicity and extensibility of our code compensates for that in my view. Also timings here most likely dominated by overhead of loading libraries, comparison on larger dataset required. Fit again, but this time computing the baseline hazard, to ensure that works as well.<jupyter_code>%%time

coxmod.fit(df_tied, Xnames=Xnames, tname=tname, dname=dname, basehaz=True)
coxmod.basehaz<jupyter_output><empty_output><jupyter_text>Predict on the training set to ensure predict_proba() method works as well.<jupyter_code>df_tied['pred'] = coxmod.predict_proba(df_tied, Xnames=Xnames, tname=tname)
df_tied<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10867052023121387, 'fraction_numerical': 0.023121387283236993, 'mean_word_length': 6.429184549356223, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '499193', 'license': 'MIT', 'n_tokens_mistral': 661, 'n_tokens_neox': 625, 'n_words': 176, 'path': 'notebooks/Torch_Cox_package_test.ipynb', 'repo_name': 'ilanfri/TorchCox', 'size': '1730'}