Skip to contents

This vignette demonstrates a basic use case of Rydra for creating a simple pricing model.

1. The Pricing Model

The pricing model calculates the total price based on the number of users and whether a discount is applied.

The model is defined in the pricing_model.yaml file:

model_name: "basic_pricing"

constants:
  base_price: 100

main_model:
  intercepts:
    baseline: 0

  coefficients:
    price_per_user: 5
    discount_percentage: -0.1
    total_price: 1

  transformations:
    - name: "user_cost"
      formula: "multiply_by(users, coefficients.price_per_user)"
    - name: "total_price"
      formula: "add_value(constants.base_price, user_cost)"
    - name: "has_discount_bool"
      formula: "ifelse(is.logical(has_discount), has_discount, tolower(as.character(has_discount)) %in% c('yes','y','true','1'))"
    - name: "discount_factor"
      formula: "add_value(1, ifelse(has_discount_bool, coefficients.discount_percentage, 0))"

  output_transformation: "multiply_by(result, discount_factor)"

2. Using the Pricing Model

We can use the rydra_calculate function to calculate the price for different scenarios.

Scenario 1: 10 users, no discount

library(Rydra)

input_data <- list(
  users = 10,
  has_discount = FALSE
)

result <- rydra_calculate(
  config_path = system.file("extdata", "pricing_model.yaml", package = "Rydra"),
  data = input_data,
  model_name = "main_model"
)

print(result)
[1] 150

Scenario 2: 20 users, with discount

input_data <- list(
  users = 20,
  has_discount = TRUE
)

result <- rydra_calculate(
  config_path = system.file("extdata", "pricing_model.yaml", package = "Rydra"),
  data = input_data,
  model_name = "main_model"
)

print(result)
[1] 180

How the calculation works

The model computes the final price in three stages:

  1. Transformations
  • user_cost = multiply_by(users, coefficients.price_per_user)
  • total_price = add_value(constants.base_price, user_cost)
  • has_discount_bool = ifelse(is.logical(has_discount), has_discount, tolower(as.character(has_discount)) %in% c(‘yes’,‘y’,‘true’,‘1’))
  • discount_factor = add_value(1, ifelse(has_discount_bool, coefficients.discount_percentage, 0))
  1. Aggregate score (result before output transformation)
  • intercepts.baseline + sum(coefficients × transformed_variables_present)
  • In this simple model we only include total_price in the aggregate via coefficients.total_price = 1, so:
  • result = 0 + 1 × total_price = total_price
  1. Output transformation
  • final_result = multiply_by(result, discount_factor)

Worked examples - 10 users, no discount: user_cost = 10 × 5 = 50; total_price = 100 + 50 = 150; has_discount_bool = FALSE; discount_factor = 1 + 0 = 1; final = 150 × 1 = 150 - 20 users, with discount: user_cost = 20 × 5 = 100; total_price = 100 + 100 = 200; has_discount_bool = TRUE; discount_factor = 1 - 0.1 = 0.9; final = 200 × 0.9 = 180

3. Conclusion

This simple example demonstrates how to use Rydra to create a versionable and transparent pricing model. The model logic is clearly defined in the YAML file, making it easy to understand, modify, and audit.