Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update Fairhold formula with new linear one #145

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions app/models/Fairhold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ it("can be instantiated", () => {

it("correctly calculates the fairhold formula below the threshold", () => {
const fairhold = new Fairhold({
affordability: 0.05,
affordability: 0.15,
landPriceOrRent: 100,
});
expect(fairhold.discountLand).toBeCloseTo(0.6877641290737884);
expect(fairhold.discountLand).toBeCloseTo(0.535);
});

it("correctly calculates the fairhold formula above the threshold", () => {
const fairhold = new Fairhold({
affordability: 0.05,
affordability: 0.5,
landPriceOrRent: 100,
plateau: 3,
});
expect(fairhold.discountLand).toBeCloseTo(0.68776);
expect(fairhold.discountLand).toBeCloseTo(0.15);
});

it("correctly deals with negative values of landPriceOrRent", () => {
Expand Down
32 changes: 2 additions & 30 deletions app/models/Fairhold.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,22 @@
import * as math from "mathjs";

const DEFAULT_AMPLITUDE = 0.25;
const DEFAULT_LENGTH = 1;
const DEFAULT_POSITION = 0.45;
const DEFAULT_PLATEAU = 0.15;
const DEFAULT_THRESHOLD = 0.5;

type ConstructorParams = Pick<Fairhold, "affordability" | "landPriceOrRent"> &
Partial<
Pick<
Fairhold,
"amplitude" | "length" | "position" | "plateau" | "threshold"
>
>;
type ConstructorParams = Pick<Fairhold, "affordability" | "landPriceOrRent">;

export class Fairhold {
public affordability: number;
public landPriceOrRent: number;
public amplitude: number;
public length: number;
public position: number;
public plateau: number;
public threshold: number;
public discountLand: number;
public discountedLandPriceOrRent: number;

constructor(params: ConstructorParams) {
this.affordability = params.affordability;
this.landPriceOrRent = params.landPriceOrRent;
this.amplitude = params.amplitude || DEFAULT_AMPLITUDE;
this.length = params.length || DEFAULT_LENGTH;
this.position = params.position || DEFAULT_POSITION;
this.plateau = params.plateau || DEFAULT_PLATEAU;
this.threshold = params.threshold || DEFAULT_THRESHOLD;
this.discountLand = this.calculateFairholdDiscount();
this.discountedLandPriceOrRent = this.calculateDiscountedPriceOrRent();
}

private calculateFairholdDiscount() {
if (this.affordability > this.threshold) return this.plateau;

const discountLand =
this.amplitude *
math.cos((2 * math.pi * this.affordability) / this.length) +
this.position;

const discountLand = math.max(-1.1 * this.affordability + 0.7, 0.15)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would still be worth making these magic numbers constants, just for documentation and readability.

I agree if they're not variables, we don't need to pass them into the class or have them as properties.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, forgot about that whole readability thing! New commit should take care of this.

return discountLand;
}

Expand Down
Loading