メインコンテンツまでスキップ

チュートリアル: 複利計算演算子

備考

プレビュー

この機能は パブリック プレビュー段階です。

このチュートリアルでは、複利を計算するLakeflow Designer 用のPython UDFオペレーターの作成について説明します。 この例を使って、個々の値や列を変換する演算子を作成する基本を学びましょう。詳細については、 Lakeflow Designer のユーザー定義演算子」を参照してください。

概要

このチュートリアルでは、 Python UDFを使用してユーザー定義オペレーターを作成する手順を説明します。 演算子は、複利式A = P × (1 + r/n)^(n×t)を使用して投資の将来価値を計算します。ここで、

  • P = 元本(開始金額)
  • r = 年利率(小数)
  • n = 年間の複利期間数
  • t = 年単位の時間

ステップ 1: Python関数を作成してテストする

まず、計算を実行するコア Python 関数を定義します。ノートブックのセルでテストして、正しく動作することを確認します。

Python
def compound_amount(principal: float,
annual_rate: float,
compounds_per_year: int,
years: float) -> float:
"""
Compute compound interest future value.

A = P * (1 + r/n)^(n*t)

principal: starting amount (P)
annual_rate: annual nominal rate as decimal (r), e.g. 0.05
compounds_per_year: compounding periods per year (n), e.g. 12
years: time in years (t), can be fractional
"""
import math
if principal is None or annual_rate is None or compounds_per_year is None or years is None:
return None

if compounds_per_year <= 0:
raise ValueError("compounds_per_year must be > 0")

return principal * math.pow(1.0 + annual_rate / compounds_per_year,
compounds_per_year * years)

次のコードで関数をテストできます。

Python
# $1,000 invested at 5% annual rate, compounded monthly for 10 years
compound_amount(1000, 0.05, 12, 10)
# Expected result: ~1647.01

ステップ 2: オペレーターの YAML を作成する

YAML 構成は、 Lakeflow Designer でオペレーターがどのように表示されるかを定義します。 この演算子の場合:

  • Principal は expressionウィジェットを使用して、ユーザーがデータから列を選択できるようにします。
  • 年率年複利年数は 、デフォルトと制約を持つnumberウィジェットを使用します
  • 演算子には、式に列データを提供する入力ポートが 1 つあります。
YAML
schema: user-defined-operator-v0.1.0
type: uc-udf
name: Compound Amount
id: finance.compound_amount
version: '1.0.0'
description: >
Computes the future value of an investment using compound interest.
Formula: A = P * (1 + r/n)^(n*t)
config:
type: object
properties:
principal:
type: string
format: expression
title: Principal
examples:
- 'Select principal column or expression'
x-ui:
widget: expression
port: in
annual_rate:
type: number
title: Annual rate (decimal)
default: 0.05
minimum: 0
examples:
- 'e.g. 0.05 for 5%'
x-ui:
widget: number
compounds_per_year:
type: number
title: Compounds per year
default: 12
minimum: 1
examples:
- 'e.g. 12 for monthly'
x-ui:
widget: number
years:
type: number
title: Years
default: 10
minimum: 0
examples:
- 'Time in years (t)'
x-ui:
widget: number
required:
- principal
- annual_rate
- compounds_per_year
- years
additionalProperties: false
ports:
input:
- name: in
title: Input
output:
- name: out
title: Output

利用可能なすべてのプロパティ、データ型、ウィジェット、およびオプションの包括的なガイドについては、ユーザー定義演算子 YAML リファレンスを参照してください。

ステップ 3: Unity Catalog関数を作成する

YAML スキーマと Python 関数を 1 つのCREATE FUNCTIONステートメントに結合します。YAML 構成は、関数本体の先頭の docstring に記述されます。

SQL
CREATE OR REPLACE FUNCTION main.my_schema.compound_amount(
principal DOUBLE,
annual_rate DOUBLE,
compounds_per_year INT,
years FLOAT)
RETURNS DOUBLE
LANGUAGE PYTHON
AS $$
"""
schema: user-defined-operator-v0.1.0
type: uc-udf
name: Compound Amount
id: finance.compound_amount
version: "1.0.0"
description: >
Computes the future value of an investment using compound interest.
Formula: A = P * (1 + r/n)^(n*t)
config:
type: object
properties:
principal:
type: string
format: expression
title: Principal
examples:
- "Select principal column or expression"
x-ui:
widget: expression
port: in
annual_rate:
type: number
title: Annual rate (decimal)
default: 0.05
minimum: 0
examples:
- "e.g. 0.05 for 5%"
x-ui:
widget: number
compounds_per_year:
type: number
title: Compounds per year
default: 12
minimum: 1
examples:
- "e.g. 12 for monthly"
x-ui:
widget: number
years:
type: number
title: Years
default: 10
minimum: 0
examples:
- "Time in years (t)"
x-ui:
widget: number
required:
- principal
- annual_rate
- compounds_per_year
- years
additionalProperties: false
ports:
input:
- name: in
title: Input
output:
- name: out
title: Output
"""

def compound_amount(principal: float,
annual_rate: float,
compounds_per_year: int,
years: float) -> float:
import math
if principal is None or annual_rate is None or compounds_per_year is None or years is None:
return None

if compounds_per_year <= 0:
raise ValueError("compounds_per_year must be > 0")

return principal * math.pow(1.0 + annual_rate / compounds_per_year,
compounds_per_year * years)

return compound_amount(principal, annual_rate, compounds_per_year, years)
$$

ステップ 4: 機能をテストする

SQL を使用して UC 関数を直接テストします。

SQL
-- Test 1: $1,000 at 5% compounded monthly for 10 years
SELECT main.my_schema.compound_amount(1000, 0.05, 12, 10)
-- Expected: ~1647.01

-- Test 2: $1,000 at 5% compounded annually for 1 year
SELECT main.my_schema.compound_amount(1000, 0.05, 1, 1)
-- Expected: 1050.00

-- Test 3: $1,000 at 15% compounded monthly for 1 year
SELECT main.my_schema.compound_amount(1000, 0.15, 12, 1)
-- Expected: ~1160.75

ステップ 5: オペレーターを登録する

.user_defined_operators.yamlファイルに演算子を追加します:

YAML
operators:
- catalog: main
schema: my_schema
functionName: compound_amount
注記

このファイルをユーザーフォルダ内に定義した場合、そのファイルはあなた以外には表示されません。詳細については、 「オペレータを検出可能にする」を参照してください。

ステップ 6: 権限を設定する

この演算子を使用する必要があるユーザーにアクセス権を付与します。

SQL
GRANT USE SCHEMA ON SCHEMA main.my_schema TO `<user>`;
GRANT EXECUTE ON FUNCTION main.my_schema.compound_amount TO `<user>`;

Lakeflow Designer での演算子の使用

登録されると、オペレーターはLakeflow Designer に次のように表示されます。

  • 入力データから主列を選択するためのドロップダウン
  • 利率、複利計算頻度、年数(適切なデフォルト値付き)の数値入力

ユーザーはこの演算子を適用して、投資データの列全体の将来値を計算できます。