Discussion
Loading...

Post

Log in
  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
Jan :rust: :ferris:
Jan :rust: :ferris:
@janriemer@floss.social  ·  activity timestamp 18 hours ago

Guys, don't be afraid of LLMs taking your job as a Software Engineer.

If it can't even get a moving average right - an algorithm so popular and simple - we're save.

It really is the opposite! As Cory Doctorow has said: in a few years they'll need the best "asbestos cleaners" in the world, because we're currently shoving it into our digital infrastructure with #LLMs!

And do you know how you can tell pretty much immediately that this function is wrong? Answer in second toot

1/2

A (wrong!) moving average function in Rust that Claude Opus 4.5 has spit out:

/// Smoothed similarity using a moving average
pub fn smooth_similarities(similarities: &[f32], window_size: usize) -> Vec<f32> {
    if similarities.is_empty() {
        return Vec::new();
    }

    let half_window = window_size / 2;
    similarities
        .iter()
        .enumerate()
        .map(|(i, _)| {
            let start = i.saturating_sub(half_window);
            let end = (i + half_window + 1).min(similarities.len());
            let window = &similarities[start..end];
            window.iter().sum::<f32>() / window.len() as f32
        })
        .collect()
}
A (wrong!) moving average function in Rust that Claude Opus 4.5 has spit out: /// Smoothed similarity using a moving average pub fn smooth_similarities(similarities: &[f32], window_size: usize) -> Vec<f32> { if similarities.is_empty() { return Vec::new(); } let half_window = window_size / 2; similarities .iter() .enumerate() .map(|(i, _)| { let start = i.saturating_sub(half_window); let end = (i + half_window + 1).min(similarities.len()); let window = &similarities[start..end]; window.iter().sum::<f32>() / window.len() as f32 }) .collect() }
A (wrong!) moving average function in Rust that Claude Opus 4.5 has spit out: /// Smoothed similarity using a moving average pub fn smooth_similarities(similarities: &[f32], window_size: usize) -> Vec<f32> { if similarities.is_empty() { return Vec::new(); } let half_window = window_size / 2; similarities .iter() .enumerate() .map(|(i, _)| { let start = i.saturating_sub(half_window); let end = (i + half_window + 1).min(similarities.len()); let window = &similarities[start..end]; window.iter().sum::<f32>() / window.len() as f32 }) .collect() }
  • Copy link
  • Flag this post
  • Block
Jan :rust: :ferris:
Jan :rust: :ferris:
@janriemer@floss.social replied  ·  activity timestamp 18 hours ago

It is because of the `map` operator on the collection we want to create the moving average (MA) on:

The resulting collection of a MA is smaller _by definition_ than the collection the MA is applied on!

`map` will create a collection of the _same_ length, so this algorithm is wrong.

I'm so tired of this shit!

If you want to read good source material for this algorithm:
https://www.statisticshowto.com/probability-and-statistics/statistics-definitions/moving-average/

(Oh, and how ironic that #LLMs get #statistics wrong... 🙃)

2/2

#LLM #Claude #Rust

  • Copy link
  • Flag this comment
  • Block
guenther
guenther
@guenther@chaos.social replied  ·  activity timestamp 5 hours ago

@janriemer

The implementation is not necessarily wrong, even if it may not be correct choice for your use case. Libraries that implement filters like this usually have a parameter or something where you can specify how to handle the border of the data.

What your LLM implemented is equivalent to scipy.ndimage.convolve with mode="nearest", and you wanted what numpy.convolve does with mode="valid"

https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.convolve.html

https://numpy.org/devdocs/reference/generated/numpy.convolve.html

numpy.convolve — NumPy v2.5.dev0 Manual

convolve — SciPy v1.17.0 Manual

  • Copy link
  • Flag this comment
  • Block
Jan :rust: :ferris:
Jan :rust: :ferris:
@janriemer@floss.social replied  ·  activity timestamp 5 hours ago

@guenther Oh wow, thank you for the explanation and the links! 😮 I didn't know that!

I have to check out your links, thanks!

I've updated the OP and linked to your toot:
https://floss.social/@janriemer/115878090852420861

  • Copy link
  • Flag this comment
  • Block
TheConstructor (he/him)
TheConstructor (he/him)
@TheConstructor@social.tchncs.de replied  ·  activity timestamp 15 hours ago

@janriemer shouldn't there be a return? There is one for empty collections…

  • Copy link
  • Flag this comment
  • Block
Bobulous :rust: :codeberg:
Bobulous :rust: :codeberg:
@bobulous@fosstodon.org replied  ·  activity timestamp 13 hours ago

@TheConstructor @janriemer The code is written in the Rust programming language, where a method or function can end with an expression which will be the returned value. So the `return` keyword is usually only used for an early return.

  • Copy link
  • Flag this comment
  • Block

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.1-beta.24 no JS en
Automatic federation enabled
Log in
  • Explore
  • About
  • Members
  • Code of Conduct