Skip to content

Symbolic Transformer #789

Answered by MilesCranmer
jmrichardson asked this question in Q&A
Dec 25, 2024 · 2 comments · 5 replies
Discussion options

You must be logged in to vote

Here’s a Spearman Correlation loss for PySR in case this is what you were after?

loss_function = """
function my_spearman_loss(tree, dataset::Dataset{T,L}, options) where {T,L}
    prediction, complete = eval_tree_array(tree, dataset.X, options)
    if !complete
        return L(Inf)
    end
    
    # Get ranks
    rank_pred = sortperm(sortperm(prediction))
    rank_y = sortperm(sortperm(dataset.y))
    
    # Calculate Spearman
    d = rank_pred .- rank_y
    rho = 1 - (6 * sum(d .^ 2)) / (dataset.n * (dataset.n^2 - 1))
    
    # Return 1-rho so it's a loss to minimize (rho ranges from -1 to 1)
    return L(1 - rho)
end
"""

model = PySRRegressor(loss_function=loss_function)

model.fit(X

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
4 replies
@jmrichardson
Comment options

@MilesCranmer
Comment options

@jmrichardson
Comment options

@MilesCranmer
Comment options

Comment options

You must be logged in to vote
1 reply
@jmrichardson
Comment options

Answer selected by jmrichardson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants