Excel LAMBDA Spotlight: INFLECTIONPOINTS

Excel LAMBDA Spotlight: INFLECTIONPOINTS

In this article I'll look at a LAMBDA function called INFLECTIONPOINTS.?

This is a function I wrote last year to quickly get the local minima and local maxima of a continuous vector.


OVERVIEW

INFLECTIONPOINTS takes a single argument - a vector (or column, array, if you prefer) and returns an array of the same length where points in the input array are returned if they are inflection points and #N/A! is returned otherwise.

Given this simple dataset and chart:

No alt text provided for this image

The INFLECTIONPOINTS function will quickly identify the local minima and local maxima:

No alt text provided for this image

FUNCTION DEFINITION

This is the original definition. I've since realised another way of doing this, which I've shared at the end of this article.

INFLECTIONPOINTS =LAMBDA(y,
? LET(
? ? _y,y,
? ? _i,SEQUENCE(ROWS(_y)),
? ? _out,SCAN(
? ? ? ? ? 0,
? ? ? ? ? _i,
? ? ? ? ? LAMBDA(a,b,
? ? ? ? ? ? LET(
? ? ? ? ? ? ? _s1,INDEX(_y,b,1),
? ? ? ? ? ? ? _s0,IF(b=1,#N/A,INDEX(_y,b-1,1)),
? ? ? ? ? ? ? _s2,IF(b=ROWS(_y),#N/A,INDEX(_y,b+1,1)),
? ? ? ? ? ? ? IF(OR(AND(_s0<_s1,_s2<_s1),AND(_s0>_s1,_s2>_s1)),_s1,#N/A)
? ? ? ? ? ? )
? ? ? ? ? )
? ? ? ? ),
? ? _out
? )
);        

INFLECTIONPOINTS takes one parameter:

  1. y - an array of numerical values (presumably the y-values from a chart)

BREAKDOWN

Let _y be a copy of y. This is a habit I was using at the time to differentiate variables from arguments. It's not necessary, but I found it helped me stay organized.

Let _i be the index of _y - a sequence of integers from 1 to the count of rows in _y.

Then, scan _i as follows:

? ? _out,SCAN(
? ? ? ? ? 0,
? ? ? ? ? _i,
? ? ? ? ? LAMBDA(a,b,
? ? ? ? ? ? LET(
? ? ? ? ? ? ? _s1,INDEX(_y,b,1),
? ? ? ? ? ? ? _s0,IF(b=1,#N/A,INDEX(_y,b-1,1)),
? ? ? ? ? ? ? _s2,IF(b=ROWS(_y),#N/A,INDEX(_y,b+1,1)),
? ? ? ? ? ? ? IF(OR(AND(_s0<_s1,_s2<_s1),AND(_s0>_s1,_s2>_s1)),_s1,#N/A)
? ? ? ? ? ? )
? ? ? ? ? )
? ? ? ? ),        

The goal of the scan will be to compare each array element in _y with the elements adjacent to it.

With that in mind, for each index element in _i:

  • Let _s1 be the indexed element of _y.
  • Let _s0 be the element before _s1. If _s1 is the first element, return #N/A. The assumption here is that the first point in a chart is not an inflection point.
  • Let _s2 be the element after _s1. If _s1 is the last element, return #N/A. The assumption here is that the last point in a chart is not an inflection point.

Then, if the current point is larger than both the previous point and the next point, or the current point is smaller than the previous point and the next point, return the current point. Otherwise return #N/A.

_out is then the output of INFLECTIONPOINTS.

ALTERNATE DEFINITION

This is an alternate definition that occurred to me while I was reviewing the original. Which one do you prefer?

INFLECTIONPOINTS_ALT = LAMBDA(y
? LET(
? ? ? _y, y,
? ? ? _after, DROP(_y, 1),
? ? ? _before, VSTACK("", DROP(_y, -1)),
? ? ? _out, BYROW(
? ? ? ? ? HSTACK(_y, IFERROR(_y - _before,NA()), _y - _after),
? ? ? ? ? LAMBDA(r,
? ? ? ? ? ? ? LET(
? ? ? ? ? ? ? ? ? y, INDEX(r, , 2),
? ? ? ? ? ? ? ? ? z, INDEX(r, , 3),
? ? ? ? ? ? ? ? ? IF(OR(AND(y < 0, z < 0), AND(y > 0, z > 0)), TAKE(r, , 1), #N/A)
? ? ? ? ? ? ? )
? ? ? ? ? )
? ? ? ),
? ? ? _out
? )
);,        

That's it for now. I hope you enjoyed this article. You can grab both function definitions from this gist.


This is the sixth in the LAMBDA Spotlight series. If you haven't read the others, check them out:

A.XMATCH.ROWS

Text.DropSliceBetween

CROSSJOIN

UNPIVOT

SHOWCLOCK

You can see the other articles I've written?here.

If you have a suggestion for a function which you'd like put under the spotlight, please message me.

Thanks for reading!


Rocio Paz Villa

Lean Six Sigma Production and Business Process / GMP Manufacturing API & Filling

7 个月

Hi There. How can I implement or add inflectionpoints to my excel?

回复
Peter Bartholomew

Technical Fellow at NAFEMS

1 年

Being picky here, but surely what you have identified are turning points (1st derivative zero) not inflection points (2nd derivative zero). My attempt was like your second but using XOR and without the need for INDEX. = LET( ??XORλ, LAMBDA(b, XOR(b)), ??supports, HSTACK( ???DROP(series, 2), ???DROP(series,-2)), ??target, DROP(DROP(series,1),-1), ??extrema, NOT(BYROW(target < supports, XORλ)), ??IF(VSTACK(FALSE, extrema, FALSE), series, NA()) ?)

Talha Khan

Data Strategy, Product & Analytics Solutions, MarTech | I will help you grow in your data goals

1 年

Owen Just tried this on dummy data - it's really useful. Will use this on real data today.

Manoj Kumar

Data Analytics Manager @KPMG UK | Generative AI enthusiast

1 年
  • 该图片无替代文字
回复

Awesome post! Check out how we are automating google sheets ?? https://coefficient.io

要查看或添加评论,请登录

Owen Price的更多文章

  • Excel LAMBDA Spotlight: Bisected Map with BMAPλ

    Excel LAMBDA Spotlight: Bisected Map with BMAPλ

    In this article I'll look at a LAMBDA function called BMAPλ, which was written by Peter Bartholomew. You can grab the…

    8 条评论
  • Excel LAMBDA Spotlight: Sudoku Solver!

    Excel LAMBDA Spotlight: Sudoku Solver!

    In this article I'll look at a LAMBDA function called solver, which was recommended to me by Bhavya Gupta and written…

    16 条评论
  • Excel LAMBDA Spotlight: SumColumnsλ

    Excel LAMBDA Spotlight: SumColumnsλ

    In this article I'll look at a LAMBDA function called SumColumnsλ, written by Craig Hatmaker. Craig is very active in…

    4 条评论
  • Excel LAMBDA Spotlight: SHOWCLOCK

    Excel LAMBDA Spotlight: SHOWCLOCK

    In this article I'll look at a LAMBDA function called SHOWCLOCK. I don't know the full name of the person who wrote…

    8 条评论
  • Excel LAMBDA Spotlight: UNPIVOT

    Excel LAMBDA Spotlight: UNPIVOT

    CREDITS In this article I'll look at a LAMBDA function called UNPIVOT. This one was written by Erik Oehm, creator of…

    18 条评论
  • Excel LAMBDA Spotlight: CROSSJOIN

    Excel LAMBDA Spotlight: CROSSJOIN

    CREDITS The CROSSJOIN LAMBDA function is the product of Erik Oehm, creator of the amazing Excel Robot. If you haven't…

    10 条评论
  • LAMBDA spotlight: Text.DropSliceBetween

    LAMBDA spotlight: Text.DropSliceBetween

    CREDITS This article will look at a single LAMBDA function called Text.DropSliceBetween.

    7 条评论
  • LAMBDA spotlight: A.XMATCH.ROWS

    LAMBDA spotlight: A.XMATCH.ROWS

    CREDITS Today I'm going to try something a little different. This article will throw a spotlight on a function that I…

    19 条评论

社区洞察

其他会员也浏览了